[TASK] Detect and import xml file as well as t3d on ext initialization 64/30464/2
authorMarc Bastian Heinrichs <typo3@mbh-software.de>
Wed, 28 May 2014 07:50:15 +0000 (09:50 +0200)
committerXavier Perseguers <xavier@typo3.org>
Wed, 11 Jun 2014 15:04:52 +0000 (17:04 +0200)
Whenever installing an extension, the file located at
EXT:ext_key/Initialisation/data.t3d is detected and its content imported.
This patch adds support for EXT:ext_key/Initialisation/data.xml file as
well.

Resolves: #59154
Releases: 6.2
Change-Id: Ie5cf843ed7fff24c0cc022892d68fa86ba14a674
Reviewed-on: https://review.typo3.org/30464
Reviewed-by: Tymoteusz Motylewski
Tested-by: Tymoteusz Motylewski
Reviewed-by: Fabien Udriot
Tested-by: Fabien Udriot
Reviewed-by: Zbigniew Jacko
Tested-by: Zbigniew Jacko
Reviewed-by: Xavier Perseguers
Reviewed-by: Steffen Ritter
Tested-by: Xavier Perseguers
typo3/sysext/extensionmanager/Classes/Utility/InstallUtility.php
typo3/sysext/extensionmanager/Tests/Unit/Utility/InstallUtilityTest.php

index 2704165..acde655 100644 (file)
@@ -431,27 +431,44 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
-        * Uses the export import extension to import a T3DFile to PID 0
+        * Uses the export import extension to import a T3D or XML file to PID 0
         * Execution state is saved in the this->registry, so it only happens once
         *
         * @param string $extensionSiteRelPath
         * @return void
         */
        protected function importT3DFile($extensionSiteRelPath) {
-               $t3dImportRelFile = $extensionSiteRelPath . 'Initialisation/data.t3d';
-               if (!$this->registry->get('extensionDataImport', $t3dImportRelFile)) {
-                       $t3dImportFile = PATH_site . $t3dImportRelFile;
-                       if (file_exists($t3dImportFile)) {
-                               $importExportUtility = $this->objectManager->get('TYPO3\\CMS\\Impexp\\Utility\\ImportExportUtility');
-                               try {
-                                       $importResult = $importExportUtility->importT3DFile($t3dImportFile, 0);
-                                       $this->registry->set('extensionDataImport', $t3dImportRelFile, 1);
-                                       $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionT3DImport', array($t3dImportRelFile, $importResult, $this));
-                               } catch (\ErrorException $e) {
-                                       /** @var \TYPO3\CMS\Core\Log\Logger $logger */
-                                       $logger = $this->objectManager->get('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger(__CLASS__);
-                                       $logger->log(\TYPO3\CMS\Core\Log\LogLevel::WARNING, $e->getMessage());
-                               }
+               $registryKeysToCheck = array(
+                       $extensionSiteRelPath . 'Initialisation/data.t3d',
+                       $extensionSiteRelPath . 'Initialisation/dataImported',
+               );
+               foreach ($registryKeysToCheck as $registryKeyToCheck) {
+                       if ($this->registry->get('extensionDataImport', $registryKeyToCheck)) {
+                               // Data was imported before => early return
+                               return;
+                       }
+               }
+               $importFileToUse = NULL;
+               $possibleImportFiles = array(
+                       $extensionSiteRelPath . 'Initialisation/data.t3d',
+                       $extensionSiteRelPath . 'Initialisation/data.xml'
+               );
+               foreach ($possibleImportFiles as $possibleImportFile) {
+                       if (!file_exists(PATH_site . $possibleImportFile)) {
+                               continue;
+                       }
+                       $importFileToUse = $possibleImportFile;
+               }
+               if ($importFileToUse !== NULL) {
+                       $importExportUtility = $this->objectManager->get('TYPO3\\CMS\\Impexp\\Utility\\ImportExportUtility');
+                       try {
+                               $importResult = $importExportUtility->importT3DFile(PATH_site . $importFileToUse, 0);
+                               $this->registry->set('extensionDataImport', $extensionSiteRelPath . 'Initialisation/dataImported', 1);
+                               $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionT3DImport', array($importFileToUse, $importResult, $this));
+                       } catch (\ErrorException $e) {
+                               /** @var \TYPO3\CMS\Core\Log\Logger $logger */
+                               $logger = $this->objectManager->get('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger(__CLASS__);
+                               $logger->log(\TYPO3\CMS\Core\Log\LogLevel::WARNING, $e->getMessage());
                        }
                }
        }
index 7445ad0..91bc2d8 100644 (file)
@@ -238,13 +238,29 @@ class InstallUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 
        /**
+        * @return array
+        */
+       public function processDatabaseUpdatesCallsImportFileDataProvider() {
+               return array(
+                       'T3D file' => array(
+                               'data.t3d'
+                       ),
+                       'XML file' => array(
+                               'data.xml'
+                       )
+               );
+       }
+
+       /**
+        * @param string $fileName
         * @test
+        * @dataProvider processDatabaseUpdatesCallsImportFileDataProvider
         */
-       public function processDatabaseUpdatesCallsImportT3DFile() {
+       public function processDatabaseUpdatesCallsImportFile($fileName) {
                $extKey = $this->createFakeExtension();
                $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
                \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . '/Initialisation');
-               file_put_contents($absPath . '/Initialisation/data.t3d', 'DUMMY');
+               file_put_contents($absPath . '/Initialisation/' . $fileName, 'DUMMY');
                $installMock = $this->getAccessibleMock(
                        'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
                        array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'),
@@ -259,20 +275,56 @@ class InstallUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 
        /**
+        * @return array
+        */
+       public function importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider() {
+               return array(
+                       'Import T3D file when T3D was imported before extension to XML' => array(
+                               'data.t3d',
+                               'dataImported',
+                               'data.t3d',
+                       ),
+                       'Import T3D file when a file was imported after extension to XML' => array(
+                               'data.t3d',
+                               'data.t3d',
+                               'dataImported'
+                       ),
+                       'Import XML file when T3D was imported before extension to XML' => array(
+                               'data.xml',
+                               'dataImported',
+                               'data.t3d'
+                       ),
+                       'Import XML file when a file was imported after extension to XML' => array(
+                               'data.xml',
+                               'data.t3d',
+                               'dataImported'
+                       )
+               );
+       }
+
+       /**
+        *
+        * @param string $fileName
+        * @param string $registryNameReturnsFalse
+        * @param string $registryNameReturnsTrue
         * @test
+        * @dataProvider importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider
         */
-       public function importT3DFileDoesNotImportFileIfAlreadyImported() {
+       public function importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue) {
                $extKey = $this->createFakeExtension();
                $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
                \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
-               file_put_contents($absPath . 'Initialisation/data.t3d', 'DUMMY');
+               file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
                $registryMock = $this->getMock('\\TYPO3\\CMS\\Core\\Registry', array('get', 'set'));
                $registryMock
-                       ->expects($this->once())
+                       ->expects($this->any())
                        ->method('get')
-                       ->with('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/data.t3d')
-                       ->will($this->returnValue(TRUE)
-                       );
+                       ->will($this->returnValueMap(
+                               array(
+                                       array('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsFalse, NULL, FALSE),
+                                       array('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsTrue, NULL, TRUE),
+                               )
+                       ));
                $installMock = $this->getAccessibleMock(
                        'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
                        array('getRegistry', 'getImportExportUtility'),