html.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Html class tests
  15. *
  16. * @group Core
  17. * @group Html
  18. */
  19. class Test_Html extends TestCase
  20. {
  21. /**
  22. * Tests Html::meta()
  23. *
  24. * @test
  25. */
  26. public function test_meta()
  27. {
  28. $output = Html::meta('description', 'Meta Example!');
  29. $expected = '<meta name="description" content="Meta Example!" />';
  30. $this->assertEquals($expected, $output);
  31. $output = Html::meta('robots', 'no-cache');
  32. $expected = '<meta name="robots" content="no-cache" />';
  33. $this->assertEquals($expected, $output);
  34. $meta = array(
  35. array('name' => 'robots', 'content' => 'no-cache'),
  36. array('name' => 'description', 'content' => 'Meta Example'),
  37. array('name' => 'keywords', 'content' => 'fuel, rocks'),
  38. );
  39. $output = Html::meta($meta);
  40. $expected = '
  41. <meta name="robots" content="no-cache" />
  42. <meta name="description" content="Meta Example" />
  43. <meta name="keywords" content="fuel, rocks" />';
  44. $this->assertEquals($expected, $output);
  45. }
  46. /**
  47. * Tests Html::anchor()
  48. *
  49. * @test
  50. */
  51. public function test_anchor()
  52. {
  53. // Query string tests
  54. Config::set('url_suffix', '');
  55. Config::set('index_file', '');
  56. // External uri
  57. $output = Html::anchor('http://google.com', 'Go to Google');
  58. $expected = '<a href="http://google.com">Go to Google</a>';
  59. $this->assertEquals($expected, $output);
  60. $output = Html::anchor('javascript:do();', 'Do()');
  61. $expected = '<a href="javascript:do();">Do()</a>';
  62. $this->assertEquals($expected, $output);
  63. $output = Html::anchor('http://google.com', 'Go to Google', array('rel' => 'example', 'class' => 'sample', 'style' => 'color:red;'));
  64. $expected = '<a rel="example" class="sample" style="color:red;" href="http://google.com">Go to Google</a>';
  65. $this->assertEquals($expected, $output);
  66. // External secure uri
  67. $output = Html::anchor('http://google.com', 'Go to Google', array('rel' => 'example', 'class' => 'sample', 'style' => 'color:red;'), true);
  68. $expected = '<a rel="example" class="sample" style="color:red;" href="https://google.com">Go to Google</a>';
  69. $this->assertEquals($expected, $output);
  70. // Internal uri
  71. $output = Html::anchor('controller/method', 'Method');
  72. $expected = '<a href="controller/method">Method</a>';
  73. $this->assertEquals($expected, $output);
  74. // Internal secure uri
  75. $host = \Input::server('http_host');
  76. $_SERVER['HTTP_HOST'] = 'fuelphp.com';
  77. $output = Html::anchor('controller/method', 'Method', array(), true);
  78. $expected = '<a href="https://'.\Input::server('http_host').'/controller/method">Method</a>';
  79. $this->assertEquals($expected, $output);
  80. $_SERVER['HTTP_HOST'] = $host;
  81. // Get original values to reset once done
  82. $index_file = Config::get('index_file');
  83. $url_suffix = Config::get('url_suffix');
  84. $output = Html::anchor('search?q=query', 'Search');
  85. $expected = '<a href="search?q=query">Search</a>';
  86. $this->assertEquals($expected, $output);
  87. Config::set('url_suffix', '.html');
  88. $output = Html::anchor('search?q=query', 'Search');
  89. $expected = '<a href="search.html?q=query">Search</a>';
  90. $this->assertEquals($expected, $output);
  91. // Reset to original values
  92. Config::set('index_file', $index_file);
  93. Config::set('url_suffix', $url_suffix);
  94. }
  95. /**
  96. * Tests Html::img()
  97. *
  98. * This test does not account for the image file existing in
  99. * the filesystem. There are no images bundled with the framework
  100. * by default, so no reliable test can be run on an actual image.
  101. *
  102. * @test
  103. */
  104. public function test_img()
  105. {
  106. $image_path = 'image.png';
  107. // Internal uri
  108. $output = Html::img('image.png');
  109. $expected = '<img src="'. $image_path . '" alt="image" />';
  110. $this->assertEquals($expected, $output);
  111. $output = Html::img('image.png', array('alt' => 'Image'));
  112. $expected = '<img alt="Image" src="'. $image_path . '" />';
  113. $this->assertEquals($expected, $output);
  114. // External uri
  115. $output = Html::img('http://google.com/image.png');
  116. $expected = '<img src="http://google.com/image.png" />';
  117. }
  118. /**
  119. * Tests Html::prep_url()
  120. *
  121. * @test
  122. */
  123. public function test_prep_url()
  124. {
  125. $output = Html::prep_url('google.com');
  126. $expected = 'http://google.com';
  127. $this->assertEquals($expected, $output);
  128. $output = Html::prep_url('google.com', 'https');
  129. $expected = 'https://google.com';
  130. $this->assertEquals($expected, $output);
  131. }
  132. /**
  133. * Tests Html::mail_to()
  134. *
  135. * @test
  136. */
  137. public function test_mail_to()
  138. {
  139. $output = Html::mail_to('[email protected]');
  140. $expected = '<a href="mailto:[email protected]">[email protected]</a>';
  141. $this->assertEquals($expected, $output);
  142. $output = Html::mail_to('[email protected]', 'Email');
  143. $expected = '<a href="mailto:[email protected]">Email</a>';
  144. $this->assertEquals($expected, $output);
  145. $output = Html::mail_to('[email protected]', NULL, 'Test');
  146. $expected = '<a href="mailto:[email protected]?subject=Test">[email protected]</a>';
  147. $this->assertEquals($expected, $output);
  148. $output = Html::mail_to('[email protected]', 'Test', 'Test');
  149. $expected = '<a href="mailto:[email protected]?subject=Test">Test</a>';
  150. $this->assertEquals($expected, $output);
  151. }
  152. /**
  153. * Tests Html::doctype()
  154. *
  155. * @test
  156. */
  157. public function test_doctype()
  158. {
  159. $output = Html::doctype();
  160. $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  161. $this->assertEquals($expected, $output);
  162. $output = Html::doctype('html5');
  163. $expected = '<!DOCTYPE html>';
  164. $this->assertEquals($expected, $output);
  165. // Ensure static::$html5 is set
  166. $doctype = Html::doctype('html5');
  167. $this->assertTrue(Html::$html5);
  168. // Ensure === false if doctype is invalid
  169. $this->assertFalse(Html::doctype('shouldfail'));
  170. }
  171. /**
  172. * Tests Html::ul() & Html::ol()
  173. *
  174. * @test
  175. */
  176. public function test_lists()
  177. {
  178. $list = array('one', 'two');
  179. $output = Html::ul($list);
  180. $expected = '<ul>'.PHP_EOL
  181. .' <li>one</li>'.PHP_EOL
  182. .' <li>two</li>'.PHP_EOL
  183. .'</ul>'.PHP_EOL;
  184. $this->assertEquals($expected, $output);
  185. $output = Html::ol($list);
  186. $expected = '<ol>'.PHP_EOL
  187. .' <li>one</li>'.PHP_EOL
  188. .' <li>two</li>'.PHP_EOL
  189. .'</ol>'.PHP_EOL;
  190. $this->assertEquals($expected, $output);
  191. }
  192. }