2
0

HtmlListTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_View_Helper_HtmlListTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HtmlListTest::main");
  25. }
  26. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. require_once 'Zend/View.php';
  30. require_once 'Zend/View/Helper/HtmlList.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_View
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_View
  38. * @group Zend_View_Helper
  39. */
  40. class Zend_View_Helper_HtmlListTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * @var Zend_View_Helper_HtmlList
  44. */
  45. public $helper;
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @access public
  50. * @static
  51. */
  52. public static function main()
  53. {
  54. require_once "PHPUnit/TextUI/TestRunner.php";
  55. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HtmlListTest");
  56. $result = PHPUnit_TextUI_TestRunner::run($suite);
  57. }
  58. /**
  59. * Sets up the fixture, for example, open a network connection.
  60. * This method is called before a test is executed.
  61. *
  62. * @access protected
  63. */
  64. protected function setUp()
  65. {
  66. $this->view = new Zend_View();
  67. $this->helper = new Zend_View_Helper_HtmlList();
  68. $this->helper->setView($this->view);
  69. }
  70. public function tearDown()
  71. {
  72. unset($this->helper);
  73. }
  74. public function testMakeUnorderedList()
  75. {
  76. $items = array('one', 'two', 'three');
  77. $list = $this->helper->htmlList($items);
  78. $this->assertContains('<ul>', $list);
  79. $this->assertContains('</ul>', $list);
  80. foreach ($items as $item) {
  81. $this->assertContains('<li>' . $item . '</li>', $list);
  82. }
  83. }
  84. public function testMakeOrderedList()
  85. {
  86. $items = array('one', 'two', 'three');
  87. $list = $this->helper->htmlList($items, true);
  88. $this->assertContains('<ol>', $list);
  89. $this->assertContains('</ol>', $list);
  90. foreach ($items as $item) {
  91. $this->assertContains('<li>' . $item . '</li>', $list);
  92. }
  93. }
  94. public function testMakeUnorderedListWithAttribs()
  95. {
  96. $items = array('one', 'two', 'three');
  97. $attribs = array('class' => 'selected', 'name' => 'list');
  98. $list = $this->helper->htmlList($items, false, $attribs);
  99. $this->assertContains('<ul', $list);
  100. $this->assertContains('class="selected"', $list);
  101. $this->assertContains('name="list"', $list);
  102. $this->assertContains('</ul>', $list);
  103. foreach ($items as $item) {
  104. $this->assertContains('<li>' . $item . '</li>', $list);
  105. }
  106. }
  107. public function testMakeOrderedListWithAttribs()
  108. {
  109. $items = array('one', 'two', 'three');
  110. $attribs = array('class' => 'selected', 'name' => 'list');
  111. $list = $this->helper->htmlList($items, true, $attribs);
  112. $this->assertContains('<ol', $list);
  113. $this->assertContains('class="selected"', $list);
  114. $this->assertContains('name="list"', $list);
  115. $this->assertContains('</ol>', $list);
  116. foreach ($items as $item) {
  117. $this->assertContains('<li>' . $item . '</li>', $list);
  118. }
  119. }
  120. /*
  121. * @see ZF-5018
  122. */
  123. public function testMakeNestedUnorderedList()
  124. {
  125. $items = array('one', array('four', 'five', 'six'), 'two', 'three');
  126. $list = $this->helper->htmlList($items);
  127. $this->assertContains('<ul>' . Zend_View_Helper_HtmlList::EOL, $list);
  128. $this->assertContains('</ul>' . Zend_View_Helper_HtmlList::EOL, $list);
  129. $this->assertContains('one<ul>' . Zend_View_Helper_HtmlList::EOL.'<li>four', $list);
  130. $this->assertContains('<li>six</li>' . Zend_View_Helper_HtmlList::EOL . '</ul>' .
  131. Zend_View_Helper_HtmlList::EOL . '</li>' . Zend_View_Helper_HtmlList::EOL . '<li>two', $list);
  132. }
  133. /*
  134. * @see ZF-5018
  135. */
  136. public function testMakeNestedDeepUnorderedList()
  137. {
  138. $items = array('one', array('four', array('six', 'seven', 'eight'), 'five'), 'two', 'three');
  139. $list = $this->helper->htmlList($items);
  140. $this->assertContains('<ul>' . Zend_View_Helper_HtmlList::EOL, $list);
  141. $this->assertContains('</ul>' . Zend_View_Helper_HtmlList::EOL, $list);
  142. $this->assertContains('one<ul>' . Zend_View_Helper_HtmlList::EOL . '<li>four', $list);
  143. $this->assertContains('<li>four<ul>' . Zend_View_Helper_HtmlList::EOL . '<li>six', $list);
  144. $this->assertContains('<li>five</li>' . Zend_View_Helper_HtmlList::EOL . '</ul>' .
  145. Zend_View_Helper_HtmlList::EOL . '</li>' . Zend_View_Helper_HtmlList::EOL . '<li>two', $list);
  146. }
  147. public function testListWithValuesToEscapeForZF2283()
  148. {
  149. $items = array('one <small> test', 'second & third', 'And \'some\' "final" test');
  150. $list = $this->helper->htmlList($items);
  151. $this->assertContains('<ul>', $list);
  152. $this->assertContains('</ul>', $list);
  153. $this->assertContains('<li>one &lt;small&gt; test</li>', $list);
  154. $this->assertContains('<li>second &amp; third</li>', $list);
  155. $this->assertContains('<li>And \'some\' &quot;final&quot; test</li>', $list);
  156. }
  157. public function testListEscapeSwitchedOffForZF2283()
  158. {
  159. $items = array('one <b>small</b> test');
  160. $list = $this->helper->htmlList($items, false, false, false);
  161. $this->assertContains('<ul>', $list);
  162. $this->assertContains('</ul>', $list);
  163. $this->assertContains('<li>one <b>small</b> test</li>', $list);
  164. }
  165. /**
  166. * @see ZF-2527
  167. */
  168. public function testEscapeFlagHonoredForMultidimensionalLists()
  169. {
  170. $items = array('<b>one</b>', array('<b>four</b>', '<b>five</b>', '<b>six</b>'), '<b>two</b>', '<b>three</b>');
  171. $list = $this->helper->htmlList($items, false, false, false);
  172. foreach ($items[1] as $item) {
  173. $this->assertContains($item, $list);
  174. }
  175. }
  176. /**
  177. * @see ZF-2527
  178. * Added the s modifier to match newlines after @see ZF-5018
  179. */
  180. public function testAttribsPassedIntoMultidimensionalLists()
  181. {
  182. $items = array('one', array('four', 'five', 'six'), 'two', 'three');
  183. $list = $this->helper->htmlList($items, false, array('class' => 'foo'));
  184. foreach ($items[1] as $item) {
  185. $this->assertRegexp('#<ul[^>]*?class="foo"[^>]*>.*?(<li>' . $item . ')#s', $list);
  186. }
  187. }
  188. /**
  189. * @see ZF-2870
  190. */
  191. public function testEscapeFlagShouldBePassedRecursively()
  192. {
  193. $items = array(
  194. '<b>one</b>',
  195. array(
  196. '<b>four</b>',
  197. '<b>five</b>',
  198. '<b>six</b>',
  199. array(
  200. '<b>two</b>',
  201. '<b>three</b>',
  202. ),
  203. ),
  204. );
  205. $list = $this->helper->htmlList($items, false, false, false);
  206. $this->assertContains('<ul>', $list);
  207. $this->assertContains('</ul>', $list);
  208. $this->markTestSkipped('Wrong array_walk_recursive behavior.');
  209. array_walk_recursive($items, array($this, 'validateItems'), $list);
  210. }
  211. public function validateItems($value, $key, $userdata)
  212. {
  213. $this->assertContains('<li>' . $value, $userdata);
  214. }
  215. }
  216. // Call Zend_View_Helper_HtmlListTest::main() if this source file is executed directly.
  217. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HtmlListTest::main") {
  218. Zend_View_Helper_HtmlListTest::main();
  219. }