mirror of
https://github.com/silverstripe/silverstripe-subsites
synced 2024-10-22 11:05:55 +02:00
123 lines
4.0 KiB
PHP
123 lines
4.0 KiB
PHP
<?php
|
|
|
|
class SubsiteTest extends SapphireTest {
|
|
static $fixture_file = 'subsites/tests/SubsiteTest.yml';
|
|
|
|
/**
|
|
* Create a new subsite from the template and verify that all the template's pages are copied
|
|
*/
|
|
function testSubsiteCreation() {
|
|
Subsite::$write_hostmap = false;
|
|
|
|
// Create the instance
|
|
$template = $this->objFromFixture('Subsite_Template', 'main');
|
|
|
|
// Test that changeSubsite is working
|
|
Subsite::changeSubsite($template->ID);
|
|
|
|
$tmplHome = DataObject::get_one('SiteTree', "\"URLSegment\" = 'home'");
|
|
|
|
// Publish all the pages in the template, testing that DataObject::get only returns pages from the chosen subsite
|
|
$pages = DataObject::get("SiteTree");
|
|
$totalPages = $pages->TotalItems();
|
|
foreach($pages as $page) {
|
|
$this->assertEquals($template->ID, $page->SubsiteID);
|
|
$page->publish('Stage', 'Live');
|
|
}
|
|
|
|
// Create a new site
|
|
$subsite = $template->createInstance('My Site', 'something.test.com');
|
|
|
|
// Check title
|
|
$this->assertEquals($subsite->Title, 'My Site');
|
|
|
|
// Check that domain generation is working
|
|
$this->assertEquals('something.test.com', $subsite->domain());
|
|
|
|
// Another test that changeSubsite is working
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$siteHome = DataObject::get_one('SiteTree', "\"URLSegment\" = 'home'");
|
|
$this->assertNotNull($siteHome);
|
|
$this->assertEquals($subsite->ID, $siteHome->SubsiteID,
|
|
'createInstance() copies existing pages retaining the same URLSegment'
|
|
);
|
|
$this->assertEquals($siteHome->MasterPageID, $tmplHome->ID, 'Check master page value');
|
|
|
|
// Check linking of child pages
|
|
$tmplStaff = $this->objFromFixture('SiteTree','staff');
|
|
$siteStaff = DataObject::get_one('SiteTree', "\"URLSegment\" = '" . Convert::raw2sql($tmplStaff->URLSegment) . "'");
|
|
$this->assertEquals($siteStaff->MasterPageID, $tmplStaff->ID);
|
|
|
|
Subsite::changeSubsite(0);
|
|
|
|
}
|
|
|
|
/**
|
|
* Confirm that domain lookup is working
|
|
*/
|
|
function testDomainLookup() {
|
|
$this->assertEquals($this->idFromFixture('Subsite','domaintest1'),
|
|
Subsite::getSubsiteIDForDomain('one.example.org'));
|
|
$this->assertEquals($this->idFromFixture('Subsite','domaintest1'),
|
|
Subsite::getSubsiteIDForDomain('one.localhost'));
|
|
|
|
$this->assertEquals($this->idFromFixture('Subsite','domaintest2'),
|
|
Subsite::getSubsiteIDForDomain('two.mysite.com'));
|
|
$this->assertEquals($this->idFromFixture('Subsite','domaintest2'),
|
|
Subsite::getSubsiteIDForDomain('other.mysite.com'));
|
|
|
|
$this->assertEquals(0, Subsite::getSubsiteIDForDomain('other.example.com'));
|
|
$this->assertEquals(0, Subsite::getSubsiteIDForDomain('two.example.com'));
|
|
}
|
|
|
|
/**
|
|
* Test the Subsite->domain() method
|
|
*/
|
|
function testDefaultDomain() {
|
|
$this->assertEquals('one.example.org',
|
|
$this->objFromFixture('Subsite','domaintest1')->domain());
|
|
|
|
$this->assertEquals('two.mysite.com',
|
|
$this->objFromFixture('Subsite','domaintest2')->domain());
|
|
|
|
$originalHTTPHost = $_SERVER['HTTP_HOST'];
|
|
|
|
$_SERVER['HTTP_HOST'] = "www.example.org";
|
|
$this->assertEquals('three.example.org',
|
|
$this->objFromFixture('Subsite','domaintest3')->domain());
|
|
|
|
$_SERVER['HTTP_HOST'] = "mysite.example.org";
|
|
$this->assertEquals('three.mysite.example.org',
|
|
$this->objFromFixture('Subsite','domaintest3')->domain());
|
|
|
|
|
|
$_SERVER['HTTP_HOST'] = $originalHTTPHost;
|
|
|
|
}
|
|
|
|
/**
|
|
* Test Subsite::accessible_sites()
|
|
*/
|
|
function testAccessibleSites() {
|
|
$member1Sites = Subsite::accessible_sites("CMS_ACCESS_CMSMain", false, null,
|
|
$this->objFromFixture('Member', 'subsite1member'));
|
|
$member1SiteTitles = $member1Sites->column("Title");
|
|
sort($member1SiteTitles);
|
|
$this->assertEquals(array('Subsite1 Template'), $member1SiteTitles);
|
|
|
|
$adminSites = Subsite::accessible_sites("CMS_ACCESS_CMSMain", false, null,
|
|
$this->objFromFixture('Member', 'admin'));
|
|
$adminSiteTitles = $adminSites->column("Title");
|
|
sort($adminSiteTitles);
|
|
$this->assertEquals(array(
|
|
'Subsite1 Template',
|
|
'Subsite2 Template',
|
|
'Template',
|
|
'Test 1',
|
|
'Test 2',
|
|
'Test 3',
|
|
), $adminSiteTitles);
|
|
}
|
|
|
|
} |