Bläddra i källkod

add tests for SelectedSiteController

Olivier Massot 4 år sedan
förälder
incheckning
fe262caf05

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

@@ -28,14 +28,24 @@ class SelectedSiteController extends ActionController
      */
     protected $currentRootUid;
 
+    /**
+     * [FOR TESTS ONLY]
+     */
+    protected $preventPropagation = false;
+
+    /**
+     *
+     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
+     */
     protected function callActionMethod() {
 
         // Check if the current be-user has a db_mountpoint, and only has one.
         // If so, this will be considered as the selected site (since its the only one available)
-        $mp = $GLOBALS['BE_USER']->user['db_mountpoints'];
+        $mountpoints = $GLOBALS['BE_USER']->returnWebmounts();
+        $mountpoints = array_filter($mountpoints, function($m) { return $m != 0; });
 
-        if ($mp && count(explode(',', $mp)) === 1) {
-            $this->currentRootUid = (int)$mp;
+        if ($mountpoints && count($mountpoints) === 1) {
+            $this->currentRootUid = (int)$mountpoints[0];
         } else {
             // No db mountpoint has been set up, or more than one.
             // Now we check if a site's page is selected
@@ -52,6 +62,8 @@ class SelectedSiteController extends ActionController
             $this->forward('displayNoSelectedPageWarning', 'SelectedSite', 'OtCore');
         }
 
+        if ($this->preventPropagation) { return; }
+
         parent::callActionMethod();
     }
 

+ 64 - 0
ot_core/Tests/Unit/Controller/SelectedSiteControllerTest.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace Opentalent\OtCore\Tests\Unit\Controller;
+
+use Nimut\TestingFramework\TestCase\UnitTestCase;
+use Opentalent\OtCore\Controller\SelectedSiteController;
+
+class SelectedSiteControllerTest extends UnitTestCase
+{
+    public function injectBeUserWithMountpoints(array $mountpoints) {
+        $be_user = $this->prophesize(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class);
+        $be_user->returnWebmounts()->shouldBeCalled()->willReturn([1]);
+        $GLOBALS['BE_USER'] = $be_user->reveal();
+    }
+
+    private function callActionMethodProxy() {
+        $controller = new SelectedSiteController();
+
+        $reflection = new \ReflectionObject($controller);
+        $method = $reflection->getMethod('callActionMethod');
+        $method->setAccessible(true);
+
+        $argumentsProperty = $reflection->getProperty('preventPropagation');
+        $argumentsProperty->setAccessible(true);;
+        $argumentsProperty->setValue($controller, true);
+
+        $method->invokeArgs($controller, []);
+
+        $currentRootUidProperty = $reflection->getProperty('currentRootUid');
+        $currentRootUidProperty->setAccessible(true);
+
+        return $currentRootUidProperty->getValue($controller);
+    }
+
+    /**
+     * If the current Be-user has only one website mounted,
+     * then the currentRootUid should this website root uid
+     *
+     * @test
+     */
+    public function callActionMethodForSingleSite() {
+
+        $this->injectBeUserWithMountpoints([1]);
+
+        $currentRootUid = $this->callActionMethodProxy();
+
+        $this->assertEquals(1, $currentRootUid);
+    }
+
+    /**
+     * If the current Be-user has only one website mounted,
+     * then the currentRootUid should this website root uid
+     *
+     * @test
+     */
+    public function callActionMethodForMultiSite() {
+
+        $this->injectBeUserWithMountpoints([1, 2]);
+
+        $currentRootUid = $this->callActionMethodProxy();
+
+        $this->assertEquals(0, $currentRootUid);
+    }
+}