FIX Allow persisted subsite IDs to session from state, fix remaining unit tests

This commit is contained in:
Robbie Averill 2017-08-30 15:29:13 +12:00
parent c155855100
commit b0087b9035
12 changed files with 170 additions and 112 deletions

View File

@ -6,10 +6,6 @@ After:
SilverStripe\AssetAdmin\Controller\AssetAdmin: SilverStripe\AssetAdmin\Controller\AssetAdmin:
treats_subsite_0_as_global: true treats_subsite_0_as_global: true
Director:
rules:
SubsiteXHRController: SilverStripe\Subsites\Controller\SubsiteXHRController
SilverStripe\Reports\Report: SilverStripe\Reports\Report:
excluded_reports: excluded_reports:
- SilverStripe\Subsites\Reports\SubsiteReportWrapper - SilverStripe\Subsites\Reports\SubsiteReportWrapper

View File

@ -2,24 +2,61 @@
namespace SilverStripe\Subsites\Middleware; namespace SilverStripe\Subsites\Middleware;
use SilverStripe\Admin\AdminRootController;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\HTTPMiddleware; use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\State\SubsiteState; use SilverStripe\Subsites\State\SubsiteState;
class InitStateMiddleware implements HTTPMiddleware class InitStateMiddleware implements HTTPMiddleware
{ {
use Configurable;
/**
* URL paths that should be considered as admin only, i.e. not frontend
*
* @config
* @var array
*/
private static $admin_url_paths = [
'dev/',
'graphql/',
];
public function process(HTTPRequest $request, callable $delegate) public function process(HTTPRequest $request, callable $delegate)
{ {
$state = SubsiteState::create(); $state = SubsiteState::create();
Injector::inst()->registerService($state); Injector::inst()->registerService($state);
// If the request is from the CMS, we should enable session storage
$state->setUseSessions($this->getIsAdmin($request));
$state->setSubsiteId($this->detectSubsiteId($request)); $state->setSubsiteId($this->detectSubsiteId($request));
return $delegate($request); return $delegate($request);
} }
/**
* Determine whether the website is being viewed from an admin protected area or not
*
* @param HTTPRequest $request
* @return bool
*/
public function getIsAdmin(HTTPRequest $request)
{
$adminPaths = static::config()->get('admin_url_paths');
$adminPaths[] = AdminRootController::config()->get('url_base') . '/';
$currentPath = rtrim($request->getURL(), '/') . '/';
foreach ($adminPaths as $adminPath) {
if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) {
return true;
}
}
return false;
}
/** /**
* Use the given request to detect the current subsite ID * Use the given request to detect the current subsite ID
* *
@ -34,7 +71,7 @@ class InitStateMiddleware implements HTTPMiddleware
$id = (int) $request->getVar('SubsiteID'); $id = (int) $request->getVar('SubsiteID');
} }
if (Subsite::$use_session_subsiteid) { if (SubsiteState::singleton()->getUseSessions()) {
$id = $request->getSession()->get('SubsiteID'); $id = $request->getSession()->get('SubsiteID');
} }

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Subsites\State; namespace SilverStripe\Subsites\State;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
@ -17,6 +18,11 @@ class SubsiteState
*/ */
protected $subsiteId; protected $subsiteId;
/**
* @var bool
*/
protected $useSessions;
/** /**
* Get the current subsite ID * Get the current subsite ID
* *
@ -37,6 +43,36 @@ class SubsiteState
{ {
$this->subsiteId = (int) $id; $this->subsiteId = (int) $id;
// Persist to session, if they are enabled
if ($this->getUseSessions() && Injector::inst()->has(HTTPRequest::class)) {
Injector::inst()
->get(HTTPRequest::class)
->getSession()
->set('SubsiteID', $id);
}
return $this;
}
/**
* Get whether to use sessions for storing the subsite ID
*
* @return bool
*/
public function getUseSessions()
{
return $this->useSessions;
}
/**
* Set whether to use sessions for storing the subsite ID
*
* @param bool $useSessions
* @return $this
*/
public function setUseSessions($useSessions)
{
$this->useSessions = $useSessions;
return $this; return $this;
} }

View File

@ -11,12 +11,10 @@ use SilverStripe\Subsites\Model\Subsite;
*/ */
class SubsiteXHRController extends LeftAndMain class SubsiteXHRController extends LeftAndMain
{ {
/**
* @todo Temporary addition due to new requirements for LeftAndMain
* descendants in SS4. Consider alternate implementation.
*/
private static $url_segment = 'subsite_xhr'; private static $url_segment = 'subsite_xhr';
private static $ignore_menuitem = true;
/** /**
* Relax the access permissions, so anyone who has access to any CMS subsite can access this controller. * Relax the access permissions, so anyone who has access to any CMS subsite can access this controller.
* @param null $member * @param null $member
@ -24,7 +22,7 @@ class SubsiteXHRController extends LeftAndMain
*/ */
public function canView($member = null) public function canView($member = null)
{ {
if (parent::canView()) { if (parent::canView($member)) {
return true; return true;
} }
@ -50,11 +48,10 @@ class SubsiteXHRController extends LeftAndMain
public function getResponseNegotiator() public function getResponseNegotiator()
{ {
$negotiator = parent::getResponseNegotiator(); $negotiator = parent::getResponseNegotiator();
$self = $this;
// Register a new callback // Register a new callback
$negotiator->setCallback('SubsiteList', function () use (&$self) { $negotiator->setCallback('SubsiteList', function () {
return $self->SubsiteList(); return $this->SubsiteList();
}); });
return $negotiator; return $negotiator;

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Subsites\Extensions; namespace SilverStripe\Subsites\Extensions;
use SilverStripe\Admin\AdminRootController;
use SilverStripe\Admin\CMSMenu; use SilverStripe\Admin\CMSMenu;
use SilverStripe\Admin\LeftAndMainExtension; use SilverStripe\Admin\LeftAndMainExtension;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
@ -153,13 +154,13 @@ class LeftAndMainSubsites extends LeftAndMainExtension
$module = ModuleLoader::getModule('silverstripe/subsites'); $module = ModuleLoader::getModule('silverstripe/subsites');
Requirements::javascript($module->getRelativeResourcePath('javascript/LeftAndMain_Subsites.js')); Requirements::javascript($module->getRelativeResourcePath('javascript/LeftAndMain_Subsites.js'));
$output = new ArrayList(); $output = ArrayList::create();
foreach ($list as $subsite) { foreach ($list as $subsite) {
$CurrentState = $subsite->ID == $currentSubsiteID ? 'selected' : ''; $currentState = $subsite->ID == $currentSubsiteID ? 'selected' : '';
$output->push(new ArrayData([ $output->push(ArrayData::create([
'CurrentState' => $CurrentState, 'CurrentState' => $currentState,
'ID' => $subsite->ID, 'ID' => $subsite->ID,
'Title' => Convert::raw2xml($subsite->Title) 'Title' => Convert::raw2xml($subsite->Title)
])); ]));
@ -175,7 +176,7 @@ class LeftAndMainSubsites extends LeftAndMainExtension
} }
// Don't display SubsiteXHRController // Don't display SubsiteXHRController
if ($controllerName == SubsiteXHRController::class) { if (singleton($controllerName) instanceof SubsiteXHRController) {
return false; return false;
} }
@ -255,31 +256,28 @@ class LeftAndMainSubsites extends LeftAndMainExtension
*/ */
public function onBeforeInit() public function onBeforeInit()
{ {
// We are accessing the CMS, so we need to let Subsites know we will be using the session.
Subsite::$use_session_subsiteid = true;
$request = Controller::curr()->getRequest(); $request = Controller::curr()->getRequest();
$session = $request->getSession(); $session = $request->getSession();
// FIRST, check if we need to change subsites due to the URL. // FIRST, check if we need to change subsites due to the URL.
// Catch forced subsite changes that need to cause CMS reloads. // Catch forced subsite changes that need to cause CMS reloads.
if ($request->getVar('SubsiteID')) { if ($request->getVar('SubsiteID') !== null) {
// Clear current page when subsite changes (or is set for the first time) // Clear current page when subsite changes (or is set for the first time)
if (!$session->get('SubsiteID') || $request->getVar('SubsiteID') != $session->get('SubsiteID')) { if (!$session->get('SubsiteID') || $request->getVar('SubsiteID') != $session->get('SubsiteID')) {
$session->clear(sprintf('%s.currentPage', get_class($this->owner))); $session->clear(sprintf('%s.currentPage', get_class($this->owner)));
} }
// Update current subsite in session // Update current subsite in session
Subsite::changeSubsite($_GET['SubsiteID']); Subsite::changeSubsite($request->getVar('SubsiteID'));
//Redirect to clear the current page // Redirect to clear the current page
if ($this->owner->canView(Security::getCurrentUser())) { if ($this->owner->canView(Security::getCurrentUser())) {
//Redirect to clear the current page
return $this->owner->redirect($this->owner->Link()); return $this->owner->redirect($this->owner->Link());
} }
//Redirect to the default CMS section
return $this->owner->redirect('admin/'); // Redirect to the default CMS section
return $this->owner->redirect(AdminRootController::config()->get('url_base') . '/');
} }
// Automatically redirect the session to appropriate subsite when requesting a record. // Automatically redirect the session to appropriate subsite when requesting a record.

View File

@ -31,6 +31,7 @@ use SilverStripe\ORM\SS_List;
use SilverStripe\Security\Group; use SilverStripe\Security\Group;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\Subsites\State\SubsiteState; use SilverStripe\Subsites\State\SubsiteState;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use UnexpectedValueException; use UnexpectedValueException;
@ -46,15 +47,6 @@ class Subsite extends DataObject
private static $table_name = 'Subsite'; private static $table_name = 'Subsite';
/**
* @var $use_session_subsiteid Boolean Set to TRUE when using the CMS and FALSE
* when browsing the frontend of a website.
*
* @todo Remove flag once the Subsite CMS works without session state,
* similarly to the Translatable module.
*/
public static $use_session_subsiteid = false;
/** /**
* @var boolean $disable_subsite_filter If enabled, bypasses the query decoration * @var boolean $disable_subsite_filter If enabled, bypasses the query decoration
* to limit DataObject::get*() calls to a specific subsite. Useful for debugging. * to limit DataObject::get*() calls to a specific subsite. Useful for debugging.
@ -155,7 +147,7 @@ class Subsite extends DataObject
/** /**
* Switch to another subsite through storing the subsite identifier in the current PHP session. * Switch to another subsite through storing the subsite identifier in the current PHP session.
* Only takes effect when {@link Subsite::$use_session_subsiteid} is set to TRUE. * Only takes effect when {@link SubsiteState::singleton()->getUseSessions()} is set to TRUE.
* *
* @param int|Subsite $subsite Either the ID of the subsite, or the subsite object itself * @param int|Subsite $subsite Either the ID of the subsite, or the subsite object itself
*/ */
@ -163,7 +155,7 @@ class Subsite extends DataObject
{ {
// Session subsite change only meaningful if the session is active. // Session subsite change only meaningful if the session is active.
// Otherwise we risk setting it to wrong value, e.g. if we rely on currentSubsiteID. // Otherwise we risk setting it to wrong value, e.g. if we rely on currentSubsiteID.
if (!Subsite::$use_session_subsiteid) { if (!SubsiteState::singleton()->getUseSessions()) {
return; return;
} }
@ -216,6 +208,12 @@ class Subsite extends DataObject
} }
$SQL_host = Convert::raw2sql($host); $SQL_host = Convert::raw2sql($host);
if (!in_array('SubsiteDomain', DB::table_list())) {
// Table hasn't been created yet. Might be a dev/build, skip.
return 0;
}
$matchingDomains = DataObject::get( $matchingDomains = DataObject::get(
SubsiteDomain::class, SubsiteDomain::class,
"'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')", "'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')",

View File

@ -4,6 +4,7 @@ namespace SilverStripe\Subsites\Tests;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\State\SubsiteState;
class BaseSubsiteTest extends SapphireTest class BaseSubsiteTest extends SapphireTest
{ {
@ -11,7 +12,7 @@ class BaseSubsiteTest extends SapphireTest
{ {
parent::setUp(); parent::setUp();
Subsite::$use_session_subsiteid = true; SubsiteState::singleton()->setUseSessions(true);
Subsite::$force_subsite = null; Subsite::$force_subsite = null;
} }

View File

@ -68,7 +68,7 @@ class LeftAndMainSubsitesTest extends FunctionalTest
$ids[] = 0; $ids[] = 0;
// Enable session-based subsite tracking. // Enable session-based subsite tracking.
Subsite::$use_session_subsiteid = true; SubsiteState::singleton()->setUseSessions(true);
foreach ($ids as $id) { foreach ($ids as $id) {
Subsite::changeSubsite($id); Subsite::changeSubsite($id);

View File

@ -2,10 +2,10 @@
namespace SilverStripe\Subsites\Tests; namespace SilverStripe\Subsites\Tests;
use Page;
use SilverStripe\CMS\Controllers\CMSPageEditController; use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Subsites\Controller\SubsiteXHRController;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\State\SubsiteState; use SilverStripe\Subsites\State\SubsiteState;
@ -37,19 +37,17 @@ class SubsiteAdminFunctionalTest extends FunctionalTest
*/ */
public function testAnonymousIsForbiddenAdminAccess() public function testAnonymousIsForbiddenAdminAccess()
{ {
$this->logOut();
$response = $this->getAndFollowAll('admin/pages/?SubsiteID=0'); $response = $this->getAndFollowAll('admin/pages/?SubsiteID=0');
$this->assertRegExp('#^Security/login.*#', $this->mainSession->lastUrl(), 'Admin is disallowed'); $this->assertContains('Security/login', $this->mainSession->lastUrl(), 'Admin is disallowed');
$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
$response = $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}"); $response = $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");
$this->assertRegExp('#^Security/login.*#', $this->mainSession->lastUrl(), 'Admin is disallowed'); $this->assertContains('Security/login', $this->mainSession->lastUrl(), 'Admin is disallowed');
$response = $this->getAndFollowAll('SubsiteXHRController'); $response = $this->getAndFollowAll('admin/subsite_xhr');
$this->assertRegExp( $this->assertContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is disallowed');
'#^Security/login.*#',
$this->mainSession->lastUrl(),
'SubsiteXHRController is disallowed'
);
} }
/** /**
@ -60,60 +58,57 @@ class SubsiteAdminFunctionalTest extends FunctionalTest
$this->logInAs('admin'); $this->logInAs('admin');
$this->getAndFollowAll('admin/pages/?SubsiteID=0'); $this->getAndFollowAll('admin/pages/?SubsiteID=0');
$this->assertEquals(SubsiteState::singleton()->getSubsiteId(), '0', 'Can access main site.'); $this->assertEquals(0, $this->session()->get('SubsiteID'), 'Can access main site.');
$this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
$this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}"); $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");
$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); // Check the session manually, since the state is unique to the request, not this test
$this->assertNotRegExp( $this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access other subsite.');
'#^Security/login.*#', $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$this->mainSession->lastUrl(),
'SubsiteXHRController is reachable' $response = $this->getAndFollowAll('admin/subsite_xhr');
); $this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
} }
public function testAdminIsRedirectedToObjectsSubsite() public function testAdminIsRedirectedToObjectsSubsite()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$mainSubsitePage = $this->objFromFixture('Page', 'mainSubsitePage'); $mainSubsitePage = $this->objFromFixture(Page::class, 'mainSubsitePage');
$subsite1Home = $this->objFromFixture('Page', 'subsite1_home'); $subsite1Home = $this->objFromFixture(Page::class, 'subsite1_home');
Config::nest();
Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', false); Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', false);
Subsite::changeSubsite(0); Subsite::changeSubsite(0);
$this->getAndFollowAll("admin/pages/edit/show/$subsite1Home->ID"); $this->getAndFollowAll("admin/pages/edit/show/$subsite1Home->ID");
$this->assertEquals( $this->assertEquals(
SubsiteState::singleton()->getSubsiteId(),
$subsite1Home->SubsiteID, $subsite1Home->SubsiteID,
$this->session()->get('SubsiteID'),
'Loading an object switches the subsite' 'Loading an object switches the subsite'
); );
$this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', true); Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', true);
Subsite::changeSubsite(0); Subsite::changeSubsite(0);
$this->getAndFollowAll("admin/pages/edit/show/$subsite1Home->ID"); $this->getAndFollowAll("admin/pages/edit/show/$subsite1Home->ID");
$this->assertEquals( $this->assertEquals(
SubsiteState::singleton()->getSubsiteId(),
$subsite1Home->SubsiteID, $subsite1Home->SubsiteID,
$this->session()->get('SubsiteID'),
'Loading a non-main-site object still switches the subsite if configured with treats_subsite_0_as_global' 'Loading a non-main-site object still switches the subsite if configured with treats_subsite_0_as_global'
); );
$this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$this->getAndFollowAll("admin/pages/edit/show/$mainSubsitePage->ID"); $this->getAndFollowAll("admin/pages/edit/show/$mainSubsitePage->ID");
$this->assertNotEquals( $this->assertNotEquals(
SubsiteState::singleton()->getSubsiteId(),
$mainSubsitePage->SubsiteID, $mainSubsitePage->SubsiteID,
$this->session()->get('SubsiteID'),
'Loading a main-site object does not change the subsite if configured with treats_subsite_0_as_global' 'Loading a main-site object does not change the subsite if configured with treats_subsite_0_as_global'
); );
$this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
Config::unnest();
} }
/** /**
@ -124,21 +119,17 @@ class SubsiteAdminFunctionalTest extends FunctionalTest
{ {
$this->logInAs('editor'); $this->logInAs('editor');
$this->getAndFollowAll('admin/pages/?SubsiteID=0'); $this->get('admin/pages/?SubsiteID=0');
$this->assertEquals(SubsiteState::singleton()->getSubsiteId(), '0', 'Can access main site.'); $this->assertEquals(0, $this->session()->get('SubsiteID'), 'Can access main site.');
$this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
$this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}"); $this->get("admin/pages/?SubsiteID={$subsite1->ID}");
$this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Can access other subsite.'); $this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access other subsite.');
$this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Lands on the correct section'); $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
$response = $this->getAndFollowAll('SubsiteXHRController'); $response = $this->get('admin/subsite_xhr');
$this->assertNotRegExp( $this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
'#^Security/login.*#',
$this->mainSession->lastUrl(),
'SubsiteXHRController is reachable'
);
} }
/** /**
@ -152,33 +143,37 @@ class SubsiteAdminFunctionalTest extends FunctionalTest
// Check allowed URL. // Check allowed URL.
$this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}"); $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");
$this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Can access own subsite.'); $this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access own subsite.');
$this->assertRegExp('#^admin/pages.*#', $this->mainSession->lastUrl(), 'Can access permitted section.'); $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Can access permitted section.');
// Check forbidden section in allowed subsite. // Check forbidden section in allowed subsite.
$this->getAndFollowAll("admin/assets/?SubsiteID={$subsite1->ID}"); $this->getAndFollowAll("admin/assets/?SubsiteID={$subsite1->ID}");
$this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Is redirected within subsite.'); $this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Is redirected within subsite.');
$this->assertNotRegExp( $this->assertNotContains(
'#^admin/assets/.*#', 'admin/assets',
$this->mainSession->lastUrl(), $this->mainSession->lastUrl(),
'Is redirected away from forbidden section' 'Is redirected away from forbidden section'
); );
// Check forbidden site, on a section that's allowed on another subsite // Check forbidden site, on a section that's allowed on another subsite
$this->getAndFollowAll('admin/pages/?SubsiteID=0'); $this->getAndFollowAll('admin/pages/?SubsiteID=0');
$this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Is redirected to permitted subsite.'); $this->assertEquals(
$this->session()->get('SubsiteID'),
$subsite1->ID,
'Is redirected to permitted subsite.'
);
// Check forbidden site, on a section that's not allowed on any other subsite // Check forbidden site, on a section that's not allowed on any other subsite
$this->getAndFollowAll('admin/assets/?SubsiteID=0'); $this->getAndFollowAll('admin/assets/?SubsiteID=0');
$this->assertEquals(SubsiteState::singleton()->getSubsiteId(), $subsite1->ID, 'Is redirected to first permitted subsite.'); $this->assertEquals(
$this->assertNotRegExp('#^Security/login.*#', $this->mainSession->lastUrl(), 'Is not denied access'); $this->session()->get('SubsiteID'),
$subsite1->ID,
'Is redirected to first permitted subsite.'
);
$this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'Is not denied access');
// Check the standalone XHR controller. // Check the standalone XHR controller.
$response = $this->getAndFollowAll(SubsiteXHRController::class); $response = $this->getAndFollowAll('admin/subsite_xhr');
$this->assertNotRegExp( $this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
'#^Security/login.*#',
$this->mainSession->lastUrl(),
'SubsiteXHRController is reachable'
);
} }
} }

View File

@ -315,8 +315,7 @@ class SubsiteTest extends BaseSubsiteTest
$domain5a = $this->objFromFixture(SubsiteDomain::class, 'dt5'); $domain5a = $this->objFromFixture(SubsiteDomain::class, 'dt5');
// Check protocol when current protocol is http:// // Check protocol when current protocol is http://
$_SERVER['HTTP_HOST'] = 'www.mysite.com'; Config::modify()->set(Director::class, 'alternate_base_url', 'http://www.mysite.com');
$_SERVER['HTTPS'] = '';
$this->assertEquals('http://two.mysite.com/', $subsite2->absoluteBaseURL()); $this->assertEquals('http://two.mysite.com/', $subsite2->absoluteBaseURL());
$this->assertEquals('http://two.mysite.com/', $domain2a->absoluteBaseURL()); $this->assertEquals('http://two.mysite.com/', $domain2a->absoluteBaseURL());
@ -328,8 +327,7 @@ class SubsiteTest extends BaseSubsiteTest
$this->assertEquals('http://www.tertiary.com/', $domain5a->absoluteBaseURL()); $this->assertEquals('http://www.tertiary.com/', $domain5a->absoluteBaseURL());
// Check protocol when current protocol is https:// // Check protocol when current protocol is https://
$_SERVER['HTTP_HOST'] = 'www.mysite.com'; Config::modify()->set(Director::class, 'alternate_base_url', 'https://www.mysite.com');
$_SERVER['HTTPS'] = 'ON';
$this->assertEquals('https://two.mysite.com/', $subsite2->absoluteBaseURL()); $this->assertEquals('https://two.mysite.com/', $subsite2->absoluteBaseURL());
$this->assertEquals('https://two.mysite.com/', $domain2a->absoluteBaseURL()); $this->assertEquals('https://two.mysite.com/', $domain2a->absoluteBaseURL());

View File

@ -14,7 +14,7 @@ class SubsiteXHRControllerTest extends FunctionalTest
// Test unauthenticated access // Test unauthenticated access
$this->logOut(); $this->logOut();
$result = $this->get('SubsiteXHRController', null, [ $result = $this->get('admin/subsite_xhr', null, [
'X-Pjax' => 'SubsiteList', 'X-Pjax' => 'SubsiteList',
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
]); ]);
@ -22,7 +22,7 @@ class SubsiteXHRControllerTest extends FunctionalTest
// Login with NO permissions // Login with NO permissions
$this->logInWithPermission('NOT_CMS_PERMISSION'); $this->logInWithPermission('NOT_CMS_PERMISSION');
$result = $this->get('SubsiteXHRController', null, [ $result = $this->get('admin/subsite_xhr', null, [
'X-Pjax' => 'SubsiteList', 'X-Pjax' => 'SubsiteList',
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
]); ]);
@ -30,12 +30,14 @@ class SubsiteXHRControllerTest extends FunctionalTest
// Test cms user // Test cms user
$this->logInWithPermission('CMS_ACCESS_CMSMain'); $this->logInWithPermission('CMS_ACCESS_CMSMain');
$result = $this->get('SubsiteXHRController', null, [ $result = $this->get('admin/subsite_xhr', null, [
'X-Pjax' => 'SubsiteList', 'X-Pjax' => 'SubsiteList',
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
]); ]);
$this->assertEquals(200, $result->getStatusCode()); $this->assertEquals(200, $result->getStatusCode());
$this->assertEquals('text/json', $result->getHeader('Content-Type')); $this->assertEquals('text/json', $result->getHeader('Content-Type'));
$body = $result->getBody(); $body = $result->getBody();
$this->assertContains('Main site', $body); $this->assertContains('Main site', $body);
$this->assertContains('Test 1', $body); $this->assertContains('Test 1', $body);

View File

@ -83,13 +83,13 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
// Publish the source page // Publish the source page
$page = $this->objFromFixture(SiteTree::class, 'page1'); $page = $this->objFromFixture(SiteTree::class, 'page1');
$this->assertTrue($page->doPublish()); $this->assertTrue($page->publishSingle());
// Create a virtual page from it, and publish that // Create a virtual page from it, and publish that
$svp = new SubsitesVirtualPage(); $svp = new SubsitesVirtualPage();
$svp->CopyContentFromID = $page->ID; $svp->CopyContentFromID = $page->ID;
$svp->write(); $svp->write();
$svp->doPublish(); $svp->publishSingle();
// Rename the file // Rename the file
$file = $this->objFromFixture(File::class, 'file1'); $file = $this->objFromFixture(File::class, 'file1');
@ -122,7 +122,7 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
$this->assertTrue($vp->IsAddedToStage); $this->assertTrue($vp->IsAddedToStage);
// VP is still orange after we publish // VP is still orange after we publish
$p->doPublish(); $p->publishSingle();
$this->fixVersionNumberCache($vp); $this->fixVersionNumberCache($vp);
$this->assertTrue($vp->IsAddedToStage); $this->assertTrue($vp->IsAddedToStage);
@ -135,12 +135,12 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
// Also remains orange after a republish // Also remains orange after a republish
$p->Content = 'new content'; $p->Content = 'new content';
$p->write(); $p->write();
$p->doPublish(); $p->publishSingle();
$this->fixVersionNumberCache($vp2); $this->fixVersionNumberCache($vp2);
$this->assertTrue($vp2->IsAddedToStage); $this->assertTrue($vp2->IsAddedToStage);
// VP is now published // VP is now published
$vp->doPublish(); $vp->publishSingle();
$this->fixVersionNumberCache($vp); $this->fixVersionNumberCache($vp);
$this->assertTrue($vp->ExistsOnLive); $this->assertTrue($vp->ExistsOnLive);
@ -155,7 +155,7 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
$this->assertTrue($vp->IsModifiedOnStage); $this->assertTrue($vp->IsModifiedOnStage);
// Publish, VP goes black // Publish, VP goes black
$p->doPublish(); $p->publishSingle();
$this->fixVersionNumberCache($vp); $this->fixVersionNumberCache($vp);
$this->assertTrue($vp->ExistsOnLive); $this->assertTrue($vp->ExistsOnLive);
$this->assertFalse($vp->IsModifiedOnStage); $this->assertFalse($vp->IsModifiedOnStage);
@ -272,8 +272,8 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
$subsite1Vp->SubsiteID = $subsite1->ID; $subsite1Vp->SubsiteID = $subsite1->ID;
$subsite1Vp->write(); $subsite1Vp->write();
$this->assertNotEquals( $this->assertNotEquals(
$subsite1Vp->URLSegment, (string) $subsite1Vp->URLSegment,
$subsite1Page->URLSegment, (string) $subsite1Page->URLSegment,
"Doesn't allow explicit URLSegment overrides when already existing in same subsite" "Doesn't allow explicit URLSegment overrides when already existing in same subsite"
); );