/tests/t3lib/tca/datastructure/t3lib_tca_datastructure_fieldTest.php

https://github.com/andreaswolf/typo3-tceforms · PHP · 243 lines · 123 code · 32 blank · 88 comment · 0 complexity · 52e7302bd1cdeca4abd2575b83a63e80 MD5 · raw file

  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
  6. * All rights reserved
  7. *
  8. * This script is part of the TYPO3 project. The TYPO3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. * A copy is found in the textfile GPL.txt and important notices to the license
  17. * from the author is found in LICENSE.txt distributed with these scripts.
  18. *
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27. /**
  28. * Testcase for the field abstraction class in TCA.
  29. *
  30. * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
  31. * @package TYPO3
  32. * @subpackage t3lib
  33. */
  34. class t3lib_TCA_DataStructure_FieldTest extends Tx_Phpunit_TestCase {
  35. protected static $localizationModeFixtures = array(
  36. 'exclude' => array(
  37. array('l10n_mode' => 'exclude')
  38. ),
  39. 'mergeIfNotBlank' => array(
  40. array('l10n_mode' => 'mergeIfNotBlank')
  41. ),
  42. 'noCopy' => array(
  43. array('l10n_mode' => 'noCopy')
  44. ),
  45. 'prefixLangTitle' => array(
  46. array('l10n_mode' => 'prefixLangTitle')
  47. )
  48. );
  49. /**
  50. * Data provider methods
  51. */
  52. public static function validLocalizationModes() {
  53. return self::$localizationModeFixtures;
  54. }
  55. /**
  56. * This provider adds an additional field configuration with a random, invalid localization mode, plus it adds a second,
  57. * boolean parameter that is TRUE if the l10n mode is valid and otherwise FALSE.
  58. *
  59. * @static
  60. * @return array
  61. */
  62. public static function validLocalizationModesPlusOneInvalid() {
  63. return array_merge(
  64. self::$localizationModeFixtures,
  65. array(
  66. 'invalidOne' => array(
  67. array('l10n_mode' => uniqid()),
  68. FALSE
  69. )
  70. )
  71. );
  72. }
  73. /**
  74. * @test
  75. * @covers t3lib_TCA_DataStructure_Field::getName
  76. */
  77. public function fieldNameIsCorrectlyStoredAndReturned() {
  78. $name = uniqid();
  79. $mockedDataStructure = $this->getMock('t3lib_TCA_DataStructure');
  80. $field = new t3lib_TCA_DataStructure_Field($mockedDataStructure, $name);
  81. $this->assertEquals($name, $field->getName());
  82. }
  83. /**
  84. * @test
  85. * @covers t3lib_TCA_DataStructure_Field::getLabel
  86. */
  87. public function labelIsProperlyExtractedFromConfiguration() {
  88. $name = uniqid();
  89. $configuration = array(
  90. 'label' => uniqid()
  91. );
  92. $mockedDataStructure = $this->getMock('t3lib_TCA_DataStructure');
  93. $mockedDataStructure->expects($this->once())->method('getFieldConfiguration')->with($name)->will($this->returnValue($configuration));
  94. $field = new t3lib_TCA_DataStructure_Field($mockedDataStructure, $name);
  95. $this->assertEquals($configuration['label'], $field->getLabel());
  96. }
  97. /**
  98. * @test
  99. * @covers t3lib_TCA_DataStructure_Field::hasConfigurationValue
  100. * @covers t3lib_TCA_DataStructure_Field::getConfigurationValue
  101. */
  102. public function configurationValuesAreCorrectlyStoredAndReturned() {
  103. $configuration = array(
  104. 'key-' . uniqid() => 'value ' . uniqid(),
  105. 'key-' . uniqid() => 'value ' . uniqid()
  106. );
  107. $mockedDataStructure = $this->getMock('t3lib_TCA_DataStructure');
  108. $mockedDataStructure->expects($this->once())->method('getFieldConfiguration')->will($this->returnValue($configuration));
  109. $field = new t3lib_TCA_DataStructure_Field($mockedDataStructure, uniqid());
  110. foreach ($configuration as $name => $value) {
  111. $this->assertTrue($field->hasConfigurationValue($name));
  112. $this->assertEquals($value, $field->getConfigurationValue($name));
  113. }
  114. }
  115. /**
  116. * @test
  117. * @covers t3lib_TCA_DataStructure_Field::hasLocalizationMode
  118. */
  119. public function hasLocalizationModeReturnsFalseIfLanguageFieldInDataStructureIsNotSet() {
  120. $fieldConfiguration = self::$localizationModeFixtures['exclude'];
  121. $mockedDataStructure = $this->getMock('t3lib_TCA_DataStructure', array('hasControlValue', 'getFieldConfiguration'));
  122. $mockedDataStructure->expects($this->once())->method('hasControlValue')->with('languageField')->will($this->returnValue(FALSE));
  123. $mockedDataStructure->expects($this->once())->method('getFieldConfiguration')->will($this->returnValue($fieldConfiguration));
  124. /** @var $mockedField t3lib_TCA_DataStructure_Field */
  125. $mockedField = $this->getMock('t3lib_TCA_DataStructure_Field', array('hasConfigurationValue'), array($mockedDataStructure, uniqid()));
  126. $mockedField->expects($this->never())->method('hasConfigurationValue');
  127. $this->assertFalse($mockedField->hasLocalizationMode());
  128. }
  129. /**
  130. * @test
  131. * @covers t3lib_TCA_DataStructure_Field::hasLocalizationMode
  132. */
  133. public function hasLocalizationModeReturnsFalseIfConfigurationValueIsNotSet() {
  134. $fieldConfiguration = self::$localizationModeFixtures['exclude'][0];
  135. $mockedDataStructure = $this->getMock('t3lib_TCA_DataStructure', array('hasControlValue', 'getFieldConfiguration'));
  136. $mockedDataStructure->expects($this->once())->method('hasControlValue')->with('languageField')->will($this->returnValue(TRUE));
  137. $mockedDataStructure->expects($this->once())->method('getFieldConfiguration')->will($this->returnValue($fieldConfiguration));
  138. /** @var $mockedField t3lib_TCA_DataStructure_Field */
  139. $mockedField = $this->getMock('t3lib_TCA_DataStructure_Field', array('getConfigurationValue', 'hasConfigurationValue'), array($mockedDataStructure, uniqid()));
  140. $mockedField->expects($this->atLeastOnce())->method('hasConfigurationValue')->with('l10n_mode')->will($this->returnValue(FALSE));
  141. $mockedField->expects($this->any())->method('getConfigurationValue')->will($this->returnValue($fieldConfiguration['l10n_mode']));
  142. $this->assertFalse($mockedField->hasLocalizationMode());
  143. }
  144. /**
  145. * @test
  146. * @dataProvider validLocalizationModesPlusOneInvalid
  147. * @covers t3lib_TCA_DataStructure_Field::hasLocalizationMode
  148. */
  149. public function hasLocalizationModeReturnsTrueOnlyForValidLocalizationModes($fieldConfiguration, $localizationModeValidity = TRUE) {
  150. $mockedDataStructure = $this->getMock('t3lib_TCA_DataStructure');
  151. $mockedDataStructure->expects($this->once())->method('getFieldConfiguration')->will($this->returnValue($fieldConfiguration));
  152. $mockedDataStructure->expects($this->once())->method('hasControlValue')->will($this->returnValue(TRUE));
  153. /** @var $mockedField t3lib_TCA_DataStructure_Field */
  154. $mockedField = $this->getMock('t3lib_TCA_DataStructure_Field', array('hasConfigurationValue'), array($mockedDataStructure, uniqid()));
  155. $mockedField->expects($this->any())->method('hasConfigurationValue')->will($this->returnValue(TRUE));
  156. $this->assertEquals($localizationModeValidity, $mockedField->hasLocalizationMode());
  157. }
  158. /**
  159. * @test
  160. * @covers t3lib_TCA_DataStructure_Field::getLocalizationMode
  161. */
  162. public function getLocalizationModeReturnsCorrectValueIfLocalizationModeIsPresent() {
  163. $fieldConfiguration = array(
  164. 'l10n_mode' => uniqid()
  165. );
  166. $mockedDataStructure = $this->getMock('t3lib_TCA_DataStructure');
  167. $mockedDataStructure->expects($this->once())->method('getFieldConfiguration')->will($this->returnValue($fieldConfiguration));
  168. /** @var $mockedField t3lib_TCA_DataStructure_Field */
  169. $mockedField = $this->getMock('t3lib_TCA_DataStructure_Field', array('hasLocalizationMode'), array($mockedDataStructure, uniqid()));
  170. $mockedField->expects($this->any())->method('hasLocalizationMode')->will($this->returnValue(TRUE));
  171. $this->assertEquals($fieldConfiguration['l10n_mode'], $mockedField->getLocalizationMode());
  172. }
  173. /**
  174. * @test
  175. */
  176. public function getLocalizationModeChecksIfLocalizationModeIsPresent() {
  177. $fieldMock = $this->getMock('t3lib_TCA_DataStructure_Field', array('hasLocalizationMode'), array(), '', FALSE);
  178. $fieldMock->expects($this->once())->method('hasLocalizationMode');
  179. $fieldMock->getLocalizationMode();
  180. }
  181. /**
  182. * @test
  183. * @covers t3lib_TCA_DataStructure_Field::getConfiguration
  184. */
  185. public function getConfigurationReturnsStoredConfigurationArray() {
  186. $configurationArray = array(
  187. 'some' => uniqid(),
  188. 'random' => uniqid(),
  189. 'configuration' => uniqid(),
  190. 'values' => uniqid(),
  191. );
  192. $mockedDataStructure = $this->getMock('t3lib_TCA_DataStructure');
  193. $mockedDataStructure->expects($this->once())->method('getFieldConfiguration')->will($this->returnValue($configurationArray));
  194. $field = new t3lib_TCA_DataStructure_Field($mockedDataStructure, uniqid());
  195. $this->assertEquals($configurationArray, $field->getConfiguration());
  196. }
  197. /**
  198. * @test
  199. */
  200. public function paletteMethodsCorrectlyHandlePalette() {
  201. /** @var $fixture t3lib_TCA_DataStructure_Field */
  202. $fixture = $this->getMock('t3lib_TCA_DataStructure_Field', NULL, array(), '', FALSE);
  203. /** @var $mockedPalette t3lib_TCA_DataStructure_Palette */
  204. $mockedPalette = $this->getMock('t3lib_TCA_DataStructure_Palette', array(), array(), '', FALSE);
  205. $this->assertFalse($fixture->hasPalette());
  206. $fixture->addPalette($mockedPalette);
  207. $this->assertTrue($fixture->hasPalette());
  208. $this->assertSame($mockedPalette, $fixture->getPalette());
  209. }
  210. }
  211. ?>