MultiselectTest.php 13 KB

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