[BUGFIX] Fix old propertymapper checking target with namespace
authorSebastian Fischer <typo3@evoweb.de>
Mon, 27 Aug 2012 04:35:11 +0000 (06:35 +0200)
committerNicole Cordes <n.cordes@biz-design.biz>
Tue, 28 Aug 2012 14:26:15 +0000 (16:26 +0200)
The propertymapper checks if the target is an object by checking on
is_string and an '_' in the string. Since namespaces are in use it
needs to check on \

Change-Id: I1e664995733fa1438a4df62f0e41758b22045692
Fixes: #40242
Related: #40095
Releases: 6.0
Reviewed-on: http://review.typo3.org/14124
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert
typo3/sysext/extbase/Classes/Property/Mapper.php
typo3/sysext/extbase/Tests/Fixture/ClassWithGettersAndSetters.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Fixture/TxClassWithGettersAndSetters.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Unit/Property/MapperTest.php [new file with mode: 0644]

index 7736ef0..989b17b 100644 (file)
@@ -199,7 +199,7 @@ class Mapper implements \TYPO3\CMS\Core\SingletonInterface {
                if (!is_object($source) && !is_array($source)) {
                        throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidSource(('The source object must be a valid object or array, ' . gettype($target)) . ' given.', 1187807099);
                }
-               if (is_string($target) && strpos($target, '_') !== FALSE) {
+               if (is_string($target) && strpbrk($target, '_\\') !== FALSE) {
                        return $this->transformToObject($source, $target, '--none--');
                }
                if (!is_object($target) && !is_array($target)) {
@@ -354,4 +354,4 @@ class Mapper implements \TYPO3\CMS\Core\SingletonInterface {
 }
 
 
-?>
\ No newline at end of file
+?>
diff --git a/typo3/sysext/extbase/Tests/Fixture/ClassWithGettersAndSetters.php b/typo3/sysext/extbase/Tests/Fixture/ClassWithGettersAndSetters.php
new file mode 100644 (file)
index 0000000..7a1e48c
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Fixture;
+
+/*                                                                        *
+ * This script belongs to the Extbase framework.                          *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License as published by the *
+ * Free Software Foundation, either version 3 of the License, or (at your *
+ * option) any later version.                                             *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
+ * General Public License for more details.                               *
+ *                                                                        *
+ * You should have received a copy of the GNU Lesser General Public       *
+ * License along with the script.                                         *
+ * If not, see http://www.gnu.org/licenses/lgpl.html                      *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ * A dummy class with getters and setters for testing data mapping
+ *
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
+ */
+class ClassWithGettersAndSetters {
+
+       /**
+        * @var mixed
+        */
+       protected $property1;
+
+       /**
+        * @var mixed
+        */
+       protected $property2;
+
+       /**
+        * @var mixed
+        */
+       public $property3;
+
+       /**
+        * @var mixed
+        */
+       public $property4;
+
+       /**
+        * @param mixed $value
+        */
+       public function setProperty1($value) {
+               $this->property1 = $value;
+       }
+
+       /**
+        * @param mixed $value
+        */
+       public function setProperty2($value) {
+               $this->property2 = $value;
+       }
+
+       /**
+        * @param mixed $value
+        */
+       protected function getProperty1() {
+               return $this->property1;
+       }
+
+       /**
+        * @return mixed
+        */
+       public function getProperty2() {
+               return $this->property2;
+       }
+
+}
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Tests/Fixture/TxClassWithGettersAndSetters.php b/typo3/sysext/extbase/Tests/Fixture/TxClassWithGettersAndSetters.php
new file mode 100644 (file)
index 0000000..999d32c
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/*                                                                        *
+ * This script belongs to the Extbase framework.                          *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License as published by the *
+ * Free Software Foundation, either version 3 of the License, or (at your *
+ * option) any later version.                                             *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
+ * General Public License for more details.                               *
+ *                                                                        *
+ * You should have received a copy of the GNU Lesser General Public       *
+ * License along with the script.                                         *
+ * If not, see http://www.gnu.org/licenses/lgpl.html                      *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ * A dummy class with getters and setters for testing data mapping
+ *
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
+ */
+class Tx_Extbase_Tests_Fixture_TxClassWithGettersAndSetters extends \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithGettersAndSetters {
+}
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Tests/Unit/Property/MapperTest.php b/typo3/sysext/extbase/Tests/Unit/Property/MapperTest.php
new file mode 100644 (file)
index 0000000..548356e
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Unit\Property;
+
+/*                                                                        *
+ * This script belongs to the Extbase framework.                            *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License as published by the *
+ * Free Software Foundation, either version 3 of the License, or (at your *
+ * option) any later version.                                             *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
+ * General Public License for more details.                               *
+ *                                                                        *
+ * You should have received a copy of the GNU Lesser General Public       *
+ * License along with the script.                                         *
+ * If not, see http://www.gnu.org/licenses/lgpl.html                      *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+require_once(__DIR__ . '/../../Fixture/TxClassWithGettersAndSetters.php');
+
+/**
+ * Testcase for the Property Mapper
+ *
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
+ * @covers \TYPO3\CMS\Extbase\Property\Mapper
+ */
+class MapperTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Property\Mapper
+        */
+       protected $fixture;
+
+       /**
+        * Sets up this test case
+        *
+        * @return void
+        * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
+        */
+       public function setUp() {
+               /** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */
+               $objectManager = new \TYPO3\CMS\Extbase\Object\ObjectManager();
+
+               /** @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager $persistenceManager */
+               $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
+
+               /** @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactory $queryFactory */
+               $queryFactory = new \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactory();
+
+               /** @var \TYPO3\CMS\Extbase\Reflection\Service $reflectionService */
+               $reflectionService = new \TYPO3\CMS\Extbase\Reflection\Service();
+               $reflectionService->injectObjectManager($objectManager);
+
+               /** @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver */
+               $validatorResolver = new \TYPO3\CMS\Extbase\Validation\ValidatorResolver();
+
+               $this->fixture = new \TYPO3\CMS\Extbase\Property\Mapper();
+               $this->fixture->injectObjectManager($objectManager);
+               $this->fixture->injectPersistenceManager($persistenceManager);
+               $this->fixture->injectQueryFactory($queryFactory);
+               $this->fixture->injectReflectionService($reflectionService);
+               $this->fixture->injectValidatorResolver($validatorResolver);
+       }
+
+       public function tearDown() {
+               unset($this->fixture);
+       }
+
+       /**
+        * @test
+        */
+       public function mapReturnsObjectForNamespaceClasses() {
+               $source = array(
+                       'property1' => 'foo',
+                       'property2' => 'bar'
+               );
+
+               $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithGettersAndSetters();
+               $expectedObject->setProperty1($source['property1']);
+               $expectedObject->setProperty2($source['property2']);
+
+               $this->assertEquals($expectedObject, $this->fixture->map(array('property1', 'property2'), $source, 'TYPO3\\CMS\\Extbase\\Tests\\Fixture\\ClassWithGettersAndSetters'));
+       }
+
+       /**
+        * @test
+        */
+       public function mapReturnsObjectForOldTxClasses() {
+               $source = array(
+                       'property1' => 'foo',
+                       'property2' => 'bar'
+               );
+
+               $expectedObject = new \Tx_Extbase_Tests_Fixture_TxClassWithGettersAndSetters();
+               $expectedObject->setProperty1($source['property1']);
+               $expectedObject->setProperty2($source['property2']);
+
+               $this->assertEquals($expectedObject, $this->fixture->map(array('property1', 'property2'), $source, 'Tx_Extbase_Tests_Fixture_TxClassWithGettersAndSetters'));
+       }
+
+}
+
+
+?>
\ No newline at end of file