2011-03-22 22:40:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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";
|
2011-03-22 22:40:09 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function setUp() {
|
2011-03-22 22:40:09 +01:00
|
|
|
parent::setUp();
|
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
|
|
|
|
$files = File::get()->exclude('ClassName', 'Folder');
|
|
|
|
foreach($files as $file) {
|
|
|
|
$destPath = AssetStoreTest_SpyStore::getLocalPath($file);
|
|
|
|
Filesystem::makeFolder(dirname($destPath));
|
|
|
|
file_put_contents($destPath, str_repeat('x', 1000000));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we can't hard-code IDs, manually inject image tracking shortcode
|
|
|
|
$imageID = $this->idFromFixture('Image', 'file1');
|
|
|
|
$page = $this->objFromFixture('Page', 'page1');
|
|
|
|
$page->Content = sprintf(
|
|
|
|
'<p><img src="/assets/FileLinkTrackingTest/55b443b601/testscript-test-file.jpg" data-fileid="%d" /></p>',
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testFileRenameUpdatesDraftAndPublishedPages() {
|
2011-03-22 22:40:09 +01:00
|
|
|
$page = $this->objFromFixture('Page', 'page1');
|
|
|
|
$this->assertTrue($page->doPublish());
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/testscript-test-file.jpg"',
|
|
|
|
DB::prepared_query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = ?", array($page->ID))->value()
|
|
|
|
);
|
2011-03-22 22:40:09 +01:00
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
$file = $this->objFromFixture('Image', 'file1');
|
|
|
|
$file->Name = 'renamed-test-file.jpg';
|
2011-03-22 22:40:09 +01:00
|
|
|
$file->write();
|
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
|
|
|
DB::prepared_query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = ?", array($page->ID))->value()
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
|
|
|
DB::prepared_query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = ?", array($page->ID))->value()
|
|
|
|
);
|
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');
|
|
|
|
$this->assertTrue($page->doPublish());
|
|
|
|
|
|
|
|
// Create a virtual page from it, and publish that
|
|
|
|
$svp = new VirtualPage();
|
|
|
|
$svp->CopyContentFromID = $page->ID;
|
|
|
|
$svp->write();
|
|
|
|
$svp->doPublish();
|
|
|
|
|
|
|
|
// Rename the file
|
2015-10-15 00:08:52 +02:00
|
|
|
$file = $this->objFromFixture('Image', 'file1');
|
|
|
|
$file->Name = 'renamed-test-file.jpg';
|
2011-03-22 22:40:09 +01:00
|
|
|
$file->write();
|
|
|
|
|
|
|
|
// Verify that the draft and publish virtual pages both have the corrected link
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
|
|
|
DB::prepared_query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = ?", array($svp->ID))->value()
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"',
|
|
|
|
DB::prepared_query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = ?", array($svp->ID))->value()
|
|
|
|
);
|
2011-03-22 22:40:09 +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
|
|
|
|
$page = $this->objFromFixture('Page', 'page1');
|
|
|
|
$this->assertTrue($page->doPublish());
|
2015-06-05 01:09:23 +02:00
|
|
|
$this->assertFalse($page->getIsModifiedOnStage());
|
2011-03-22 22:40:09 +01:00
|
|
|
|
|
|
|
// Rename the file
|
2015-10-15 00:08:52 +02:00
|
|
|
$file = $this->objFromFixture('Image', 'file1');
|
|
|
|
$file->Name = 'renamed-test-file.jpg';
|
2011-03-22 22:40:09 +01:00
|
|
|
$file->write();
|
|
|
|
|
|
|
|
// Caching hack
|
|
|
|
Versioned::prepopulate_versionnumber_cache('SiteTree', 'Stage', array($page->ID));
|
|
|
|
Versioned::prepopulate_versionnumber_cache('SiteTree', 'Live', array($page->ID));
|
|
|
|
|
|
|
|
// Confirm that the page hasn't gone green.
|
2015-06-05 01:09:23 +02:00
|
|
|
$this->assertFalse($page->getIsModifiedOnStage());
|
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');
|
|
|
|
$this->assertTrue($page->doPublish());
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/testscript-test-file.jpg"',
|
2013-06-21 00:45:33 +02:00
|
|
|
DB::prepared_query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = ?", array($page->ID))->value());
|
2011-03-22 22:40:09 +01:00
|
|
|
|
|
|
|
// Rename the file twice
|
2015-10-15 00:08:52 +02:00
|
|
|
$file = $this->objFromFixture('Image', 'file1');
|
|
|
|
$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()
|
|
|
|
$file = DataObject::get_by_id('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();
|
|
|
|
|
|
|
|
// Confirm that the correct image is shown in both the draft and live site
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file-second-time.jpg"',
|
|
|
|
DB::prepared_query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = ?", array($page->ID))->value()
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file-second-time.jpg"',
|
|
|
|
DB::prepared_query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = ?", array($page->ID))->value()
|
|
|
|
);
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:40:49 +01:00
|
|
|
|