2013-10-16 05:24:01 +02:00
|
|
|
<?php
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
namespace SilverStripe\Subsites\Tests;
|
|
|
|
|
2017-06-01 15:57:53 +02:00
|
|
|
use SilverStripe\Admin\LeftAndMain;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
|
2017-05-29 13:42:42 +02:00
|
|
|
use SilverStripe\CMS\Controllers\CMSMain;
|
|
|
|
use SilverStripe\CMS\Controllers\CMSPageEditController;
|
2017-06-01 15:57:53 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2018-08-24 06:58:34 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2017-06-01 15:57:53 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2018-08-24 06:58:34 +02:00
|
|
|
use SilverStripe\Subsites\Extensions\LeftAndMainSubsites;
|
2017-05-24 15:25:34 +02:00
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
2017-08-30 01:06:07 +02:00
|
|
|
use SilverStripe\Subsites\State\SubsiteState;
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
class LeftAndMainSubsitesTest extends FunctionalTest
|
|
|
|
{
|
2017-08-29 07:22:32 +02:00
|
|
|
protected static $fixture_file = 'SubsiteTest.yml';
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Avoid subsites filtering on fixture fetching.
|
2017-08-29 07:22:32 +02:00
|
|
|
* @param string $className
|
|
|
|
* @param string $identifier
|
2017-06-01 15:10:07 +02:00
|
|
|
* @return \SilverStripe\ORM\DataObject
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-08-29 07:22:32 +02:00
|
|
|
protected function objFromFixture($className, $identifier)
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
Subsite::disable_subsite_filter(true);
|
2017-08-30 01:06:07 +02:00
|
|
|
$obj = parent::objFromFixture($className, $identifier);
|
2017-05-24 15:26:28 +02:00
|
|
|
Subsite::disable_subsite_filter(false);
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:58:34 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider sectionSitesProvider
|
|
|
|
*
|
|
|
|
* @param string $identifier
|
|
|
|
* @param string $className
|
|
|
|
* @param array $expected
|
|
|
|
* @param string $message
|
|
|
|
* @param string $assertion
|
|
|
|
*/
|
|
|
|
public function testSectionSites($identifier, $className, $expected, $message, $assertion = 'assertListEquals')
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2018-08-24 06:58:34 +02:00
|
|
|
$member = $this->objFromFixture(Member::class, $identifier);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2018-08-24 06:58:34 +02:00
|
|
|
/** @var CMSMain|LeftAndMainSubsites $cmsmain */
|
|
|
|
$cmsMain = Injector::inst()->create($className);
|
|
|
|
$subsites = $cmsMain->sectionSites(true, 'Main site', $member);
|
|
|
|
$this->$assertion($expected, $subsites, $message);
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2018-08-24 06:58:34 +02:00
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function sectionSitesProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'subsite1member',
|
|
|
|
CMSMain::class,
|
|
|
|
[['Title' => 'Subsite1 Template']],
|
|
|
|
'Lists member-accessible sites for the accessible controller.',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'subsite1member',
|
|
|
|
AssetAdmin::class,
|
|
|
|
[[]],
|
|
|
|
'Does not list any sites for forbidden controller.',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'editor',
|
|
|
|
CMSMain::class,
|
|
|
|
[['Title' => 'Main site']],
|
|
|
|
'Includes the main site for members who can access all sites.',
|
|
|
|
'assertListContains',
|
|
|
|
],
|
|
|
|
];
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 06:58:34 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider accessChecksProvider
|
|
|
|
*
|
|
|
|
* @param string $identifier
|
|
|
|
*/
|
|
|
|
public function testAccessChecksDontChangeCurrentSubsite($identifier)
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-08-29 07:22:32 +02:00
|
|
|
$this->logInAs('admin');
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2018-08-24 06:58:34 +02:00
|
|
|
/** @var Subsite $subsite */
|
|
|
|
$subsite = $this->objFromFixture(Subsite::class, $identifier);
|
|
|
|
$id = $subsite->ID;
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
// Enable session-based subsite tracking.
|
2017-08-30 05:29:13 +02:00
|
|
|
SubsiteState::singleton()->setUseSessions(true);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2018-08-24 06:58:34 +02:00
|
|
|
Subsite::changeSubsite($id);
|
|
|
|
$this->assertEquals($id, SubsiteState::singleton()->getSubsiteId(), 'Subsite ID is in the state');
|
|
|
|
|
|
|
|
$left = new LeftAndMain();
|
|
|
|
$this->assertTrue($left->canView(), "Admin user can view subsites LeftAndMain with id = '$id'");
|
|
|
|
$this->assertEquals(
|
|
|
|
$id,
|
|
|
|
SubsiteState::singleton()->getSubsiteId(),
|
|
|
|
'The current subsite has not been changed in the process of checking permissions for admin user.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function accessChecksProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['domaintest1'],
|
|
|
|
['domaintest3'],
|
|
|
|
['domaintest3'],
|
|
|
|
];
|
2017-06-01 15:57:53 +02:00
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
public function testShouldChangeSubsite()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
$l = new LeftAndMain();
|
|
|
|
|
2017-05-30 15:14:28 +02:00
|
|
|
Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', false);
|
2017-05-29 13:42:42 +02:00
|
|
|
$this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 0, 5));
|
|
|
|
$this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 0, 0));
|
|
|
|
$this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 1, 5));
|
|
|
|
$this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 1, 1));
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-05-30 15:14:28 +02:00
|
|
|
Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', true);
|
2017-05-29 13:42:42 +02:00
|
|
|
$this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 0, 5));
|
|
|
|
$this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 0, 0));
|
|
|
|
$this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 1, 5));
|
|
|
|
$this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 1, 1));
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
2010-07-26 05:56:03 +02:00
|
|
|
}
|