MultiselectTest.php 12 KB

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