From e129cafa947cdf6a05d24959b7d3cd756545c20e Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 30 Aug 2017 11:06:07 +1200 Subject: [PATCH] NEW Add SubsiteState and initialisation middleware, replace Subsite::currentSubsiteID use --- _config.php | 4 ++ _config/middleware.yml | 10 ++++ code/Middleware/InitStateMiddleware.php | 47 +++++++++++++++ code/State/SubsiteState.php | 60 +++++++++++++++++++ .../CMSPageAddControllerExtension.php | 8 +-- code/extensions/FileSubsites.php | 23 +++---- code/extensions/GroupSubsites.php | 13 ++-- code/extensions/LeftAndMainSubsites.php | 44 ++++++++------ code/extensions/SiteConfigSubsites.php | 12 ++-- code/extensions/SiteTreeSubsites.php | 23 ++++--- code/model/Subsite.php | 29 +++------ tests/php/LeftAndMainSubsitesTest.php | 10 ++-- tests/php/SiteTreeSubsitesTest.php | 14 ++--- tests/php/SubsiteAdminFunctionalTest.php | 28 ++++----- tests/php/SubsiteFunctionalTest.php | 6 +- tests/php/SubsiteTest.php | 4 +- 16 files changed, 232 insertions(+), 103 deletions(-) create mode 100644 _config/middleware.yml create mode 100644 code/Middleware/InitStateMiddleware.php create mode 100644 code/State/SubsiteState.php diff --git a/_config.php b/_config.php index b3d9bbc7..a3682d6a 100644 --- a/_config.php +++ b/_config.php @@ -1 +1,5 @@ registerService($state); + + $state->setSubsiteId($this->detectSubsiteId($request)); + + return $delegate($request); + } + + /** + * Use the given request to detect the current subsite ID + * + * @param HTTPRequest $request + * @return int + */ + protected function detectSubsiteId(HTTPRequest $request) + { + $id = null; + + if ($request->getVar('SubsiteID')) { + $id = (int) $request->getVar('SubsiteID'); + } + + if (Subsite::$use_session_subsiteid) { + $id = $request->getSession()->get('SubsiteID'); + } + + if ($id === null) { + $id = Subsite::getSubsiteIDForDomain(); + } + + return (int) $id; + } +} diff --git a/code/State/SubsiteState.php b/code/State/SubsiteState.php new file mode 100644 index 00000000..98be55e0 --- /dev/null +++ b/code/State/SubsiteState.php @@ -0,0 +1,60 @@ +subsiteId; + } + + /** + * Set the current subsite ID + * + * @param int $id + * @return $this + */ + public function setSubsiteId($id) + { + $this->subsiteId = (int) $id; + + return $this; + } + + /** + * Perform a given action within the context of a new, isolated state. Modifications are temporary + * and the existing state will be restored afterwards. + * + * @param callable $callback Callback to run. Will be passed the nested state as a parameter + * @return mixed Result of callback + */ + public function withState(callable $callback) + { + $newState = clone $this; + try { + Injector::inst()->registerService($newState); + return $callback($newState); + } finally { + Injector::inst()->registerService($this); + } + } +} diff --git a/code/extensions/CMSPageAddControllerExtension.php b/code/extensions/CMSPageAddControllerExtension.php index 175e0db3..f0ec36e2 100644 --- a/code/extensions/CMSPageAddControllerExtension.php +++ b/code/extensions/CMSPageAddControllerExtension.php @@ -3,14 +3,14 @@ namespace SilverStripe\Subsites\Extensions; use SilverStripe\Core\Extension; +use SilverStripe\Forms\FieldList; use SilverStripe\Forms\HiddenField; -use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\State\SubsiteState; class CMSPageAddControllerExtension extends Extension { - - public function updatePageOptions(&$fields) + public function updatePageOptions(FieldList $fields) { - $fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID())); + $fields->push(HiddenField::create('SubsiteID', 'SubsiteID', SubsiteState::singleton()->getSubsiteId())); } } diff --git a/code/extensions/FileSubsites.php b/code/extensions/FileSubsites.php index 7f736906..43a3a9b3 100644 --- a/code/extensions/FileSubsites.php +++ b/code/extensions/FileSubsites.php @@ -3,7 +3,6 @@ namespace SilverStripe\Subsites\Extensions; use SilverStripe\Assets\Folder; -use SilverStripe\Control\Session; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\LiteralField; @@ -12,6 +11,7 @@ use SilverStripe\ORM\Queries\SQLSelect; use SilverStripe\Security\Permission; use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\State\SubsiteState; /** * Extension for the File object to add subsites support @@ -96,7 +96,10 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) return; } - $subsiteID = (int)Subsite::currentSubsiteID(); + $subsiteID = SubsiteState::singleton()->getSubsiteId(); + if ($subsiteID === null) { + return; + } // The foreach is an ugly way of getting the first key :-) foreach ($query->getFrom() as $tableName => $info) { @@ -120,7 +123,7 @@ public function onBeforeWrite() if (self::$default_root_folders_global) { $this->owner->SubsiteID = 0; } else { - $this->owner->SubsiteID = Subsite::currentSubsiteID(); + $this->owner->SubsiteID = SubsiteState::singleton()->getSubsiteId(); } } } @@ -131,7 +134,7 @@ public function onAfterUpload() if ($this->owner->Parent()) { $this->owner->SubsiteID = $this->owner->Parent()->SubsiteID; } else { - $this->owner->SubsiteID = Subsite::currentSubsiteID(); + $this->owner->SubsiteID = SubsiteState::singleton()->getSubsiteId(); } $this->owner->write(); } @@ -139,16 +142,16 @@ public function onAfterUpload() public function canEdit($member = null) { // Check the CMS_ACCESS_SecurityAdmin privileges on the subsite that owns this group - $subsiteID = Session::get('SubsiteID'); + $subsiteID = SubsiteState::singleton()->getSubsiteId(); if ($subsiteID && $subsiteID == $this->owner->SubsiteID) { return true; } - Session::set('SubsiteID', $this->owner->SubsiteID); - $access = Permission::check(['CMS_ACCESS_AssetAdmin', 'CMS_ACCESS_LeftAndMain']); - Session::set('SubsiteID', $subsiteID); + return SubsiteState::singleton()->withState(function ($newState) { + $newState->setSubsiteId($this->owner->SubsiteID); - return $access; + return Permission::check(['CMS_ACCESS_AssetAdmin', 'CMS_ACCESS_LeftAndMain']); + }); } /** @@ -158,6 +161,6 @@ public function canEdit($member = null) */ public function cacheKeyComponent() { - return 'subsite-' . Subsite::currentSubsiteID(); + return 'subsite-' . SubsiteState::singleton()->getSubsiteId(); } } diff --git a/code/extensions/GroupSubsites.php b/code/extensions/GroupSubsites.php index d74a18d8..bd60f00a 100644 --- a/code/extensions/GroupSubsites.php +++ b/code/extensions/GroupSubsites.php @@ -15,6 +15,7 @@ use SilverStripe\Security\Group; use SilverStripe\Security\PermissionProvider; use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\State\SubsiteState; /** * Extension for the Group object to add subsites support @@ -154,10 +155,10 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) // If you're querying by ID, ignore the sub-site - this is a bit ugly... if (!$query->filtersOnID()) { - /*if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID; - - else */ - $subsiteID = (int)Subsite::currentSubsiteID(); + $subsiteID = SubsiteState::singleton()->getSubsiteId(); + if ($subsiteID === null) { + return; + } // Don't filter by Group_Subsites if we've already done that $hasGroupSubsites = false; @@ -198,7 +199,7 @@ public function onBeforeWrite() { // New record test approximated by checking whether the ID has changed. // Note also that the after write test is only used when we're *not* on a subsite - if ($this->owner->isChanged('ID') && !Subsite::currentSubsiteID()) { + if ($this->owner->isChanged('ID') && !SubsiteState::singleton()->getSubsiteId()) { $this->owner->AccessAllSubsites = 1; } } @@ -207,7 +208,7 @@ public function onAfterWrite() { // New record test approximated by checking whether the ID has changed. // Note also that the after write test is only used when we're on a subsite - if ($this->owner->isChanged('ID') && $currentSubsiteID = Subsite::currentSubsiteID()) { + if ($this->owner->isChanged('ID') && $currentSubsiteID = SubsiteState::singleton()->getSubsiteId()) { $subsites = $this->owner->Subsites(); $subsites->add($currentSubsiteID); } diff --git a/code/extensions/LeftAndMainSubsites.php b/code/extensions/LeftAndMainSubsites.php index 748290a0..49f53c90 100644 --- a/code/extensions/LeftAndMainSubsites.php +++ b/code/extensions/LeftAndMainSubsites.php @@ -3,12 +3,12 @@ namespace SilverStripe\Subsites\Extensions; use SilverStripe\Admin\CMSMenu; +use SilverStripe\Admin\LeftAndMainExtension; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Controller; -use SilverStripe\Control\Session; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Convert; -use SilverStripe\Core\Extension; +use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\Forms\HiddenField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; @@ -17,6 +17,7 @@ use SilverStripe\Security\Security; use SilverStripe\Subsites\Controller\SubsiteXHRController; use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\State\SubsiteState; use SilverStripe\View\ArrayData; use SilverStripe\View\Requirements; @@ -25,7 +26,7 @@ * * @package subsites */ -class LeftAndMainSubsites extends Extension +class LeftAndMainSubsites extends LeftAndMainExtension { private static $allowed_actions = ['CopyToSubsite']; @@ -38,9 +39,11 @@ class LeftAndMainSubsites extends Extension public function init() { - Requirements::css('subsites/css/LeftAndMain_Subsites.css'); - Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js'); - Requirements::javascript('subsites/javascript/VirtualPage_Subsites.js'); + $module = ModuleLoader::getModule('silverstripe/subsites'); + + Requirements::css($module->getRelativeResourcePath('css/LeftAndMain_Subsites.css')); + Requirements::javascript($module->getRelativeResourcePath('javascript/LeftAndMain_Subsites.js')); + Requirements::javascript($module->getRelativeResourcePath('javascript/VirtualPage_Subsites.js')); } /** @@ -54,7 +57,7 @@ public function getCMSTreeTitle() public function updatePageOptions(&$fields) { - $fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID())); + $fields->push(HiddenField::create('SubsiteID', 'SubsiteID', SubsiteState::singleton()->getSubsiteId())); } /** @@ -141,13 +144,14 @@ public function Subsites() public function ListSubsites() { $list = $this->Subsites(); - $currentSubsiteID = Subsite::currentSubsiteID(); + $currentSubsiteID = SubsiteState::singleton()->getSubsiteId(); if ($list == null || $list->count() == 1 && $list->first()->DefaultSite == true) { return false; } - Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js'); + $module = ModuleLoader::getModule('silverstripe/subsites'); + Requirements::javascript($module->getRelativeResourcePath('javascript/LeftAndMain_Subsites.js')); $output = new ArrayList(); @@ -176,7 +180,7 @@ public function alternateMenuDisplayCheck($controllerName) } // Check subsite support. - if (Subsite::currentSubsiteID() == 0) { + if (SubsiteState::singleton()->getSubsiteId() == 0) { // Main site always supports everything. return true; } @@ -194,9 +198,9 @@ public function CanAddSubsites() /** * Helper for testing if the subsite should be adjusted. - * @param $adminClass - * @param $recordSubsiteID - * @param $currentSubsiteID + * @param string $adminClass + * @param int $recordSubsiteID + * @param int $currentSubsiteID * @return bool */ public function shouldChangeSubsite($adminClass, $recordSubsiteID, $currentSubsiteID) @@ -227,7 +231,7 @@ public function canAccess() // Check if we have access to current section on the current subsite. $accessibleSites = $this->owner->sectionSites(true, 'Main site', $member); - return $accessibleSites->count() && $accessibleSites->find('ID', Subsite::currentSubsiteID()); + return $accessibleSites->count() && $accessibleSites->find('ID', SubsiteState::singleton()->getSubsiteId()); } /** @@ -283,7 +287,11 @@ public function onBeforeInit() if ($record && isset($record->SubsiteID, $this->owner->urlParams['ID']) && is_numeric($record->SubsiteID) - && $this->shouldChangeSubsite($this->owner->class, $record->SubsiteID, Subsite::currentSubsiteID()) + && $this->shouldChangeSubsite( + $this->owner->class, + $record->SubsiteID, + SubsiteState::singleton()->getSubsiteId() + ) ) { // Update current subsite in session Subsite::changeSubsite($record->SubsiteID); @@ -306,7 +314,9 @@ public function onBeforeInit() foreach ($menu as $candidate) { if ($candidate->controller && $candidate->controller != $this->owner->class) { $accessibleSites = singleton($candidate->controller)->sectionSites(true, 'Main site', $member); - if ($accessibleSites->count() && $accessibleSites->find('ID', Subsite::currentSubsiteID())) { + if ($accessibleSites->count() + && $accessibleSites->find('ID', SubsiteState::singleton()->getSubsiteId()) + ) { // Section is accessible, redirect there. return $this->owner->redirect(singleton($candidate->controller)->Link()); } @@ -334,7 +344,7 @@ public function onBeforeInit() public function augmentNewSiteTreeItem(&$item) { - $item->SubsiteID = isset($_POST['SubsiteID']) ? $_POST['SubsiteID'] : Subsite::currentSubsiteID(); + $item->SubsiteID = isset($_POST['SubsiteID']) ? $_POST['SubsiteID'] : SubsiteState::singleton()->getSubsiteId(); } public function onAfterSave($record) diff --git a/code/extensions/SiteConfigSubsites.php b/code/extensions/SiteConfigSubsites.php index f6e2080b..fca8d997 100644 --- a/code/extensions/SiteConfigSubsites.php +++ b/code/extensions/SiteConfigSubsites.php @@ -9,6 +9,7 @@ use SilverStripe\ORM\Queries\SQLSelect; use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\State\SubsiteState; /** * Extension for the SiteConfig object to add subsites support @@ -41,7 +42,10 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) } } - $subsiteID = (int)Subsite::currentSubsiteID(); + $subsiteID = SubsiteState::singleton()->getSubsiteId(); + if ($subsiteID === null) { + return; + } $froms = $query->getFrom(); $froms = array_keys($froms); @@ -55,7 +59,7 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) public function onBeforeWrite() { if ((!is_numeric($this->owner->ID) || !$this->owner->ID) && !$this->owner->SubsiteID) { - $this->owner->SubsiteID = Subsite::currentSubsiteID(); + $this->owner->SubsiteID = SubsiteState::singleton()->getSubsiteId(); } } @@ -64,11 +68,11 @@ public function onBeforeWrite() */ public function cacheKeyComponent() { - return 'subsite-' . Subsite::currentSubsiteID(); + return 'subsite-' . SubsiteState::singleton()->getSubsiteId(); } public function updateCMSFields(FieldList $fields) { - $fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID())); + $fields->push(HiddenField::create('SubsiteID', 'SubsiteID', SubsiteState::singleton()->getSubsiteId())); } } diff --git a/code/extensions/SiteTreeSubsites.php b/code/extensions/SiteTreeSubsites.php index 7631007f..72d31060 100644 --- a/code/extensions/SiteTreeSubsites.php +++ b/code/extensions/SiteTreeSubsites.php @@ -2,6 +2,7 @@ namespace SilverStripe\Subsites\Extensions; +use Page; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; @@ -20,6 +21,7 @@ use SilverStripe\Security\Member; use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\State\SubsiteState; use SilverStripe\View\SSViewer; /** @@ -64,12 +66,15 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) return; } + $subsiteID = null; if (Subsite::$force_subsite) { $subsiteID = Subsite::$force_subsite; } else { - /*if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID; - else */ - $subsiteID = (int)Subsite::currentSubsiteID(); + $subsiteID = SubsiteState::singleton()->getSubsiteId(); + } + + if ($subsiteID === null) { + return; } // The foreach is an ugly way of getting the first key :-) @@ -87,7 +92,7 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) public function onBeforeWrite() { if (!$this->owner->ID && !$this->owner->SubsiteID) { - $this->owner->SubsiteID = Subsite::currentSubsiteID(); + $this->owner->SubsiteID = SubsiteState::singleton()->getSubsiteId(); } parent::onBeforeWrite(); @@ -128,7 +133,7 @@ public function updateCMSFields(FieldList $fields) )->setHeadingLevel(4) ); - + // @todo check if this needs re-implementation // $copyAction->includeDefaultJS(false); } @@ -163,7 +168,7 @@ public function duplicateToSubsitePrep($subsiteID) $subsiteID = $subsiteID->ID; } - $oldSubsite = Subsite::currentSubsiteID(); + $oldSubsite = SubsiteState::singleton()->getSubsiteId(); if ($subsiteID) { Subsite::changeSubsite($subsiteID); } else { @@ -276,7 +281,7 @@ public function canEdit($member = null) // // We do the second best: fetch the likely SubsiteID from the session. The drawback is this might // make it possible to force relations to point to other (forbidden) subsites. - $subsiteID = Subsite::currentSubsiteID(); + $subsiteID = SubsiteState::singleton()->getSubsiteId(); } // Return true if they have access to this object's site @@ -427,7 +432,7 @@ public function augmentSyncLinkTracking() public function augmentValidURLSegment() { // If this page is being filtered in the current subsite, then no custom validation query is required. - $subsite = Subsite::$force_subsite ?: Subsite::currentSubsiteID(); + $subsite = Subsite::$force_subsite ?: SubsiteState::singleton()->getSubsiteId(); if (empty($this->owner->SubsiteID) || $subsite == $this->owner->SubsiteID) { return null; } @@ -450,7 +455,7 @@ public function augmentValidURLSegment() */ public function cacheKeyComponent() { - return 'subsite-' . Subsite::currentSubsiteID(); + return 'subsite-' . SubsiteState::singleton()->getSubsiteId(); } /** diff --git a/code/model/Subsite.php b/code/model/Subsite.php index e22233f0..841d1619 100644 --- a/code/model/Subsite.php +++ b/code/model/Subsite.php @@ -4,7 +4,6 @@ use SilverStripe\Admin\CMSMenu; use SilverStripe\CMS\Model\SiteTree; -use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Control\Session; use SilverStripe\Core\Convert; @@ -32,6 +31,7 @@ use SilverStripe\Security\Group; use SilverStripe\Security\Member; use SilverStripe\Security\Permission; +use SilverStripe\Subsites\State\SubsiteState; use SilverStripe\Versioned\Versioned; use UnexpectedValueException; @@ -132,7 +132,7 @@ public static function set_allowed_themes($themes) */ public static function currentSubsite() { - return Subsite::get()->byID(self::currentSubsiteID()); + return Subsite::get()->byID(SubsiteState::singleton()->getSubsiteId()); } /** @@ -143,25 +143,14 @@ public static function currentSubsite() * * You can simulate subsite access without creating virtual hosts by appending ?SubsiteID= to the request. * - * @todo Pass $request object from controller so we don't have to rely on $_GET - * * @return int ID of the current subsite instance + * + * @deprecated 2.0..3.0 Use SubsiteState::singleton()->getSubsiteId() instead */ public static function currentSubsiteID() { - $id = null; - - if (isset($_GET['SubsiteID'])) { - $id = (int)$_GET['SubsiteID']; - } elseif (Subsite::$use_session_subsiteid) { - $id = Controller::curr()->getRequest()->getSession()->get('SubsiteID'); - } - - if ($id === null) { - $id = self::getSubsiteIDForDomain(); - } - - return (int)$id; + Deprecation::notice('3.0', 'Use SubsiteState::singleton()->getSubsiteId() instead'); + return SubsiteState::singleton()->getSubsiteId(); } /** @@ -184,7 +173,7 @@ public static function changeSubsite($subsite) $subsiteID = $subsite; } - Controller::curr()->getRequest()->getSession()->set('SubsiteID', (int)$subsiteID); + SubsiteState::singleton()->setSubsiteId($subsiteID); // Set locale if (is_object($subsite) && $subsite->Language !== '') { @@ -381,7 +370,7 @@ public static function accessible_sites( $mainSiteTitle = 'Main site', $member = null ) { - + // Rationalise member arguments if (!$member) { $member = Member::currentUser(); @@ -940,7 +929,7 @@ public function duplicate($doWrite = true, $manyMany = 'many_many') { $duplicate = parent::duplicate($doWrite); - $oldSubsiteID = Session::get('SubsiteID'); + $oldSubsiteID = SubsiteState::singleton()->getSubsiteId(); self::changeSubsite($this->ID); /* diff --git a/tests/php/LeftAndMainSubsitesTest.php b/tests/php/LeftAndMainSubsitesTest.php index 001a6e53..de118a8b 100644 --- a/tests/php/LeftAndMainSubsitesTest.php +++ b/tests/php/LeftAndMainSubsitesTest.php @@ -10,6 +10,7 @@ use SilverStripe\Dev\FunctionalTest; use SilverStripe\Security\Member; use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\State\SubsiteState; class LeftAndMainSubsitesTest extends FunctionalTest { @@ -24,7 +25,7 @@ class LeftAndMainSubsitesTest extends FunctionalTest protected function objFromFixture($className, $identifier) { Subsite::disable_subsite_filter(true); - $obj = parent::objFromFixture($classname, $identifier); + $obj = parent::objFromFixture($className, $identifier); Subsite::disable_subsite_filter(false); return $obj; @@ -71,13 +72,13 @@ public function testAccessChecksDontChangeCurrentSubsite() foreach ($ids as $id) { Subsite::changeSubsite($id); - $this->assertEquals($id, Subsite::currentSubsiteID()); + $this->assertEquals($id, SubsiteState::singleton()->getSubsiteId()); $left = new LeftAndMain(); $this->assertTrue($left->canView(), "Admin user can view subsites LeftAndMain with id = '$id'"); $this->assertEquals( $id, - Subsite::currentSubsiteID(), + SubsiteState::singleton()->getSubsiteId(), 'The current subsite has not been changed in the process of checking permissions for admin user.' ); } @@ -86,7 +87,6 @@ public function testAccessChecksDontChangeCurrentSubsite() public function testShouldChangeSubsite() { $l = new LeftAndMain(); - Config::nest(); Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', false); $this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 0, 5)); @@ -99,7 +99,5 @@ public function testShouldChangeSubsite() $this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 0, 0)); $this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 1, 5)); $this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 1, 1)); - - Config::unnest(); } } diff --git a/tests/php/SiteTreeSubsitesTest.php b/tests/php/SiteTreeSubsitesTest.php index 4103f02a..5b794ddc 100644 --- a/tests/php/SiteTreeSubsitesTest.php +++ b/tests/php/SiteTreeSubsitesTest.php @@ -8,7 +8,6 @@ use SilverStripe\ErrorPage\ErrorPage; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Director; -use SilverStripe\Control\Session; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Convert; use SilverStripe\Forms\FieldList; @@ -107,7 +106,8 @@ public function testCanEditSiteTree() $subsite2 = $this->objFromFixture(Subsite::class, 'subsite2'); // Cant pass member as arguments to canEdit() because of GroupSubsites - Session::set('loggedInAs', $admin->ID); + $this->logInAs($admin); + $this->assertTrue( (bool)$subsite1page->canEdit(), 'Administrators can edit all subsites' @@ -116,13 +116,13 @@ public function testCanEditSiteTree() // @todo: Workaround because GroupSubsites->augmentSQL() is relying on session state Subsite::changeSubsite($subsite1); - Session::set('loggedInAs', $subsite1member->ID); + $this->logInAs($subsite1member->ID); $this->assertTrue( (bool)$subsite1page->canEdit(), 'Members can edit pages on a subsite if they are in a group belonging to this subsite' ); - Session::set('loggedInAs', $subsite2member->ID); + $this->logInAs($subsite2member->ID); $this->assertFalse( (bool)$subsite1page->canEdit(), 'Members cant edit pages on a subsite if they are not in a group belonging to this subsite' @@ -169,8 +169,7 @@ public function testTwoPagesWithSameURLOnDifferentSubsites() public function testPageTypesBlacklistInClassDropdown() { - $editor = $this->objFromFixture(Member::class, 'editor'); - Session::set('loggedInAs', $editor->ID); + $this->logInAs('editor'); $s1 = $this->objFromFixture(Subsite::class, 'domaintest1'); $s2 = $this->objFromFixture(Subsite::class, 'domaintest2'); @@ -241,8 +240,7 @@ public function testCopyToSubsite() public function testPageTypesBlacklistInCMSMain() { - $editor = $this->objFromFixture(Member::class, 'editor'); - Session::set('loggedInAs', $editor->ID); + $this->logInAs('editor'); $cmsmain = new CMSMain(); diff --git a/tests/php/SubsiteAdminFunctionalTest.php b/tests/php/SubsiteAdminFunctionalTest.php index 63819cef..4e5ecd79 100644 --- a/tests/php/SubsiteAdminFunctionalTest.php +++ b/tests/php/SubsiteAdminFunctionalTest.php @@ -3,12 +3,11 @@ namespace SilverStripe\Subsites\Tests; use SilverStripe\CMS\Controllers\CMSPageEditController; -use SilverStripe\Control\Session; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\FunctionalTest; -use SilverStripe\Security\Member; use SilverStripe\Subsites\Controller\SubsiteXHRController; use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\State\SubsiteState; class SubsiteAdminFunctionalTest extends FunctionalTest { @@ -61,12 +60,12 @@ public function testAdminCanAccessAllSubsites() $this->logInAs('admin'); $this->getAndFollowAll('admin/pages/?SubsiteID=0'); - $this->assertEquals(Subsite::currentSubsiteID(), '0', 'Can access main site.'); + $this->assertEquals(SubsiteState::singleton()->getSubsiteId(), '0', 'Can access main site.'); $this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}"); - $this->assertEquals(Subsite::currentSubsiteID(), $subsite1->ID, 'Can access other subsite.'); + $this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Can access other subsite.'); $this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $response = $this->getAndFollowAll(SubsiteXHRController::class); @@ -90,7 +89,7 @@ public function testAdminIsRedirectedToObjectsSubsite() Subsite::changeSubsite(0); $this->getAndFollowAll("admin/pages/edit/show/$subsite1Home->ID"); $this->assertEquals( - Subsite::currentSubsiteID(), + SubsiteState::singleton()->getSubsiteId(), $subsite1Home->SubsiteID, 'Loading an object switches the subsite' ); @@ -100,7 +99,7 @@ public function testAdminIsRedirectedToObjectsSubsite() Subsite::changeSubsite(0); $this->getAndFollowAll("admin/pages/edit/show/$subsite1Home->ID"); $this->assertEquals( - Subsite::currentSubsiteID(), + SubsiteState::singleton()->getSubsiteId(), $subsite1Home->SubsiteID, 'Loading a non-main-site object still switches the subsite if configured with treats_subsite_0_as_global' ); @@ -108,7 +107,7 @@ public function testAdminIsRedirectedToObjectsSubsite() $this->getAndFollowAll("admin/pages/edit/show/$mainSubsitePage->ID"); $this->assertNotEquals( - Subsite::currentSubsiteID(), + SubsiteState::singleton()->getSubsiteId(), $mainSubsitePage->SubsiteID, 'Loading a main-site object does not change the subsite if configured with treats_subsite_0_as_global' ); @@ -126,12 +125,12 @@ public function testEditorCanAccessAllSubsites() $this->logInAs('editor'); $this->getAndFollowAll('admin/pages/?SubsiteID=0'); - $this->assertEquals(Subsite::currentSubsiteID(), '0', 'Can access main site.'); + $this->assertEquals(SubsiteState::singleton()->getSubsiteId(), '0', 'Can access main site.'); $this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}"); - $this->assertEquals(Subsite::currentSubsiteID(), $subsite1->ID, 'Can access other subsite.'); + $this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Can access other subsite.'); $this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $response = $this->getAndFollowAll('SubsiteXHRController'); @@ -147,19 +146,18 @@ public function testEditorCanAccessAllSubsites() */ public function testSubsiteAdmin() { - $member = $this->objFromFixture(Member::class, 'subsite1member'); - Session::set('loggedInAs', $member->ID); + $this->logInAs('subsite1member'); $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); // Check allowed URL. $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}"); - $this->assertEquals(Subsite::currentSubsiteID(), $subsite1->ID, 'Can access own subsite.'); + $this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Can access own subsite.'); $this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Can access permitted section.'); // Check forbidden section in allowed subsite. $this->getAndFollowAll("admin/assets/?SubsiteID={$subsite1->ID}"); - $this->assertEquals(Subsite::currentSubsiteID(), $subsite1->ID, 'Is redirected within subsite.'); + $this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Is redirected within subsite.'); $this->assertNotRegExp( '#^admin/assets/.*#', $this->mainSession->lastUrl(), @@ -168,11 +166,11 @@ public function testSubsiteAdmin() // Check forbidden site, on a section that's allowed on another subsite $this->getAndFollowAll('admin/pages/?SubsiteID=0'); - $this->assertEquals(Subsite::currentSubsiteID(), $subsite1->ID, 'Is redirected to permitted subsite.'); + $this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Is redirected to permitted subsite.'); // Check forbidden site, on a section that's not allowed on any other subsite $this->getAndFollowAll('admin/assets/?SubsiteID=0'); - $this->assertEquals(Subsite::currentSubsiteID(), $subsite1->ID, 'Is redirected to first permitted subsite.'); + $this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Is redirected to first permitted subsite.'); $this->assertNotRegExp('#^Security/login.*#', $this->mainSession->lastUrl(), 'Is not denied access'); // Check the standalone XHR controller. diff --git a/tests/php/SubsiteFunctionalTest.php b/tests/php/SubsiteFunctionalTest.php index 6d25bcfe..6a61f633 100644 --- a/tests/php/SubsiteFunctionalTest.php +++ b/tests/php/SubsiteFunctionalTest.php @@ -1,6 +1,6 @@ objFromFixture(Page::class, 'contact'); $this->get($subsitePage->AbsoluteLink()); - $this->assertEquals($subsitePage->SubsiteID, Subsite::currentSubsiteID(), 'Subsite should be changed'); + $this->assertEquals($subsitePage->SubsiteID, SubsiteState::singleton()->getSubsiteId(), 'Subsite should be changed'); $this->assertEquals( SSViewer::get_themes(), $defaultThemes, @@ -32,7 +32,7 @@ public function testIfSubsiteThemeIsSetToThemeList() $pageWithTheme = $this->objFromFixture(Page::class, 'subsite1_contactus'); $this->get($pageWithTheme->AbsoluteLink()); $subsiteTheme = $pageWithTheme->Subsite()->Theme; - $this->assertEquals($pageWithTheme->SubsiteID, Subsite::currentSubsiteID(), 'Subsite should be changed'); + $this->assertEquals($pageWithTheme->SubsiteID, SubsiteState::singleton()->getSubsiteId(), 'Subsite should be changed'); $this->assertEquals( SSViewer::get_themes(), array_merge([$subsiteTheme], $defaultThemes), diff --git a/tests/php/SubsiteTest.php b/tests/php/SubsiteTest.php index 04fa4a8c..fe6aa74c 100644 --- a/tests/php/SubsiteTest.php +++ b/tests/php/SubsiteTest.php @@ -10,6 +10,8 @@ use SilverStripe\Security\Member; use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\SubsiteDomain; +use SilverStripe\Subsites\State\SubsiteState; +use UnexpectedValueException; class SubsiteTest extends BaseSubsiteTest { @@ -59,7 +61,7 @@ public function testSubsiteCreation() // Test that changeSubsite is working Subsite::changeSubsite($template->ID); - $this->assertEquals($template->ID, Subsite::currentSubsiteID()); + $this->assertEquals($template->ID, SubsiteState::singleton()->getSubsiteId()); $tmplStaff = $this->objFromFixture('Page', 'staff'); $tmplHome = DataObject::get_one('Page', "\"URLSegment\" = 'home'");