2010-03-01 03:56:19 +01:00
|
|
|
<?php
|
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
class SubsitesVirtualPageTest extends BaseSubsiteTest
|
|
|
|
{
|
|
|
|
public static $fixture_file = array(
|
|
|
|
'subsites/tests/SubsiteTest.yml',
|
|
|
|
'subsites/tests/SubsitesVirtualPageTest.yml',
|
|
|
|
);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2016-08-23 02:36:12 +02:00
|
|
|
protected $illegalExtensions = array(
|
|
|
|
'SiteTree' => array('Translatable')
|
|
|
|
);
|
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$fh = fopen(Director::baseFolder() . '/assets/testscript-test-file.pdf', "w");
|
|
|
|
fwrite($fh, str_repeat('x', 1000000));
|
|
|
|
fclose($fh);
|
|
|
|
}
|
2013-01-03 14:46:56 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
$testFiles = array(
|
|
|
|
'/assets/testscript-test-file.pdf',
|
|
|
|
'/assets/renamed-test-file.pdf',
|
|
|
|
'/assets/renamed-test-file-second-time.pdf',
|
|
|
|
);
|
|
|
|
foreach ($testFiles as $file) {
|
|
|
|
if (file_exists(Director::baseFolder().$file)) {
|
|
|
|
unlink(Director::baseFolder().$file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Attempt to bring main:linky to subsite2:linky
|
|
|
|
public function testVirtualPageFromAnotherSubsite()
|
|
|
|
{
|
|
|
|
Subsite::$write_hostmap = false;
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$subsite = $this->objFromFixture('Subsite', 'subsite2');
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
Subsite::$disable_subsite_filter = false;
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$linky = $this->objFromFixture('Page', 'linky');
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$svp = new SubsitesVirtualPage();
|
|
|
|
$svp->CopyContentFromID = $linky->ID;
|
|
|
|
$svp->SubsiteID = $subsite->ID;
|
|
|
|
$svp->URLSegment = 'linky';
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$svp->write();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$this->assertEquals($svp->SubsiteID, $subsite->ID);
|
|
|
|
$this->assertEquals($svp->Title, $linky->Title);
|
|
|
|
}
|
2010-03-01 23:27:46 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function testFileLinkRewritingOnVirtualPages()
|
|
|
|
{
|
|
|
|
// File setup
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
touch(Director::baseFolder() . '/assets/testscript-test-file.pdf');
|
2010-03-01 23:27:46 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Publish the source page
|
|
|
|
$page = $this->objFromFixture('SiteTree', 'page1');
|
|
|
|
$this->assertTrue($page->doPublish());
|
2010-03-01 23:27:46 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Create a virtual page from it, and publish that
|
|
|
|
$svp = new SubsitesVirtualPage();
|
|
|
|
$svp->CopyContentFromID = $page->ID;
|
|
|
|
$svp->write();
|
|
|
|
$svp->doPublish();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Rename the file
|
|
|
|
$file = $this->objFromFixture('File', 'file1');
|
|
|
|
$file->Name = 'renamed-test-file.pdf';
|
|
|
|
$file->write();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Verify that the draft and publish virtual pages both have the corrected link
|
|
|
|
$this->assertContains('<img src="assets/renamed-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value());
|
|
|
|
$this->assertContains('<img src="assets/renamed-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $svp->ID")->value());
|
2010-03-01 23:27:46 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// File teardown
|
|
|
|
$testFiles = array(
|
|
|
|
'/assets/testscript-test-file.pdf',
|
|
|
|
'/assets/renamed-test-file.pdf',
|
|
|
|
);
|
|
|
|
foreach ($testFiles as $file) {
|
|
|
|
if (file_exists(Director::baseFolder().$file)) {
|
|
|
|
unlink(Director::baseFolder().$file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-01 23:27:46 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function testSubsiteVirtualPagesArentInappropriatelyPublished()
|
|
|
|
{
|
|
|
|
// Fixture
|
|
|
|
$p = new Page();
|
|
|
|
$p->Content = "test content";
|
|
|
|
$p->write();
|
|
|
|
$vp = new SubsitesVirtualPage();
|
|
|
|
$vp->CopyContentFromID = $p->ID;
|
|
|
|
$vp->write();
|
2010-03-01 23:32:24 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// VP is oragne
|
|
|
|
$this->assertTrue($vp->IsAddedToStage);
|
2010-03-01 23:32:24 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// VP is still orange after we publish
|
|
|
|
$p->doPublish();
|
|
|
|
$this->fixVersionNumberCache($vp);
|
|
|
|
$this->assertTrue($vp->IsAddedToStage);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// A new VP created after P's initial construction
|
|
|
|
$vp2 = new SubsitesVirtualPage();
|
|
|
|
$vp2->CopyContentFromID = $p->ID;
|
|
|
|
$vp2->write();
|
|
|
|
$this->assertTrue($vp2->IsAddedToStage);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Also remains orange after a republish
|
|
|
|
$p->Content = "new content";
|
|
|
|
$p->write();
|
|
|
|
$p->doPublish();
|
|
|
|
$this->fixVersionNumberCache($vp2);
|
|
|
|
$this->assertTrue($vp2->IsAddedToStage);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// VP is now published
|
|
|
|
$vp->doPublish();
|
2010-03-01 23:32:24 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$this->fixVersionNumberCache($vp);
|
|
|
|
$this->assertTrue($vp->ExistsOnLive);
|
|
|
|
$this->assertFalse($vp->IsModifiedOnStage);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// P edited, VP and P both go green
|
|
|
|
$p->Content = "third content";
|
|
|
|
$p->write();
|
2010-03-01 23:32:24 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$this->fixVersionNumberCache($vp, $p);
|
|
|
|
$this->assertTrue($p->IsModifiedOnStage);
|
|
|
|
$this->assertTrue($vp->IsModifiedOnStage);
|
2010-03-01 23:32:24 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Publish, VP goes black
|
|
|
|
$p->doPublish();
|
|
|
|
$this->fixVersionNumberCache($vp);
|
|
|
|
$this->assertTrue($vp->ExistsOnLive);
|
|
|
|
$this->assertFalse($vp->IsModifiedOnStage);
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* This test ensures published Subsites Virtual Pages immediately reflect updates
|
|
|
|
* to their published target pages. Note - this has to happen when the virtual page
|
|
|
|
* is in a different subsite to the page you are editing and republishing,
|
|
|
|
* otherwise the test will pass falsely due to current subsite ID being the same.
|
|
|
|
*/
|
|
|
|
public function testPublishedSubsiteVirtualPagesUpdateIfTargetPageUpdates()
|
|
|
|
{
|
|
|
|
// create page
|
|
|
|
$p = new Page();
|
|
|
|
$p->Content = 'Content';
|
|
|
|
$p->Title = 'Title';
|
|
|
|
$p->writeToStage('Stage');
|
|
|
|
$p->publish('Stage', 'Live');
|
|
|
|
$this->assertTrue($p->ExistsOnLive);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// change to subsite
|
|
|
|
$subsite = $this->objFromFixture('Subsite', 'subsite2');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
Subsite::$disable_subsite_filter = false;
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// create svp in subsite
|
|
|
|
$svp = new SubsitesVirtualPage();
|
|
|
|
$svp->CopyContentFromID = $p->ID;
|
|
|
|
$svp->write();
|
|
|
|
$svp->writeToStage('Stage');
|
|
|
|
$svp->publish('Stage', 'Live');
|
|
|
|
$this->assertEquals($svp->SubsiteID, $subsite->ID);
|
|
|
|
$this->assertTrue($svp->ExistsOnLive);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// change back to original subsite ("Main site")
|
|
|
|
Subsite::changeSubsite(0);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// update original page
|
|
|
|
$p->Title = 'New Title';
|
|
|
|
// "save & publish"
|
|
|
|
$p->writeToStage('Stage');
|
|
|
|
$p->publish('Stage', 'Live');
|
|
|
|
$this->assertNotEquals($p->SubsiteID, $subsite->ID);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// reload SVP from database
|
|
|
|
// can't use DO::get by id because caches.
|
|
|
|
$svpdb = $svp->get()->byID($svp->ID);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// ensure title changed
|
|
|
|
$this->assertEquals($svpdb->Title, $p->Title);
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function testUnpublishingParentPageUnpublishesSubsiteVirtualPages()
|
|
|
|
{
|
|
|
|
Config::inst()->update('StaticPublisher', 'disable_realtime', true);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Go to main site, get parent page
|
|
|
|
$subsite = $this->objFromFixture('Subsite', 'main');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$page = $this->objFromFixture('Page', 'importantpage');
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Create two SVPs on other subsites
|
|
|
|
$subsite = $this->objFromFixture('Subsite', 'subsite1');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$vp1 = new SubsitesVirtualPage();
|
|
|
|
$vp1->CopyContentFromID = $page->ID;
|
|
|
|
$vp1->write();
|
|
|
|
$vp1->doPublish();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$subsite = $this->objFromFixture('Subsite', 'subsite2');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$vp2 = new SubsitesVirtualPage();
|
|
|
|
$vp2->CopyContentFromID = $page->ID;
|
|
|
|
$vp2->write();
|
|
|
|
$vp2->doPublish();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Switch back to main site, unpublish source
|
|
|
|
$subsite = $this->objFromFixture('Subsite', 'main');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$page = $this->objFromFixture('Page', 'importantpage');
|
|
|
|
$page->doUnpublish();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
Subsite::changeSubsite($vp1->SubsiteID);
|
|
|
|
$onLive = Versioned::get_one_by_stage('SubsitesVirtualPage', 'Live', "\"SiteTree_Live\".\"ID\" = ".$vp1->ID);
|
|
|
|
$this->assertNull($onLive, 'SVP has been removed from live');
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$subsite = $this->objFromFixture('Subsite', 'subsite2');
|
|
|
|
Subsite::changeSubsite($vp2->SubsiteID);
|
|
|
|
$onLive = Versioned::get_one_by_stage('SubsitesVirtualPage', 'Live', "\"SiteTree_Live\".\"ID\" = ".$vp2->ID);
|
|
|
|
$this->assertNull($onLive, 'SVP has been removed from live');
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* Similar to {@link SiteTreeSubsitesTest->testTwoPagesWithSameURLOnDifferentSubsites()}
|
|
|
|
* and {@link SiteTreeSubsitesTest->testPagesInDifferentSubsitesCanShareURLSegment()}.
|
|
|
|
*/
|
|
|
|
public function testSubsiteVirtualPageCanHaveSameUrlsegmentAsOtherSubsite()
|
|
|
|
{
|
|
|
|
Subsite::$write_hostmap = false;
|
|
|
|
$subsite1 = $this->objFromFixture('Subsite', 'subsite1');
|
|
|
|
$subsite2 = $this->objFromFixture('Subsite', 'subsite2');
|
|
|
|
Subsite::changeSubsite($subsite1->ID);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$subsite1Page = $this->objFromFixture('Page', 'subsite1_staff');
|
|
|
|
$subsite1Page->URLSegment = 'staff';
|
|
|
|
$subsite1Page->write();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// saving on subsite1, and linking to subsite1
|
|
|
|
$subsite1Vp = new SubsitesVirtualPage();
|
|
|
|
$subsite1Vp->CopyContentFromID = $subsite1Page->ID;
|
|
|
|
$subsite1Vp->SubsiteID = $subsite1->ID;
|
|
|
|
$subsite1Vp->write();
|
|
|
|
$this->assertNotEquals(
|
|
|
|
$subsite1Vp->URLSegment,
|
|
|
|
$subsite1Page->URLSegment,
|
|
|
|
"Doesn't allow explicit URLSegment overrides when already existing in same subsite"
|
|
|
|
);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
//Change to subsite 2
|
|
|
|
Subsite::changeSubsite($subsite2->ID);
|
2012-07-16 01:14:13 +02:00
|
|
|
|
2016-05-23 00:41:51 +02:00
|
|
|
// saving in subsite2 (which already has a page with URLSegment 'contact-us'),
|
2015-11-23 04:53:45 +01:00
|
|
|
// but linking to a page in subsite1
|
|
|
|
$subsite2Vp = new SubsitesVirtualPage();
|
|
|
|
$subsite2Vp->CopyContentFromID = $subsite1Page->ID;
|
|
|
|
$subsite2Vp->SubsiteID = $subsite2->ID;
|
|
|
|
$subsite2Vp->write();
|
|
|
|
$this->assertEquals(
|
|
|
|
$subsite2Vp->URLSegment,
|
|
|
|
$subsite1Page->URLSegment,
|
|
|
|
"Does allow explicit URLSegment overrides when only existing in a different subsite"
|
2016-05-23 00:41:51 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// When changing subsites and re-saving this page, it doesn't trigger a change
|
|
|
|
Subsite::changeSubsite($subsite1->ID);
|
|
|
|
$subsite1Page->write();
|
|
|
|
$subsite2Vp->write();
|
|
|
|
$this->assertEquals(
|
|
|
|
$subsite2Vp->URLSegment,
|
|
|
|
$subsite1Page->URLSegment,
|
|
|
|
"SubsiteVirtualPage doesn't change urls when being written in another subsite"
|
2015-11-23 04:53:45 +01:00
|
|
|
);
|
|
|
|
}
|
2010-03-01 23:27:46 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function fixVersionNumberCache($page)
|
|
|
|
{
|
|
|
|
$pages = func_get_args();
|
|
|
|
foreach ($pages as $p) {
|
|
|
|
Versioned::prepopulate_versionnumber_cache('SiteTree', 'Stage', array($p->ID));
|
|
|
|
Versioned::prepopulate_versionnumber_cache('SiteTree', 'Live', array($p->ID));
|
|
|
|
}
|
|
|
|
}
|
2013-04-30 05:25:21 +02:00
|
|
|
}
|