FIX Automatically create default SiteTree records for new subsites

This commit is contained in:
Robbie Averill 2018-04-20 15:19:50 +12:00
parent e04d04dc70
commit b4943fb77c
3 changed files with 39 additions and 1 deletions

View File

@ -743,16 +743,35 @@ class Subsite extends DataObject
} }
/** /**
* Whenever a Subsite is written, rewrite the hostmap * Whenever a Subsite is written, rewrite the hostmap and create some default pages
* *
* @return void * @return void
*/ */
public function onAfterWrite() public function onAfterWrite()
{ {
Subsite::writeHostMap(); Subsite::writeHostMap();
if ($this->isChanged('ID')) {
$this->createDefaultPages();
}
parent::onAfterWrite(); parent::onAfterWrite();
} }
/**
* Automatically create default pages for new subsites
*/
protected function createDefaultPages()
{
$currentSubsite = Subsite::currentSubsiteID();
Subsite::changeSubsite($this->ID);
// Silence DB schema output
DB::quiet();
$siteTree = new SiteTree();
$siteTree->requireDefaultRecords();
Subsite::changeSubsite($currentSubsite);
}
/** /**
* Return the primary domain of this site. Tries to "normalize" the domain name, * Return the primary domain of this site. Tries to "normalize" the domain name,
* by replacing potential wildcards. * by replacing potential wildcards.

View File

@ -13,6 +13,14 @@ class SiteTreeSubsitesTest extends BaseSubsiteTest
'SiteTree' => array('Translatable') 'SiteTree' => array('Translatable')
); );
public function setUp()
{
// We have our own home page fixtures, prevent the default one being created in this test suite.
Config::inst()->update('SiteTree', 'create_default_pages', false);
parent::setUp();
}
public function testPagesInDifferentSubsitesCanShareURLSegment() public function testPagesInDifferentSubsitesCanShareURLSegment()
{ {
$subsiteMain = $this->objFromFixture('Subsite', 'main'); $subsiteMain = $this->objFromFixture('Subsite', 'main');

View File

@ -442,4 +442,15 @@ class SubsiteTest extends BaseSubsiteTest
$subsite2->activate(); $subsite2->activate();
$this->assertEquals('MyNewAwesomePage', DataObject::get_by_id('Page', $page2->ID)->Title); $this->assertEquals('MyNewAwesomePage', DataObject::get_by_id('Page', $page2->ID)->Title);
} }
public function testDefaultPageCreatedWhenCreatingSubsite()
{
$subsite = new Subsite();
$subsite->Title = 'New Subsite';
$subsite->write();
$subsite->activate();
$pages = SiteTree::get();
$this->assertGreaterThanOrEqual(1, $pages->count());
}
} }