HtmlTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\template\helper;
  9. use lithium\net\http\Router;
  10. use lithium\template\helper\Html;
  11. use lithium\action\Request;
  12. use lithium\action\Response;
  13. use lithium\tests\mocks\template\MockRenderer;
  14. class HtmlTest extends \lithium\test\Unit {
  15. /**
  16. * Test object instance
  17. *
  18. * @var object
  19. */
  20. public $html = null;
  21. protected $_routes = array();
  22. /**
  23. * Initialize test by creating a new object instance with a default context.
  24. */
  25. public function setUp() {
  26. $this->_routes = Router::get();
  27. Router::reset();
  28. Router::connect('/{:controller}/{:action}/{:id}.{:type}');
  29. Router::connect('/{:controller}/{:action}.{:type}');
  30. $this->context = new MockRenderer(array(
  31. 'request' => new Request(array(
  32. 'base' => '', 'env' => array('HTTP_HOST' => 'foo.local')
  33. )),
  34. 'response' => new Response()
  35. ));
  36. $this->html = new Html(array('context' => &$this->context));
  37. }
  38. /**
  39. * Clean up after the test.
  40. */
  41. public function tearDown() {
  42. Router::reset();
  43. foreach ($this->_routes as $route) {
  44. Router::connect($route);
  45. }
  46. unset($this->html);
  47. }
  48. /**
  49. * Tests that character set declarations render the
  50. * correct character set and short meta tag.
  51. */
  52. public function testCharset() {
  53. $result = $this->html->charset();
  54. $this->assertTags($result, array('meta' => array(
  55. 'charset' => 'UTF-8'
  56. )));
  57. $result = $this->html->charset('utf-8');
  58. $this->assertTags($result, array('meta' => array(
  59. 'charset' => 'utf-8'
  60. )));
  61. $result = $this->html->charset('UTF-7');
  62. $this->assertTags($result, array('meta' => array(
  63. 'charset' => 'UTF-7'
  64. )));
  65. }
  66. /**
  67. * Tests meta linking.
  68. */
  69. public function testMetaLink() {
  70. $result = $this->html->link(
  71. 'RSS Feed',
  72. array('controller' => 'posts', 'type' => 'rss'),
  73. array('type' => 'rss')
  74. );
  75. $this->assertTags($result, array('link' => array(
  76. 'href' => 'regex:/.*\/posts\/index\.rss/',
  77. 'type' => 'application/rss+xml',
  78. 'rel' => 'alternate',
  79. 'title' => 'RSS Feed'
  80. )));
  81. $result = $this->html->link(
  82. 'Atom Feed', array('controller' => 'posts', 'type' => 'xml'), array('type' => 'atom')
  83. );
  84. $this->assertTags($result, array('link' => array(
  85. 'href' => 'regex:/.*\/posts\/index\.xml/',
  86. 'type' => 'application/atom+xml',
  87. 'title' => 'Atom Feed',
  88. 'rel' => 'alternate'
  89. )));
  90. $result = $this->html->link('No-existy', '/posts.xmp', array('type' => 'rong'));
  91. $this->assertTags($result, array('link' => array(
  92. 'href' => 'regex:/.*\/posts\.xmp/',
  93. 'title' => 'No-existy'
  94. )));
  95. $result = $this->html->link('No-existy', '/posts.xpp', array('type' => 'atom'));
  96. $this->assertTags($result, array('link' => array(
  97. 'href' => 'regex:/.*\/posts\.xpp/',
  98. 'type' => 'application/atom+xml',
  99. 'title' => 'No-existy',
  100. 'rel' => 'alternate'
  101. )));
  102. $result = $this->html->link('Favicon', array(), array('type' => 'icon'));
  103. $expected = array(
  104. 'link' => array(
  105. 'href' => 'regex:/.*favicon\.ico/',
  106. 'type' => 'image/x-icon',
  107. 'rel' => 'icon',
  108. 'title' => 'Favicon'
  109. ),
  110. array('link' => array(
  111. 'href' => 'regex:/.*favicon\.ico/',
  112. 'type' => 'image/x-icon',
  113. 'rel' => 'shortcut icon',
  114. 'title' => 'Favicon'
  115. ))
  116. );
  117. $this->assertTags($result, $expected);
  118. }
  119. /**
  120. * Tests <a /> elements generated by `HtmlHelper::link()`
  121. */
  122. public function testLink() {
  123. $result = $this->html->link('/home');
  124. $expected = array('a' => array('href' => '/home'), 'regex:/\/home/', '/a');
  125. $this->assertTags($result, $expected);
  126. $result = $this->html->link('Next >', '#');
  127. $expected = array('a' => array('href' => '#'), 'Next &gt;', '/a');
  128. $this->assertTags($result, $expected);
  129. $result = $this->html->link('Next >', '#', array('escape' => true));
  130. $expected = array(
  131. 'a' => array('href' => '#'),
  132. 'Next &gt;',
  133. '/a'
  134. );
  135. $this->assertTags($result, $expected);
  136. $result = $this->html->link('Next >', '#', array('escape' => 'utf-8'));
  137. $expected = array(
  138. 'a' => array('href' => '#'),
  139. 'Next &gt;',
  140. '/a'
  141. );
  142. $this->assertTags($result, $expected);
  143. $result = $this->html->link('Next >', '#', array('escape' => false));
  144. $expected = array('a' => array('href' => '#'), 'Next >', '/a');
  145. $this->assertTags($result, $expected);
  146. $result = $this->html->link('Next >', '#', array(
  147. 'title' => 'to escape &#8230; or not escape?',
  148. 'escape' => false
  149. ));
  150. $expected = array(
  151. 'a' => array('href' => '#', 'title' => 'to escape &#8230; or not escape?'),
  152. 'Next >',
  153. '/a'
  154. );
  155. $this->assertTags($result, $expected);
  156. $result = $this->html->link('Next >', '#', array(
  157. 'title' => 'to escape &#8230; or not escape?', 'escape' => true
  158. ));
  159. $expected = array(
  160. 'a' => array('href' => '#', 'title' => 'to escape &amp;#8230; or not escape?'),
  161. 'Next &gt;',
  162. '/a'
  163. );
  164. $this->assertTags($result, $expected);
  165. }
  166. /**
  167. * Tests basic JavaScript linking using the <script /> tag
  168. */
  169. public function testScriptLinking() {
  170. $result = $this->html->script('script.js');
  171. $expected = '<script type="text/javascript" src="/js/script.js"></script>';
  172. $this->assertEqual($expected, $result);
  173. $result = $this->html->script('script');
  174. $expected = '<script type="text/javascript" src="/js/script.js"></script>';
  175. $this->assertEqual($expected, $result);
  176. $result = $this->html->script('scriptaculous.js?load=effects');
  177. $expected = '<script type="text/javascript"';
  178. $expected .= ' src="/js/scriptaculous.js?load=effects"></script>';
  179. $this->assertEqual($expected, $result);
  180. $result = $this->html->script('jquery-1.1.2');
  181. $expected = '<script type="text/javascript" src="/js/jquery-1.1.2.js"></script>';
  182. $this->assertEqual($result, $expected);
  183. $result = $this->html->script('jquery-1.1.2');
  184. $expected = '<script type="text/javascript" src="/js/jquery-1.1.2.js"></script>';
  185. $this->assertEqual($result, $expected);
  186. $result = $this->html->script('/plugin/js/jquery-1.1.2');
  187. $expected = '<script type="text/javascript" src="/plugin/js/jquery-1.1.2.js"></script>';
  188. $this->assertEqual($result, $expected);
  189. $result = $this->html->script('/some_other_path/myfile.1.2.2.min.js');
  190. $expected = '<script type="text/javascript"';
  191. $expected .= ' src="/some_other_path/myfile.1.2.2.min.js"></script>';
  192. $this->assertEqual($result, $expected);
  193. $result = $this->html->script('some_other_path/myfile.1.2.2.min.js');
  194. $expected = '<script type="text/javascript"';
  195. $expected .= ' src="/js/some_other_path/myfile.1.2.2.min.js"></script>';
  196. $this->assertEqual($result, $expected);
  197. $result = $this->html->script('some_other_path/myfile.1.2.2.min');
  198. $expected = '<script type="text/javascript"';
  199. $expected .= ' src="/js/some_other_path/myfile.1.2.2.min.js"></script>';
  200. $this->assertEqual($result, $expected);
  201. $result = $this->html->script('http://example.com/jquery.js');
  202. $expected = '<script type="text/javascript" src="http://example.com/jquery.js"></script>';
  203. $this->assertEqual($result, $expected);
  204. $result = $this->html->script('//example.com/jquery.js');
  205. $expected = '<script type="text/javascript" src="//example.com/jquery.js"></script>';
  206. $this->assertEqual($result, $expected);
  207. $result = $this->html->script(array('prototype', 'scriptaculous'));
  208. $this->assertPattern(
  209. '/^\s*<script\s+type="text\/javascript"\s+src=".*js\/prototype\.js"[^<>]*><\/script>/',
  210. $result
  211. );
  212. $this->assertPattern('/<\/script>\s*<script[^<>]+>/', $result);
  213. $this->assertPattern(
  214. '/<script\s+type="text\/javascript"\s+src=".*js\/scriptaculous\.js"[^<>]*>' .
  215. '<\/script>\s*$/',
  216. $result
  217. );
  218. $result = $this->html->script("foo", array(
  219. 'async' => true, 'defer' => true, 'onload' => 'init()'
  220. ));
  221. $this->assertTags($result, array('script' => array(
  222. 'type' => 'text/javascript',
  223. 'src' => '/js/foo.js',
  224. 'async' => 'async',
  225. 'defer' => 'defer',
  226. 'onload' => 'init()'
  227. )));
  228. }
  229. /**
  230. * Tests generating image tags
  231. */
  232. public function testImage() {
  233. $result = $this->html->image('test.gif');
  234. $this->assertTags($result, array('img' => array('src' => '/img/test.gif', 'alt' => '')));
  235. $result = $this->html->image('http://example.com/logo.gif');
  236. $this->assertTags($result, array('img' => array(
  237. 'src' => 'http://example.com/logo.gif', 'alt' => ''
  238. )));
  239. $result = $this->html->image(array(
  240. 'controller' => 'test', 'action' => 'view', 'id' => '1', 'type' => 'gif'
  241. ));
  242. $this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
  243. $result = $this->html->image('/test/view/1.gif');
  244. $this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
  245. }
  246. /**
  247. * Tests inline style linking with <link /> tags
  248. */
  249. public function testStyleLink() {
  250. $result = $this->html->style('screen');
  251. $expected = array('link' => array(
  252. 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'regex:/.*css\/screen\.css/'
  253. ));
  254. $this->assertTags($result, $expected);
  255. $result = $this->html->style('screen.css');
  256. $this->assertTags($result, $expected);
  257. $result = $this->html->style('screen.css?1234');
  258. $expected['link']['href'] = 'regex:/.*css\/screen\.css\?1234/';
  259. $this->assertTags($result, $expected);
  260. $result = $this->html->style('http://whatever.com/screen.css?1234');
  261. $expected['link']['href'] = 'regex:/http:\/\/.*\/screen\.css\?1234/';
  262. $this->assertTags($result, $expected);
  263. }
  264. /**
  265. * Tests generating random tags for the <head> section
  266. */
  267. public function testHead() {
  268. $result = $this->html->head('meta', array('options' => array('author' => 'foo')));
  269. $expected = array('meta' => array('author' => 'foo'));
  270. $this->assertTags($result, $expected);
  271. $result = $this->html->head('unexisting-name', array(
  272. 'options' => array('author' => 'foo')
  273. ));
  274. $this->assertNull($result);
  275. }
  276. /**
  277. * Tests generating multiple <link /> or <style /> tags in a single call with an array
  278. */
  279. public function testStyleMulti() {
  280. $result = $this->html->style(array('base', 'layout'));
  281. $expected = array(
  282. 'link' => array(
  283. 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'regex:/.*css\/base\.css/'
  284. ),
  285. array(
  286. 'link' => array(
  287. 'rel' => 'stylesheet', 'type' => 'text/css',
  288. 'href' => 'regex:/.*css\/layout\.css/'
  289. )
  290. )
  291. );
  292. $this->assertTags($result, $expected);
  293. }
  294. /**
  295. * Tests that script and style tags with `'inline'` set to `false` are written to the rendering
  296. * context instead of being returned directly.
  297. */
  298. public function testNonInlineScriptsAndStyles() {
  299. $result = trim($this->context->scripts());
  300. $this->assertFalse($result);
  301. $result = $this->html->script('application', array('inline' => false));
  302. $this->assertFalse($result);
  303. $result = $this->context->scripts();
  304. $this->assertTags($result, array('script' => array(
  305. 'type' => 'text/javascript', 'src' => 'regex:/.*js\/application\.js/'
  306. )));
  307. $result = trim($this->context->styles());
  308. $this->assertFalse($result);
  309. $result = $this->html->style('base', array('inline' => false));
  310. $this->assertFalse($result);
  311. $result = $this->context->styles();
  312. $this->assertTags($result, array('link' => array(
  313. 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'regex:/.*css\/base\.css/'
  314. )));
  315. }
  316. /**
  317. * Tests that scripts and styles are correctly written to the rendering context even when
  318. * passing multiple scripts or styles to a single method call.
  319. */
  320. public function testMultiNonInlineScriptsAndStyles() {
  321. $result = $this->html->script(array('foo', 'bar'));
  322. $expected = array(
  323. array('script' => array('type' => 'text/javascript', 'src' => 'regex:/.*\/foo\.js/')),
  324. '/script',
  325. array('script' => array('type' => 'text/javascript', 'src' => 'regex:/.*\/bar\.js/')),
  326. '/script'
  327. );
  328. $this->assertTags($result, $expected);
  329. $this->assertNulL($this->html->script(array('foo', 'bar'), array('inline' => false)));
  330. $result = $this->context->scripts();
  331. $this->assertTags($result, $expected);
  332. }
  333. }
  334. ?>