[TASK] Add functional tests for QueryGeneratorTest 74/56974/7
authorAlexander Schnitzler <git@alexanderschnitzler.de>
Wed, 16 May 2018 10:21:30 +0000 (12:21 +0200)
committerJan Helke <typo3@helke.de>
Wed, 16 May 2018 16:27:08 +0000 (18:27 +0200)
Releases: master
Resolves: #85024
Change-Id: I623bff02e95056422c1faf859a7d34740ba6b60f
Reviewed-on: https://review.typo3.org/56974
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
typo3/sysext/core/Classes/Database/QueryGenerator.php
typo3/sysext/core/Tests/Functional/Database/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv [new file with mode: 0644]
typo3/sysext/core/Tests/Functional/Database/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv [new file with mode: 0644]
typo3/sysext/core/Tests/Functional/Database/QueryGeneratorTest.php [new file with mode: 0644]

index a4c0fcc..4a81935 100644 (file)
@@ -1581,7 +1581,8 @@ class QueryGenerator
             $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
             $queryBuilder->select('uid')
                 ->from('pages')
-                ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)));
+                ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
+                ->orderBy('uid');
             if ($permClause !== '') {
                 $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($permClause));
             }
diff --git a/typo3/sysext/core/Tests/Functional/Database/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv b/typo3/sysext/core/Tests/Functional/Database/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv
new file mode 100644 (file)
index 0000000..e09d377
--- /dev/null
@@ -0,0 +1,7 @@
+"pages",,,
+,"uid","pid","title"
+,1,0,"A"
+,2,1,"B"
+,3,2,"C"
+,4,1,"D"
+,5,4,"E"
diff --git a/typo3/sysext/core/Tests/Functional/Database/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv b/typo3/sysext/core/Tests/Functional/Database/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv
new file mode 100644 (file)
index 0000000..5e0197b
--- /dev/null
@@ -0,0 +1,9 @@
+"pages",,,,
+,"uid","pid","hidden","deleted"
+,1,0,0,0
+,2,1,0,0
+,3,2,0,0
+,4,3,0,0
+,5,4,0,0
+,6,5,1,0
+,7,6,0,1
diff --git a/typo3/sysext/core/Tests/Functional/Database/QueryGeneratorTest.php b/typo3/sysext/core/Tests/Functional/Database/QueryGeneratorTest.php
new file mode 100644 (file)
index 0000000..e55d549
--- /dev/null
@@ -0,0 +1,271 @@
+<?php
+declare(strict_types = 1);
+
+namespace TYPO3\CMS\Core\Tests\Functional\Database;
+
+/*
+ * 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\Database\QueryGenerator;
+use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
+
+/**
+ * Test case
+ */
+class QueryGeneratorTest extends FunctionalTestCase
+{
+    /**
+     * @test
+     */
+    public function getTreeListReturnsIngoingIdIfDepthIsZero(): void
+    {
+        $id = 1;
+        $depth = 0;
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth);
+
+        static::assertSame($id, $treeList);
+    }
+
+    /**
+     * @test
+     */
+    public function getTreeListReturnsIngoingIdIfIdIsZero(): void
+    {
+        $id = 0;
+        $depth = 1;
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth);
+
+        static::assertSame($id, $treeList);
+    }
+
+    /**
+     * @test
+     */
+    public function getTreeListReturnsPositiveIngoingIdIfIdIsNegative(): void
+    {
+        $id = -1;
+        $depth = 0;
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth);
+
+        static::assertSame(1, $treeList);
+    }
+
+    /**
+     * @test
+     */
+    public function getTreeListReturnsEmptyStringIfIdAndDepthAreZeroAndBeginDoesNotEqualZero(): void
+    {
+        $id = 0;
+        $depth = 0;
+        $begin = 1;
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth, $begin);
+
+        static::assertSame('', $treeList);
+    }
+
+    /**
+     * @test
+     */
+    public function getTreeListReturnsIncomingIdIfNoSubPageRecordsOfThatIdExist(): void
+    {
+        $id = 1;
+        $depth = 1;
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth);
+
+        static::assertSame($id, $treeList);
+    }
+
+    /**
+     * @test
+     */
+    public function getTreeListRespectsPermClauses(): void
+    {
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv');
+
+        $id = 1;
+        $depth = 99;
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth, 0, 'hidden = 0');
+
+        static::assertSame('1,2,3,4,5', $treeList);
+    }
+
+    /**
+     * @test
+     * @dataProvider dataForGetTreeListReturnsListOfIdsWithBeginSetToZero
+     * @param int $id
+     * @param int $depth
+     * @param mixed $expectation
+     */
+    public function getTreeListReturnsListOfIdsWithBeginSetToZero(int $id, int $depth, $expectation): void
+    {
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv');
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth);
+
+        static::assertSame($expectation, $treeList);
+    }
+
+    /**
+     * @return array
+     */
+    public function dataForGetTreeListReturnsListOfIdsWithBeginSetToZero(): array
+    {
+        return [
+            // [$id, $depth, $expectation]
+            [
+                1,
+                1,
+                '1,2'
+            ],
+            [
+                1,
+                2,
+                '1,2,3'
+            ],
+            [
+                1,
+                99,
+                '1,2,3,4,5,6'
+            ],
+            [
+                2,
+                1,
+                '2,3'
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider dataForGetTreeListReturnsListOfIdsWithBeginSetToMinusOne
+     * @param int $id
+     * @param int $depth
+     * @param mixed $expectation
+     */
+    public function getTreeListReturnsListOfIdsWithBeginSetToMinusOne(int $id, int $depth, $expectation): void
+    {
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv');
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth, -1);
+
+        static::assertSame($expectation, $treeList);
+    }
+
+    /**
+     * @return array
+     */
+    public function dataForGetTreeListReturnsListOfIdsWithBeginSetToMinusOne(): array
+    {
+        return [
+            // [$id, $depth, $expectation]
+            [
+                1,
+                1,
+                ',2'
+            ],
+            [
+                1,
+                2,
+                ',2,3'
+            ],
+            [
+                1,
+                99,
+                ',2,3,4,5,6'
+            ],
+            [
+                2,
+                1,
+                ',3'
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     */
+    public function getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToZero(): void
+    {
+        $id = 1;
+        $depth = 3;
+
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv');
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth);
+
+        static::assertSame('1,2,3,4,5', $treeList);
+    }
+
+    /**
+     * @test
+     */
+    public function getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToOne(): void
+    {
+        $this->markTestSkipped('Test describes a bug in QueryGenerator::getTreeList, thus this test would fail.');
+
+        $id = 1;
+        $depth = 3;
+        $begin = 1;
+
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv');
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth, $begin);
+
+        static::assertSame('2,3,4,5', $treeList);
+        // --- Expected
+        // +++ Actual
+        // @@ @@
+        // -'2,3,4,5'
+        // +'2,34,5'
+    }
+
+    /**
+     * @test
+     */
+    public function getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToTwo(): void
+    {
+        $this->markTestSkipped('Test describes a bug in QueryGenerator::getTreeList, thus this test would fail.');
+
+        $id = 1;
+        $depth = 3;
+        $begin = 2;
+
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv');
+
+        $queryGenerator = new QueryGenerator();
+        $treeList = $queryGenerator->getTreeList($id, $depth, $begin);
+
+        static::assertSame('3,5', $treeList);
+        // --- Expected
+        // +++ Actual
+        // @@ @@
+        // -'3,5'
+        // +'35'
+    }
+}