2010-03-01 03:56:19 +01:00
|
|
|
<?php
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
namespace SilverStripe\Subsites\Tests;
|
|
|
|
|
2017-05-30 17:35:02 +02:00
|
|
|
use Page;
|
2017-06-01 15:57:53 +02:00
|
|
|
use SilverStripe\Assets\File;
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\Assets\Filesystem;
|
2017-05-24 15:25:34 +02:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
2017-05-29 13:42:42 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2017-06-01 15:57:53 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2017-05-29 13:42:42 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-06-01 15:57:53 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2017-05-24 15:25:34 +02:00
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
|
|
|
use SilverStripe\Subsites\Pages\SubsitesVirtualPage;
|
2017-06-01 15:57:53 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
class SubsitesVirtualPageTest extends BaseSubsiteTest
|
|
|
|
{
|
2017-08-29 07:22:32 +02:00
|
|
|
protected static $fixture_file = [
|
|
|
|
'SubsiteTest.yml',
|
|
|
|
'SubsitesVirtualPageTest.yml',
|
2017-06-01 15:57:53 +02:00
|
|
|
];
|
2017-05-29 13:42:42 +02:00
|
|
|
|
2017-08-29 07:22:32 +02:00
|
|
|
protected static $illegal_extensions = [
|
|
|
|
SiteTree::class => ['Translatable'] // @todo implement Translatable namespace
|
2017-06-01 15:57:53 +02:00
|
|
|
];
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-08-29 07:22:32 +02:00
|
|
|
protected function setUp()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-01-18 05:48:09 +01:00
|
|
|
Config::modify()->set(Subsite::class, 'write_hostmap', false);
|
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
// Set backend root to /DataDifferencerTest
|
|
|
|
TestAssetStore::activate('SubsitesVirtualPageTest');
|
|
|
|
|
|
|
|
// Create a test files for each of the fixture references
|
2017-05-29 13:42:42 +02:00
|
|
|
$file = $this->objFromFixture(File::class, 'file1');
|
|
|
|
$page = $this->objFromFixture(SiteTree::class, 'page1');
|
2017-05-24 15:26:28 +02:00
|
|
|
$fromPath = __DIR__ . '/testscript-test-file.pdf';
|
|
|
|
$destPath = TestAssetStore::getLocalPath($file);
|
|
|
|
Filesystem::makeFolder(dirname($destPath));
|
|
|
|
copy($fromPath, $destPath);
|
|
|
|
|
|
|
|
// Hack in site link tracking after the fact
|
|
|
|
$page->Content = '<p><img src="' . $file->getURL() . '" data-fileid="' . $file->ID . '" /></p>';
|
|
|
|
$page->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
TestAssetStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to bring main:linky to subsite2:linky
|
2017-05-31 06:41:45 +02:00
|
|
|
public function testVirtualPageFromAnotherSubsite()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
$subsite = $this->objFromFixture(Subsite::class, 'subsite2');
|
|
|
|
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
Subsite::$disable_subsite_filter = false;
|
|
|
|
|
2017-08-29 07:22:32 +02:00
|
|
|
$linky = $this->objFromFixture(Page::class, 'linky');
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
$svp = new SubsitesVirtualPage();
|
|
|
|
$svp->CopyContentFromID = $linky->ID;
|
|
|
|
$svp->SubsiteID = $subsite->ID;
|
|
|
|
$svp->URLSegment = 'linky';
|
|
|
|
|
|
|
|
$svp->write();
|
|
|
|
|
|
|
|
$this->assertEquals($svp->SubsiteID, $subsite->ID);
|
|
|
|
$this->assertEquals($svp->Title, $linky->Title);
|
|
|
|
}
|
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
public function testFileLinkRewritingOnVirtualPages()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-05-31 06:41:45 +02:00
|
|
|
$this->markTestSkipped('File handling needs update');
|
2017-05-24 15:26:28 +02:00
|
|
|
// File setup
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-05-29 13:42:42 +02:00
|
|
|
touch(Director::baseFolder() . '/assets/testscript-test-file.pdf');
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
// Publish the source page
|
2017-05-29 13:42:42 +02:00
|
|
|
$page = $this->objFromFixture(SiteTree::class, 'page1');
|
2017-08-30 05:29:13 +02:00
|
|
|
$this->assertTrue($page->publishSingle());
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
// Create a virtual page from it, and publish that
|
|
|
|
$svp = new SubsitesVirtualPage();
|
|
|
|
$svp->CopyContentFromID = $page->ID;
|
|
|
|
$svp->write();
|
2017-08-30 05:29:13 +02:00
|
|
|
$svp->publishSingle();
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
// Rename the file
|
2017-05-29 13:42:42 +02:00
|
|
|
$file = $this->objFromFixture(File::class, 'file1');
|
2017-05-24 15:26:28 +02:00
|
|
|
$file->Name = 'renamed-test-file.pdf';
|
|
|
|
$file->write();
|
|
|
|
|
|
|
|
// Verify that the draft and publish virtual pages both have the corrected link
|
2017-08-29 07:30:22 +02:00
|
|
|
$this->assertContains(
|
2017-08-29 07:07:24 +02:00
|
|
|
'<img src="/assets/SubsitesVirtualPageTest/464dedb70a/renamed-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value()
|
|
|
|
);
|
2017-08-29 07:30:22 +02:00
|
|
|
$this->assertContains(
|
2017-08-29 07:07:24 +02:00
|
|
|
'<img src="/assets/SubsitesVirtualPageTest/464dedb70a/renamed-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $svp->ID")->value()
|
|
|
|
);
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
public function testSubsiteVirtualPagesArentInappropriatelyPublished()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-05-31 06:41:45 +02:00
|
|
|
$this->markTestSkipped('Needs some update or refactoring');
|
2017-05-24 15:26:28 +02:00
|
|
|
// Fixture
|
|
|
|
$p = new Page();
|
2017-06-01 14:49:55 +02:00
|
|
|
$p->Content = 'test content';
|
2017-05-24 15:26:28 +02:00
|
|
|
$p->write();
|
|
|
|
$vp = new SubsitesVirtualPage();
|
|
|
|
$vp->CopyContentFromID = $p->ID;
|
|
|
|
$vp->write();
|
|
|
|
|
|
|
|
// VP is oragne
|
|
|
|
$this->assertTrue($vp->IsAddedToStage);
|
|
|
|
|
|
|
|
// VP is still orange after we publish
|
2017-08-30 05:29:13 +02:00
|
|
|
$p->publishSingle();
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->fixVersionNumberCache($vp);
|
|
|
|
$this->assertTrue($vp->IsAddedToStage);
|
|
|
|
|
|
|
|
// A new VP created after P's initial construction
|
|
|
|
$vp2 = new SubsitesVirtualPage();
|
|
|
|
$vp2->CopyContentFromID = $p->ID;
|
|
|
|
$vp2->write();
|
|
|
|
$this->assertTrue($vp2->IsAddedToStage);
|
|
|
|
|
|
|
|
// Also remains orange after a republish
|
2017-06-01 14:49:55 +02:00
|
|
|
$p->Content = 'new content';
|
2017-05-24 15:26:28 +02:00
|
|
|
$p->write();
|
2017-08-30 05:29:13 +02:00
|
|
|
$p->publishSingle();
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->fixVersionNumberCache($vp2);
|
|
|
|
$this->assertTrue($vp2->IsAddedToStage);
|
|
|
|
|
|
|
|
// VP is now published
|
2017-08-30 05:29:13 +02:00
|
|
|
$vp->publishSingle();
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
$this->fixVersionNumberCache($vp);
|
|
|
|
$this->assertTrue($vp->ExistsOnLive);
|
|
|
|
$this->assertFalse($vp->IsModifiedOnStage);
|
|
|
|
|
|
|
|
// P edited, VP and P both go green
|
2017-06-01 14:49:55 +02:00
|
|
|
$p->Content = 'third content';
|
2017-05-24 15:26:28 +02:00
|
|
|
$p->write();
|
|
|
|
|
|
|
|
$this->fixVersionNumberCache($vp, $p);
|
|
|
|
$this->assertTrue($p->IsModifiedOnStage);
|
|
|
|
$this->assertTrue($vp->IsModifiedOnStage);
|
|
|
|
|
|
|
|
// Publish, VP goes black
|
2017-08-30 05:29:13 +02:00
|
|
|
$p->publishSingle();
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->fixVersionNumberCache($vp);
|
|
|
|
$this->assertTrue($vp->ExistsOnLive);
|
|
|
|
$this->assertFalse($vp->IsModifiedOnStage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
public function testPublishedSubsiteVirtualPagesUpdateIfTargetPageUpdates()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-05-31 06:41:45 +02:00
|
|
|
$this->markTestSkipped('Needs some update or refactoring');
|
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
// create page
|
|
|
|
$p = new Page();
|
|
|
|
$p->Content = 'Content';
|
|
|
|
$p->Title = 'Title';
|
|
|
|
$p->writeToStage('Stage');
|
2017-06-01 14:57:28 +02:00
|
|
|
$p->copyVersionToStage('Stage', 'Live');
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->assertTrue($p->ExistsOnLive);
|
|
|
|
|
|
|
|
// change to subsite
|
|
|
|
$subsite = $this->objFromFixture(Subsite::class, 'subsite2');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
Subsite::$disable_subsite_filter = false;
|
|
|
|
|
|
|
|
// create svp in subsite
|
|
|
|
$svp = new SubsitesVirtualPage();
|
|
|
|
$svp->CopyContentFromID = $p->ID;
|
|
|
|
$svp->write();
|
|
|
|
$svp->writeToStage('Stage');
|
2017-06-01 14:57:28 +02:00
|
|
|
$svp->copyVersionToStage('Stage', 'Live');
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->assertEquals($svp->SubsiteID, $subsite->ID);
|
|
|
|
$this->assertTrue($svp->ExistsOnLive);
|
|
|
|
|
|
|
|
// change back to original subsite ("Main site")
|
|
|
|
Subsite::changeSubsite(0);
|
|
|
|
|
|
|
|
// update original page
|
|
|
|
$p->Title = 'New Title';
|
|
|
|
// "save & publish"
|
|
|
|
$p->writeToStage('Stage');
|
2017-06-01 14:57:28 +02:00
|
|
|
$p->copyVersionToStage('Stage', 'Live');
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->assertNotEquals($p->SubsiteID, $subsite->ID);
|
|
|
|
|
|
|
|
// reload SVP from database
|
|
|
|
// can't use DO::get by id because caches.
|
|
|
|
$svpdb = $svp->get()->byID($svp->ID);
|
|
|
|
|
|
|
|
// ensure title changed
|
|
|
|
$this->assertEquals($svpdb->Title, $p->Title);
|
|
|
|
}
|
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
public function testUnpublishingParentPageUnpublishesSubsiteVirtualPages()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-08-31 02:33:25 +02:00
|
|
|
$this->markTestIncomplete('@todo fix this test');
|
|
|
|
|
2017-05-30 15:14:28 +02:00
|
|
|
Config::modify()->set('StaticPublisher', 'disable_realtime', true);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
// Go to main site, get parent page
|
|
|
|
$subsite = $this->objFromFixture(Subsite::class, 'main');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$page = $this->objFromFixture('Page', 'importantpage');
|
|
|
|
|
|
|
|
// Create two SVPs on other subsites
|
|
|
|
$subsite = $this->objFromFixture(Subsite::class, 'subsite1');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$vp1 = new SubsitesVirtualPage();
|
|
|
|
$vp1->CopyContentFromID = $page->ID;
|
|
|
|
$vp1->write();
|
2017-05-31 06:41:45 +02:00
|
|
|
$vp1->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
$subsite = $this->objFromFixture(Subsite::class, 'subsite2');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$vp2 = new SubsitesVirtualPage();
|
|
|
|
$vp2->CopyContentFromID = $page->ID;
|
|
|
|
$vp2->write();
|
2017-05-31 06:41:45 +02:00
|
|
|
$vp2->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
// Switch back to main site, unpublish source
|
|
|
|
$subsite = $this->objFromFixture(Subsite::class, 'main');
|
|
|
|
Subsite::changeSubsite($subsite->ID);
|
|
|
|
$page = $this->objFromFixture('Page', 'importantpage');
|
|
|
|
$page->doUnpublish();
|
|
|
|
|
|
|
|
Subsite::changeSubsite($vp1->SubsiteID);
|
2017-06-01 15:57:53 +02:00
|
|
|
$onLive = Versioned::get_one_by_stage(SubsitesVirtualPage::class, 'Live', '"SiteTree_Live"."ID" = ' . $vp1->ID);
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->assertNull($onLive, 'SVP has been removed from live');
|
|
|
|
|
|
|
|
$subsite = $this->objFromFixture(Subsite::class, 'subsite2');
|
|
|
|
Subsite::changeSubsite($vp2->SubsiteID);
|
2017-06-01 15:57:53 +02:00
|
|
|
$onLive = Versioned::get_one_by_stage(SubsitesVirtualPage::class, 'Live', '"SiteTree_Live"."ID" = ' . $vp2->ID);
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->assertNull($onLive, 'SVP has been removed from live');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to {@link SiteTreeSubsitesTest->testTwoPagesWithSameURLOnDifferentSubsites()}
|
|
|
|
* and {@link SiteTreeSubsitesTest->testPagesInDifferentSubsitesCanShareURLSegment()}.
|
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
public function testSubsiteVirtualPageCanHaveSameUrlsegmentAsOtherSubsite()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-08-31 02:33:25 +02:00
|
|
|
$this->markTestIncomplete('@todo fix this test');
|
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
|
|
|
|
$subsite2 = $this->objFromFixture(Subsite::class, 'subsite2');
|
|
|
|
Subsite::changeSubsite($subsite1->ID);
|
|
|
|
|
2017-08-30 23:44:09 +02:00
|
|
|
$subsite1Page = $this->objFromFixture(Page::class, 'subsite1_staff');
|
2017-05-24 15:26:28 +02:00
|
|
|
$subsite1Page->URLSegment = 'staff';
|
|
|
|
$subsite1Page->write();
|
|
|
|
|
|
|
|
// saving on subsite1, and linking to subsite1
|
|
|
|
$subsite1Vp = new SubsitesVirtualPage();
|
|
|
|
$subsite1Vp->CopyContentFromID = $subsite1Page->ID;
|
|
|
|
$subsite1Vp->SubsiteID = $subsite1->ID;
|
|
|
|
$subsite1Vp->write();
|
2017-08-30 23:44:09 +02:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
$this->assertNotEquals(
|
2017-08-30 23:44:09 +02:00
|
|
|
$subsite1Vp->URLSegment,
|
|
|
|
$subsite1Page->URLSegment,
|
2017-05-24 15:26:28 +02:00
|
|
|
"Doesn't allow explicit URLSegment overrides when already existing in same subsite"
|
|
|
|
);
|
|
|
|
|
|
|
|
//Change to subsite 2
|
|
|
|
Subsite::changeSubsite($subsite2->ID);
|
|
|
|
|
|
|
|
// saving in subsite2 (which already has a page with URLSegment 'contact-us'),
|
|
|
|
// 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,
|
2017-06-01 14:49:55 +02:00
|
|
|
'Does allow explicit URLSegment overrides when only existing in a different subsite'
|
2017-05-29 13:42:42 +02:00
|
|
|
);
|
|
|
|
|
2017-06-01 15:57:53 +02:00
|
|
|
// When changing subsites and re-saving this page, it doesn't trigger a change
|
|
|
|
Subsite::changeSubsite($subsite1->ID);
|
|
|
|
$subsite1Page->write();
|
|
|
|
$subsite2Vp->write();
|
2017-05-29 13:42:42 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
$subsite2Vp->URLSegment,
|
|
|
|
$subsite1Page->URLSegment,
|
|
|
|
"SubsiteVirtualPage doesn't change urls when being written in another subsite"
|
2017-05-24 15:26:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-29 07:22:32 +02:00
|
|
|
protected function fixVersionNumberCache($page)
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
$pages = func_get_args();
|
|
|
|
foreach ($pages as $p) {
|
2017-06-01 15:57:53 +02:00
|
|
|
Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Stage', [$p->ID]);
|
|
|
|
Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Live', [$p->ID]);
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 05:25:21 +02:00
|
|
|
}
|