[TASK] Doctrine: Migrate RecordCollection 44/49544/2
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Mon, 22 Aug 2016 18:10:20 +0000 (20:10 +0200)
committerBenni Mack <benni@typo3.org>
Mon, 22 Aug 2016 19:33:46 +0000 (21:33 +0200)
Change-Id: Ib6d0105e0436621742914fc41340d3fd7af694b4
Resolves: #77590
Releases: master
Reviewed-on: https://review.typo3.org/49544
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Classes/Collection/RecordCollectionRepository.php
typo3/sysext/core/Tests/Functional/Collection/RecordCollectionRepositoryTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Collection/RecordCollectionRepositoryTest.php [deleted file]

index 1752ba5..5889d8f 100644 (file)
@@ -14,7 +14,6 @@ namespace TYPO3\CMS\Core\Collection;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -86,10 +85,13 @@ class RecordCollectionRepository
      */
     public function findByTableName($tableName)
     {
-        $conditions = array(
-            $this->tableField . '=' . $this->getDatabaseConnection()->fullQuoteStr($tableName, $this->table)
-        );
-        return $this->queryMultipleRecords($conditions);
+        $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable($tableName)
+            ->expr();
+
+        return $this->queryMultipleRecords([
+            $expressionBuilder->eq($this->tableField, $expressionBuilder->literal($tableName))
+        ]);
     }
 
     /**
@@ -100,10 +102,13 @@ class RecordCollectionRepository
      */
     public function findByType($type)
     {
-        $conditions = array(
-            $this->typeField . '=' . $this->getDatabaseConnection()->fullQuoteStr($type, $this->table)
-        );
-        return $this->queryMultipleRecords($conditions);
+        $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable($this->table)
+            ->expr();
+
+        return $this->queryMultipleRecords([
+            $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type))
+        ]);
     }
 
     /**
@@ -115,11 +120,14 @@ class RecordCollectionRepository
      */
     public function findByTypeAndTableName($type, $tableName)
     {
-        $conditions = array(
-            $this->typeField . '=' . $this->getDatabaseConnection()->fullQuoteStr($type, $this->table),
-            $this->tableField . '=' . $this->getDatabaseConnection()->fullQuoteStr($tableName, $this->table)
-        );
-        return $this->queryMultipleRecords($conditions);
+        $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable($this->table)
+            ->expr();
+
+        return $this->queryMultipleRecords([
+            $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type)),
+            $expressionBuilder->eq($this->tableField, $expressionBuilder->literal($tableName))
+        ]);
     }
 
     /**
@@ -130,7 +138,13 @@ class RecordCollectionRepository
      */
     public function deleteByUid($uid)
     {
-        $this->getDatabaseConnection()->exec_UPDATEquery($this->table, 'uid=' . (int)$uid, array('deleted' => 1, 'tstamp' => $GLOBALS['EXEC_TIME']));
+        GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getConnectionForTable($this->table)
+            ->update(
+                $this->table,
+                ['deleted' => 1, 'tstamp' => (int)$GLOBALS['EXEC_TIME']],
+                ['uid' => (int)$uid]
+            );
     }
 
     /**
@@ -142,19 +156,24 @@ class RecordCollectionRepository
     protected function queryMultipleRecords(array $conditions = array())
     {
         $result = null;
+
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
+        $queryBuilder->getRestrictions()
+            ->removeAll()
+            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+
+        $queryBuilder->select('*')
+            ->from($this->table);
+
         if (!empty($conditions)) {
-            $conditionsWhereClause = implode(' AND ', $conditions);
-        } else {
-            $conditionsWhereClause = '1=1';
+            $queryBuilder->where(...$conditions);
         }
-        $data = $this->getDatabaseConnection()->exec_SELECTgetRows(
-            '*',
-            $this->table,
-            $conditionsWhereClause . BackendUtility::deleteClause($this->table)
-        );
-        if ($data !== null) {
+
+        $data = $queryBuilder->execute()->fetchAll();
+        if (!empty($data)) {
             $result = $this->createMultipleDomainObjects($data);
         }
+
         return $result;
     }
 
@@ -191,14 +210,4 @@ class RecordCollectionRepository
         }
         return $collections;
     }
-
-    /**
-     * Gets the database object.
-     *
-     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
-     */
-    protected function getDatabaseConnection()
-    {
-        return $GLOBALS['TYPO3_DB'];
-    }
 }
diff --git a/typo3/sysext/core/Tests/Functional/Collection/RecordCollectionRepositoryTest.php b/typo3/sysext/core/Tests/Functional/Collection/RecordCollectionRepositoryTest.php
new file mode 100644 (file)
index 0000000..ce677af
--- /dev/null
@@ -0,0 +1,152 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Functional\Collection;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Collection\RecordCollectionRepository;
+use TYPO3\CMS\Core\Collection\StaticRecordCollection;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Tests\FunctionalTestCase;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Test case for \TYPO3\CMS\Core\Collection\RecordCollectionRepository
+ */
+class RecordCollectionRepositoryTest extends FunctionalTestCase
+{
+    /**
+     * @var RecordCollectionRepository
+     */
+    protected $subject;
+
+    /**
+     * @var string
+     */
+    protected $testTableName;
+
+    /**
+     * Sets up this test case.
+     */
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $this->subject = GeneralUtility::makeInstance(RecordCollectionRepository::class);
+        $this->testTableName = $this->getUniqueId('tx_testtable');
+    }
+
+    protected function tearDown()
+    {
+        parent::tearDown();
+
+        GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getConnectionForTable('sys_collection')
+            ->truncate('sys_collection');
+    }
+
+    /**
+     * @test
+     */
+    public function doesFindByTypeReturnNull()
+    {
+        $type = RecordCollectionRepository::TYPE_Static;
+        $objects = $this->subject->findByType($type);
+        $this->assertNull($objects);
+    }
+
+    /**
+     * @test
+     */
+    public function doesFindByTypeReturnObjects()
+    {
+        $type = RecordCollectionRepository::TYPE_Static;
+        $this->insertTestData([
+            ['type' => $type, 'table_name' => $this->testTableName],
+            ['type' => $type, 'table_name' => $this->testTableName]
+        ]);
+
+        $objects = $this->subject->findByType($type);
+        $this->assertCount(2, $objects);
+        $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
+        $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
+    }
+
+    /**
+     * @test
+     */
+    public function doesFindByTableNameReturnNull()
+    {
+        $objects = $this->subject->findByTableName($this->testTableName);
+        $this->assertNull($objects);
+    }
+
+    /**
+     * @test
+     */
+    public function doesFindByTableNameReturnObjects()
+    {
+        $type = RecordCollectionRepository::TYPE_Static;
+        $this->insertTestData([
+            ['type' => $type, 'table_name' => $this->testTableName],
+            ['type' => $type, 'table_name' => $this->testTableName]
+        ]);
+        $objects = $this->subject->findByTableName($this->testTableName);
+
+        $this->assertCount(2, $objects);
+        $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
+        $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
+    }
+
+    /**
+     * @test
+     */
+    public function doesFindByTypeAndTableNameReturnNull()
+    {
+        $type = RecordCollectionRepository::TYPE_Static;
+        $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
+
+        $this->assertNull($objects);
+    }
+
+    /**
+     * @test
+     */
+    public function doesFindByTypeAndTableNameReturnObjects()
+    {
+        $type = RecordCollectionRepository::TYPE_Static;
+        $this->insertTestData([
+            ['type' => $type, 'table_name' => $this->testTableName],
+            ['type' => $type, 'table_name' => $this->testTableName]
+        ]);
+        $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
+
+        $this->assertCount(2, $objects);
+        $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
+        $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
+    }
+
+    /**
+     * Insert test rows into the sys_collection table
+     *
+     * @param array $rows
+     */
+    protected function insertTestData(array $rows)
+    {
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_collection');
+
+        foreach ($rows as $row) {
+            $connection->insert('sys_collection', $row);
+        }
+    }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Collection/RecordCollectionRepositoryTest.php b/typo3/sysext/core/Tests/Unit/Collection/RecordCollectionRepositoryTest.php
deleted file mode 100644 (file)
index 540365a..0000000
+++ /dev/null
@@ -1,244 +0,0 @@
-<?php
-namespace TYPO3\CMS\Core\Tests\Unit\Collection;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-use Prophecy\Argument;
-use TYPO3\CMS\Core\Database\Connection;
-use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
-use TYPO3\CMS\Core\Database\Query\QueryBuilder;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-
-/**
- * Test case for \TYPO3\CMS\Core\Collection\RecordCollectionRepository
- */
-class RecordCollectionRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
-{
-    /**
-     * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Collection\RecordCollectionRepository
-     */
-    protected $subject;
-
-    /**
-     * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Database\DatabaseConnection
-     */
-    protected $databaseMock;
-
-    /**
-     * @var NULL|array
-     */
-    protected $getSingleRowCallbackReturnValue;
-
-    /**
-     * @var NULL|array
-     */
-    protected $getRowsCallbackReturnValue;
-
-    /**
-     * @var string
-     */
-    protected $testTableName;
-
-    /**
-     * @var array
-     */
-    protected $singletonInstances;
-
-    /**
-     * Sets up this test case.
-     */
-    protected function setUp()
-    {
-        $this->singletonInstances = GeneralUtility::getSingletonInstances();
-
-        // Setup mocking for BackendUtility::deleteClause()
-        $connectionProphet = $this->prophesize(Connection::class);
-        $connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
-
-        $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
-        $queryBuilderProphet->expr()->willReturn(
-            GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
-        );
-
-        $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
-        $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
-        GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
-
-        $this->databaseMock = $this->getMockBuilder(\TYPO3\CMS\Core\Database\DatabaseConnection::class)
-            ->setMethods(array('exec_UPDATEquery', 'exec_SELECTgetSingleRow', 'exec_SELECTgetRows', 'fullQuoteStr'))
-            ->getMock();
-        $this->subject = $this->getMockBuilder(\TYPO3\CMS\Core\Collection\RecordCollectionRepository::class)
-            ->setMethods(array('getDatabaseConnection'))
-            ->getMock();
-        $this->subject->expects($this->any())
-            ->method('getDatabaseConnection')
-            ->will($this->returnValue($this->databaseMock));
-        $this->testTableName = $this->getUniqueId('tx_testtable');
-    }
-
-    protected function tearDown()
-    {
-        GeneralUtility::purgeInstances();
-        GeneralUtility::resetSingletonInstances($this->singletonInstances);
-        parent::tearDown();
-    }
-
-    /**
-     * @test
-     */
-    public function doesFindByTypeReturnNull()
-    {
-        $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
-        $this->getRowsCallbackReturnValue = null;
-        $objects = $this->subject->findByType($type);
-        $this->assertNull($objects);
-    }
-
-    /**
-     * @test
-     */
-    public function doesFindByTypeReturnObjects()
-    {
-        $testUid = rand(1, 1000);
-        $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
-        $this->getRowsCallbackReturnValue = array(
-            array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
-            array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
-        );
-        $objects = $this->subject->findByType($type);
-        $this->assertEquals(2, count($objects));
-        $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
-        $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
-    }
-
-    /**
-     * @test
-     */
-    public function doesFindByTableNameReturnNull()
-    {
-        $testTable = $this->getUniqueId('sys_collection_');
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
-        $this->getRowsCallbackReturnValue = null;
-        $objects = $this->subject->findByTableName($testTable);
-        $this->assertNull($objects);
-    }
-
-    /**
-     * @test
-     */
-    public function doesFindByTableNameReturnObjects()
-    {
-        $testUid = rand(1, 1000);
-        $testTable = $this->getUniqueId('sys_collection_');
-        $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
-        $this->getRowsCallbackReturnValue = array(
-            array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
-            array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
-        );
-        $objects = $this->subject->findByTableName($testTable);
-        $this->assertEquals(2, count($objects));
-        $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
-        $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
-    }
-
-    /**
-     * @test
-     */
-    public function doesFindByTypeAndTableNameReturnNull()
-    {
-        $testTable = $this->getUniqueId('sys_collection_');
-        $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
-        $this->getRowsCallbackReturnValue = null;
-        $objects = $this->subject->findByTypeAndTableName($type, $testTable);
-        $this->assertNull($objects);
-    }
-
-    /**
-     * @test
-     */
-    public function doesFindByTypeAndTableNameReturnObjects()
-    {
-        $testUid = rand(1, 1000);
-        $testTable = $this->getUniqueId('sys_collection_');
-        $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
-        $this->getRowsCallbackReturnValue = array(
-            array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
-            array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
-        );
-        $objects = $this->subject->findByTypeAndTableName($type, $testTable);
-        $this->assertEquals(2, count($objects));
-        $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
-        $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $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 (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($fields, $field)) {
-                $result[$field] = $value;
-            }
-        }
-        return $result;
-    }
-}