HtmlListTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. // Call Zend_View_Helper_HtmlListTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HtmlListTest::main");
  5. }
  6. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/View.php';
  10. require_once 'Zend/View/Helper/HtmlList.php';
  11. class Zend_View_Helper_HtmlListTest extends PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var Zend_View_Helper_HtmlList
  15. */
  16. public $helper;
  17. /**
  18. * Runs the test methods of this class.
  19. *
  20. * @access public
  21. * @static
  22. */
  23. public static function main()
  24. {
  25. require_once "PHPUnit/TextUI/TestRunner.php";
  26. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HtmlListTest");
  27. $result = PHPUnit_TextUI_TestRunner::run($suite);
  28. }
  29. /**
  30. * Sets up the fixture, for example, open a network connection.
  31. * This method is called before a test is executed.
  32. *
  33. * @access protected
  34. */
  35. protected function setUp()
  36. {
  37. $this->view = new Zend_View();
  38. $this->helper = new Zend_View_Helper_HtmlList();
  39. $this->helper->setView($this->view);
  40. }
  41. public function tearDown()
  42. {
  43. unset($this->helper);
  44. }
  45. public function testMakeUnorderedList()
  46. {
  47. $items = array('one', 'two', 'three');
  48. $list = $this->helper->htmlList($items);
  49. $this->assertContains('<ul>', $list);
  50. $this->assertContains('</ul>', $list);
  51. foreach ($items as $item) {
  52. $this->assertContains('<li>' . $item . '</li>', $list);
  53. }
  54. }
  55. public function testMakeOrderedList()
  56. {
  57. $items = array('one', 'two', 'three');
  58. $list = $this->helper->htmlList($items, true);
  59. $this->assertContains('<ol>', $list);
  60. $this->assertContains('</ol>', $list);
  61. foreach ($items as $item) {
  62. $this->assertContains('<li>' . $item . '</li>', $list);
  63. }
  64. }
  65. public function testMakeUnorderedListWithAttribs()
  66. {
  67. $items = array('one', 'two', 'three');
  68. $attribs = array('class' => 'selected', 'name' => 'list');
  69. $list = $this->helper->htmlList($items, false, $attribs);
  70. $this->assertContains('<ul', $list);
  71. $this->assertContains('class="selected"', $list);
  72. $this->assertContains('name="list"', $list);
  73. $this->assertContains('</ul>', $list);
  74. foreach ($items as $item) {
  75. $this->assertContains('<li>' . $item . '</li>', $list);
  76. }
  77. }
  78. public function testMakeOrderedListWithAttribs()
  79. {
  80. $items = array('one', 'two', 'three');
  81. $attribs = array('class' => 'selected', 'name' => 'list');
  82. $list = $this->helper->htmlList($items, true, $attribs);
  83. $this->assertContains('<ol', $list);
  84. $this->assertContains('class="selected"', $list);
  85. $this->assertContains('name="list"', $list);
  86. $this->assertContains('</ol>', $list);
  87. foreach ($items as $item) {
  88. $this->assertContains('<li>' . $item . '</li>', $list);
  89. }
  90. }
  91. /*
  92. * @see ZF-5018
  93. */
  94. public function testMakeNestedUnorderedList()
  95. {
  96. $items = array('one', array('four', 'five', 'six'), 'two', 'three');
  97. $list = $this->helper->htmlList($items);
  98. $this->assertContains('<ul>' . Zend_View_Helper_HtmlList::EOL, $list);
  99. $this->assertContains('</ul>' . Zend_View_Helper_HtmlList::EOL, $list);
  100. $this->assertContains('one<ul>' . Zend_View_Helper_HtmlList::EOL.'<li>four', $list);
  101. $this->assertContains('<li>six</li>' . Zend_View_Helper_HtmlList::EOL . '</ul>' .
  102. Zend_View_Helper_HtmlList::EOL . '</li>' . Zend_View_Helper_HtmlList::EOL . '<li>two', $list);
  103. }
  104. /*
  105. * @see ZF-5018
  106. */
  107. public function testMakeNestedDeepUnorderedList()
  108. {
  109. $items = array('one', array('four', array('six', 'seven', 'eight'), 'five'), 'two', 'three');
  110. $list = $this->helper->htmlList($items);
  111. $this->assertContains('<ul>' . Zend_View_Helper_HtmlList::EOL, $list);
  112. $this->assertContains('</ul>' . Zend_View_Helper_HtmlList::EOL, $list);
  113. $this->assertContains('one<ul>' . Zend_View_Helper_HtmlList::EOL . '<li>four', $list);
  114. $this->assertContains('<li>four<ul>' . Zend_View_Helper_HtmlList::EOL . '<li>six', $list);
  115. $this->assertContains('<li>five</li>' . Zend_View_Helper_HtmlList::EOL . '</ul>' .
  116. Zend_View_Helper_HtmlList::EOL . '</li>' . Zend_View_Helper_HtmlList::EOL . '<li>two', $list);
  117. }
  118. public function testListWithValuesToEscapeForZF2283()
  119. {
  120. $items = array('one <small> test', 'second & third', 'And \'some\' "final" test');
  121. $list = $this->helper->htmlList($items);
  122. $this->assertContains('<ul>', $list);
  123. $this->assertContains('</ul>', $list);
  124. $this->assertContains('<li>one &lt;small&gt; test</li>', $list);
  125. $this->assertContains('<li>second &amp; third</li>', $list);
  126. $this->assertContains('<li>And \'some\' &quot;final&quot; test</li>', $list);
  127. }
  128. public function testListEscapeSwitchedOffForZF2283()
  129. {
  130. $items = array('one <b>small</b> test');
  131. $list = $this->helper->htmlList($items, false, false, false);
  132. $this->assertContains('<ul>', $list);
  133. $this->assertContains('</ul>', $list);
  134. $this->assertContains('<li>one <b>small</b> test</li>', $list);
  135. }
  136. /**
  137. * @see ZF-2527
  138. */
  139. public function testEscapeFlagHonoredForMultidimensionalLists()
  140. {
  141. $items = array('<b>one</b>', array('<b>four</b>', '<b>five</b>', '<b>six</b>'), '<b>two</b>', '<b>three</b>');
  142. $list = $this->helper->htmlList($items, false, false, false);
  143. foreach ($items[1] as $item) {
  144. $this->assertContains($item, $list);
  145. }
  146. }
  147. /**
  148. * @see ZF-2527
  149. * Added the s modifier to match newlines after @see ZF-5018
  150. */
  151. public function testAttribsPassedIntoMultidimensionalLists()
  152. {
  153. $items = array('one', array('four', 'five', 'six'), 'two', 'three');
  154. $list = $this->helper->htmlList($items, false, array('class' => 'foo'));
  155. foreach ($items[1] as $item) {
  156. $this->assertRegexp('#<ul[^>]*?class="foo"[^>]*>.*?(<li>' . $item . ')#s', $list);
  157. }
  158. }
  159. /**
  160. * @see ZF-2870
  161. */
  162. public function testEscapeFlagShouldBePassedRecursively()
  163. {
  164. $items = array(
  165. '<b>one</b>',
  166. array(
  167. '<b>four</b>',
  168. '<b>five</b>',
  169. '<b>six</b>',
  170. array(
  171. '<b>two</b>',
  172. '<b>three</b>',
  173. ),
  174. ),
  175. );
  176. $list = $this->helper->htmlList($items, false, false, false);
  177. $this->assertContains('<ul>', $list);
  178. $this->assertContains('</ul>', $list);
  179. array_walk_recursive($items, array($this, 'validateItems'), $list);
  180. }
  181. public function validateItems($value, $key, $userdata)
  182. {
  183. $this->assertContains('<li>' . $value, $userdata);
  184. }
  185. }
  186. // Call Zend_View_Helper_HtmlListTest::main() if this source file is executed directly.
  187. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HtmlListTest::main") {
  188. Zend_View_Helper_HtmlListTest::main();
  189. }