PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/t3lib/t3lib_extmgmTest.php

https://github.com/andreaswolf/typo3-tceforms
PHP | 540 lines | 293 code | 81 blank | 166 comment | 0 complexity | 49f2b4cdd08958bd6bbb78d7623084d3 MD5 | raw file
Possible License(s): Apache-2.0, BSD-2-Clause, LGPL-3.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009-2011 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. * Testcase for class t3lib_extMgm
  26. *
  27. * @author Oliver Hader <oliver@typo3.org>
  28. * @author Oliver Klee <typo3-coding@oliverklee.de>
  29. *
  30. * @package TYPO3
  31. * @subpackage t3lib
  32. */
  33. class t3lib_extmgmTest extends tx_phpunit_testcase {
  34. /**
  35. * backup of defined GLOBALS
  36. *
  37. * @var array
  38. */
  39. protected $globals = array();
  40. public function setUp() {
  41. $this->globals = array(
  42. 'TYPO3_CONF_VARS' => serialize($GLOBALS['TYPO3_CONF_VARS']),
  43. 'TYPO3_LOADED_EXT' => serialize($GLOBALS['TYPO3_LOADED_EXT']),
  44. 'TCA' => serialize($GLOBALS['TCA']),
  45. );
  46. }
  47. public function tearDown() {
  48. t3lib_extMgm::clearExtensionKeyMap();
  49. foreach ($this->globals as $key => $value) {
  50. $GLOBALS[$key] = unserialize($value);
  51. }
  52. }
  53. //////////////////////
  54. // Utility functions
  55. //////////////////////
  56. /**
  57. * Generates a basic TCA for a given table.
  58. *
  59. * @param string $table name of the table, must not be empty
  60. * @return array generated TCA for the given table, will not be empty
  61. */
  62. private function generateTCAForTable($table) {
  63. $tca = array();
  64. $tca[$table] = array();
  65. $tca[$table]['columns'] = array(
  66. 'fieldA' => array(),
  67. 'fieldC' => array(),
  68. );
  69. $tca[$table]['types'] = array(
  70. 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
  71. 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
  72. 'typeC' => array('showitem' => 'fieldC;;paletteD'),
  73. );
  74. $tca[$table]['palettes'] = array(
  75. 'paletteA' => array('showitem' => 'fieldX, fieldY'),
  76. 'paletteB' => array('showitem' => 'fieldX, fieldY'),
  77. 'paletteC' => array('showitem' => 'fieldX, fieldY'),
  78. 'paletteD' => array('showitem' => 'fieldX, fieldY'),
  79. );
  80. return $tca;
  81. }
  82. /**
  83. * Returns the fixtures path for this testcase relative to PATH_site.
  84. *
  85. * @return string the fixtures path for this testcase, will not be empty
  86. */
  87. private function determineFixturesPath() {
  88. return t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder')
  89. ->getRelativeCoreTestsPath() . 't3lib/fixtures/';
  90. }
  91. /////////////////////////////////////////////
  92. // Tests concerning getExtensionKeyByPrefix
  93. /////////////////////////////////////////////
  94. /**
  95. * @test
  96. * @see t3lib_extMgm::getExtensionKeyByPrefix
  97. */
  98. public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
  99. t3lib_extMgm::clearExtensionKeyMap();
  100. $uniqueSuffix = uniqid('test');
  101. $extensionKey = 'tt_news' . $uniqueSuffix;
  102. $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
  103. $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
  104. $this->assertEquals(
  105. $extensionKey,
  106. t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
  107. );
  108. }
  109. /**
  110. * @test
  111. * @see t3lib_extMgm::getExtensionKeyByPrefix
  112. */
  113. public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
  114. t3lib_extMgm::clearExtensionKeyMap();
  115. $uniqueSuffix = uniqid('test');
  116. $extensionKey = 'kickstarter' . $uniqueSuffix;
  117. $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
  118. $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
  119. $this->assertEquals(
  120. $extensionKey,
  121. t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
  122. );
  123. }
  124. /**
  125. * @test
  126. * @see t3lib_extMgm::getExtensionKeyByPrefix
  127. */
  128. public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse(){
  129. t3lib_extMgm::clearExtensionKeyMap();
  130. $uniqueSuffix = uniqid('test');
  131. $extensionKey = 'unloadedextension' . $uniqueSuffix;
  132. $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
  133. $this->assertFalse(
  134. t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
  135. );
  136. }
  137. //////////////////////////////////////
  138. // Tests concerning addToAllTCAtypes
  139. //////////////////////////////////////
  140. /**
  141. * Tests whether fields can be add to all TCA types and duplicate fields are considered.
  142. * @test
  143. * @see t3lib_extMgm::addToAllTCAtypes()
  144. */
  145. public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
  146. $table = uniqid('tx_coretest_table');
  147. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  148. t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
  149. // Checking typeA:
  150. $this->assertEquals(
  151. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  152. $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
  153. );
  154. // Checking typeB:
  155. $this->assertEquals(
  156. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  157. $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
  158. );
  159. }
  160. /**
  161. * Tests whether fields can be add to all TCA types and duplicate fields are considered.
  162. * @test
  163. * @see t3lib_extMgm::addToAllTCAtypes()
  164. */
  165. public function canAddFieldsToAllTCATypesAfterExistingOnes() {
  166. $table = uniqid('tx_coretest_table');
  167. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  168. t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
  169. // Checking typeA:
  170. $this->assertEquals(
  171. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  172. $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
  173. );
  174. // Checking typeB:
  175. $this->assertEquals(
  176. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  177. $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
  178. );
  179. }
  180. /**
  181. * Tests whether fields can be add to a TCA type before existing ones
  182. * @test
  183. * @see t3lib_extMgm::addToAllTCAtypes()
  184. */
  185. public function canAddFieldsToTCATypeBeforeExistingOnes() {
  186. $table = uniqid('tx_coretest_table');
  187. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  188. t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
  189. // Checking typeA:
  190. $this->assertEquals(
  191. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  192. $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
  193. );
  194. // Checking typeB:
  195. $this->assertEquals(
  196. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
  197. $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
  198. );
  199. }
  200. /**
  201. * Tests whether fields can be add to a TCA type after existing ones
  202. * @test
  203. * @see t3lib_extMgm::addToAllTCAtypes()
  204. */
  205. public function canAddFieldsToTCATypeAfterExistingOnes() {
  206. $table = uniqid('tx_coretest_table');
  207. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  208. t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
  209. // Checking typeA:
  210. $this->assertEquals(
  211. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  212. $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
  213. );
  214. // Checking typeB:
  215. $this->assertEquals(
  216. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
  217. $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
  218. );
  219. }
  220. /**
  221. * Test wheter replacing other TCA fields works as promissed
  222. * @test
  223. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  224. */
  225. public function canAddFieldsToTCATypeAndReplaceExistingOnes() {
  226. $table = uniqid('tx_coretest_table');
  227. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  228. $typesBefore = $GLOBALS['TCA'][$table]['types'];
  229. t3lib_extMgm::addToAllTCAtypes($table, 'fieldZ', '', 'replace:fieldX');
  230. $this->assertEquals(
  231. $typesBefore,
  232. $GLOBALS['TCA'][$table]['types'],
  233. 'It\'s wrong that the "types" array changes here - the replaced field is only on palettes'
  234. );
  235. // unchanged because the palette is not used
  236. $this->assertEquals(
  237. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  238. );
  239. // unchanged because the palette is not used
  240. $this->assertEquals(
  241. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  242. );
  243. $this->assertEquals(
  244. 'fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  245. );
  246. $this->assertEquals(
  247. 'fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  248. );
  249. }
  250. ///////////////////////////////////////////////////
  251. // Tests concerning addFieldsToAllPalettesOfField
  252. ///////////////////////////////////////////////////
  253. /**
  254. * Tests whether fields can be added to a palette before existing elements.
  255. * @test
  256. * @see t3lib_extMgm::addFieldsToPalette()
  257. */
  258. public function canAddFieldsToPaletteBeforeExistingOnes() {
  259. $table = uniqid('tx_coretest_table');
  260. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  261. t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
  262. $this->assertEquals(
  263. 'fieldX, newA, newB, fieldY',
  264. $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  265. );
  266. }
  267. /**
  268. * Tests whether fields can be added to a palette after existing elements.
  269. * @test
  270. * @see t3lib_extMgm::addFieldsToPalette()
  271. */
  272. public function canAddFieldsToPaletteAfterExistingOnes() {
  273. $table = uniqid('tx_coretest_table');
  274. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  275. t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
  276. $this->assertEquals(
  277. 'fieldX, newA, newB, fieldY',
  278. $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  279. );
  280. }
  281. /**
  282. * Tests whether fields can be added to a palette after a not existing elements.
  283. * @test
  284. * @see t3lib_extMgm::addFieldsToPalette()
  285. */
  286. public function canAddFieldsToPaletteAfterNotExistingOnes() {
  287. $table = uniqid('tx_coretest_table');
  288. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  289. t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
  290. $this->assertEquals(
  291. 'fieldX, fieldY, newA, newB',
  292. $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  293. );
  294. }
  295. /**
  296. * Tests whether fields can be added to all palettes of a regular field before existing ones.
  297. * @test
  298. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  299. */
  300. public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
  301. $table = uniqid('tx_coretest_table');
  302. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  303. t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
  304. $this->assertEquals(
  305. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  306. );
  307. $this->assertEquals(
  308. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  309. );
  310. $this->assertEquals(
  311. 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  312. );
  313. $this->assertEquals(
  314. 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  315. );
  316. }
  317. /**
  318. * Tests whether fields can be added to all palettes of a regular field after existing ones.
  319. * @test
  320. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  321. */
  322. public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
  323. $table = uniqid('tx_coretest_table');
  324. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  325. t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
  326. $this->assertEquals(
  327. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  328. );
  329. $this->assertEquals(
  330. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  331. );
  332. $this->assertEquals(
  333. 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  334. );
  335. $this->assertEquals(
  336. 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  337. );
  338. }
  339. /**
  340. * Tests whether fields can be added to all palettes of a regular field after a not existing field.
  341. * @test
  342. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  343. */
  344. public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
  345. $table = uniqid('tx_coretest_table');
  346. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  347. t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
  348. $this->assertEquals(
  349. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  350. );
  351. $this->assertEquals(
  352. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  353. );
  354. $this->assertEquals(
  355. 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  356. );
  357. $this->assertEquals(
  358. 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  359. );
  360. }
  361. /**
  362. * Tests whether fields are added to a new palette that did not exist before.
  363. * @test
  364. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  365. */
  366. public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
  367. $table = uniqid('tx_coretest_table');
  368. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  369. t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
  370. $this->assertEquals(
  371. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  372. );
  373. $this->assertEquals(
  374. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  375. );
  376. $this->assertEquals(
  377. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  378. );
  379. $this->assertEquals(
  380. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  381. );
  382. $this->assertEquals(
  383. 'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
  384. );
  385. }
  386. /////////////////////////////////////////
  387. // Tests concerning getExtensionVersion
  388. /////////////////////////////////////////
  389. /**
  390. * Data provider for negative getExtensionVersion() tests.
  391. *
  392. * @return array
  393. */
  394. public function getExtensionVersionFaultyDataProvider() {
  395. return array(
  396. array(''),
  397. array(0),
  398. array(new stdClass()),
  399. array(TRUE),
  400. );
  401. }
  402. /**
  403. * @test
  404. * @expectedException InvalidArgumentException
  405. * @dataProvider getExtensionVersionFaultyDataProvider
  406. */
  407. public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
  408. t3lib_extMgm::getExtensionVersion($key);
  409. }
  410. /**
  411. * @test
  412. */
  413. public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
  414. t3lib_extMgm::clearExtensionKeyMap();
  415. $uniqueSuffix = uniqid('test');
  416. $extensionKey = 'unloadedextension' . $uniqueSuffix;
  417. $this->assertEquals(
  418. '',
  419. t3lib_extMgm::getExtensionVersion($extensionKey)
  420. );
  421. }
  422. /**
  423. * @test
  424. */
  425. public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
  426. t3lib_extMgm::clearExtensionKeyMap();
  427. $uniqueSuffix = uniqid('test');
  428. $extensionKey = 'unloadedextension' . $uniqueSuffix;
  429. $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array(
  430. 'siteRelPath' => $this->determineFixturesPath(),
  431. );
  432. $this->assertEquals(
  433. '1.2.3',
  434. t3lib_extMgm::getExtensionVersion($extensionKey)
  435. );
  436. }
  437. /**
  438. * @test
  439. */
  440. public function getEnabledExtensionListConsidersRequiredExtensions() {
  441. $testrequiRedExtension = uniqid('test');
  442. $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = $testrequiRedExtension;
  443. $extensions = explode(',', t3lib_extMgm::getEnabledExtensionList());
  444. $this->assertTrue(in_array($testrequiRedExtension, $extensions));
  445. }
  446. /**
  447. * @test
  448. */
  449. public function getEnabledExtensionListConsidersRequiredAndIgnoredExtensions() {
  450. $testRequiredExtension = uniqid('test');
  451. $testIgnoredExtension = uniqid('test');
  452. $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = $testRequiredExtension . ',' . $testIgnoredExtension;
  453. $GLOBALS['TYPO3_CONF_VARS']['EXT']['ignoredExt'] = $testIgnoredExtension;
  454. $extensions = explode(',', t3lib_extMgm::getEnabledExtensionList());
  455. $this->assertTrue(in_array($testRequiredExtension, $extensions));
  456. $this->assertFalse(in_array($testIgnoredExtension, $extensions));
  457. }
  458. }
  459. ?>