2011-03-22 22:40:09 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Model\VirtualPage;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Filesystem;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-03-22 22:40:09 +01:00
|
|
|
/**
|
|
|
|
* Tests link tracking to files and images.
|
|
|
|
*/
|
|
|
|
class FileLinkTrackingTest extends SapphireTest {
|
2013-03-18 11:47:15 +01:00
|
|
|
protected static $fixture_file = "FileLinkTrackingTest.yml";
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function setUp() {
|
2011-03-22 22:40:09 +01:00
|
|
|
parent::setUp();
|
2016-01-26 06:38:42 +01:00
|
|
|
|
2016-03-17 01:02:50 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-01-26 06:38:42 +01:00
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
AssetStoreTest_SpyStore::activate('FileLinkTrackingTest');
|
2011-03-22 22:40:09 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2015-10-15 00:08:52 +02:00
|
|
|
|
|
|
|
// Write file contents
|
2016-08-23 04:36:06 +02:00
|
|
|
$files = File::get()->exclude('ClassName', 'SilverStripe\\Assets\\Folder');
|
2015-10-15 00:08:52 +02:00
|
|
|
foreach($files as $file) {
|
|
|
|
$destPath = AssetStoreTest_SpyStore::getLocalPath($file);
|
|
|
|
Filesystem::makeFolder(dirname($destPath));
|
|
|
|
file_put_contents($destPath, str_repeat('x', 1000000));
|
2016-01-26 06:38:42 +01:00
|
|
|
// Ensure files are published, thus have public urls
|
2016-04-01 05:17:37 +02:00
|
|
|
$file->publishRecursive();
|
2015-10-15 00:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since we can't hard-code IDs, manually inject image tracking shortcode
|
2016-08-23 04:36:06 +02:00
|
|
|
$imageID = $this->idFromFixture('SilverStripe\\Assets\\Image', 'file1');
|
2015-10-15 00:08:52 +02:00
|
|
|
$page = $this->objFromFixture('Page', 'page1');
|
|
|
|
$page->Content = sprintf(
|
2016-03-09 23:57:39 +01:00
|
|
|
'<p>[image src="/assets/FileLinkTrackingTest/55b443b601/testscript-test-file.jpg" id="%d"]</p>',
|
2015-10-15 00:08:52 +02:00
|
|
|
$imageID
|
|
|
|
);
|
|
|
|
$page->write();
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
2011-12-16 17:29:50 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function tearDown() {
|
2015-10-15 00:08:52 +02:00
|
|
|
AssetStoreTest_SpyStore::reset();
|
2011-03-22 22:40:09 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-03-09 23:57:39 +01:00
|
|
|
/**
|
|
|
|
* Test uses global state through Versioned::set_reading_mode() since
|
|
|
|
* the shortcode parser doesn't pass along the underlying DataObject
|
|
|
|
* context, hence we can't call getSourceQueryParams().
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testFileRenameUpdatesDraftAndPublishedPages() {
|
2011-03-22 22:40:09 +01:00
|
|
|
$page = $this->objFromFixture('Page', 'page1');
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->publishRecursive();
|
2016-01-26 06:38:42 +01:00
|
|
|
|
|
|
|
// Live and stage pages both have link to public file
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-01-26 06:38:42 +01:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/testscript-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2016-01-26 06:38:42 +01:00
|
|
|
);
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/testscript-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2015-10-15 00:08:52 +02:00
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-08-23 04:36:06 +02:00
|
|
|
$file = $this->objFromFixture('SilverStripe\\Assets\\Image', 'file1');
|
2015-10-15 00:08:52 +02:00
|
|
|
$file->Name = 'renamed-test-file.jpg';
|
2011-03-22 22:40:09 +01:00
|
|
|
$file->write();
|
2016-01-26 06:38:42 +01:00
|
|
|
|
|
|
|
// Staged record now points to secure URL of renamed file, live record remains unchanged
|
|
|
|
// Note that the "secure" url doesn't have the "FileLinkTrackingTest" component because
|
|
|
|
// the mocked test location disappears for secure files.
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-01-26 06:38:42 +01:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/55b443b601/renamed-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2016-01-26 06:38:42 +01:00
|
|
|
);
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2016-01-26 06:38:42 +01:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/testscript-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2016-01-26 06:38:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Publishing the file should result in a direct public link (indicated by "FileLinkTrackingTest")
|
|
|
|
// Although the old live page will still point to the old record.
|
|
|
|
// @todo - Ensure shortcodes are used with all images to prevent live records having broken links
|
2016-04-01 05:17:37 +02:00
|
|
|
$file->publishRecursive();
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-01-26 06:38:42 +01:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2016-01-26 06:38:42 +01:00
|
|
|
);
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2016-01-26 06:38:42 +01:00
|
|
|
$this->assertContains(
|
2016-03-09 23:57:39 +01:00
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2016-01-26 06:38:42 +01:00
|
|
|
);
|
|
|
|
|
2016-03-09 23:57:39 +01:00
|
|
|
// Publishing the page after publishing the asset should retain linking
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->publishRecursive();
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2015-10-15 00:08:52 +02:00
|
|
|
);
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2015-10-15 00:08:52 +02:00
|
|
|
);
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testFileLinkRewritingOnVirtualPages() {
|
2011-03-22 22:40:09 +01:00
|
|
|
// Publish the source page
|
|
|
|
$page = $this->objFromFixture('Page', 'page1');
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page->publishRecursive());
|
2011-03-22 22:40:09 +01:00
|
|
|
|
|
|
|
// Create a virtual page from it, and publish that
|
|
|
|
$svp = new VirtualPage();
|
|
|
|
$svp->CopyContentFromID = $page->ID;
|
|
|
|
$svp->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$svp->publishRecursive();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-22 22:40:09 +01:00
|
|
|
// Rename the file
|
2016-08-23 04:36:06 +02:00
|
|
|
$file = $this->objFromFixture('SilverStripe\\Assets\\Image', 'file1');
|
2015-10-15 00:08:52 +02:00
|
|
|
$file->Name = 'renamed-test-file.jpg';
|
2011-03-22 22:40:09 +01:00
|
|
|
$file->write();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
// Verify that the draft virtual pages have the correct content
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
2016-01-26 06:38:42 +01:00
|
|
|
'<img src="/assets/55b443b601/renamed-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2015-10-15 00:08:52 +02:00
|
|
|
);
|
2016-01-26 06:38:42 +01:00
|
|
|
|
|
|
|
// Publishing both file and page will update the live record
|
2016-04-01 05:17:37 +02:00
|
|
|
$file->publishRecursive();
|
|
|
|
$page->publishRecursive();
|
2016-01-26 06:38:42 +01:00
|
|
|
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2015-10-15 00:08:52 +02:00
|
|
|
);
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testLinkRewritingOnAPublishedPageDoesntMakeItEditedOnDraft() {
|
2011-03-22 22:40:09 +01:00
|
|
|
// Publish the source page
|
2016-10-05 03:08:34 +02:00
|
|
|
/** @var Page $page */
|
2011-03-22 22:40:09 +01:00
|
|
|
$page = $this->objFromFixture('Page', 'page1');
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page->publishRecursive());
|
2016-10-05 03:08:34 +02:00
|
|
|
$this->assertFalse($page->isModifiedOnDraft());
|
2011-03-22 22:40:09 +01:00
|
|
|
|
|
|
|
// Rename the file
|
2016-08-23 04:36:06 +02:00
|
|
|
$file = $this->objFromFixture('SilverStripe\\Assets\\Image', 'file1');
|
2015-10-15 00:08:52 +02:00
|
|
|
$file->Name = 'renamed-test-file.jpg';
|
2011-03-22 22:40:09 +01:00
|
|
|
$file->write();
|
|
|
|
|
|
|
|
// Confirm that the page hasn't gone green.
|
2016-10-05 03:08:34 +02:00
|
|
|
$this->assertFalse($page->isModifiedOnDraft());
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testTwoFileRenamesInARowWork() {
|
2011-03-22 22:40:09 +01:00
|
|
|
$page = $this->objFromFixture('Page', 'page1');
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page->publishRecursive());
|
2016-03-09 23:57:39 +01:00
|
|
|
|
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/testscript-test-file.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
|
|
|
);
|
2011-03-22 22:40:09 +01:00
|
|
|
|
|
|
|
// Rename the file twice
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-08-23 04:36:06 +02:00
|
|
|
$file = $this->objFromFixture('SilverStripe\\Assets\\Image', 'file1');
|
2015-10-15 00:08:52 +02:00
|
|
|
$file->Name = 'renamed-test-file.jpg';
|
2011-03-22 22:40:09 +01:00
|
|
|
$file->write();
|
|
|
|
|
|
|
|
// TODO Workaround for bug in DataObject->getChangedFields(), which returns stale data,
|
|
|
|
// and influences File->updateFilesystem()
|
2016-08-23 04:36:06 +02:00
|
|
|
$file = DataObject::get_by_id('SilverStripe\\Assets\\File', $file->ID);
|
2015-10-15 00:08:52 +02:00
|
|
|
$file->Name = 'renamed-test-file-second-time.jpg';
|
2011-03-22 22:40:09 +01:00
|
|
|
$file->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$file->publishRecursive();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-22 22:40:09 +01:00
|
|
|
// Confirm that the correct image is shown in both the draft and live site
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file-second-time.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2015-10-15 00:08:52 +02:00
|
|
|
);
|
2016-01-26 06:38:42 +01:00
|
|
|
|
|
|
|
// Publishing this record also updates live record
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->publishRecursive();
|
2016-03-09 23:57:39 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file-second-time.jpg"',
|
2016-03-09 23:57:39 +01:00
|
|
|
Page::get()->byID($page->ID)->dbObject('Content')->forTemplate()
|
2015-10-15 00:08:52 +02:00
|
|
|
);
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:40:49 +01:00
|
|
|
|