[BUGFIX] Unknown record collection type in RecordCollectionRepository
authorOliver Hader <oliver@typo3.org>
Wed, 15 Feb 2012 12:25:24 +0000 (13:25 +0100)
committerSusanne Moog <typo3@susannemoog.de>
Fri, 17 Feb 2012 08:32:15 +0000 (09:32 +0100)
t3lib_collection_RecordCollectionRepository::findByUid() only
has "uid" in the SQL query. The required "type" field is
missing here and leads to the accordant error message.

Change-Id: I02366e58436ab6ef294c25316f0f7b37f0b8be5e
Fixes: #33938
Releases: 4.7
Reviewed-on: http://review.typo3.org/9027
Reviewed-by: Tolleiv Nietsch
Tested-by: Tolleiv Nietsch
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
t3lib/collection/RecordCollectionRepository.php
tests/t3lib/collection/RecordCollectionRepositoryTest.php

index e49407a..6b96deb 100644 (file)
@@ -56,13 +56,13 @@ class t3lib_collection_RecordCollectionRepository {
         * Finds a record collection by uid.
         *
         * @param integer $uid The uid to be looked up
-        * @return NULL|t3lib_collection_RecordCollection
+        * @return NULL|t3lib_collection_AbstractRecordCollection
         */
        public function findByUid($uid) {
                $result = NULL;
 
                $data = $this->getDatabase()->exec_SELECTgetSingleRow(
-                       'uid',
+                       '*',
                        $this->table,
                        'uid=' . intval($uid) . t3lib_BEfunc::deleteClause($this->table)
                );
@@ -78,7 +78,7 @@ class t3lib_collection_RecordCollectionRepository {
         * Finds record collections by table name.
         *
         * @param string $tableName Name of the table to be looked up
-        * @return t3lib_collection_RecordCollection[]
+        * @return t3lib_collection_AbstractRecordCollection[]
         */
        public function findByTableName($tableName) {
                $conditions = array(
@@ -92,7 +92,7 @@ class t3lib_collection_RecordCollectionRepository {
         * Finds record collection by type.
         *
         * @param string $type Type to be looked up
-        * @return NULL|t3lib_collection_RecordCollection[]
+        * @return NULL|t3lib_collection_AbstractRecordCollection[]
         */
        public function findByType($type) {
                $conditions = array(
@@ -107,7 +107,7 @@ class t3lib_collection_RecordCollectionRepository {
         *
         * @param string $type Type to be looked up
         * @param string $tableName Name of the table to be looked up
-        * @return NULL|t3lib_collection_RecordCollection[]
+        * @return NULL|t3lib_collection_AbstractRecordCollection[]
         */
        public function findByTypeAndTableName($type, $tableName) {
                $conditions = array(
@@ -134,7 +134,7 @@ class t3lib_collection_RecordCollectionRepository {
         * Queries for multiple records for the given conditions.
         *
         * @param array $conditions Conditions concatenated with AND for query
-        * @return NULL|t3lib_collection_RecordCollection[]
+        * @return NULL|t3lib_collection_AbstractRecordCollection[]
         */
        protected function queryMultipleRecords(array $conditions = array()) {
                $result = NULL;
@@ -156,7 +156,7 @@ class t3lib_collection_RecordCollectionRepository {
         * Creates a record collection domain object.
         *
         * @param array $record Database record to be reconstituted
-        * @return t3lib_collection_RecordCollection
+        * @return t3lib_collection_AbstractRecordCollection
         */
        protected function createDomainObject(array $record) {
                switch ($record['type']) {
@@ -174,7 +174,7 @@ class t3lib_collection_RecordCollectionRepository {
         * Creates multiple record collection domain objects.
         *
         * @param array $data Array of multiple database records to be reconstituted
-        * @return t3lib_collection_RecordCollection[]
+        * @return t3lib_collection_AbstractRecordCollection[]
         */
        protected function createMultipleDomainObjects(array $data) {
                $collections = array();
index b12ca83..99e0078 100644 (file)
  */
 class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCase {
        /**
-        * @var t3lib_collection_RecordCollectionRepository
+        * @var PHPUnit_Framework_MockObject_MockObject|t3lib_collection_RecordCollectionRepository
         */
        protected $fixture;
 
        /**
-        * @var t3lib_DB
+        * @var PHPUnit_Framework_MockObject_MockObject|t3lib_DB
         */
        protected $databaseMock;
 
+       /**
+        * @var NULL|array
+        */
+       protected $getSingleRowCallbackReturnValue;
+
+       /**
+        * @var NULL|array
+        */
+       protected $getRowsCallbackReturnValue;
+
+       /**
+        * Sets up this test case.
+        */
        protected function setUp() {
                $this->databaseMock = $this->getMock(
                        't3lib_DB',
@@ -59,9 +72,14 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                        ->will($this->returnValue($this->databaseMock));
        }
 
+       /**
+        * Cleans up this test case.
+        */
        protected function tearDown() {
                unset($this->databaseMock);
                unset($this->fixture);
+               unset($this->getSingleRowCallbackReturnValue);
+               unset($this->getRowsCallbackReturnValue);
        }
 
        /**
@@ -73,7 +91,8 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetSingleRow')
-                       ->will($this->returnValue(NULL));
+                       ->will($this->returnCallback(array($this, 'getSingleRowCallback')));
+               $this->getSingleRowCallbackReturnValue = NULL;
 
                $object = $this->fixture->findByUid($testUid);
                $this->assertNull($object);
@@ -88,9 +107,11 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetSingleRow')
-                       ->will($this->returnValue(
-                               array('uid' => $testUid, 'type' => t3lib_collection_RecordCollectionRepository::TYPE_Static)
-                       ));
+                       ->will($this->returnCallback(array($this, 'getSingleRowCallback')));
+               $this->getSingleRowCallbackReturnValue = array(
+                       'uid' => $testUid,
+                       'type' => t3lib_collection_RecordCollectionRepository::TYPE_Static,
+               );
 
                $object = $this->fixture->findByUid($testUid);
                $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $object);
@@ -106,9 +127,11 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetSingleRow')
-                       ->will($this->returnValue(
-                               array('uid' => $testUid, 'type' => uniqid('unknown'))
-                       ));
+                       ->will($this->returnCallback(array($this, 'getSingleRowCallback')));
+               $this->getSingleRowCallbackReturnValue = array(
+                       'uid' => $testUid,
+                       'type' => uniqid('unknown'),
+               );
 
                $object = $this->fixture->findByUid($testUid);
        }
@@ -122,7 +145,8 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetRows')
-                       ->will($this->returnValue(NULL));
+                       ->will($this->returnCallback(array($this, 'getRowsCallback')));
+               $this->getRowsCallbackReturnValue = NULL;
 
                $objects = $this->fixture->findByType($type);
 
@@ -139,12 +163,11 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetRows')
-                       ->will($this->returnValue(
-                               array(
-                                       array('uid' => $testUid, 'type' => $type),
-                                       array('uid' => $testUid, 'type' => $type),
-                               )
-                       ));
+                       ->will($this->returnCallback(array($this, 'getRowsCallback')));
+               $this->getRowsCallbackReturnValue =     array(
+                       array('uid' => $testUid, 'type' => $type),
+                       array('uid' => $testUid, 'type' => $type),
+               );
 
                $objects = $this->fixture->findByType($type);
 
@@ -162,7 +185,8 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetRows')
-                       ->will($this->returnValue(NULL));
+                       ->will($this->returnCallback(array($this, 'getRowsCallback')));
+               $this->getRowsCallbackReturnValue = NULL;
 
                $objects = $this->fixture->findByTableName($testTable);
 
@@ -180,12 +204,11 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetRows')
-                       ->will($this->returnValue(
-                               array(
-                                       array('uid' => $testUid, 'type' => $type),
-                                       array('uid' => $testUid, 'type' => $type),
-                               )
-                       ));
+                       ->will($this->returnCallback(array($this, 'getRowsCallback')));
+               $this->getRowsCallbackReturnValue = array(
+                       array('uid' => $testUid, 'type' => $type),
+                       array('uid' => $testUid, 'type' => $type),
+               );
 
                $objects = $this->fixture->findByTableName($testTable);
 
@@ -204,7 +227,8 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetRows')
-                       ->will($this->returnValue(NULL));
+                       ->will($this->returnCallback(array($this, 'getRowsCallback')));
+               $this->getRowsCallbackReturnValue = NULL;
 
                $objects = $this->fixture->findByTypeAndTableName($type, $testTable);
 
@@ -222,12 +246,11 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->databaseMock
                        ->expects($this->once())
                        ->method('exec_SELECTgetRows')
-                       ->will($this->returnValue(
-                               array(
-                                       array('uid' => $testUid, 'type' => $type),
-                                       array('uid' => $testUid, 'type' => $type),
-                               )
-                       ));
+                       ->will($this->returnCallback(array($this, 'getRowsCallback')));
+               $this->getRowsCallbackReturnValue = array(
+                       array('uid' => $testUid, 'type' => $type),
+                       array('uid' => $testUid, 'type' => $type),
+               );
 
                $objects = $this->fixture->findByTypeAndTableName($type, $testTable);
 
@@ -235,5 +258,65 @@ class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCas
                $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $objects[0]);
                $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $objects[1]);
        }
+
+       /**
+        * Callback for exec_SELECTgetSingleRow
+        *
+        * @param string $fields
+        * @param string $table
+        * @return NULL|array
+        */
+       public function getSingleRowCallback($fields, $table) {
+               if (!is_array($this->getSingleRowCallbackReturnValue) || $fields === '*') {
+                       $returnValue = $this->getSingleRowCallbackReturnValue;
+               } else {
+                       $returnValue = $this->limitRecordFields(
+                               $fields,
+                               $this->getSingleRowCallbackReturnValue
+                       );
+               }
+
+               return $returnValue;
+       }
+
+       /**
+        * Callback for exec_SELECTgetRows
+        *
+        * @param string $fields
+        * @param string $table
+        * @return NULL|array
+        */
+       public function getRowsCallback($fields, $table) {
+               if (!is_array($this->getRowsCallbackReturnValue) || $fields === '*') {
+                       $returnValue = $this->getRowsCallbackReturnValue;
+               } else {
+                       $returnValue = array();
+
+                       foreach ($this->getRowsCallbackReturnValue as $record) {
+                               $returnValue[] = $this->limitRecordFields($fields, $record);
+                       }
+               }
+
+               return $returnValue;
+       }
+
+       /**
+        * Limits record fields to a given field list.
+        *
+        * @param string $fields List of fields
+        * @param array $record The database record (or the simulated one)
+        * @return array
+        */
+       protected function limitRecordFields($fields, array $record) {
+               $result = array();
+
+               foreach ($record as $field => $value) {
+                       if (t3lib_div::inList($fields, $field)) {
+                               $result[$field] = $value;
+                       }
+               }
+
+               return $result;
+       }
 }
 ?>
\ No newline at end of file