MultiselectTest.php 12 KB

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