Bladeren bron

Complete tests for SelectedSiteController

Olivier Massot 4 jaren geleden
bovenliggende
commit
2840fa083f

+ 15 - 7
ot_core/Classes/Controller/SelectedSiteController.php

@@ -28,11 +28,15 @@ class SelectedSiteController extends ActionController
      */
     protected $currentRootUid;
 
-    /**
-     * [FOR TESTS ONLY]
-     */
+    protected $otPageRepository;
+
+    /** [FOR TESTS ONLY] */
     protected $preventPropagation = false;
 
+    public function injectOtPageRepository(OtPageRepository $otPageRepository) {
+        $this->otPageRepository = $otPageRepository;
+    }
+
     /**
      *
      * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
@@ -50,20 +54,24 @@ class SelectedSiteController extends ActionController
             // No db mountpoint has been set up, or more than one.
             // Now we check if a site's page is selected
             try {
-                $otPageRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtPageRepository::class);
-                $this->currentRootUid = $otPageRepository->getCurrentRootUid();
+                if ($this->otPageRepository == null) {
+                    $objectManager = new ObjectManager();
+                    $this->otPageRepository = $objectManager->get(OtPageRepository::class);
+                }
+                $this->currentRootUid = $this->otPageRepository->getCurrentRootUid();
             } catch (NoSiteSelected $e) {
                 $this->currentRootUid = null;
             }
         }
 
+        // [TESTS ONLY]
+        if ($this->preventPropagation) { return; }
+
         // No site is selected, redirect to the warning page
         if ($this->actionMethodName != 'displayNoSelectedPageWarningAction' && $this->currentRootUid == null) {
             $this->forward('displayNoSelectedPageWarning', 'SelectedSite', 'OtCore');
         }
 
-        if ($this->preventPropagation) { return; }
-
         parent::callActionMethod();
     }
 

+ 49 - 12
ot_core/Tests/Unit/Controller/SelectedSiteControllerTest.php

@@ -4,37 +4,55 @@ namespace Opentalent\OtCore\Tests\Unit\Controller;
 
 use Nimut\TestingFramework\TestCase\UnitTestCase;
 use Opentalent\OtCore\Controller\SelectedSiteController;
+use Opentalent\OtCore\Exception\NoSiteSelected;
+use Opentalent\OtCore\Page\OtPageRepository;
 
 class SelectedSiteControllerTest extends UnitTestCase
 {
-    public function injectBeUserWithMountpoints(array $mountpoints) {
+    private $controller;
+
+    public function setUp() {
+        $this->controller = new SelectedSiteController();
+    }
+
+    private function injectBeUserWithMountpoints(array $mountpoints) {
         $be_user = $this->prophesize(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class);
-        $be_user->returnWebmounts()->shouldBeCalled()->willReturn([1]);
+        $be_user->returnWebmounts()->shouldBeCalled()->willReturn($mountpoints);
         $GLOBALS['BE_USER'] = $be_user->reveal();
     }
 
-    private function callActionMethodProxy() {
-        $controller = new SelectedSiteController();
+    private function injectPageRepositoryWithSelectedUid(?int $selectedUid) {
+        $otPageRepository = $this->prophesize(OtPageRepository::class);
+        if ($selectedUid != null) {
+            $otPageRepository->getCurrentRootUid()->shouldBeCalled()->willReturn($selectedUid);
+        } else {
+            $otPageRepository->getCurrentRootUid()->shouldBeCalled()->willThrow(new NoSiteSelected());
+        }
+        $this->controller->injectOtPageRepository($otPageRepository->reveal());
+    }
 
-        $reflection = new \ReflectionObject($controller);
+    private function callActionMethodProxy() {
+        $reflection = new \ReflectionObject($this->controller);
         $method = $reflection->getMethod('callActionMethod');
         $method->setAccessible(true);
 
         $argumentsProperty = $reflection->getProperty('preventPropagation');
         $argumentsProperty->setAccessible(true);;
-        $argumentsProperty->setValue($controller, true);
+        $argumentsProperty->setValue($this->controller, true);
 
-        $method->invokeArgs($controller, []);
+        $method->invokeArgs($this->controller, []);
 
         $currentRootUidProperty = $reflection->getProperty('currentRootUid');
         $currentRootUidProperty->setAccessible(true);
 
-        return $currentRootUidProperty->getValue($controller);
+        return $currentRootUidProperty->getValue($this->controller);
     }
 
+
+
     /**
      * If the current Be-user has only one website mounted,
-     * then the currentRootUid should this website root uid
+     * then the currentRootUid should be this website root uid
      *
      * @test
      */
@@ -48,8 +66,8 @@ class SelectedSiteControllerTest extends UnitTestCase
     }
 
     /**
-     * If the current Be-user has only one website mounted,
-     * then the currentRootUid should this website root uid
+     * If the current Be-user has many websites mounted and a website page is selected,
+     * then the currentRootUid should be the currently selected website root page uid
      *
      * @test
      */
@@ -57,8 +75,27 @@ class SelectedSiteControllerTest extends UnitTestCase
 
         $this->injectBeUserWithMountpoints([1, 2]);
 
+        $this->injectPageRepositoryWithSelectedUid(2);
+
+        $currentRootUid = $this->callActionMethodProxy();
+
+        $this->assertEquals(2, $currentRootUid);
+    }
+
+    /**
+     * If the current Be-user has many websites mounted and no website page is selected,
+     * then the currentRootUid should be null
+     *
+     * @test
+     */
+    public function callActionMethodForNoSite() {
+
+        $this->injectBeUserWithMountpoints([1, 2]);
+
+        $this->injectPageRepositoryWithSelectedUid(null);
+
         $currentRootUid = $this->callActionMethodProxy();
 
-        $this->assertEquals(0, $currentRootUid);
+        $this->assertNull($currentRootUid);
     }
 }