MultiselectTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. // Call Zend_Form_Element_MultiselectTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_MultiselectTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Form/Element/Multiselect.php';
  8. require_once 'Zend/Translate.php';
  9. /**
  10. * Test class for Zend_Form_Element_Multiselect
  11. */
  12. class Zend_Form_Element_MultiselectTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @return void
  18. */
  19. public static function main()
  20. {
  21. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_MultiselectTest");
  22. $result = PHPUnit_TextUI_TestRunner::run($suite);
  23. }
  24. /**
  25. * Sets up the fixture, for example, open a network connection.
  26. * This method is called before a test is executed.
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. $this->element = new Zend_Form_Element_Multiselect('foo');
  33. }
  34. /**
  35. * Tears down the fixture, for example, close a network connection.
  36. * This method is called after a test is executed.
  37. *
  38. * @return void
  39. */
  40. public function tearDown()
  41. {
  42. }
  43. public function getView()
  44. {
  45. require_once 'Zend/View.php';
  46. $view = new Zend_View();
  47. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper/');
  48. return $view;
  49. }
  50. public function testMultiselectElementInstanceOfMultiElement()
  51. {
  52. $this->assertTrue($this->element instanceof Zend_Form_Element_Multi);
  53. }
  54. public function testMultiselectElementInstanceOfXhtmlElement()
  55. {
  56. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  57. }
  58. public function testMultiselectElementInstanceOfBaseElement()
  59. {
  60. $this->assertTrue($this->element instanceof Zend_Form_Element);
  61. }
  62. public function testMultiselectElementIsAnArrayByDefault()
  63. {
  64. $this->assertTrue($this->element->isArray());
  65. }
  66. public function testMultiselectElementUsesSelectHelperInViewHelperDecoratorByDefault()
  67. {
  68. $this->_checkZf2794();
  69. $decorator = $this->element->getDecorator('viewHelper');
  70. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  71. $decorator->setElement($this->element);
  72. $helper = $decorator->getHelper();
  73. $this->assertEquals('formSelect', $helper);
  74. }
  75. public function testMultipleOptionSetByDefault()
  76. {
  77. $this->assertNotNull($this->element->multiple);
  78. $this->assertEquals('multiple', $this->element->multiple);
  79. }
  80. public function testHasDefaultSeparator()
  81. {
  82. $this->assertEquals('<br />', $this->element->getSeparator());
  83. }
  84. public function testCanSetSeparator()
  85. {
  86. $this->testHasDefaultSeparator();
  87. $this->element->setSeparator("\n");
  88. $this->assertEquals("\n", $this->element->getSeparator());
  89. }
  90. public function testMultiOptionsEmptyByDefault()
  91. {
  92. $options = $this->element->getMultiOptions();
  93. $this->assertTrue(is_array($options));
  94. $this->assertTrue(empty($options));
  95. }
  96. public function testCanSetMultiOptions()
  97. {
  98. $this->testMultiOptionsEmptyByDefault();
  99. $this->element->addMultiOption('foo', 'foovalue');
  100. $this->assertEquals('foovalue', $this->element->getMultiOption('foo'));
  101. $this->element->setMultiOptions(array('bar' => 'barvalue', 'baz' => 'bazvalue'));
  102. $this->assertEquals(array('bar' => 'barvalue', 'baz' => 'bazvalue'), $this->element->getMultiOptions());
  103. $this->element->addMultiOptions(array('bat' => 'batvalue', 'foo' => 'foovalue'));
  104. $this->assertEquals(array('bar' => 'barvalue', 'baz' => 'bazvalue', 'bat' => 'batvalue', 'foo' => 'foovalue'), $this->element->getMultiOptions());
  105. $this->element->addMultiOption('test', 'testvalue');
  106. $this->assertEquals(array('bar' => 'barvalue', 'baz' => 'bazvalue', 'bat' => 'batvalue', 'foo' => 'foovalue', 'test' => 'testvalue'), $this->element->getMultiOptions());
  107. }
  108. /**
  109. * @see ZF-2824
  110. */
  111. public function testCanSetMultiOptionsUsingAssocArraysWithKeyValueKeys()
  112. {
  113. $options = array(
  114. array(
  115. 'value' => '1',
  116. 'key' => 'aa',
  117. ),
  118. array (
  119. 'key' => '2',
  120. 'value' => 'xxxx',
  121. ),
  122. array (
  123. 'value' => '444',
  124. 'key' => 'ssss',
  125. ),
  126. );
  127. $this->element->addMultiOptions($options);
  128. $this->assertEquals($options[0]['value'], $this->element->getMultiOption('aa'));
  129. $this->assertEquals($options[1]['value'], $this->element->getMultiOption(2));
  130. $this->assertEquals($options[2]['value'], $this->element->getMultiOption('ssss'));
  131. }
  132. /**
  133. * @see ZF-2824
  134. */
  135. public function testCanSetMultiOptionsUsingConfigWithKeyValueKeys()
  136. {
  137. require_once 'Zend/Config/Xml.php';
  138. $config = new Zend_Config_Xml(dirname(__FILE__) . '/../_files/config/multiOptions.xml', 'testing');
  139. $this->element->setMultiOptions($config->options->toArray());
  140. $this->assertEquals($config->options->first->value, $this->element->getMultiOption('aa'));
  141. $this->assertEquals($config->options->second->value, $this->element->getMultiOption(2));
  142. $this->assertEquals($config->options->third->value, $this->element->getMultiOption('ssss'));
  143. require_once 'Zend/Config/Ini.php';
  144. $config = new Zend_Config_Ini(dirname(__FILE__) . '/../_files/config/multiOptions.ini', 'testing');
  145. $this->element->setMultiOptions($config->options->toArray());
  146. $this->assertEquals($config->options->first->value, $this->element->getMultiOption('aa'));
  147. $this->assertEquals($config->options->second->value, $this->element->getMultiOption(2));
  148. $this->assertEquals($config->options->third->value, $this->element->getMultiOption('ssss'));
  149. }
  150. public function testCanRemoveMultiOption()
  151. {
  152. $this->testMultiOptionsEmptyByDefault();
  153. $this->element->addMultiOption('foo', 'foovalue');
  154. $this->assertEquals('foovalue', $this->element->getMultiOption('foo'));
  155. $this->element->removeMultiOption('foo');
  156. $this->assertNull($this->element->getMultiOption('foo'));
  157. }
  158. public function testOptionsAreRenderedInFinalMarkup()
  159. {
  160. $options = array(
  161. 'foovalue' => 'Foo',
  162. 'barvalue' => 'Bar'
  163. );
  164. $this->element->addMultiOptions($options);
  165. $html = $this->element->render($this->getView());
  166. foreach ($options as $value => $label) {
  167. $this->assertRegexp('/<option.*value="' . $value . '"[^>]*>' . $label . '/s', $html, $html);
  168. }
  169. }
  170. public function testTranslatedOptionsAreRenderedInFinalMarkupWhenTranslatorPresent()
  171. {
  172. $translations = array(
  173. 'ThisShouldNotShow' => 'Foo Value',
  174. 'ThisShouldNeverShow' => 'Bar Value'
  175. );
  176. require_once 'Zend/Translate.php';
  177. $translate = new Zend_Translate('array', $translations, 'en');
  178. $translate->setLocale('en');
  179. $options = array(
  180. 'foovalue' => 'ThisShouldNotShow',
  181. 'barvalue' => 'ThisShouldNeverShow'
  182. );
  183. $this->element->setTranslator($translate)
  184. ->addMultiOptions($options);
  185. $html = $this->element->render($this->getView());
  186. foreach ($options as $value => $label) {
  187. $this->assertNotContains($label, $html, $html);
  188. $this->assertRegexp('/<option.*value="' . $value . '"[^>]*>' . $translations[$label] . '/s', $html, $html);
  189. }
  190. }
  191. public function testOptionLabelsAreTranslatedWhenTranslateAdapterIsPresent()
  192. {
  193. $translations = include dirname(__FILE__) . '/../_files/locale/array.php';
  194. $translate = new Zend_Translate('array', $translations, 'en');
  195. $translate->setLocale('en');
  196. $options = array(
  197. 'foovalue' => 'Foo',
  198. 'barvalue' => 'Bar'
  199. );
  200. $this->element->addMultiOptions($options)
  201. ->setTranslator($translate);
  202. $test = $this->element->getMultiOption('barvalue');
  203. $this->assertEquals($translations[$options['barvalue']], $test);
  204. $test = $this->element->getMultiOptions();
  205. foreach ($test as $key => $value) {
  206. $this->assertEquals($translations[$options[$key]], $value);
  207. }
  208. }
  209. public function testOptionLabelsAreUntouchedIfTranslatonDoesNotExistInnTranslateAdapter()
  210. {
  211. $translations = include dirname(__FILE__) . '/../_files/locale/array.php';
  212. $translate = new Zend_Translate('array', $translations, 'en');
  213. $translate->setLocale('en');
  214. $options = array(
  215. 'foovalue' => 'Foo',
  216. 'barvalue' => 'Bar',
  217. 'testing' => 'Test Value',
  218. );
  219. $this->element->addMultiOptions($options)
  220. ->setTranslator($translate);
  221. $test = $this->element->getMultiOption('testing');
  222. $this->assertEquals($options['testing'], $test);
  223. }
  224. public function testMultiselectIsArrayByDefault()
  225. {
  226. $this->assertTrue($this->element->isArray());
  227. }
  228. /**
  229. * @group ZF-5568
  230. */
  231. public function testOptGroupTranslationsShouldWorkAfterPopulatingElement()
  232. {
  233. $translations = array(
  234. 'ThisIsTheLabel' => 'Optgroup label',
  235. 'ThisShouldNotShow' => 'Foo Value',
  236. 'ThisShouldNeverShow' => 'Bar Value'
  237. );
  238. require_once 'Zend/Translate.php';
  239. $translate = new Zend_Translate('array', $translations, 'en');
  240. $translate->setLocale('en');
  241. $options = array(
  242. 'ThisIsTheLabel' => array(
  243. 'foovalue' => 'ThisShouldNotShow',
  244. 'barvalue' => 'ThisShouldNeverShow',
  245. ),
  246. );
  247. $this->element->setTranslator($translate)
  248. ->addMultiOptions($options);
  249. $this->element->setValue('barValue');
  250. $html = $this->element->render($this->getView());
  251. $this->assertContains($translations['ThisIsTheLabel'], $html, $html);
  252. }
  253. /**
  254. * @group ZF-5937
  255. */
  256. public function testAddMultiOptionShouldWorkAfterTranslatorIsDisabled()
  257. {
  258. $options = array(
  259. 'foovalue' => 'Foo',
  260. );
  261. $this->element->setDisableTranslator(true)
  262. ->addMultiOptions($options);
  263. $test = $this->element->getMultiOption('foovalue');
  264. $this->assertEquals($options['foovalue'], $test);
  265. }
  266. /**
  267. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  268. *
  269. * @link http://framework.zend.com/issues/browse/ZF-2794
  270. * @return void
  271. */
  272. protected function _checkZf2794()
  273. {
  274. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  275. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  276. }
  277. }
  278. }
  279. // Call Zend_Form_Element_MultiselectTest::main() if this source file is executed directly.
  280. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_MultiselectTest::main") {
  281. Zend_Form_Element_MultiselectTest::main();
  282. }