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();
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
2011-12-16 17:29:50 +01:00
|
|
|
if(!file_exists(ASSETS_PATH)) mkdir(ASSETS_PATH);
|
|
|
|
$fh = fopen(ASSETS_PATH . '/testscript-test-file.pdf', "w");
|
2011-03-22 22:40:09 +01:00
|
|
|
fwrite($fh, str_repeat('x',1000000));
|
|
|
|
fclose($fh);
|
|
|
|
}
|
2011-12-16 17:29:50 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function tearDown() {
|
2011-03-22 22:40:09 +01:00
|
|
|
parent::tearDown();
|
|
|
|
$testFiles = array(
|
2011-12-16 17:29:50 +01:00
|
|
|
'/testscript-test-file.pdf',
|
|
|
|
'/renamed-test-file.pdf',
|
|
|
|
'/renamed-test-file-second-time.pdf',
|
2011-03-22 22:40:09 +01:00
|
|
|
);
|
|
|
|
foreach($testFiles as $file) {
|
2011-12-16 17:29:50 +01:00
|
|
|
if(file_exists(ASSETS_PATH . $file)) unlink(ASSETS_PATH . $file);
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
$this->assertContains('<img src="assets/testscript-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $page->ID")->value());
|
|
|
|
|
|
|
|
$file = $this->objFromFixture('File', 'file1');
|
|
|
|
$file->Name = 'renamed-test-file.pdf';
|
|
|
|
$file->write();
|
|
|
|
|
|
|
|
$this->assertContains('<img src="assets/renamed-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $page->ID")->value());
|
|
|
|
$this->assertContains('<img src="assets/renamed-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $page->ID")->value());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
$file = $this->objFromFixture('File', 'file1');
|
|
|
|
$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/renamed-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value());
|
|
|
|
$this->assertContains('<img src="assets/renamed-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $svp->ID")->value());
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
$this->assertFalse($page->IsModifiedOnStage);
|
|
|
|
|
|
|
|
// Rename the file
|
|
|
|
$file = $this->objFromFixture('File', 'file1');
|
|
|
|
$file->Name = 'renamed-test-file.pdf';
|
|
|
|
$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.
|
|
|
|
$this->assertFalse($page->IsModifiedOnStage);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
$this->assertContains('<img src="assets/testscript-test-file.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $page->ID")->value());
|
|
|
|
|
|
|
|
// Rename the file twice
|
|
|
|
$file = $this->objFromFixture('File', 'file1');
|
|
|
|
$file->Name = 'renamed-test-file.pdf';
|
|
|
|
$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);
|
|
|
|
$file->Name = 'renamed-test-file-second-time.pdf';
|
|
|
|
$file->write();
|
|
|
|
|
|
|
|
// Confirm that the correct image is shown in both the draft and live site
|
|
|
|
$this->assertContains('<img src="assets/renamed-test-file-second-time.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $page->ID")->value());
|
|
|
|
$this->assertContains('<img src="assets/renamed-test-file-second-time.pdf"',
|
|
|
|
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $page->ID")->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:40:49 +01:00
|
|
|
|