[BUGFIX] Importing extensions from repository fails 62/18962/2
authorChristian Kuhn <lolli@schwarzbu.ch>
Fri, 15 Mar 2013 21:54:00 +0000 (22:54 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 15 Mar 2013 22:46:35 +0000 (23:46 +0100)
If importing the extension list from TER, an incoming XML extension
row has a field 'category' that contains either a string like
'plugin' or a category number.
The list is persisted to the 'extension' repository. In the
database, the category is stored as integer. There is a mapping
function in the extension model to get the integer category
index from given XML input. The method is faulty and returns
strings instead of an index if the given category is a string
itself. This results in database errors during insert.

The patch fixes the method, renames it along the way and adds
unit tests to document desired behavior.

Change-Id: I601b84d0829f3d20a4cdd25a3f7f37e8128dbce1
Fixes: #46115
Releases: 6.1, 6.0
Reviewed-on: https://review.typo3.org/18962
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/extensionmanager/Classes/Domain/Model/Extension.php
typo3/sysext/extensionmanager/Classes/Utility/Importer/ExtensionListUtility.php
typo3/sysext/extensionmanager/Tests/Unit/Domain/Model/ExtensionTest.php [new file with mode: 0644]

index d5b2c96..f83a370 100644 (file)
@@ -224,36 +224,26 @@ class Extension extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
        }
 
        /**
-        * Returns either array with all default categories or index/title
-        * of a category entry.
+        * Returns category index from a given string or a integer.
+        * Fallback to 4 - 'misc' in case string is not found or integer ist out of range.
         *
-        * @param mixed $cat category title or category index
-        * @return mixed
-        */
-       public function getDefaultCategory($cat = NULL) {
-               $return = '';
-               if (is_null($cat)) {
-                       $return = self::$defaultCategories;
-               } else {
-                       if (is_string($cat)) {
-                               // default category
-                               $catIndex = 4;
-                               if (array_key_exists(strtolower($cat), self::$defaultCategories)) {
-                                       $catIndex = self::$defaultCategories[strtolower($cat)];
-                               }
-                               $return = $catIndex;
-                       } else {
-                               if (is_int($cat) && $cat >= 0) {
-                                       $catTitle = array_search($cat, self::$defaultCategories);
-                                       // default category
-                                       if (!$catTitle) {
-                                               $catTitle = 'misc';
-                                       }
-                                       $return = $catTitle;
-                               }
+        * @param string|integer $category Category string or integer
+        * @return integer Valid category index
+        */
+       public function getCategoryIndexFromStringOrNumber($category) {
+               $categoryIndex = 4;
+               if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($category)) {
+                       $categoryIndex = (integer)$category;
+                       if ($categoryIndex < 0 || $categoryIndex > 9) {
+                               $categoryIndex = 4;
+                       }
+               } elseif (is_string($category)) {
+                       $categoryIndex = array_search($category, self::$defaultCategories);
+                       if ($categoryIndex === FALSE) {
+                               $categoryIndex = 4;
                        }
                }
-               return $return;
+               return $categoryIndex;
        }
 
        /**
index 2a6b4c7..6285bbb 100644 (file)
@@ -193,7 +193,7 @@ class ExtensionListUtility implements \SplObserver {
                        $this->repositoryUid,
                        $this->extensionModel->getDefaultState($subject->getState() ? $subject->getState() : ''),
                        intval($subject->getReviewstate()),
-                       $this->extensionModel->getDefaultCategory($subject->getCategory() ? $subject->getCategory() : ''),
+                       $this->extensionModel->getCategoryIndexFromStringOrNumber($subject->getCategory() ? $subject->getCategory() : ''),
                        $subject->getDescription() ? $subject->getDescription() : '',
                        $subject->getDependencies() ? $subject->getDependencies() : '',
                        $subject->getUploadcomment() ? $subject->getUploadcomment() : ''
diff --git a/typo3/sysext/extensionmanager/Tests/Unit/Domain/Model/ExtensionTest.php b/typo3/sysext/extensionmanager/Tests/Unit/Domain/Model/ExtensionTest.php
new file mode 100644 (file)
index 0000000..d587710
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Domain\Model;
+
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Extension test
+ */
+class ExtensionTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
+
+       /**
+        * Data provider for getCategoryIndexFromStringOrNumberReturnsIndex
+        *
+        * @return array
+        */
+       public function getCategoryIndexFromStringOrNumberReturnsIndexDataProvider() {
+               return array(
+                       'empty string' => array(
+                               '',
+                               4
+                       ),
+                       'existing category string' => array(
+                               'plugin',
+                               3
+                       ),
+                       'not existing category string' => array(
+                               'foo',
+                               4
+                       ),
+                       'string number 3' => array(
+                               '3',
+                               3
+                       ),
+                       'integer 3' => array(
+                               3,
+                               3
+                       ),
+                       'string number not in range -1' => array(
+                               '-1',
+                               4
+                       ),
+                       'integer not in range -1' => array(
+                               -1,
+                               4
+                       ),
+                       'string number not in range 11' => array(
+                               '11',
+                               4
+                       ),
+                       'integer not in range 11' => array(
+                               11,
+                               4
+                       ),
+                       'object' => array(
+                               new \stdClass(),
+                               4
+                       ),
+                       'array' => array(
+                               array(),
+                               4
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider getCategoryIndexFromStringOrNumberReturnsIndexDataProvider
+        * @param string|integer $input Given input
+        * @param integer $expected Expected result
+        * @return void
+        */
+       public function getCategoryIndexFromStringOrNumberReturnsIndex($input, $expected) {
+               $extension = new \TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
+               $this->assertEquals($expected, $extension->getCategoryIndexFromStringOrNumber($input));
+       }
+}
+
+?>
\ No newline at end of file