HTMLTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * Tests HTML
  4. *
  5. * @group ko7
  6. * @group ko7.core
  7. * @group ko7.core.html
  8. *
  9. * @package KO7
  10. * @category Tests
  11. *
  12. * @author BRMatt <matthew@sigswitch.com>
  13. * @copyright (c) 2007-2016 Kohana Team
  14. * @copyright (c) since 2016 Koseven Team
  15. * @license https://koseven.dev/LICENSE
  16. */
  17. class KO7_HTMLTest extends Unittest_TestCase {
  18. /**
  19. * Sets up the environment
  20. */
  21. // @codingStandardsIgnoreStart
  22. public function setUp(): void
  23. // @codingStandardsIgnoreEnd
  24. {
  25. parent::setUp();
  26. KO7::$config->load('url')->set('trusted_hosts', ['www\.koseven\.dev']);
  27. }
  28. /**
  29. * Defaults for this test
  30. * @var array
  31. */
  32. // @codingStandardsIgnoreStart
  33. protected $environmentDefault = [
  34. 'KO7::$base_url' => '/ko7/',
  35. 'KO7::$index_file' => 'index.php',
  36. 'HTML::$strict' => TRUE,
  37. 'HTTP_HOST' => 'www.koseven.dev',
  38. ];
  39. // @codingStandardsIgnoreStart
  40. /**
  41. * Provides test data for test_attributes()
  42. *
  43. * @return array
  44. */
  45. public function provider_attributes()
  46. {
  47. return [
  48. [
  49. ['name' => 'field', 'random' => 'not_quite', 'id' => 'unique_field'],
  50. [],
  51. ' id="unique_field" name="field" random="not_quite"'
  52. ],
  53. [
  54. ['invalid' => NULL],
  55. [],
  56. ''
  57. ],
  58. [
  59. [],
  60. [],
  61. ''
  62. ],
  63. [
  64. ['name' => 'field', 'checked'],
  65. [],
  66. ' name="field" checked="checked"',
  67. ],
  68. [
  69. ['id' => 'disabled_field', 'disabled'],
  70. ['HTML::$strict' => FALSE],
  71. ' id="disabled_field" disabled',
  72. ],
  73. ];
  74. }
  75. /**
  76. * Tests HTML::attributes()
  77. *
  78. * @test
  79. * @dataProvider provider_attributes
  80. * @param array $attributes Attributes to use
  81. * @param array $options Environment options to use
  82. * @param string $expected Expected output
  83. */
  84. public function test_attributes(array $attributes, array $options, $expected)
  85. {
  86. $this->setEnvironment($options);
  87. $this->assertSame(
  88. $expected,
  89. HTML::attributes($attributes)
  90. );
  91. }
  92. /**
  93. * Provides test data for test_script
  94. *
  95. * @return array Array of test data
  96. */
  97. public function provider_script()
  98. {
  99. return [
  100. [
  101. '<script type="text/javascript" src="http://google.com/script.js"></script>',
  102. 'http://google.com/script.js',
  103. ],
  104. [
  105. '<script type="text/javascript" src="http://www.koseven.dev/ko7/index.php/my/script.js"></script>',
  106. 'my/script.js',
  107. NULL,
  108. 'http',
  109. TRUE
  110. ],
  111. [
  112. '<script type="text/javascript" src="https://www.koseven.dev/ko7/my/script.js"></script>',
  113. 'my/script.js',
  114. NULL,
  115. 'https',
  116. FALSE
  117. ],
  118. [
  119. '<script type="text/javascript" src="https://www.koseven.dev/ko7/my/script.js"></script>',
  120. '/my/script.js', // Test absolute paths
  121. NULL,
  122. 'https',
  123. FALSE
  124. ],
  125. [
  126. '<script type="text/javascript" src="//google.com/script.js"></script>',
  127. '//google.com/script.js',
  128. ],
  129. ];
  130. }
  131. /**
  132. * Tests HTML::script()
  133. *
  134. * @test
  135. * @dataProvider provider_script
  136. * @param string $expected Expected output
  137. * @param string $file URL to script
  138. * @param array $attributes HTML attributes for the anchor
  139. * @param string $protocol Protocol to use
  140. * @param bool $index Should the index file be included in url?
  141. */
  142. public function test_script($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  143. {
  144. $this->assertSame(
  145. $expected,
  146. HTML::script($file, $attributes, $protocol, $index)
  147. );
  148. }
  149. /**
  150. * Data provider for the style test
  151. *
  152. * @return array Array of test data
  153. */
  154. public function provider_style()
  155. {
  156. return [
  157. [
  158. '<link type="text/css" href="http://google.com/style.css" rel="stylesheet" />',
  159. 'http://google.com/style.css',
  160. [],
  161. NULL,
  162. FALSE
  163. ],
  164. [
  165. '<link type="text/css" href="/ko7/my/style.css" rel="stylesheet" />',
  166. 'my/style.css',
  167. [],
  168. NULL,
  169. FALSE
  170. ],
  171. [
  172. '<link type="text/css" href="https://www.koseven.dev/ko7/my/style.css" rel="stylesheet" />',
  173. 'my/style.css',
  174. [],
  175. 'https',
  176. FALSE
  177. ],
  178. [
  179. '<link type="text/css" href="https://www.koseven.dev/ko7/index.php/my/style.css" rel="stylesheet" />',
  180. 'my/style.css',
  181. [],
  182. 'https',
  183. TRUE
  184. ],
  185. [
  186. '<link type="text/css" href="https://www.koseven.dev/ko7/index.php/my/style.css" rel="stylesheet" />',
  187. '/my/style.css',
  188. [],
  189. 'https',
  190. TRUE
  191. ],
  192. [
  193. // #4283: http://koseven.dev/issues/4283
  194. '<link type="text/css" href="https://www.koseven.dev/ko7/index.php/my/style.css" rel="stylesheet/less" />',
  195. 'my/style.css',
  196. [
  197. 'rel' => 'stylesheet/less'
  198. ],
  199. 'https',
  200. TRUE
  201. ],
  202. [
  203. '<link type="text/css" href="//google.com/style.css" rel="stylesheet" />',
  204. '//google.com/style.css',
  205. [],
  206. NULL,
  207. FALSE
  208. ],
  209. ];
  210. }
  211. /**
  212. * Tests HTML::style()
  213. *
  214. * @test
  215. * @dataProvider provider_style
  216. * @param string $expected The expected output
  217. * @param string $file The file to link to
  218. * @param array $attributes Any extra attributes for the link
  219. * @param string $protocol Protocol to use
  220. * @param bool $index Whether the index file should be added to the link
  221. */
  222. public function test_style($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  223. {
  224. $this->assertSame(
  225. $expected,
  226. HTML::style($file, $attributes, $protocol, $index)
  227. );
  228. }
  229. /**
  230. * Provides test data for test_anchor
  231. *
  232. * @return array Test data
  233. */
  234. public function provider_anchor()
  235. {
  236. return [
  237. // a fragment-only anchor
  238. [
  239. '<a href="#go-to-section-ko7">KO7</a>',
  240. [],
  241. '#go-to-section-ko7',
  242. 'KO7',
  243. ],
  244. // a query-only anchor
  245. [
  246. '<a href="?cat=a">Category A</a>',
  247. [],
  248. '?cat=a',
  249. 'Category A',
  250. ],
  251. [
  252. '<a href="http://koseven.dev/">KO7</a>',
  253. [],
  254. 'http://koseven.dev/',
  255. 'KO7',
  256. ],
  257. [
  258. '<a href="http://google.com" target="_blank">GOOGLE</a>',
  259. [],
  260. 'http://google.com',
  261. 'GOOGLE',
  262. ['target' => '_blank'],
  263. 'http',
  264. ],
  265. [
  266. '<a href="//google.com/">GOOGLE</a>',
  267. [],
  268. '//google.com/',
  269. 'GOOGLE',
  270. ],
  271. [
  272. '<a href="https://www.koseven.dev/ko7/users/example">KO7</a>',
  273. [],
  274. 'users/example',
  275. 'KO7',
  276. NULL,
  277. 'https',
  278. FALSE,
  279. ],
  280. [
  281. '<a href="https://www.koseven.dev/ko7/index.php/users/example">KO7</a>',
  282. [],
  283. 'users/example',
  284. 'KO7',
  285. NULL,
  286. 'https',
  287. TRUE,
  288. ],
  289. [
  290. '<a href="https://www.koseven.dev/ko7/index.php/users/example">KO7</a>',
  291. [],
  292. 'users/example',
  293. 'KO7',
  294. NULL,
  295. 'https',
  296. ],
  297. [
  298. '<a href="https://www.koseven.dev/ko7/index.php/users/example">KO7</a>',
  299. [],
  300. 'users/example',
  301. 'KO7',
  302. NULL,
  303. 'https',
  304. TRUE,
  305. ],
  306. [
  307. '<a href="https://www.koseven.dev/ko7/users/example">KO7</a>',
  308. [],
  309. 'users/example',
  310. 'KO7',
  311. NULL,
  312. 'https',
  313. FALSE,
  314. ],
  315. [
  316. '<a href="https://www.koseven.dev/ko7/users/example">KO7</a>',
  317. [],
  318. '/users/example',
  319. 'KO7',
  320. NULL,
  321. 'https',
  322. FALSE,
  323. ],
  324. ];
  325. }
  326. /**
  327. * Tests HTML::anchor
  328. *
  329. * @test
  330. * @dataProvider provider_anchor
  331. */
  332. public function test_anchor($expected, array $options, $uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = TRUE)
  333. {
  334. // $this->setEnvironment($options);
  335. $this->assertSame(
  336. $expected,
  337. HTML::anchor($uri, $title, $attributes, $protocol, $index)
  338. );
  339. }
  340. /**
  341. * Data provider for test_file_anchor
  342. *
  343. * @return array
  344. */
  345. public function provider_file_anchor()
  346. {
  347. return [
  348. [
  349. '<a href="/ko7/mypic.png">My picture file</a>',
  350. [],
  351. 'mypic.png',
  352. 'My picture file',
  353. ],
  354. [
  355. '<a href="https://www.koseven.dev/ko7/index.php/mypic.png" attr="value">My picture file</a>',
  356. ['attr' => 'value'],
  357. 'mypic.png',
  358. 'My picture file',
  359. 'https',
  360. TRUE
  361. ],
  362. [
  363. '<a href="ftp://www.koseven.dev/ko7/mypic.png">My picture file</a>',
  364. [],
  365. 'mypic.png',
  366. 'My picture file',
  367. 'ftp',
  368. FALSE
  369. ],
  370. [
  371. '<a href="ftp://www.koseven.dev/ko7/mypic.png">My picture file</a>',
  372. [],
  373. '/mypic.png',
  374. 'My picture file',
  375. 'ftp',
  376. FALSE
  377. ],
  378. ];
  379. }
  380. /**
  381. * Test for HTML::file_anchor()
  382. *
  383. * @test
  384. * @covers HTML::file_anchor
  385. * @dataProvider provider_file_anchor
  386. */
  387. public function test_file_anchor($expected, array $attributes, $file, $title = NULL, $protocol = NULL, $index = FALSE)
  388. {
  389. $this->assertSame(
  390. $expected,
  391. HTML::file_anchor($file, $title, $attributes, $protocol, $index)
  392. );
  393. }
  394. /**
  395. * Provides test data for test_image
  396. *
  397. * @return array Array of test data
  398. */
  399. public function provider_image()
  400. {
  401. return [
  402. [
  403. '<img src="http://google.com/image.png" />',
  404. 'http://google.com/image.png',
  405. ],
  406. [
  407. '<img src="//google.com/image.png" />',
  408. '//google.com/image.png',
  409. ],
  410. [
  411. '<img src="/ko7/img/image.png" />',
  412. 'img/image.png',
  413. ],
  414. [
  415. '<img src="https://www.koseven.dev/ko7/index.php/img/image.png" alt="..." />',
  416. 'img/image.png',
  417. ['alt' => '...',],
  418. 'https',
  419. TRUE
  420. ],
  421. ];
  422. }
  423. /**
  424. * Tests HTML::image()
  425. *
  426. * @test
  427. * @dataProvider provider_image
  428. * @param string $expected Expected output
  429. * @param string $file file name
  430. * @param array $attributes HTML attributes for the image
  431. * @param string $protocol Protocol to use
  432. * @param bool $index Should the index file be included in url?
  433. */
  434. public function test_image($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  435. {
  436. $this->assertSame(
  437. $expected,
  438. HTML::image($file, $attributes, $protocol, $index)
  439. );
  440. }
  441. }