HorizontalSliderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_Dojo
  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_Dojo_View_Helper_HorizontalSliderTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_View_Helper_HorizontalSliderTest::main");
  25. }
  26. /** Zend_Dojo_View_Helper_HorizontalSlider */
  27. require_once 'Zend/Dojo/View/Helper/HorizontalSlider.php';
  28. /** Zend_View */
  29. require_once 'Zend/View.php';
  30. /** Zend_Registry */
  31. require_once 'Zend/Registry.php';
  32. /** Zend_Dojo_Form */
  33. require_once 'Zend/Dojo/Form.php';
  34. /** Zend_Dojo_Form_SubForm */
  35. require_once 'Zend/Dojo/Form/SubForm.php';
  36. /** Zend_Dojo_View_Helper_Dojo */
  37. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  38. /**
  39. * Test class for Zend_Dojo_View_Helper_HorizontalSlider.
  40. *
  41. * @category Zend
  42. * @package Zend_Dojo
  43. * @subpackage UnitTests
  44. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. * @group Zend_Dojo
  47. * @group Zend_Dojo_View
  48. */
  49. class Zend_Dojo_View_Helper_HorizontalSliderTest extends PHPUnit_Framework_TestCase
  50. {
  51. /**
  52. * Runs the test methods of this class.
  53. *
  54. * @return void
  55. */
  56. public static function main()
  57. {
  58. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_View_Helper_HorizontalSliderTest");
  59. $result = PHPUnit_TextUI_TestRunner::run($suite);
  60. }
  61. /**
  62. * Sets up the fixture, for example, open a network connection.
  63. * This method is called before a test is executed.
  64. *
  65. * @return void
  66. */
  67. public function setUp()
  68. {
  69. Zend_Registry::_unsetInstance();
  70. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  71. $this->view = $this->getView();
  72. $this->helper = new Zend_Dojo_View_Helper_HorizontalSlider();
  73. $this->helper->setView($this->view);
  74. }
  75. /**
  76. * Tears down the fixture, for example, close a network connection.
  77. * This method is called after a test is executed.
  78. *
  79. * @return void
  80. */
  81. public function tearDown()
  82. {
  83. }
  84. public function getView()
  85. {
  86. require_once 'Zend/View.php';
  87. $view = new Zend_View();
  88. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  89. return $view;
  90. }
  91. public function getElement()
  92. {
  93. return $this->helper->horizontalSlider(
  94. 'elementId',
  95. '',
  96. array(
  97. 'minimum' => -10,
  98. 'maximum' => 10,
  99. 'discreteValues' => 11,
  100. 'topDecoration' => array(
  101. 'labels' => array(
  102. ' ',
  103. '20%',
  104. '40%',
  105. '60%',
  106. '80%',
  107. ' ',
  108. ),
  109. 'container' => 'top',
  110. 'attribs' => array(
  111. 'container' => array(
  112. 'style' => 'height:1.2em; font-size=75%;color:gray;',
  113. ),
  114. 'labels' => array(
  115. 'style' => 'height:1em; font-size=75%;color:gray;',
  116. ),
  117. ),
  118. 'dijit' => 'HorizontalRuleLabels',
  119. ),
  120. 'bottomDecoration' => array(
  121. 'labels' => array(
  122. '0%',
  123. '50%',
  124. '100%',
  125. ),
  126. 'attribs' => array(
  127. 'labels' => array(
  128. 'style' => 'height:1em; font-size=75%;color:gray;',
  129. ),
  130. ),
  131. ),
  132. 'leftDecoration' => array(
  133. 'labels' => array(
  134. ' ',
  135. '20%',
  136. '40%',
  137. '60%',
  138. '80%',
  139. ' ',
  140. ),
  141. 'attribs' => array(
  142. 'container' => array(
  143. 'style' => 'height:1.2em; font-size=75%;color:gray;',
  144. ),
  145. 'labels' => array(
  146. 'style' => 'height:1em; font-size=75%;color:gray;',
  147. ),
  148. ),
  149. 'dijit' => 'VerticalRuleLabels',
  150. ),
  151. 'rightDecoration' => array(
  152. 'labels' => array(
  153. '0%',
  154. '50%',
  155. '100%',
  156. ),
  157. 'attribs' => array(
  158. 'labels' => array(
  159. 'style' => 'height:1em; font-size=75%;color:gray;',
  160. ),
  161. ),
  162. ),
  163. ),
  164. array()
  165. );
  166. }
  167. public function testShouldAllowDeclarativeDijitCreation()
  168. {
  169. $html = $this->getElement();
  170. $this->assertRegexp('/<div[^>]*(dojoType="dijit.form.HorizontalSlider")/', $html, $html);
  171. }
  172. public function testShouldAllowProgrammaticDijitCreation()
  173. {
  174. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
  175. $html = $this->getElement();
  176. $this->assertNotRegexp('/<div[^>]*(dojoType="dijit.form.HorizontalSlider")/', $html);
  177. $this->assertNotNull($this->view->dojo()->getDijit('elementId-slider'));
  178. }
  179. public function testShouldCreateOnChangeAttributeByDefault()
  180. {
  181. $html = $this->getElement();
  182. // Note that ' is converted to &#39; in Zend_View_Helper_HtmlElement::_htmlAttribs() (line 116)
  183. $this->assertContains('onChange="dojo.byId(&#39;elementId&#39;).value = arguments[0];"', $html, $html);
  184. }
  185. public function testShouldCreateHiddenElementWithValue()
  186. {
  187. $html = $this->getElement();
  188. if (!preg_match('/(<input[^>]*(type="hidden")[^>]*>)/', $html, $m)) {
  189. $this->fail('No hidden element found');
  190. }
  191. $this->assertContains('id="elementId"', $m[1]);
  192. $this->assertContains('value="', $m[1]);
  193. }
  194. public function testShouldCreateTopAndBottomDecorationsWhenRequested()
  195. {
  196. $html = $this->getElement();
  197. $this->assertRegexp('/<div[^>]*(dojoType="dijit.form.HorizontalRule")/', $html, $html);
  198. $this->assertRegexp('/<ol[^>]*(dojoType="dijit.form.HorizontalRuleLabels")/', $html, $html);
  199. $this->assertContains('topDecoration', $html);
  200. $this->assertContains('bottomDecoration', $html);
  201. }
  202. public function testShouldIgnoreLeftAndRightDecorationsWhenPassed()
  203. {
  204. $html = $this->getElement();
  205. $this->assertNotContains('leftDecoration', $html);
  206. $this->assertNotContains('rightDecoration', $html);
  207. }
  208. /**
  209. * @expectedException Zend_Dojo_View_Exception
  210. */
  211. public function testSliderShouldRaiseExceptionIfMissingRequiredParameters()
  212. {
  213. $this->helper->prepareSlider('foo', 4);
  214. }
  215. public function testShouldAllowPassingLabelParametersViaDecorationParameters()
  216. {
  217. $html = $this->helper->horizontalSlider(
  218. 'elementId',
  219. '',
  220. array(
  221. 'minimum' => -10,
  222. 'maximum' => 10,
  223. 'discreteValues' => 11,
  224. 'topDecoration' => array(
  225. 'labels' => array(
  226. ' ',
  227. '20%',
  228. '40%',
  229. '60%',
  230. '80%',
  231. ' ',
  232. ),
  233. 'params' => array(
  234. 'required' => true,
  235. 'labels' => array(
  236. 'minimum' => 5,
  237. )
  238. ),
  239. 'dijit' => 'HorizontalRuleLabels',
  240. ),
  241. )
  242. );
  243. $this->assertContains('required="', $html);
  244. $this->assertContains('minimum="', $html);
  245. }
  246. /**
  247. * @group ZF-4435
  248. */
  249. public function testShouldCreateAppropriateIdsForElementsInSubForms()
  250. {
  251. $form = new Zend_Dojo_Form;
  252. $form->setDecorators(array(
  253. 'FormElements',
  254. array('TabContainer', array(
  255. 'id' => 'tabContainer',
  256. 'style' => 'width: 600px; height: 300px;',
  257. 'dijitParams' => array(
  258. 'tabPosition' => 'top'
  259. ),
  260. )),
  261. 'DijitForm',
  262. ));
  263. $sliderForm = new Zend_Dojo_Form_SubForm();
  264. $sliderForm->setAttribs(array(
  265. 'name' => 'slidertab',
  266. 'legend' => 'Slider Elements',
  267. ));
  268. $sliderForm->addElement(
  269. 'HorizontalSlider',
  270. 'slide1',
  271. array(
  272. 'label' => 'Slide me:',
  273. 'minimum' => 0,
  274. 'maximum' => 25,
  275. 'discreteValues' => 10,
  276. 'style' => 'width: 450px;',
  277. 'topDecorationDijit' => 'HorizontalRuleLabels',
  278. 'topDecorationLabels' => array('0%', '50%', '100%'),
  279. 'topDecorationParams' => array('style' => 'padding-bottom: 20px;')
  280. )
  281. );
  282. $form->addSubForm($sliderForm, 'slidertab')
  283. ->setView($this->getView());
  284. $html = $form->render();
  285. $this->assertContains('id="slidertab-slide1-slider"', $html);
  286. $this->assertContains('id="slidertab-slide1-slider-topDecoration"', $html);
  287. $this->assertContains('id="slidertab-slide1-slider-topDecoration-labels"', $html);
  288. }
  289. /**
  290. * @group ZF-5220
  291. */
  292. public function testLabelDivShouldOpenAndCloseBeforeLabelOl()
  293. {
  294. $html = $this->getElement();
  295. $this->assertNotRegexp('/<div[^>]*(dojoType="dijit.form.HorizontalRuleLabels")[^>]*><\/div>\s*<ol/s', $html, $html);
  296. $this->assertRegexp('/<div[^>]*><\/div>\s*<ol[^>]*(dojoType="dijit.form.HorizontalRuleLabels")/s', $html, $html);
  297. }
  298. }
  299. // Call Zend_Dojo_View_Helper_HorizontalSliderTest::main() if this source file is executed directly.
  300. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_HorizontalSliderTest::main") {
  301. Zend_Dojo_View_Helper_HorizontalSliderTest::main();
  302. }