[!!!][TASK] Drop "documentation" extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Functional / Page / PageRepositoryTest.php
index 73d74dd..b24b527 100644 (file)
@@ -14,15 +14,18 @@ namespace TYPO3\CMS\Frontend\Tests\Functional\Page;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\Page\PageRepository;
-use TYPO3\CMS\Core\Tests\FunctionalTestCase;
+use TYPO3\CMS\Frontend\Page\PageRepositoryGetPageHookInterface;
 
 /**
  * Test case
  */
-class PageRepositoryTest extends FunctionalTestCase
+class PageRepositoryTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
 {
-    protected $coreExtensionsToLoad = array('frontend');
+    protected $coreExtensionsToLoad = ['frontend'];
 
     /**
      * @var \TYPO3\CMS\Frontend\Page\PageRepository
@@ -32,6 +35,7 @@ class PageRepositoryTest extends FunctionalTestCase
     protected function setUp()
     {
         parent::setUp();
+        $GLOBALS['TSFE']->gr_list = '';
         $this->importDataSet(__DIR__ . '/../Fixtures/pages.xml');
         $this->pageRepo = new PageRepository();
         $this->pageRepo->init(false);
@@ -64,9 +68,9 @@ class PageRepositoryTest extends FunctionalTestCase
     /**
      * @test
      */
-    public function getMenuMulipleUid()
+    public function getMenuMultipleUid()
     {
-        $rows = $this->pageRepo->getMenu(array(2, 3), 'uid, title');
+        $rows = $this->pageRepo->getMenu([2, 3], 'uid, title');
         $this->assertArrayHasKey(5, $rows);
         $this->assertArrayHasKey(6, $rows);
         $this->assertArrayHasKey(7, $rows);
@@ -82,7 +86,7 @@ class PageRepositoryTest extends FunctionalTestCase
     {
         $this->pageRepo->sys_language_uid = 1;
 
-        $rows = $this->pageRepo->getMenu(array(2, 3), 'uid, title');
+        $rows = $this->pageRepo->getMenu([2, 3], 'uid, title');
         $this->assertEquals('Attrappe 1-2-5', $rows[5]['title']);
         $this->assertEquals('Attrappe 1-2-6', $rows[6]['title']);
         $this->assertEquals('Dummy 1-2-7', $rows[7]['title']);
@@ -145,7 +149,7 @@ class PageRepositoryTest extends FunctionalTestCase
     public function getPagesOverlayByIdSingle()
     {
         $this->pageRepo->sys_language_uid = 1;
-        $rows = $this->pageRepo->getPagesOverlay(array(1));
+        $rows = $this->pageRepo->getPagesOverlay([1]);
         $this->assertInternalType('array', $rows);
         $this->assertCount(1, $rows);
         $this->assertArrayHasKey(0, $rows);
@@ -163,7 +167,7 @@ class PageRepositoryTest extends FunctionalTestCase
     public function getPagesOverlayByIdMultiple()
     {
         $this->pageRepo->sys_language_uid = 1;
-        $rows = $this->pageRepo->getPagesOverlay(array(1, 5));
+        $rows = $this->pageRepo->getPagesOverlay([1, 5]);
         $this->assertInternalType('array', $rows);
         $this->assertCount(2, $rows);
         $this->assertArrayHasKey(0, $rows);
@@ -188,7 +192,7 @@ class PageRepositoryTest extends FunctionalTestCase
     public function getPagesOverlayByIdMultipleSomeNotOverlaid()
     {
         $this->pageRepo->sys_language_uid = 1;
-        $rows = $this->pageRepo->getPagesOverlay(array(1, 4, 5, 8));
+        $rows = $this->pageRepo->getPagesOverlay([1, 4, 5, 8]);
         $this->assertInternalType('array', $rows);
         $this->assertCount(2, $rows);
         $this->assertArrayHasKey(0, $rows);
@@ -211,7 +215,7 @@ class PageRepositoryTest extends FunctionalTestCase
         $origRow = $this->pageRepo->getPage(1);
 
         $this->pageRepo->sys_language_uid = 1;
-        $rows = $this->pageRepo->getPagesOverlay(array($origRow));
+        $rows = $this->pageRepo->getPagesOverlay([$origRow]);
         $this->assertInternalType('array', $rows);
         $this->assertCount(1, $rows);
         $this->assertArrayHasKey(0, $rows);
@@ -232,7 +236,7 @@ class PageRepositoryTest extends FunctionalTestCase
         $orig2 = $this->pageRepo->getPage(5);
 
         $this->pageRepo->sys_language_uid = 1;
-        $rows = $this->pageRepo->getPagesOverlay(array(1 => $orig1, 5 => $orig2));
+        $rows = $this->pageRepo->getPagesOverlay([1 => $orig1, 5 => $orig2]);
         $this->assertInternalType('array', $rows);
         $this->assertCount(2, $rows);
         $this->assertArrayHasKey(1, $rows);
@@ -261,7 +265,7 @@ class PageRepositoryTest extends FunctionalTestCase
         $orig3 = $this->pageRepo->getPage(9);
 
         $this->pageRepo->sys_language_uid = 1;
-        $rows = $this->pageRepo->getPagesOverlay(array($orig1, $orig2, $orig3));
+        $rows = $this->pageRepo->getPagesOverlay([$orig1, $orig2, $orig3]);
         $this->assertInternalType('array', $rows);
         $this->assertCount(3, $rows);
         $this->assertArrayHasKey(0, $rows);
@@ -281,6 +285,235 @@ class PageRepositoryTest extends FunctionalTestCase
         $this->assertEquals('Attrappe 1-3-9', $row['title']);
     }
 
+    /**
+     * Tests whether the getPage Hook is called correctly.
+     *
+     * @test
+     */
+    public function isGetPageHookCalled()
+    {
+        // Create a hook mock object
+        $getPageHookProphet = $this->prophesize(\stdClass::class);
+        $getPageHookProphet->willImplement(PageRepositoryGetPageHookInterface::class);
+        $getPageHookProphet->getPage_preProcess(42, false, Argument::type(PageRepository::class))->shouldBeCalled();
+        $getPageHookMock = $getPageHookProphet->reveal();
+        $className = get_class($getPageHookMock);
+
+        // Register hook mock object
+        GeneralUtility::addInstance($className, $getPageHookMock);
+        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'][] = $className;
+        $this->pageRepo->getPage(42, false);
+    }
+
+    /**
+     * @test
+     */
+    public function initSetsPublicPropertyCorrectlyForWorkspacePreview()
+    {
+        $this->pageRepo->versioningWorkspaceId = 2;
+        $this->pageRepo->init(false);
+
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
+
+        $expectedSQL = sprintf(
+            ' AND (%s = 0) AND ((%s = 0) OR (%s = 2))',
+            $connection->quoteIdentifier('pages.deleted'),
+            $connection->quoteIdentifier('pages.t3ver_wsid'),
+            $connection->quoteIdentifier('pages.t3ver_wsid')
+        );
+
+        $this->assertSame($expectedSQL, $this->pageRepo->where_hid_del);
+    }
+
+    /**
+     * @test
+     */
+    public function initSetsPublicPropertyCorrectlyForLive()
+    {
+        $GLOBALS['SIM_ACCESS_TIME'] = 123;
+
+        $this->pageRepo->versioningWorkspaceId = 0;
+        $this->pageRepo->init(false);
+
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
+        $expectedSQL = sprintf(
+            ' AND (%s = 0) AND (%s <= 0) AND (%s = 0) AND (%s <= 123) AND ((%s = 0) OR (%s > 123))',
+            $connection->quoteIdentifier('pages.deleted'),
+            $connection->quoteIdentifier('pages.t3ver_state'),
+            $connection->quoteIdentifier('pages.hidden'),
+            $connection->quoteIdentifier('pages.starttime'),
+            $connection->quoteIdentifier('pages.endtime'),
+            $connection->quoteIdentifier('pages.endtime')
+        );
+
+        $this->assertSame($expectedSQL, $this->pageRepo->where_hid_del);
+    }
+
+    ////////////////////////////////
+    // Tests concerning workspaces
+    ////////////////////////////////
+
+    /**
+     * @test
+     */
+    public function previewShowsPagesFromLiveAndCurrentWorkspace()
+    {
+        // initialization
+        $wsid = 987654321;
+
+        // simulate calls from \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->fetch_the_id()
+        $this->pageRepo->versioningWorkspaceId = $wsid;
+        $this->pageRepo->init(false);
+
+        $pageRec = $this->pageRepo->getPage(11);
+
+        $this->assertEquals(11, $pageRec['uid']);
+        $this->assertEquals(11, $pageRec['t3ver_oid']);
+        $this->assertEquals(987654321, $pageRec['t3ver_wsid']);
+        $this->assertEquals(-1, $pageRec['t3ver_state']);
+        $this->assertSame('First draft version', $pageRec['t3ver_label']);
+    }
+
+    /**
+     * @test
+     */
+    public function getWorkspaceVersionReturnsTheCorrectMethod()
+    {
+        // initialization
+        $wsid = 987654321;
+
+        // simulate calls from \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->fetch_the_id()
+        $this->pageRepo->versioningWorkspaceId = $wsid;
+        $this->pageRepo->init(false);
+
+        $pageRec = $this->pageRepo->getWorkspaceVersionOfRecord($wsid, 'pages', 11);
+
+        $this->assertEquals(12, $pageRec['uid']);
+        $this->assertEquals(11, $pageRec['t3ver_oid']);
+        $this->assertEquals(987654321, $pageRec['t3ver_wsid']);
+        $this->assertEquals(-1, $pageRec['t3ver_state']);
+        $this->assertSame('First draft version', $pageRec['t3ver_label']);
+    }
+
+    ////////////////////////////////
+    // Tests concerning versioning
+    ////////////////////////////////
+
+    /**
+     * @test
+     */
+    public function enableFieldsHidesVersionedRecordsAndPlaceholders()
+    {
+        $table = $this->getUniqueId('aTable');
+        $GLOBALS['TCA'][$table] = [
+            'ctrl' => [
+                'versioningWS' => true
+            ]
+        ];
+
+        $this->pageRepo->versioningWorkspaceId = 0;
+        $this->pageRepo->init(false);
+
+        $conditions = $this->pageRepo->enableFields($table);
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
+
+        $this->assertThat(
+            $conditions,
+            $this->stringContains(' AND (' . $connection->quoteIdentifier($table . '.t3ver_state') . ' <= 0)'),
+            'Versioning placeholders'
+        );
+        $this->assertThat(
+            $conditions,
+            $this->stringContains(' AND (' . $connection->quoteIdentifier($table . '.pid') . ' <> -1)'),
+            'Records from page -1'
+        );
+    }
+
+    /**
+     * @test
+     */
+    public function enableFieldsDoesNotHidePlaceholdersInPreview()
+    {
+        $table = $this->getUniqueId('aTable');
+        $GLOBALS['TCA'][$table] = [
+            'ctrl' => [
+                'versioningWS' => true
+            ]
+        ];
+
+        $this->pageRepo->versioningWorkspaceId = 13;
+        $this->pageRepo->init(false);
+
+        $conditions = $this->pageRepo->enableFields($table);
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
+
+        $this->assertThat(
+            $conditions,
+            $this->logicalNot($this->stringContains(' AND (' . $connection->quoteIdentifier($table . '.t3ver_state') . ' <= 0)')),
+            'No versioning placeholders'
+        );
+        $this->assertThat(
+            $conditions,
+            $this->stringContains(' AND (' . $connection->quoteIdentifier($table . '.pid') . ' <> -1)'),
+            'Records from page -1'
+        );
+    }
+
+    /**
+     * @test
+     */
+    public function enableFieldsDoesFilterToCurrentAndLiveWorkspaceForRecordsInPreview()
+    {
+        $table = $this->getUniqueId('aTable');
+        $GLOBALS['TCA'][$table] = [
+            'ctrl' => [
+                'versioningWS' => true
+            ]
+        ];
+
+        $this->pageRepo->versioningWorkspaceId = 2;
+        $this->pageRepo->init(false);
+
+        $conditions = $this->pageRepo->enableFields($table);
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
+
+        $this->assertThat(
+            $conditions,
+            $this->stringContains(' AND ((' . $connection->quoteIdentifier($table . '.t3ver_wsid') . ' = 0) OR (' . $connection->quoteIdentifier($table . '.t3ver_wsid') . ' = 2))'),
+            'No versioning placeholders'
+        );
+    }
+
+    /**
+     * @test
+     */
+    public function enableFieldsDoesNotHideVersionedRecordsWhenCheckingVersionOverlays()
+    {
+        $table = $this->getUniqueId('aTable');
+        $GLOBALS['TCA'][$table] = [
+            'ctrl' => [
+                'versioningWS' => true
+            ]
+        ];
+
+        $this->pageRepo->versioningWorkspaceId = 23;
+        $this->pageRepo->init(false);
+
+        $conditions = $this->pageRepo->enableFields($table, -1, [], true);
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
+
+        $this->assertThat(
+            $conditions,
+            $this->logicalNot($this->stringContains(' AND (' . $connection->quoteIdentifier($table . '.t3ver_state') . ' <= 0)')),
+            'No versioning placeholders'
+        );
+        $this->assertThat(
+            $conditions,
+            $this->logicalNot($this->stringContains(' AND (' . $connection->quoteIdentifier($table . '.pid') . ' <> -1)')),
+            'No records from page -1'
+        );
+    }
+
     protected function assertOverlayRow($row)
     {
         $this->assertInternalType('array', $row);