[+BUGFIX] (Persistence) clone setDefaultQuerySettings for new queries
authorMarc Bastian Heinrichs <heinrichs@mbh-web.de>
Sun, 6 Mar 2011 15:25:47 +0000 (16:25 +0100)
committerBastian Waidelich <bastian@typo3.org>
Mon, 7 Mar 2011 15:20:02 +0000 (16:20 +0100)
If you set the default querySettings object for an repository the same
instance is used for any query-object created with createQuery
method. So any repository method modificating its (normaly) own
querySettings object will also modificate the defaultQuerySettings
object. Thanks to Benjamin Schulte

Fixes: #12702

Change-Id: I9c00e064dee4349735a52e1ae01c6b5d28774fe1

typo3/sysext/extbase/Classes/Persistence/Repository.php
typo3/sysext/extbase/Tests/Unit/Persistence/RepositoryTest.php

index 586f574..541c852 100644 (file)
@@ -352,7 +352,7 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi
                        $query->setOrderings($this->defaultOrderings);
                }
                if ($this->defaultQuerySettings !== NULL) {
-                       $query->setQuerySettings($this->defaultQuerySettings);
+                       $query->setQuerySettings(clone $this->defaultQuerySettings);
                }
                return $query;
        }
index d26f509..efd9382 100644 (file)
@@ -194,6 +194,25 @@ class Tx_Extbase_Tests_Unit_Persistence_RepositoryTest extends Tx_Extbase_Tests_
        /**
         * @test
         */
+       public function createQueryReturnsQueryWithUnmodifiedDefaultQuerySettings() {
+               $mockQueryFactory = $this->getMock('Tx_Extbase_Persistence_QueryFactory');
+               $mockQuery = new Tx_Extbase_Persistence_Query('foo');
+
+               $mockDefaultQuerySettings = $this->getMock('Tx_Extbase_Persistence_QuerySettingsInterface');
+               $this->repository->injectQueryFactory($mockQueryFactory);
+               $this->repository->setDefaultQuerySettings($mockDefaultQuerySettings);
+
+               $mockQueryFactory->expects($this->once())->method('create')->will($this->returnValue($mockQuery));
+               $this->repository->createQuery();
+
+               $instanceQuerySettings = $mockQuery->getQuerySettings();
+               $this->assertEquals($mockDefaultQuerySettings, $instanceQuerySettings);
+               $this->assertNotSame($mockDefaultQuerySettings, $instanceQuerySettings);
+       }
+
+       /**
+        * @test
+        */
        public function findAllCreatesQueryAndReturnsResultOfExecuteCall() {
                $expectedResult = $this->getMock('Tx_Extbase_Persistence_QueryResultInterface');
                $this->mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($expectedResult));