Follow-up to feature #9897: Added unit tests for addFieldsToPalette() and addToAllTCA...
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_extmgm_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Oliver Hader <oliver@typo3.org>
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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Testcase for class t3lib_extMgm
28 *
29 * @author Oliver Hader <oliver@typo3.org>
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_extmgm_testcase extends tx_phpunit_testcase {
34 /**
35 * Contains backup of defined GLOBALS
36 * @var array
37 */
38 protected $globals = array();
39
40 public function setUp() {
41 $this->globals = array(
42 'TYPO3_LOADED_EXT' => serialize($GLOBALS['TYPO3_LOADED_EXT']),
43 );
44 }
45
46 /**
47 * @test
48 * @see t3lib_extMgm::getExtensionKeyByPrefix
49 */
50 public function checkGetExtensionKeyByPrefix() {
51 $uniqueSuffix = uniqid('test');
52 $GLOBALS['TYPO3_LOADED_EXT']['tt_news' . $uniqueSuffix] = array();
53 $GLOBALS['TYPO3_LOADED_EXT']['kickstarter' . $uniqueSuffix] = array();
54
55 $this->assertEquals(
56 'tt_news' . $uniqueSuffix,
57 t3lib_extMgm::getExtensionKeyByPrefix('tx_ttnews' . $uniqueSuffix)
58 );
59 $this->assertEquals(
60 'kickstarter' . $uniqueSuffix,
61 t3lib_extMgm::getExtensionKeyByPrefix('tx_kickstarter' . $uniqueSuffix)
62 );
63 $this->assertFalse(
64 t3lib_extMgm::getExtensionKeyByPrefix('tx_unloadedextension' . $uniqueSuffix)
65 );
66 }
67
68 /**
69 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
70 * @test
71 * @see t3lib_extMgm::addToAllTCAtypes()
72 */
73 public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
74 $table = uniqid('tx_coretest_table');
75 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
76
77 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newB, fieldA', '', 'before:fieldD');
78
79 // Checking typeA:
80 $this->assertEquals(
81 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
82 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
83 );
84 // Checking typeB:
85 $this->assertEquals(
86 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
87 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
88 );
89 }
90
91 /**
92 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
93 * @test
94 * @see t3lib_extMgm::addToAllTCAtypes()
95 */
96 public function canAddFieldsToAllTCATypesAfterExistingOnes() {
97 $table = uniqid('tx_coretest_table');
98 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
99
100 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newB, fieldA', '', 'after:fieldC');
101
102 // Checking typeA:
103 $this->assertEquals(
104 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
105 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
106 );
107 // Checking typeB:
108 $this->assertEquals(
109 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
110 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
111 );
112 }
113
114 /**
115 * Tests whether fields can be add to a TCA type before existing ones
116 * @test
117 * @see t3lib_extMgm::addToAllTCAtypes()
118 */
119 public function canAddFieldsToTCATypeBeforeExistingOnes() {
120 $table = uniqid('tx_coretest_table');
121 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
122
123 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newB, fieldA', 'typeA', 'before:fieldD');
124
125 // Checking typeA:
126 $this->assertEquals(
127 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
128 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
129 );
130 // Checking typeB:
131 $this->assertEquals(
132 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
133 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
134 );
135 }
136
137 /**
138 * Tests whether fields can be add to a TCA type after existing ones
139 * @test
140 * @see t3lib_extMgm::addToAllTCAtypes()
141 */
142 public function canAddFieldsToTCATypeAfterExistingOnes() {
143 $table = uniqid('tx_coretest_table');
144 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
145
146 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newB, fieldA', 'typeA', 'after:fieldC');
147
148 // Checking typeA:
149 $this->assertEquals(
150 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
151 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
152 );
153 // Checking typeB:
154 $this->assertEquals(
155 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
156 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
157 );
158 }
159
160 /**
161 * Tests whether fields can be added to a palette before existing elements.
162 * @test
163 * @see t3lib_extMgm::addFieldsToPalette()
164 */
165 public function canAddFieldsToPaletteBeforeExistingOnes() {
166 $table = uniqid('tx_coretest_table');
167 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
168
169 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newB, fieldX', 'before:fieldY');
170
171 $this->assertEquals(
172 'fieldX, newA, newB, fieldY',
173 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
174 );
175 }
176
177 /**
178 * Tests whether fields can be added to a palette after existing elements.
179 * @test
180 * @see t3lib_extMgm::addFieldsToPalette()
181 */
182 public function canAddFieldsToPaletteAfterExistingOnes() {
183 $table = uniqid('tx_coretest_table');
184 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
185
186 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newB, fieldX', 'after:fieldX');
187
188 $this->assertEquals(
189 'fieldX, newA, newB, fieldY',
190 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
191 );
192 }
193
194 public function tearDown() {
195 foreach ($this->globals as $key => $value) {
196 $GLOBALS[$key] = unserialize($value);
197 }
198 }
199
200 /**
201 * Generates a basic TCA for a given table.
202 *
203 * @param string $table: Name of the table
204 * @return array Generated TCA for the given table
205 */
206 private function generateTCAForTable($table) {
207 $tca = array();
208 $tca[$table] = array();
209 $tca[$table]['types'] = array(
210 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
211 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
212 );
213 $tca[$table]['palettes'] = array(
214 'paletteA' => array('showitem' => 'fieldX, fieldY'),
215 'paletteB' => array('showitem' => 'fieldX, fieldY'),
216 );
217
218 return $tca;
219 }
220 }
221
222 ?>