HTMLTest.php 9.7 KB

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