2007-08-16 08:38:29 +02:00
< ? php
2013-01-03 14:46:56 +01:00
class SubsiteTest extends BaseSubsiteTest {
2011-09-09 11:59:46 +02:00
2007-08-16 08:38:29 +02:00
static $fixture_file = 'subsites/tests/SubsiteTest.yml' ;
2011-09-09 11:59:46 +02:00
function setUp () {
parent :: setUp ();
$this -> origStrictSubdomainMatching = Subsite :: $strict_subdomain_matching ;
Subsite :: $strict_subdomain_matching = false ;
}
function tearDown () {
parent :: tearDown ();
Subsite :: $strict_subdomain_matching = $this -> origStrictSubdomainMatching ;
}
2007-08-21 00:37:43 +02:00
2007-08-16 08:38:29 +02:00
/**
* Create a new subsite from the template and verify that all the template ' s pages are copied
*/
function testSubsiteCreation () {
2010-03-01 03:58:13 +01:00
Subsite :: $write_hostmap = false ;
2007-08-16 08:38:29 +02:00
// Create the instance
2012-08-07 17:37:44 +02:00
$template = $this -> objFromFixture ( 'Subsite' , 'main' );
2009-10-16 03:15:54 +02:00
2007-08-16 08:38:29 +02:00
// Test that changeSubsite is working
Subsite :: changeSubsite ( $template -> ID );
2012-08-07 17:37:44 +02:00
$tmplStaff = $this -> objFromFixture ( 'Page' , 'staff' );
$tmplHome = DataObject :: get_one ( 'Page' , " \" URLSegment \" = 'home' " );
2009-10-16 03:15:54 +02:00
2007-08-16 08:38:29 +02:00
// Publish all the pages in the template, testing that DataObject::get only returns pages from the chosen subsite
$pages = DataObject :: get ( " SiteTree " );
2012-07-11 15:32:10 +02:00
$totalPages = $pages -> Count ();
2007-08-16 08:38:29 +02:00
foreach ( $pages as $page ) {
$this -> assertEquals ( $template -> ID , $page -> SubsiteID );
$page -> publish ( 'Stage' , 'Live' );
}
2012-08-07 17:37:44 +02:00
2007-08-16 08:38:29 +02:00
// Create a new site
2012-08-07 17:37:44 +02:00
$subsite = $template -> duplicate ();
2009-10-16 03:15:54 +02:00
2007-08-16 08:38:29 +02:00
// Check title
2012-08-07 17:37:44 +02:00
$this -> assertEquals ( $subsite -> Title , $template -> Title );
2007-08-16 08:38:29 +02:00
// Another test that changeSubsite is working
2010-03-31 00:50:37 +02:00
$subsite -> activate ();
2009-10-16 03:15:54 +02:00
2012-08-07 17:37:44 +02:00
$siteHome = DataObject :: get_one ( 'Page' , " \" URLSegment \" = 'home' " );
2012-07-16 01:14:13 +02:00
$this -> assertNotEquals ( $siteHome , false , 'Home Page for subsite not found' );
2009-10-16 03:15:54 +02:00
$this -> assertEquals ( $subsite -> ID , $siteHome -> SubsiteID ,
'createInstance() copies existing pages retaining the same URLSegment'
);
$this -> assertEquals ( $siteHome -> MasterPageID , $tmplHome -> ID , 'Check master page value' );
2007-08-16 08:38:29 +02:00
// Check linking of child pages
2012-08-07 17:37:44 +02:00
$siteStaff = DataObject :: get_one ( 'Page' , " \" URLSegment \" = ' " . Convert :: raw2sql ( $tmplStaff -> URLSegment ) . " ' " );
2007-08-16 08:38:29 +02:00
$this -> assertEquals ( $siteStaff -> MasterPageID , $tmplStaff -> ID );
2008-11-24 07:37:22 +01:00
Subsite :: changeSubsite ( 0 );
2007-08-16 08:38:29 +02:00
}
2010-03-01 03:48:45 +01:00
/**
* Confirm that domain lookup is working
*/
function testDomainLookup () {
2011-09-09 11:59:46 +02:00
// Clear existing fixtures
foreach ( DataObject :: get ( 'Subsite' ) as $subsite ) $subsite -> delete ();
foreach ( DataObject :: get ( 'SubsiteDomain' ) as $domain ) $domain -> delete ();
// Much more expressive than YML in this case
$subsite1 = $this -> createSubsiteWithDomains ( array (
'one.example.org' => true ,
'one.*' => false ,
));
$subsite2 = $this -> createSubsiteWithDomains ( array (
'two.mysite.com' => true ,
'*.mysite.com' => false ,
'subdomain.onmultiplesubsites.com' => false ,
));
$subsite3 = $this -> createSubsiteWithDomains ( array (
'three.*' => true , // wildcards in primary domain are not recommended
'subdomain.unique.com' => false ,
'*.onmultiplesubsites.com' => false ,
));
$this -> assertEquals (
$subsite3 -> ID ,
Subsite :: getSubsiteIDForDomain ( 'subdomain.unique.com' ),
'Full unique match'
);
2011-09-09 10:33:43 +02:00
$this -> assertEquals (
2011-09-09 11:59:46 +02:00
$subsite1 -> ID ,
2011-09-09 10:50:52 +02:00
Subsite :: getSubsiteIDForDomain ( 'one.example.org' ),
2011-09-09 11:59:46 +02:00
'Full match, doesn\'t complain about multiple matches within a single subsite'
);
$failed = false ;
try {
Subsite :: getSubsiteIDForDomain ( 'subdomain.onmultiplesubsites.com' );
} catch ( UnexpectedValueException $e ) {
$failed = true ;
}
$this -> assertTrue (
$failed ,
'Fails on multiple matches with wildcard vs. www across multiple subsites'
2011-09-09 10:33:43 +02:00
);
$this -> assertEquals (
2011-09-09 11:59:46 +02:00
$subsite1 -> ID ,
Subsite :: getSubsiteIDForDomain ( 'one.unique.com' ),
'Fuzzy match suffixed with wildcard (rule "one.*")'
2011-09-09 10:33:43 +02:00
);
2011-09-09 10:50:52 +02:00
2011-09-09 10:33:43 +02:00
$this -> assertEquals (
2011-09-09 11:59:46 +02:00
$subsite2 -> ID ,
2011-09-09 10:50:52 +02:00
Subsite :: getSubsiteIDForDomain ( 'two.mysite.com' ),
'Matches correct subsite for rule'
2011-09-09 10:33:43 +02:00
);
$this -> assertEquals (
2011-09-09 11:59:46 +02:00
$subsite2 -> ID ,
2011-09-09 10:50:52 +02:00
Subsite :: getSubsiteIDForDomain ( 'other.mysite.com' ),
2011-09-09 11:59:46 +02:00
'Fuzzy match prefixed with wildcard (rule "*.mysite.com")'
2011-09-09 10:33:43 +02:00
);
2010-03-01 03:48:45 +01:00
2011-09-09 10:50:52 +02:00
$this -> assertEquals (
0 ,
Subsite :: getSubsiteIDForDomain ( 'unknown.madeup.com' ),
" Doesn't match unknown subsite "
);
2010-03-31 00:50:37 +02:00
2010-03-01 03:48:45 +01:00
}
2011-09-09 11:59:46 +02:00
function testStrictSubdomainMatching () {
// Clear existing fixtures
foreach ( DataObject :: get ( 'Subsite' ) as $subsite ) $subsite -> delete ();
foreach ( DataObject :: get ( 'SubsiteDomain' ) as $domain ) $domain -> delete ();
// Much more expressive than YML in this case
$subsite1 = $this -> createSubsiteWithDomains ( array (
'example.org' => true ,
'example.com' => false ,
'*.wildcard.com' => false ,
));
$subsite2 = $this -> createSubsiteWithDomains ( array (
'www.example.org' => true ,
'www.wildcard.com' => false ,
));
Subsite :: $strict_subdomain_matching = false ;
$this -> assertEquals (
$subsite1 -> ID ,
Subsite :: getSubsiteIDForDomain ( 'example.org' ),
'Exact matches without strict checking when not using www prefix'
);
$this -> assertEquals (
$subsite1 -> ID ,
Subsite :: getSubsiteIDForDomain ( 'www.example.org' ),
'Matches without strict checking when using www prefix, still matching first domain regardless of www prefix (falling back to subsite primary key ordering)'
);
$this -> assertEquals (
$subsite1 -> ID ,
Subsite :: getSubsiteIDForDomain ( 'www.example.com' ),
'Fuzzy matches without strict checking with www prefix'
);
$this -> assertEquals (
0 ,
Subsite :: getSubsiteIDForDomain ( 'www.wildcard.com' ),
'Doesn\'t match www prefix without strict check, even if a wildcard subdomain is in place'
);
Subsite :: $strict_subdomain_matching = true ;
$this -> assertEquals (
$subsite1 -> ID ,
Subsite :: getSubsiteIDForDomain ( 'example.org' ),
'Matches with strict checking when not using www prefix'
);
$this -> assertEquals (
$subsite2 -> ID , // not 1
Subsite :: getSubsiteIDForDomain ( 'www.example.org' ),
'Matches with strict checking when using www prefix'
);
$this -> assertEquals (
0 ,
Subsite :: getSubsiteIDForDomain ( 'www.example.com' ),
'Doesn\'t fuzzy match with strict checking when using www prefix'
);
$failed = false ;
try {
Subsite :: getSubsiteIDForDomain ( 'www.wildcard.com' );
} catch ( UnexpectedValueException $e ) {
$failed = true ;
}
$this -> assertTrue (
$failed ,
'Fails on multiple matches with strict checking and wildcard vs. www'
);
}
protected function createSubsiteWithDomains ( $domains ) {
$subsite = new Subsite ();
$subsite -> write ();
foreach ( $domains as $domainStr => $isPrimary ) {
$domain = new SubsiteDomain ( array (
'Domain' => $domainStr ,
'IsPrimary' => $isPrimary ,
'SubsiteID' => $subsite -> ID
));
$domain -> write ();
}
return $subsite ;
}
2010-03-01 03:48:45 +01:00
/**
* 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 ());
2010-03-31 00:50:37 +02:00
$this -> assertEquals ( $_SERVER [ 'HTTP_HOST' ], singleton ( 'Subsite' ) -> PrimaryDomain );
$this -> assertEquals ( 'http://' . $_SERVER [ 'HTTP_HOST' ] . Director :: baseURL (), singleton ( 'Subsite' ) -> absoluteBaseURL ());
2010-03-01 03:48:45 +01:00
$_SERVER [ 'HTTP_HOST' ] = $originalHTTPHost ;
}
2010-03-01 22:37:56 +01:00
/**
* 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 );
2011-10-05 04:02:57 +02:00
$this -> assertEquals ( 'Subsite1 Template' , $member1SiteTitles [ 0 ], 'Member can get to a subsite via a group' );
2010-03-01 22:37:56 +01:00
$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 );
2011-10-05 04:02:57 +02:00
$member2Sites = Subsite :: accessible_sites ( " CMS_ACCESS_CMSMain " , false , null ,
$this -> objFromFixture ( 'Member' , 'subsite1member2' ));
$member2SiteTitles = $member2Sites -> column ( " Title " );
sort ( $member2SiteTitles );
$this -> assertEquals ( 'Subsite1 Template' , $member2SiteTitles [ 0 ], 'Member can get to subsite via a group role' );
2010-03-01 22:37:56 +01:00
}
2011-09-05 17:24:48 +02:00
function testhasMainSitePermission () {
$admin = $this -> objFromFixture ( 'Member' , 'admin' );
$subsite1member = $this -> objFromFixture ( 'Member' , 'subsite1member' );
$subsite1admin = $this -> objFromFixture ( 'Member' , 'subsite1admin' );
$allsubsitesauthor = $this -> objFromFixture ( 'Member' , 'allsubsitesauthor' );
$this -> assertTrue (
Subsite :: hasMainSitePermission ( $admin ),
'Default permissions granted for super-admin'
);
$this -> assertTrue (
Subsite :: hasMainSitePermission ( $admin , array ( " ADMIN " )),
'ADMIN permissions granted for super-admin'
);
$this -> assertFalse (
Subsite :: hasMainSitePermission ( $subsite1admin , array ( " ADMIN " )),
'ADMIN permissions (on main site) denied for subsite1 admin'
);
$this -> assertFalse (
Subsite :: hasMainSitePermission ( $subsite1admin , array ( " CMS_ACCESS_CMSMain " )),
'CMS_ACCESS_CMSMain (on main site) denied for subsite1 admin'
);
$this -> assertFalse (
Subsite :: hasMainSitePermission ( $allsubsitesauthor , array ( " ADMIN " )),
'ADMIN permissions (on main site) denied for CMS author with edit rights on all subsites'
);
$this -> assertTrue (
Subsite :: hasMainSitePermission ( $allsubsitesauthor , array ( " CMS_ACCESS_CMSMain " )),
'CMS_ACCESS_CMSMain (on main site) granted for CMS author with edit rights on all subsites'
);
$this -> assertFalse (
Subsite :: hasMainSitePermission ( $subsite1member , array ( " ADMIN " )),
'ADMIN (on main site) denied for subsite1 subsite1 cms author'
);
$this -> assertFalse (
Subsite :: hasMainSitePermission ( $subsite1member , array ( " CMS_ACCESS_CMSMain " )),
'CMS_ACCESS_CMSMain (on main site) denied for subsite1 cms author'
);
}
2010-03-01 22:37:56 +01:00
2010-03-31 00:50:37 +02:00
function testDuplicateSubsite () {
// get subsite1 & create page
$subsite1 = $this -> objFromFixture ( 'Subsite' , 'domaintest1' );
$subsite1 -> activate ();
$page1 = new Page ();
$page1 -> Title = 'MyAwesomePage' ;
$page1 -> write ();
$page1 -> doPublish ();
$this -> assertEquals ( $page1 -> SubsiteID , $subsite1 -> ID );
// duplicate
$subsite2 = $subsite1 -> duplicate ();
$subsite2 -> activate ();
// change content on dupe
2010-04-30 00:58:10 +02:00
$page2 = DataObject :: get_one ( 'Page' , " \" Title \" = 'MyAwesomePage' " );
2010-03-31 00:50:37 +02:00
$page2 -> Title = 'MyNewAwesomePage' ;
$page2 -> write ();
$page2 -> doPublish ();
// check change & check change has not affected subiste1
$subsite1 -> activate ();
$this -> assertEquals ( 'MyAwesomePage' , DataObject :: get_by_id ( 'Page' , $page1 -> ID ) -> Title );
$subsite2 -> activate ();
$this -> assertEquals ( 'MyNewAwesomePage' , DataObject :: get_by_id ( 'Page' , $page2 -> ID ) -> Title );
}
2007-08-16 08:38:29 +02:00
}