HTMLTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  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 <[email protected]>
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  15. */
  16. class Kohana_HTMLTest extends Unittest_TestCase
  17. {
  18. /**
  19. * Defaults for this test
  20. * @var array
  21. */
  22. // @codingStandardsIgnoreStart
  23. protected $environmentDefault = array(
  24. 'Kohana::$base_url' => '/kohana/',
  25. 'Kohana::$index_file' => 'index.php',
  26. 'HTML::$strict' => TRUE,
  27. 'HTTP_HOST' => 'www.kohanaframework.org',
  28. );
  29. // @codingStandardsIgnoreStart
  30. /**
  31. * Provides test data for test_attributes()
  32. *
  33. * @return array
  34. */
  35. public function provider_attributes()
  36. {
  37. return array(
  38. array(
  39. array('name' => 'field', 'random' => 'not_quite', 'id' => 'unique_field'),
  40. array(),
  41. ' id="unique_field" name="field" random="not_quite"'
  42. ),
  43. array(
  44. array('invalid' => NULL),
  45. array(),
  46. ''
  47. ),
  48. array(
  49. array(),
  50. array(),
  51. ''
  52. ),
  53. array(
  54. array('name' => 'field', 'checked'),
  55. array(),
  56. ' name="field" checked="checked"',
  57. ),
  58. array(
  59. array('id' => 'disabled_field', 'disabled'),
  60. array('HTML::$strict' => FALSE),
  61. ' id="disabled_field" disabled',
  62. ),
  63. );
  64. }
  65. /**
  66. * Tests HTML::attributes()
  67. *
  68. * @test
  69. * @dataProvider provider_attributes
  70. * @param array $attributes Attributes to use
  71. * @param array $options Environment options to use
  72. * @param string $expected Expected output
  73. */
  74. public function test_attributes(array $attributes, array $options, $expected)
  75. {
  76. $this->setEnvironment($options);
  77. $this->assertSame(
  78. $expected,
  79. HTML::attributes($attributes)
  80. );
  81. }
  82. /**
  83. * Provides test data for test_script
  84. *
  85. * @return array Array of test data
  86. */
  87. public function provider_script()
  88. {
  89. return array(
  90. array(
  91. '<script type="text/javascript" src="http://google.com/script.js"></script>',
  92. 'http://google.com/script.js',
  93. ),
  94. array(
  95. '<script type="text/javascript" src="http://www.kohanaframework.org/kohana/index.php/my/script.js"></script>',
  96. 'my/script.js',
  97. NULL,
  98. 'http',
  99. TRUE
  100. ),
  101. array(
  102. '<script type="text/javascript" src="https://www.kohanaframework.org/kohana/my/script.js"></script>',
  103. 'my/script.js',
  104. NULL,
  105. 'https',
  106. FALSE
  107. ),
  108. array(
  109. '<script type="text/javascript" src="https://www.kohanaframework.org/kohana/my/script.js"></script>',
  110. '/my/script.js', // Test absolute paths
  111. NULL,
  112. 'https',
  113. FALSE
  114. ),
  115. );
  116. }
  117. /**
  118. * Tests HTML::script()
  119. *
  120. * @test
  121. * @dataProvider provider_script
  122. * @param string $expected Expected output
  123. * @param string $file URL to script
  124. * @param array $attributes HTML attributes for the anchor
  125. * @param string $protocol Protocol to use
  126. * @param bool $index Should the index file be included in url?
  127. */
  128. public function test_script($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  129. {
  130. $this->assertSame(
  131. $expected,
  132. HTML::script($file, $attributes, $protocol, $index)
  133. );
  134. }
  135. /**
  136. * Data provider for the style test
  137. *
  138. * @return array Array of test data
  139. */
  140. public function provider_style()
  141. {
  142. return array(
  143. array(
  144. '<link type="text/css" href="http://google.com/style.css" rel="stylesheet" />',
  145. 'http://google.com/style.css',
  146. array(),
  147. NULL,
  148. FALSE
  149. ),
  150. array(
  151. '<link type="text/css" href="/kohana/my/style.css" rel="stylesheet" />',
  152. 'my/style.css',
  153. array(),
  154. NULL,
  155. FALSE
  156. ),
  157. array(
  158. '<link type="text/css" href="https://www.kohanaframework.org/kohana/my/style.css" rel="stylesheet" />',
  159. 'my/style.css',
  160. array(),
  161. 'https',
  162. FALSE
  163. ),
  164. array(
  165. '<link type="text/css" href="https://www.kohanaframework.org/kohana/index.php/my/style.css" rel="stylesheet" />',
  166. 'my/style.css',
  167. array(),
  168. 'https',
  169. TRUE
  170. ),
  171. array(
  172. '<link type="text/css" href="https://www.kohanaframework.org/kohana/index.php/my/style.css" rel="stylesheet" />',
  173. '/my/style.css',
  174. array(),
  175. 'https',
  176. TRUE
  177. ),
  178. array(
  179. // #4283: http://dev.kohanaframework.org/issues/4283
  180. '<link type="text/css" href="https://www.kohanaframework.org/kohana/index.php/my/style.css" rel="stylesheet/less" />',
  181. 'my/style.css',
  182. array(
  183. 'rel' => 'stylesheet/less'
  184. ),
  185. 'https',
  186. TRUE
  187. ),
  188. );
  189. }
  190. /**
  191. * Tests HTML::style()
  192. *
  193. * @test
  194. * @dataProvider provider_style
  195. * @param string $expected The expected output
  196. * @param string $file The file to link to
  197. * @param array $attributes Any extra attributes for the link
  198. * @param string $protocol Protocol to use
  199. * @param bool $index Whether the index file should be added to the link
  200. */
  201. public function test_style($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  202. {
  203. $this->assertSame(
  204. $expected,
  205. HTML::style($file, $attributes, $protocol, $index)
  206. );
  207. }
  208. /**
  209. * Provides test data for test_anchor
  210. *
  211. * @return array Test data
  212. */
  213. public function provider_anchor()
  214. {
  215. return array(
  216. array(
  217. '<a href="http://kohanaframework.org">Kohana</a>',
  218. array(),
  219. 'http://kohanaframework.org',
  220. 'Kohana',
  221. ),
  222. array(
  223. '<a href="http://google.com" target="_blank">GOOGLE</a>',
  224. array(),
  225. 'http://google.com',
  226. 'GOOGLE',
  227. array('target' => '_blank'),
  228. 'http',
  229. ),
  230. array(
  231. '<a href="https://www.kohanaframework.org/kohana/users/example">Kohana</a>',
  232. array(),
  233. 'users/example',
  234. 'Kohana',
  235. NULL,
  236. 'https',
  237. FALSE,
  238. ),
  239. array(
  240. '<a href="https://www.kohanaframework.org/kohana/index.php/users/example">Kohana</a>',
  241. array(),
  242. 'users/example',
  243. 'Kohana',
  244. NULL,
  245. 'https',
  246. TRUE,
  247. ),
  248. array(
  249. '<a href="https://www.kohanaframework.org/kohana/index.php/users/example">Kohana</a>',
  250. array(),
  251. 'users/example',
  252. 'Kohana',
  253. NULL,
  254. 'https',
  255. ),
  256. array(
  257. '<a href="https://www.kohanaframework.org/kohana/index.php/users/example">Kohana</a>',
  258. array(),
  259. 'users/example',
  260. 'Kohana',
  261. NULL,
  262. 'https',
  263. TRUE,
  264. ),
  265. array(
  266. '<a href="https://www.kohanaframework.org/kohana/users/example">Kohana</a>',
  267. array(),
  268. 'users/example',
  269. 'Kohana',
  270. NULL,
  271. 'https',
  272. FALSE,
  273. ),
  274. array(
  275. '<a href="https://www.kohanaframework.org/kohana/users/example">Kohana</a>',
  276. array(),
  277. '/users/example',
  278. 'Kohana',
  279. NULL,
  280. 'https',
  281. FALSE,
  282. ),
  283. );
  284. }
  285. /**
  286. * Tests HTML::anchor
  287. *
  288. * @test
  289. * @dataProvider provider_anchor
  290. */
  291. public function test_anchor($expected, array $options, $uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = TRUE)
  292. {
  293. // $this->setEnvironment($options);
  294. $this->assertSame(
  295. $expected,
  296. HTML::anchor($uri, $title, $attributes, $protocol, $index)
  297. );
  298. }
  299. /**
  300. * Data provider for test_file_anchor
  301. *
  302. * @return array
  303. */
  304. public function provider_file_anchor()
  305. {
  306. return array(
  307. array(
  308. '<a href="/kohana/mypic.png">My picture file</a>',
  309. array(),
  310. 'mypic.png',
  311. 'My picture file',
  312. ),
  313. array(
  314. '<a href="https://www.kohanaframework.org/kohana/index.php/mypic.png" attr="value">My picture file</a>',
  315. array('attr' => 'value'),
  316. 'mypic.png',
  317. 'My picture file',
  318. 'https',
  319. TRUE
  320. ),
  321. array(
  322. '<a href="ftp://www.kohanaframework.org/kohana/mypic.png">My picture file</a>',
  323. array(),
  324. 'mypic.png',
  325. 'My picture file',
  326. 'ftp',
  327. FALSE
  328. ),
  329. array(
  330. '<a href="ftp://www.kohanaframework.org/kohana/mypic.png">My picture file</a>',
  331. array(),
  332. '/mypic.png',
  333. 'My picture file',
  334. 'ftp',
  335. FALSE
  336. ),
  337. );
  338. }
  339. /**
  340. * Test for HTML::file_anchor()
  341. *
  342. * @test
  343. * @covers HTML::file_anchor
  344. * @dataProvider provider_file_anchor
  345. */
  346. public function test_file_anchor($expected, array $attributes, $file, $title = NULL, $protocol = NULL, $index = FALSE)
  347. {
  348. $this->assertSame(
  349. $expected,
  350. HTML::file_anchor($file, $title, $attributes, $protocol, $index)
  351. );
  352. }
  353. }