silverstripe-subsites/tests/php/LeftAndMainSubsitesTest.php

102 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
2017-05-29 13:42:42 +02:00
use SilverStripe\Security\Member;
use SilverStripe\CMS\Controllers\CMSMain;
2016-09-22 16:38:29 +02:00
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Core\Config\Config;
2017-05-29 13:42:42 +02:00
use SilverStripe\CMS\Controllers\CMSPageEditController;
2016-09-22 16:38:29 +02:00
use SilverStripe\Dev\FunctionalTest;
2017-05-24 15:25:34 +02:00
use SilverStripe\Subsites\Model\Subsite;
2016-09-22 16:38:29 +02:00
2017-05-24 15:26:28 +02:00
class LeftAndMainSubsitesTest extends FunctionalTest
{
public static $fixture_file = 'subsites/tests/php/SubsiteTest.yml';
2017-05-24 15:26:28 +02:00
/**
* Avoid subsites filtering on fixture fetching.
*/
2017-05-29 13:42:42 +02:00
public function objFromFixture($class, $id)
2017-05-24 15:26:28 +02:00
{
Subsite::disable_subsite_filter(true);
$obj = parent::objFromFixture($class, $id);
Subsite::disable_subsite_filter(false);
return $obj;
}
2017-05-29 13:42:42 +02:00
public function testSectionSites()
2017-05-24 15:26:28 +02:00
{
2017-05-29 13:42:42 +02:00
$member = $this->objFromFixture(Member::class, 'subsite1member');
2017-05-24 15:26:28 +02:00
2017-05-29 13:42:42 +02:00
$cmsmain = singleton(CMSMain::class);
2017-05-24 15:26:28 +02:00
$subsites = $cmsmain->sectionSites(true, "Main site", $member);
2017-05-29 13:42:42 +02:00
$this->assertDOSEquals(array(
array('Title' =>'Subsite1 Template')
), $subsites, 'Lists member-accessible sites for the accessible controller.');
2017-05-24 15:26:28 +02:00
$assetadmin = singleton(AssetAdmin::class);
$subsites = $assetadmin->sectionSites(true, "Main site", $member);
$this->assertDOSEquals([], $subsites, 'Does not list any sites for forbidden controller.');
2017-05-29 13:42:42 +02:00
$member = $this->objFromFixture(Member::class, 'editor');
2017-05-24 15:26:28 +02:00
2017-05-29 13:42:42 +02:00
$cmsmain = singleton(CMSMain::class);
2017-05-24 15:26:28 +02:00
$subsites = $cmsmain->sectionSites(true, "Main site", $member);
2017-05-29 13:42:42 +02:00
$this->assertDOSContains(array(
array('Title' =>'Main site')
), $subsites, 'Includes the main site for members who can access all sites.');
2017-05-24 15:26:28 +02:00
}
2017-05-29 13:42:42 +02:00
public function testAccessChecksDontChangeCurrentSubsite()
2017-05-24 15:26:28 +02:00
{
2017-05-29 13:42:42 +02:00
$admin = $this->objFromFixture(Member::class, "admin");
2017-05-24 15:26:28 +02:00
$this->loginAs($admin);
2017-05-29 13:42:42 +02:00
$ids = array();
2017-05-24 15:26:28 +02:00
$subsite1 = $this->objFromFixture(Subsite::class, 'domaintest1');
$subsite2 = $this->objFromFixture(Subsite::class, 'domaintest2');
$subsite3 = $this->objFromFixture(Subsite::class, 'domaintest3');
$ids[] = $subsite1->ID;
$ids[] = $subsite2->ID;
$ids[] = $subsite3->ID;
$ids[] = 0;
// Enable session-based subsite tracking.
Subsite::$use_session_subsiteid = true;
foreach ($ids as $id) {
Subsite::changeSubsite($id);
$this->assertEquals($id, Subsite::currentSubsiteID());
2017-05-29 13:42:42 +02:00
$left = new LeftAndMain();
$this->assertTrue($left->canView(), "Admin user can view subsites LeftAndMain with id = '$id'");
$this->assertEquals($id, Subsite::currentSubsiteID(),
"The current subsite has not been changed in the process of checking permissions for admin user.");
}
2017-05-24 15:26:28 +02:00
2017-05-29 13:42:42 +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();
Config::nest();
2017-05-24 15:26: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
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
Config::unnest();
2017-05-24 15:26:28 +02:00
}
}