Ver código fonte

cleaner writing for unit tests and new tests

Olivier Massot 4 anos atrás
pai
commit
bf734a344b
1 arquivos alterados com 186 adições e 67 exclusões
  1. 186 67
      ot_core/Tests/Unit/Page/OtPageRepositoryTest.php

+ 186 - 67
ot_core/Tests/Unit/Page/OtPageRepositoryTest.php

@@ -6,8 +6,7 @@ use Opentalent\OtCore\Page\OtPageRepository;
 use Nimut\TestingFramework\TestCase\UnitTestCase;
 use Opentalent\OtCore\Tests\Unit\Fixtures\PageFixtures;
 use Opentalent\OtCore\Tests\Unit\QueryBuilderProphet;
-use Prophecy\Argument;
-use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 
 class OtPageRepositoryTest extends UnitTestCase
@@ -19,27 +18,23 @@ class OtPageRepositoryTest extends UnitTestCase
     protected $pageFixtures;
     protected $otPageRepository;
 
-    /**
-     * @var array[] a typical rootline
-     */
-    protected $rootLine = [
-        0 => ['uid' => 1, 'pid' => 0, 'title' => 'Folder', 'hidden' => 0, 'doktype' => 254, 'is_siteroot' => 0, 'nav_hide' => 0],
-        1 => ['uid' => 2, 'pid' => 1, 'title' => 'Rootpage', 'hidden' => 0, 'doktype' => 1, 'is_siteroot' => 1, 'nav_hide' => 0],
-        2 => ['uid' => 3, 'pid' => 2, 'title' => 'Page', 'hidden' => 0, 'doktype' => 1, 'is_siteroot' => 0, 'nav_hide' => 0],
-        3 => ['uid' => 4, 'pid' => 3, 'title' => 'Subpage', 'hidden' => 0, 'doktype' => 1, 'is_siteroot' => 0, 'nav_hide' => 0],
-    ];
-
     public function setUp() {
+        // Prophecies to be injected in the under test OtPageRepository
         $this->pageService = $this->prophesize(\FluidTYPO3\Vhs\Service\PageService::class);
         $this->pageRepository = $this->prophesize(\TYPO3\CMS\Frontend\Page\PageRepository::class);
         $this->connectionPool = $this->prophesize(\TYPO3\CMS\Core\Database\ConnectionPool::class);
         $this->siteFinder = $this->prophesize(\TYPO3\CMS\Core\Site\SiteFinder::class);
 
+        // Fixtures
         $this->pageFixtures = new PageFixtures();
 
+        // The under test OtPageRepository
         $this->otPageRepository = new OtPageRepository();
     }
 
+    /**
+     * Inject the prophecies instantiated at setUp into the OtPageRepository under test object
+     */
     protected function injectProphecies() {
         $this->otPageRepository->injectPageService($this->pageService->reveal());
         $this->otPageRepository->injectPageRepository($this->pageRepository->reveal());
@@ -48,115 +43,199 @@ class OtPageRepositoryTest extends UnitTestCase
     }
 
     /**
-     * If the given page is a subpage of a root page, the the root page is returned
+     * Calling getPagesByPid() on a page should return its direct descendants
+     *
+     * @test
+     */
+    public function getPagesByPid() {
+
+        // Prepare prophecies
+        $page_uid = 210;
+        $subpages = $this->pageFixtures->getDirectSubpagesFor($page_uid);
+
+        $prophet = new QueryBuilderProphet($subpages);
+        $queryBuilder = $prophet->prophesize();
+        $this->connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
+        $this->injectProphecies();
+
+        // Test
+        $expected = $subpages;
+        $actual = $this->otPageRepository->getPagesByPid($page_uid);
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * Calling getRootPageFor() on a site's page should return the root page of the site
      *
      * @test
      */
     public function getRootPageForSiteSubpage() {
-        $start_page = $this->pageFixtures->getByUid(210);
-        $root_page = $this->pageFixtures->getByUid(200);
-        $root_line = $this->pageFixtures->getRootlineFor($start_page['uid']);
 
-        $this->pageService->getRootLine($start_page['uid'])->shouldBeCalled()->willReturn($root_line);
-        $this->pageRepository->getPage($root_page['uid'])->shouldBeCalled()->willReturn($root_page);
+        // Prepare prophecies
+        $page_uid = 210;
+
+        $root_uid = 200;
+        $root_page = $this->pageFixtures->getByUid($root_uid);
+
+        $root_line = $this->pageFixtures->getRootlineFor($page_uid);
+
+        $this->pageService->getRootLine($page_uid)->shouldBeCalled()->willReturn($root_line);
+        $this->pageRepository->getPage($root_uid)->shouldBeCalled()->willReturn($root_page);
         $this->injectProphecies();
 
-        $this->assertEquals(
-            $root_page,
-            $this->otPageRepository->getRootPageFor($start_page['uid'])
-        );
+        // Test
+        $expected = $root_page;
+        $actual = $this->otPageRepository->getRootPageFor($page_uid);
+
+        $this->assertEquals($expected, $actual);
     }
 
     /**
-     * If the given page is already a root page, it is returned as it is
+     * Calling getRootPageFor() on a site's root page should return this page
      *
      * @test
      */
     public function getRootPageForSiteRootpage() {
 
-        $start_and_root_page = $this->pageFixtures->getByUid(200);
-        $root_line = $this->pageFixtures->getRootlineFor($start_and_root_page['uid']);
+        // Prepare prophecies
+        $root_uid = 200;
+        $root_page = $this->pageFixtures->getByUid($root_uid);
+
+        $root_line = $this->pageFixtures->getRootlineFor($root_uid);
 
-        $this->pageService->getRootLine($start_and_root_page['uid'])->shouldBeCalled()->willReturn($root_line);
-        $this->pageRepository->getPage($start_and_root_page['uid'])->shouldBeCalled()->willReturn($start_and_root_page);
+        $this->pageService->getRootLine($root_uid)->shouldBeCalled()->willReturn($root_line);
+        $this->pageRepository->getPage($root_uid)->shouldBeCalled()->willReturn($root_page);
         $this->injectProphecies();
 
-        $this->assertEquals(
-            $start_and_root_page,
-            $this->otPageRepository->getRootPageFor($start_and_root_page['uid'])
-        );
+        // Test
+        $expected = $root_page;
+        $actual = $this->otPageRepository->getRootPageFor($root_uid);
+
+        $this->assertEquals($expected, $actual);
     }
 
     /**
-     * If the given page is not a subpage of a root page, an empty array is returned
+     * Calling getRootPageFor() on a page that is not a site's page should return an empty array
      *
      * @test
      */
     public function getRootPageForNonSitePage() {
-        $non_site_page_uid = 100;
-        $rootline = $this->pageFixtures->getRootlineFor($non_site_page_uid);
 
-        $this->pageService->getRootLine($non_site_page_uid)->shouldBeCalled()->willReturn($rootline);
+        // Prepare prophecies
+        $page_uid = 100;
+
+        $rootline = $this->pageFixtures->getRootlineFor($page_uid);
+
+        $this->pageService->getRootLine($page_uid)->shouldBeCalled()->willReturn($rootline);
         $this->injectProphecies();
 
-        $this->assertEquals(
-            [],
-            $this->otPageRepository->getRootPageFor($non_site_page_uid)
-        );
+        // Test
+        $expected = [];
+        $actual = $this->otPageRepository->getRootPageFor($page_uid);
+
+        $this->assertEquals($expected, $actual);
     }
 
     /**
+     * Calling getAllSubpagesForPage() on a site's page should return an array containing
+     * as many records that it has subpages
+     *
      * @test
      */
     public function getAllSubpagesForPageFromSubPage() {
 
-        // Start page is 230, subpages shall be 231, 232, 233
-        $sub_page_uid = 230;
+        // Prepare prophecies
+        $page_uid = 230;
 
-        $expected = [
+        $sub_pages = [
                 $this->pageFixtures->getByUid(231),
                 $this->pageFixtures->getByUid(232),
                 $this->pageFixtures->getByUid(233),
             ];
 
-        $prophet = new QueryBuilderProphet($expected, [], [], []);
+        $prophet = new QueryBuilderProphet($sub_pages, [], [], []);
+        $queryBuilder = $prophet->prophesize();
+        $this->connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
+        $this->injectProphecies();
+
+        // Test
+        $expected = $sub_pages;
+        $actual = $this->otPageRepository->getAllSubpagesForPage($page_uid);
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * Calling getAllSubpagesForPage() on a site's root page should return an array containing
+     * as many records that it has pages
+     *
+     * @test
+     */
+    public function getAllSubpagesForPageFromRootPage() {
+
+        // Prepare prophecies
+        $page_uid = 200;
+
+        $sub_pages = [];
+        foreach ($this->pageFixtures->getAll() as $page) {
+            if ($page['uid'] > 200) {
+                $sub_pages[] = $page;
+            }
+        }
+
+        $willReturn = [$this->pageFixtures->getDirectSubpagesFor($page_uid)];
+        foreach ($sub_pages as $page) {
+            $willReturn[] = $this->pageFixtures->getDirectSubpagesFor($page['uid']);
+        }
+
+        $prophet = new QueryBuilderProphet(...$willReturn);
         $queryBuilder = $prophet->prophesize();
         $this->connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
         $this->injectProphecies();
 
-        $this->assertEquals(
-            $expected,
-            $this->otPageRepository->getAllSubpagesForPage($sub_page_uid)
-        );
+        // Test
+        $expected = $sub_pages;
+        $actual = $this->otPageRepository->getAllSubpagesForPage($page_uid);
+
+        $this->assertEquals($expected, $actual);
     }
 
     /**
+     * Calling getAllSubpagesForPage() on a site's page that has got no child should
+     * return an empty array
+     *
      * @test
      */
     public function getAllSubpagesForPageFromEndpage() {
 
-        // Start page is 230, subpages shall be 231, 232, 233
-        $sub_page_uid = 211;
-        $expected = [];
+        // Prepare prophecies
+        $page_uid = 211;
 
         $prophet = new QueryBuilderProphet([]);
         $queryBuilder = $prophet->prophesize();
         $this->connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
         $this->injectProphecies();
 
-        $this->assertEquals(
-            $expected,
-            $this->otPageRepository->getAllSubpagesForPage($sub_page_uid)
-        );
+        // Test
+        $expected = [];
+        $actual = $this->otPageRepository->getAllSubpagesForPage($page_uid);
+
+        $this->assertEquals($expected, $actual);
     }
 
     /**
+     * Calling getSiteByRootPageId() on a site's root page should return the site object
+     * as the typo3 SiteFinder class would do
+     *
      * @test
      */
     public function getSiteForSiteRootPage() {
-        $root_uid = 200;
 
+        // Prepare prophecies
+        $root_uid = 200;
         $root_page = $this->pageFixtures->getByUid($root_uid);
+
         $root_line = $this->pageFixtures->getRootlineFor($root_uid);
 
         $this->pageService->getRootLine($root_uid)->shouldBeCalled()->willReturn($root_line);
@@ -167,33 +246,73 @@ class OtPageRepositoryTest extends UnitTestCase
 
         $this->injectProphecies();
 
-        $this->assertEquals(
-            $site->reveal(),
-            $this->otPageRepository->getSiteFor($root_uid)
-        );
+        // Test
+        $expected = $site->reveal();
+        $actual = $this->otPageRepository->getSiteFor($root_uid);
+
+        $this->assertEquals($expected, $actual);
     }
 
     /**
+     * Calling getSiteByRootPageId() on a site's page should return the site object
+     *
      * @test
      */
     public function getSiteForSiteSubPage() {
+
+        // Prepare prophecies
         $page_uid = 211;
 
-        $page = $this->pageFixtures->getByUid($page_uid);
-        $root_page = $this->pageFixtures->getByUid(200);
+        $root_uid = 200;
+        $root_page = $this->pageFixtures->getByUid($root_uid);
+
         $root_line = $this->pageFixtures->getRootlineFor($page_uid);
 
         $this->pageService->getRootLine($page_uid)->shouldBeCalled()->willReturn($root_line);
-        $this->pageRepository->getPage($page_uid)->shouldBeCalled()->willReturn($page);
+        $this->pageRepository->getPage($root_uid)->shouldBeCalled()->willReturn($root_page);
 
         $site = $this->prophesize(\TYPO3\CMS\Core\Site\Entity\Site::class);
-        $this->siteFinder->getSiteByRootPageId($root_page['uid'])->shouldBeCalled()->willReturn($site->reveal());
+        $this->siteFinder->getSiteByRootPageId($root_uid)->shouldBeCalled()->willReturn($site->reveal());
 
         $this->injectProphecies();
 
-        $this->assertEquals(
-            $site->reveal(),
-            $this->otPageRepository->getSiteFor($page_uid)
-        );
+        // Test
+        $expected = $site->reveal();
+        $actual = $this->otPageRepository->getSiteFor($page_uid);
+
+        $this->assertEquals($expected, $actual);
+    }
+
+
+
+    /**
+     * Calling getCurrentRootUid() when the current page is a site's page
+     * should return the root page uid of the site
+     *
+     * @test
+     */
+    public function getCurrentRootUidOnSitePage() {
+
+        // Prepare prophecies
+        $page_uid = 210;
+
+        $root_uid = 200;
+        $root_page = $this->pageFixtures->getByUid($root_uid);
+
+        $root_line = $this->pageFixtures->getRootlineFor($page_uid);
+
+        $this->pageService->getRootLine($page_uid)->shouldBeCalled()->willReturn($root_line);
+        $this->pageRepository->getPage($root_uid)->shouldBeCalled()->willReturn($root_page);
+        $this->injectProphecies();
+
+        $GLOBALS['_POST'] = ['id' => $page_uid];
+
+        // Test
+        $expected = $root_uid;
+        $actual = $this->otPageRepository->getCurrentRootUid();
+
+        $this->assertEquals($expected, $actual);
     }
+
+
 }