silverstripe-subsites/tests/php/SubsitesVirtualPageTest.php

307 lines
11 KiB
PHP
Raw Normal View History

2010-03-01 03:56:19 +01:00
<?php
namespace SilverStripe\Subsites\Tests;
use Page;
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\Control\Director;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Assets\File;
2017-05-24 15:26:28 +02:00
use SilverStripe\ORM\DB;
2017-05-29 13:42:42 +02:00
use SilverStripe\Core\Config\Config;
use SilverStripe\Versioned\Versioned;
2017-05-24 15:25:34 +02:00
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\Pages\SubsitesVirtualPage;
2016-09-22 16:38:29 +02:00
2017-05-24 15:26:28 +02:00
class SubsitesVirtualPageTest extends BaseSubsiteTest
{
2017-05-29 13:42:42 +02:00
public static $fixture_file = array(
'subsites/tests/php/SubsiteTest.yml',
'subsites/tests/php/SubsitesVirtualPageTest.yml',
2017-05-29 13:42:42 +02:00
);
protected $illegalExtensions = array(
'SiteTree' => array('Translatable')
);
2017-05-24 15:26:28 +02:00
public function setUp()
{
parent::setUp();
// 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
function testVirtualPageFromAnotherSubsite()
{
Subsite::$write_hostmap = false;
$subsite = $this->objFromFixture(Subsite::class, 'subsite2');
Subsite::changeSubsite($subsite->ID);
Subsite::$disable_subsite_filter = false;
$linky = $this->objFromFixture('Page', 'linky');
$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
{
// 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-05-24 15:26:28 +02:00
$this->assertTrue($page->doPublish());
// Create a virtual page from it, and publish that
$svp = new SubsitesVirtualPage();
$svp->CopyContentFromID = $page->ID;
$svp->write();
$svp->doPublish();
// 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-29 13:42:42 +02:00
public function testSubsiteVirtualPagesArentInappropriatelyPublished()
2017-05-24 15:26:28 +02:00
{
// Fixture
$p = new Page();
$p->Content = "test content";
$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->doPublish();
$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";
$p->write();
$p->doPublish();
$this->fixVersionNumberCache($vp2);
$this->assertTrue($vp2->IsAddedToStage);
// VP is now published
$vp->doPublish();
$this->fixVersionNumberCache($vp);
$this->assertTrue($vp->ExistsOnLive);
$this->assertFalse($vp->IsModifiedOnStage);
// P edited, VP and P both go green
$p->Content = "third content";
$p->write();
$this->fixVersionNumberCache($vp, $p);
$this->assertTrue($p->IsModifiedOnStage);
$this->assertTrue($vp->IsModifiedOnStage);
// Publish, VP goes black
$p->doPublish();
$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
{
// create page
$p = new Page();
$p->Content = 'Content';
$p->Title = 'Title';
$p->writeToStage('Stage');
$p->publish('Stage', 'Live');
$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->publish('Stage', 'Live');
$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->publish('Stage', 'Live');
$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
{
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->doPublish();
$subsite = $this->objFromFixture(Subsite::class, 'subsite2');
Subsite::changeSubsite($subsite->ID);
$vp2 = new SubsitesVirtualPage();
$vp2->CopyContentFromID = $page->ID;
$vp2->write();
$vp2->doPublish();
// 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-05-29 13:42:42 +02:00
$onLive = Versioned::get_one_by_stage('SubsitesVirtualPage', '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-05-29 13:42:42 +02:00
$onLive = Versioned::get_one_by_stage('SubsitesVirtualPage', '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
{
Subsite::$write_hostmap = false;
$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
$subsite2 = $this->objFromFixture(Subsite::class, 'subsite2');
Subsite::changeSubsite($subsite1->ID);
$subsite1Page = $this->objFromFixture('Page', 'subsite1_staff');
$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();
$this->assertNotEquals(
$subsite1Vp->URLSegment,
$subsite1Page->URLSegment,
"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
);
// 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"
2017-05-24 15:26:28 +02:00
);
}
2017-05-29 13:42:42 +02:00
public function fixVersionNumberCache($page)
2017-05-24 15:26:28 +02:00
{
$pages = func_get_args();
foreach ($pages as $p) {
2017-05-29 13:42:42 +02:00
Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Stage', array($p->ID));
Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Live', array($p->ID));
2017-05-24 15:26:28 +02:00
}
}
}