PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Form/SelectViewHelperTest.php

https://github.com/andreaswolf/typo3-tceforms
PHP | 432 lines | 287 code | 58 blank | 87 comment | 0 complexity | f361f44c3425ed8b502296461223a489 MD5 | raw file
Possible License(s): Apache-2.0, BSD-2-Clause, LGPL-3.0
  1. <?php
  2. /* *
  3. * This script belongs to the FLOW3 package "Fluid". *
  4. * *
  5. * It is free software; you can redistribute it and/or modify it under *
  6. * the terms of the GNU Lesser General Public License as published by the *
  7. * Free Software Foundation, either version 3 of the License, or (at your *
  8. * option) any later version. *
  9. * *
  10. * This script is distributed in the hope that it will be useful, but *
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
  12. * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
  13. * General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU Lesser General Public *
  16. * License along with the script. *
  17. * If not, see http://www.gnu.org/licenses/lgpl.html *
  18. * *
  19. * The TYPO3 project - inspiring people to share! *
  20. * */
  21. require_once(dirname(__FILE__) . '/Fixtures/EmptySyntaxTreeNode.php');
  22. require_once(dirname(__FILE__) . '/Fixtures/Fixture_UserDomainClass.php');
  23. require_once(dirname(__FILE__) . '/../ViewHelperBaseTestcase.php');
  24. /**
  25. * Test for the "Select" Form view helper
  26. *
  27. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
  28. */
  29. class Tx_Fluid_Tests_Unit_ViewHelpers_Form_SelectViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
  30. /**
  31. * var Tx_Fluid_ViewHelpers_Form_SelectViewHelper
  32. */
  33. protected $viewHelper;
  34. public function setUp() {
  35. parent::setUp();
  36. $this->viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_SelectViewHelper', array('setErrorClassAttribute', 'registerFieldNameForFormTokenGeneration'));
  37. $this->injectDependenciesIntoViewHelper($this->viewHelper);
  38. $this->viewHelper->initializeArguments();
  39. }
  40. /**
  41. * @test
  42. * @author Bastian Waidelich <bastian@typo3.org>
  43. */
  44. public function selectCorrectlySetsTagName() {
  45. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName'), array(), '', FALSE);
  46. $mockTagBuilder->expects($this->once())->method('setTagName')->with('select');
  47. $this->viewHelper->_set('tag', $mockTagBuilder);
  48. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  49. 'options' => array()
  50. ));
  51. $this->viewHelper->setArguments($arguments);
  52. $this->viewHelper->initialize();
  53. $this->viewHelper->render();
  54. }
  55. /**
  56. * @test
  57. * @author Sebastian Kurfürst <sebastian@typo3.org>
  58. * @author Bastian Waidelich <bastian@typo3.org>
  59. */
  60. public function selectCreatesExpectedOptions() {
  61. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  62. $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
  63. $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
  64. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
  65. $mockTagBuilder->expects($this->once())->method('render');
  66. $this->viewHelper->_set('tag', $mockTagBuilder);
  67. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  68. 'options' => array(
  69. 'value1' => 'label1',
  70. 'value2' => 'label2'
  71. ),
  72. 'value' => 'value2',
  73. 'name' => 'myName'
  74. ));
  75. $this->viewHelper->setArguments($arguments);
  76. $this->viewHelper->initialize();
  77. $this->viewHelper->render();
  78. }
  79. /**
  80. * @test
  81. * @author Bastian Waidelich <bastian@typo3.org>
  82. */
  83. public function anEmptyOptionTagIsRenderedIfOptionsArrayIsEmptyToAssureXhtmlCompatibility() {
  84. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  85. $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
  86. $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
  87. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value=""></option>' . chr(10));
  88. $mockTagBuilder->expects($this->once())->method('render');
  89. $this->viewHelper->_set('tag', $mockTagBuilder);
  90. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  91. 'options' => array(),
  92. 'value' => 'value2',
  93. 'name' => 'myName'
  94. ));
  95. $this->viewHelper->setArguments($arguments);
  96. $this->viewHelper->initialize();
  97. $this->viewHelper->render();
  98. }
  99. /**
  100. * @test
  101. * @author Bastian Waidelich <bastian@typo3.org>
  102. */
  103. public function OrderOfOptionsIsNotAlteredByDefault() {
  104. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  105. $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
  106. $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
  107. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value3">label3</option>' . chr(10) . '<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
  108. $mockTagBuilder->expects($this->once())->method('render');
  109. $this->viewHelper->_set('tag', $mockTagBuilder);
  110. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  111. 'options' => array(
  112. 'value3' => 'label3',
  113. 'value1' => 'label1',
  114. 'value2' => 'label2'
  115. ),
  116. 'value' => 'value2',
  117. 'name' => 'myName'
  118. ));
  119. $this->viewHelper->setArguments($arguments);
  120. $this->viewHelper->initialize();
  121. $this->viewHelper->render();
  122. }
  123. /**
  124. * @test
  125. * @author Bastian Waidelich <bastian@typo3.org>
  126. */
  127. public function optionsAreSortedByLabelIfSortByOptionLabelIsSet() {
  128. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  129. $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
  130. $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
  131. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
  132. $mockTagBuilder->expects($this->once())->method('render');
  133. $this->viewHelper->_set('tag', $mockTagBuilder);
  134. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  135. 'options' => array(
  136. 'value3' => 'label3',
  137. 'value1' => 'label1',
  138. 'value2' => 'label2'
  139. ),
  140. 'value' => 'value2',
  141. 'name' => 'myName',
  142. 'sortByOptionLabel' => TRUE
  143. ));
  144. $this->viewHelper->setArguments($arguments);
  145. $this->viewHelper->initialize();
  146. $this->viewHelper->render();
  147. }
  148. /**
  149. * @test
  150. * @author Bastian Waidelich <bastian@typo3.org>
  151. */
  152. public function multipleSelectCreatesExpectedOptions() {
  153. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  154. $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('multiple', 'multiple');
  155. $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('name', 'myName[]');
  156. $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
  157. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1" selected="selected">label1</option>' . chr(10) . '<option value="value2">label2</option>' . chr(10) . '<option value="value3" selected="selected">label3</option>' . chr(10));
  158. $mockTagBuilder->expects($this->once())->method('render');
  159. $this->viewHelper->_set('tag', $mockTagBuilder);
  160. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  161. 'options' => array(
  162. 'value1' => 'label1',
  163. 'value2' => 'label2',
  164. 'value3' => 'label3'
  165. ),
  166. 'value' => array('value3', 'value1'),
  167. 'name' => 'myName',
  168. 'multiple' => 'multiple',
  169. ));
  170. $this->viewHelper->setArguments($arguments);
  171. $this->viewHelper->initialize();
  172. $this->viewHelper->render();
  173. }
  174. /**
  175. * @test
  176. * @author Sebastian Kurfürst <sebastian@typo3.org>
  177. * @author Bastian Waidelich <bastian@typo3.org>
  178. * @author Robert Lemke <robert@typo3.org>
  179. */
  180. public function selectOnDomainObjectsCreatesExpectedOptions() {
  181. $mockPersistenceBackend = $this->getMock('Tx_Extbase_Persistence_BackendInterface');
  182. $mockPersistenceBackend->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
  183. $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
  184. $mockPersistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($mockPersistenceBackend));
  185. $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
  186. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  187. $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
  188. $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
  189. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="1">Ingmar</option>' . chr(10) . '<option value="2" selected="selected">Sebastian</option>' . chr(10) . '<option value="3">Robert</option>' . chr(10));
  190. $mockTagBuilder->expects($this->once())->method('render');
  191. $this->viewHelper->_set('tag', $mockTagBuilder);
  192. $user_is = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
  193. $user_sk = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
  194. $user_rl = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(3, 'Robert', 'Lemke');
  195. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  196. 'options' => array(
  197. $user_is,
  198. $user_sk,
  199. $user_rl
  200. ),
  201. 'value' => $user_sk,
  202. 'optionValueField' => 'id',
  203. 'optionLabelField' => 'firstName',
  204. 'name' => 'myName'
  205. ));
  206. $this->viewHelper->setArguments($arguments);
  207. $this->viewHelper->initialize();
  208. $this->viewHelper->render();
  209. }
  210. /**
  211. * @test
  212. * @author Bastian Waidelich <bastian@typo3.org>
  213. */
  214. public function multipleSelectOnDomainObjectsCreatesExpectedOptions() {
  215. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  216. $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('multiple', 'multiple');
  217. $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('name', 'myName[]');
  218. $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
  219. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="1" selected="selected">Schlecht</option>' . chr(10) . '<option value="2">Kurfuerst</option>' . chr(10) . '<option value="3" selected="selected">Lemke</option>' . chr(10));
  220. $mockTagBuilder->expects($this->once())->method('render');
  221. $this->viewHelper->_set('tag', $mockTagBuilder);
  222. $user_is = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
  223. $user_sk = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
  224. $user_rl = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(3, 'Robert', 'Lemke');
  225. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  226. 'options' => array(
  227. $user_is,
  228. $user_sk,
  229. $user_rl
  230. ),
  231. 'value' => array($user_rl, $user_is),
  232. 'optionValueField' => 'id',
  233. 'optionLabelField' => 'lastName',
  234. 'name' => 'myName',
  235. 'multiple' => 'multiple'
  236. ));
  237. $this->viewHelper->setArguments($arguments);
  238. $this->viewHelper->initialize();
  239. $this->viewHelper->render();
  240. }
  241. /**
  242. * @test
  243. * @author Karsten Dambekalns <karsten@typo3.org>
  244. */
  245. public function selectWithoutFurtherConfigurationOnDomainObjectsUsesUuidForValueAndLabel() { $this->markTestIncomplete("This does not work right now due to a renaming in FLOW3.");
  246. $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
  247. $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
  248. $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
  249. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  250. $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
  251. $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
  252. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">fakeUID</option>' . chr(10));
  253. $mockTagBuilder->expects($this->once())->method('render');
  254. $this->viewHelper->_set('tag', $mockTagBuilder);
  255. $user = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
  256. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  257. 'options' => array(
  258. $user
  259. ),
  260. 'name' => 'myName'
  261. ));
  262. $this->viewHelper->setArguments($arguments);
  263. $this->viewHelper->initialize();
  264. $this->viewHelper->render();
  265. }
  266. /**
  267. * @test
  268. * @author Karsten Dambekalns <karsten@typo3.org>
  269. */
  270. public function selectWithoutFurtherConfigurationOnDomainObjectsUsesToStringForLabelIfAvailable() { $this->markTestIncomplete("This does not work right now due to a renaming in FLOW3.");
  271. $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
  272. $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
  273. $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
  274. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  275. $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
  276. $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
  277. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">toStringResult</option>' . chr(10));
  278. $mockTagBuilder->expects($this->once())->method('render');
  279. $this->viewHelper->_set('tag', $mockTagBuilder);
  280. $user = $this->getMock('Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass', array('__toString'), array(1, 'Ingmar', 'Schlecht'));
  281. $user->expects($this->atLeastOnce())->method('__toString')->will($this->returnValue('toStringResult'));
  282. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  283. 'options' => array(
  284. $user
  285. ),
  286. 'name' => 'myName'
  287. ));
  288. $this->viewHelper->setArguments($arguments);
  289. $this->viewHelper->initialize();
  290. $this->viewHelper->render();
  291. }
  292. /**
  293. * @test
  294. * @author Karsten Dambekalns <karsten@typo3.org>
  295. * @expectedException Tx_Fluid_Core_ViewHelper_Exception
  296. */
  297. public function selectOnDomainObjectsThrowsExceptionIfNoValueCanBeFound() {
  298. $mockPersistenceBackend = $this->getMock('Tx_Extbase_Persistence_BackendInterface');
  299. $mockPersistenceBackend->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
  300. $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
  301. $mockPersistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($mockPersistenceBackend));
  302. $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
  303. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  304. $this->viewHelper->_set('tag', $mockTagBuilder);
  305. $user = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
  306. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  307. 'options' => array(
  308. $user
  309. ),
  310. 'name' => 'myName'
  311. ));
  312. $this->viewHelper->setArguments($arguments);
  313. $this->viewHelper->initialize();
  314. $this->viewHelper->render();
  315. }
  316. /**
  317. * @test
  318. * @author Bastian Waidelich <bastian@typo3.org>
  319. */
  320. public function renderCallsSetErrorClassAttribute() {
  321. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  322. 'options' => array()
  323. ));
  324. $this->viewHelper->setArguments($arguments);
  325. $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
  326. $this->viewHelper->render();
  327. }
  328. /**
  329. * @test
  330. * @author Bastian Waidelich <bastian@typo3.org>
  331. */
  332. public function allOptionsAreSelectedIfSelectAllIsTrue() {
  333. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  334. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1" selected="selected">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10) . '<option value="value3" selected="selected">label3</option>' . chr(10));
  335. $this->viewHelper->_set('tag', $mockTagBuilder);
  336. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  337. 'options' => array(
  338. 'value1' => 'label1',
  339. 'value2' => 'label2',
  340. 'value3' => 'label3'
  341. ),
  342. 'name' => 'myName',
  343. 'multiple' => 'multiple',
  344. 'selectAllByDefault' => TRUE
  345. ));
  346. $this->viewHelper->setArguments($arguments);
  347. $this->viewHelper->initialize();
  348. $this->viewHelper->render();
  349. }
  350. /**
  351. * @test
  352. * @author Bastian Waidelich <bastian@typo3.org>
  353. */
  354. public function selectAllHasNoEffectIfValueIsSet() {
  355. $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
  356. $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1" selected="selected">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
  357. $this->viewHelper->_set('tag', $mockTagBuilder);
  358. $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
  359. 'options' => array(
  360. 'value1' => 'label1',
  361. 'value2' => 'label2',
  362. 'value3' => 'label3'
  363. ),
  364. 'value' => array('value2', 'value1'),
  365. 'name' => 'myName',
  366. 'multiple' => 'multiple',
  367. 'selectAllByDefault' => TRUE
  368. ));
  369. $this->viewHelper->setArguments($arguments);
  370. $this->viewHelper->initialize();
  371. $this->viewHelper->render();
  372. }
  373. }
  374. ?>