silverstripe-subsites/tests/php/SubsitesVirtualPageTest.php

319 lines
11 KiB
PHP
Raw Normal View History

2010-03-01 03:56:19 +01:00
<?php
namespace SilverStripe\Subsites\Tests;
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
{
protected static $fixture_file = [
'SubsiteTest.yml',
'SubsitesVirtualPageTest.yml',
2017-06-01 15:57:53 +02:00
];
2017-05-29 13:42:42 +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
protected function setUp()
2017-05-24 15:26:28 +02:00
{
parent::setUp();
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
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;
$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
{
$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');
$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();
$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
$this->assertContains(
'<img src="/assets/SubsitesVirtualPageTest/464dedb70a/renamed-test-file.pdf"',
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value()
);
$this->assertContains(
'<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
{
$this->markTestSkipped('Needs some update or refactoring');
2017-05-24 15:26:28 +02:00
// Fixture
$p = new Page();
$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
$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
$p->Content = 'new content';
2017-05-24 15:26:28 +02:00
$p->write();
$p->publishSingle();
2017-05-24 15:26:28 +02:00
$this->fixVersionNumberCache($vp2);
$this->assertTrue($vp2->IsAddedToStage);
// VP is now published
$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
$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
$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
{
$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');
$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');
$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');
$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
{
$this->markTestIncomplete('@todo fix this test');
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();
$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();
$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
{
$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);
$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-05-24 15:26:28 +02:00
$this->assertNotEquals(
$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,
'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
);
}
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
}
}
}