PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/t3lib/t3lib_extmgmTest.php

https://github.com/foxsoft/typo3v4core
PHP | 466 lines | 250 code | 68 blank | 148 comment | 0 complexity | c99fd902bf77b1bcbc8c2afa2539fbfe MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009-2010 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_LOADED_EXT' => serialize($GLOBALS['TYPO3_LOADED_EXT']),
  43. 'TCA' => serialize($GLOBALS['TCA']),
  44. );
  45. }
  46. public function tearDown() {
  47. t3lib_extMgm::clearExtensionKeyMap();
  48. foreach ($this->globals as $key => $value) {
  49. $GLOBALS[$key] = unserialize($value);
  50. }
  51. }
  52. //////////////////////
  53. // Utility functions
  54. //////////////////////
  55. /**
  56. * Generates a basic TCA for a given table.
  57. *
  58. * @param string $table name of the table, must not be empty
  59. * @return array generated TCA for the given table, will not be empty
  60. */
  61. private function generateTCAForTable($table) {
  62. $tca = array();
  63. $tca[$table] = array();
  64. $tca[$table]['columns'] = array(
  65. 'fieldA' => array(),
  66. 'fieldC' => array(),
  67. );
  68. $tca[$table]['types'] = array(
  69. 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
  70. 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
  71. 'typeC' => array('showitem' => 'fieldC;;paletteD'),
  72. );
  73. $tca[$table]['palettes'] = array(
  74. 'paletteA' => array('showitem' => 'fieldX, fieldY'),
  75. 'paletteB' => array('showitem' => 'fieldX, fieldY'),
  76. 'paletteC' => array('showitem' => 'fieldX, fieldY'),
  77. 'paletteD' => array('showitem' => 'fieldX, fieldY'),
  78. );
  79. return $tca;
  80. }
  81. /////////////////////////////////////////////
  82. // Tests concerning getExtensionKeyByPrefix
  83. /////////////////////////////////////////////
  84. /**
  85. * @test
  86. * @see t3lib_extMgm::getExtensionKeyByPrefix
  87. */
  88. public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
  89. t3lib_extMgm::clearExtensionKeyMap();
  90. $uniqueSuffix = uniqid('test');
  91. $extensionKey = 'tt_news' . $uniqueSuffix;
  92. $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
  93. $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
  94. $this->assertEquals(
  95. $extensionKey,
  96. t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
  97. );
  98. }
  99. /**
  100. * @test
  101. * @see t3lib_extMgm::getExtensionKeyByPrefix
  102. */
  103. public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
  104. t3lib_extMgm::clearExtensionKeyMap();
  105. $uniqueSuffix = uniqid('test');
  106. $extensionKey = 'kickstarter' . $uniqueSuffix;
  107. $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
  108. $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
  109. $this->assertEquals(
  110. $extensionKey,
  111. t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
  112. );
  113. }
  114. /**
  115. * @test
  116. * @see t3lib_extMgm::getExtensionKeyByPrefix
  117. */
  118. public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse(){
  119. t3lib_extMgm::clearExtensionKeyMap();
  120. $uniqueSuffix = uniqid('test');
  121. $extensionKey = 'unloadedextension' . $uniqueSuffix;
  122. $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
  123. $this->assertFalse(
  124. t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
  125. );
  126. }
  127. //////////////////////////////////////
  128. // Tests concerning addToAllTCAtypes
  129. //////////////////////////////////////
  130. /**
  131. * Tests whether fields can be add to all TCA types and duplicate fields are considered.
  132. * @test
  133. * @see t3lib_extMgm::addToAllTCAtypes()
  134. */
  135. public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
  136. $table = uniqid('tx_coretest_table');
  137. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  138. t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
  139. // Checking typeA:
  140. $this->assertEquals(
  141. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  142. $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
  143. );
  144. // Checking typeB:
  145. $this->assertEquals(
  146. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  147. $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
  148. );
  149. }
  150. /**
  151. * Tests whether fields can be add to all TCA types and duplicate fields are considered.
  152. * @test
  153. * @see t3lib_extMgm::addToAllTCAtypes()
  154. */
  155. public function canAddFieldsToAllTCATypesAfterExistingOnes() {
  156. $table = uniqid('tx_coretest_table');
  157. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  158. t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
  159. // Checking typeA:
  160. $this->assertEquals(
  161. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  162. $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
  163. );
  164. // Checking typeB:
  165. $this->assertEquals(
  166. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  167. $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
  168. );
  169. }
  170. /**
  171. * Tests whether fields can be add to a TCA type before existing ones
  172. * @test
  173. * @see t3lib_extMgm::addToAllTCAtypes()
  174. */
  175. public function canAddFieldsToTCATypeBeforeExistingOnes() {
  176. $table = uniqid('tx_coretest_table');
  177. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  178. t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
  179. // Checking typeA:
  180. $this->assertEquals(
  181. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  182. $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
  183. );
  184. // Checking typeB:
  185. $this->assertEquals(
  186. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
  187. $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
  188. );
  189. }
  190. /**
  191. * Tests whether fields can be add to a TCA type after existing ones
  192. * @test
  193. * @see t3lib_extMgm::addToAllTCAtypes()
  194. */
  195. public function canAddFieldsToTCATypeAfterExistingOnes() {
  196. $table = uniqid('tx_coretest_table');
  197. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  198. t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
  199. // Checking typeA:
  200. $this->assertEquals(
  201. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
  202. $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
  203. );
  204. // Checking typeB:
  205. $this->assertEquals(
  206. 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
  207. $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
  208. );
  209. }
  210. ///////////////////////////////////////////////////
  211. // Tests concerning addFieldsToAllPalettesOfField
  212. ///////////////////////////////////////////////////
  213. /**
  214. * Tests whether fields can be added to a palette before existing elements.
  215. * @test
  216. * @see t3lib_extMgm::addFieldsToPalette()
  217. */
  218. public function canAddFieldsToPaletteBeforeExistingOnes() {
  219. $table = uniqid('tx_coretest_table');
  220. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  221. t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
  222. $this->assertEquals(
  223. 'fieldX, newA, newB, fieldY',
  224. $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  225. );
  226. }
  227. /**
  228. * Tests whether fields can be added to a palette after existing elements.
  229. * @test
  230. * @see t3lib_extMgm::addFieldsToPalette()
  231. */
  232. public function canAddFieldsToPaletteAfterExistingOnes() {
  233. $table = uniqid('tx_coretest_table');
  234. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  235. t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
  236. $this->assertEquals(
  237. 'fieldX, newA, newB, fieldY',
  238. $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  239. );
  240. }
  241. /**
  242. * Tests whether fields can be added to a palette after a not existing elements.
  243. * @test
  244. * @see t3lib_extMgm::addFieldsToPalette()
  245. */
  246. public function canAddFieldsToPaletteAfterNotExistingOnes() {
  247. $table = uniqid('tx_coretest_table');
  248. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  249. t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
  250. $this->assertEquals(
  251. 'fieldX, fieldY, newA, newB',
  252. $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  253. );
  254. }
  255. /**
  256. * Tests whether fields can be added to all palettes of a regular field before existing ones.
  257. * @test
  258. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  259. */
  260. public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
  261. $table = uniqid('tx_coretest_table');
  262. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  263. t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
  264. $this->assertEquals(
  265. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  266. );
  267. $this->assertEquals(
  268. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  269. );
  270. $this->assertEquals(
  271. 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  272. );
  273. $this->assertEquals(
  274. 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  275. );
  276. }
  277. /**
  278. * Tests whether fields can be added to all palettes of a regular field after existing ones.
  279. * @test
  280. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  281. */
  282. public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
  283. $table = uniqid('tx_coretest_table');
  284. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  285. t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
  286. $this->assertEquals(
  287. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  288. );
  289. $this->assertEquals(
  290. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  291. );
  292. $this->assertEquals(
  293. 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  294. );
  295. $this->assertEquals(
  296. 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  297. );
  298. }
  299. /**
  300. * Tests whether fields can be added to all palettes of a regular field after a not existing field.
  301. * @test
  302. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  303. */
  304. public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
  305. $table = uniqid('tx_coretest_table');
  306. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  307. t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
  308. $this->assertEquals(
  309. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  310. );
  311. $this->assertEquals(
  312. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  313. );
  314. $this->assertEquals(
  315. 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  316. );
  317. $this->assertEquals(
  318. 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  319. );
  320. }
  321. /**
  322. * Tests whether fields are added to a new palette that did not exist before.
  323. * @test
  324. * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
  325. */
  326. public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
  327. $table = uniqid('tx_coretest_table');
  328. $GLOBALS['TCA'] = $this->generateTCAForTable($table);
  329. t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
  330. $this->assertEquals(
  331. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
  332. );
  333. $this->assertEquals(
  334. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
  335. );
  336. $this->assertEquals(
  337. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
  338. );
  339. $this->assertEquals(
  340. 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
  341. );
  342. $this->assertEquals(
  343. 'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
  344. );
  345. }
  346. /////////////////////////////////////////
  347. // Tests concerning getExtensionVersion
  348. /////////////////////////////////////////
  349. /**
  350. * Data provider for negative getExtensionVersion() tests.
  351. *
  352. * @return array
  353. */
  354. public function getExtensionVersionFaultyDataProvider() {
  355. return array(
  356. array(''),
  357. array(0),
  358. array(new stdClass()),
  359. array(TRUE),
  360. );
  361. }
  362. /**
  363. * @test
  364. * @expectedException InvalidArgumentException
  365. * @dataProvider getExtensionVersionFaultyDataProvider
  366. */
  367. public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
  368. t3lib_extMgm::getExtensionVersion($key);
  369. }
  370. /**
  371. * @test
  372. */
  373. public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
  374. t3lib_extMgm::clearExtensionKeyMap();
  375. $uniqueSuffix = uniqid('test');
  376. $extensionKey = 'unloadedextension' . $uniqueSuffix;
  377. $this->assertEquals(
  378. '',
  379. t3lib_extMgm::getExtensionVersion($extensionKey)
  380. );
  381. }
  382. /**
  383. * @test
  384. */
  385. public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
  386. t3lib_extMgm::clearExtensionKeyMap();
  387. $uniqueSuffix = uniqid('test');
  388. $extensionKey = 'unloadedextension' . $uniqueSuffix;
  389. $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array(
  390. 'siteRelPath' => 'typo3_src/tests/t3lib/fixtures/',
  391. );
  392. $this->assertEquals(
  393. '1.2.3',
  394. t3lib_extMgm::getExtensionVersion($extensionKey)
  395. );
  396. }
  397. }
  398. ?>