Permalink
Browse files

MINOR Moved File->BackLinkTracking feature from 'sapphire' to 'cms'

  • Loading branch information...
1 parent 42fa757 commit d93025d6687e29375f19a21a63e22f9a7b3954f5 @chillu chillu committed Mar 22, 2011
Showing with 1 addition and 176 deletions.
  1. +1 −54 filesystem/File.php
  2. +0 −112 tests/FileLinkTrackingTest.php
  3. +0 −10 tests/FileLinkTrackingTest.yml
View
55 filesystem/File.php
@@ -86,10 +86,6 @@ class File extends DataObject {
static $many_many = array();
- static $belongs_many_many = array(
- "BackLinkTracking" => "SiteTree",
- );
-
static $defaults = array();
static $extensions = array(
@@ -181,20 +177,6 @@ function TreeTitle() {
function getTreeTitle() {
return $this->Title;
}
-
- /**
- * @todo Unnecessary shortcut for AssetTableField, coupled with cms module.
- *
- * @return Integer
- */
- function BackLinkTrackingCount() {
- $pages = $this->BackLinkTracking();
- if($pages) {
- return $pages->Count();
- } else {
- return 0;
- }
- }
/**
* Event handler called before deleting from the database.
@@ -213,28 +195,6 @@ protected function onBeforeDelete() {
}
/**
- * Updates link tracking.
- */
- protected function onAfterDelete() {
- parent::onAfterDelete();
-
- $brokenPages = $this->BackLinkTracking();
- if($brokenPages) {
- $origStage = Versioned::current_stage();
-
- // This will syncLinkTracking on draft
- Versioned::reading_stage('Stage');
- foreach($brokenPages as $brokenPage) $brokenPage->write();
-
- // This will syncLinkTracking on published
- Versioned::reading_stage('Live');
- foreach($brokenPages as $brokenPage) $brokenPage->write();
-
- Versioned::reading_stage($origStage);
- }
- }
-
- /**
* @todo Enforce on filesystem URL level via mod_rewrite
*
* @return boolean
@@ -508,24 +468,11 @@ function setName($name) {
}
/**
- * Rewrite links to the $old file to now point to the $new file.
- *
- * @uses SiteTree->rewriteFileURL()
- *
* @param String $old File path relative to the webroot
* @param String $new File path relative to the webroot
*/
protected function updateLinks($old, $new) {
- if(class_exists('Subsite')) Subsite::disable_subsite_filter(true);
-
- $pages = $this->BackLinkTracking();
-
- $summary = "";
- if($pages) {
- foreach($pages as $page) $page->rewriteFileURL($old,$new);
- }
-
- if(class_exists('Subsite')) Subsite::disable_subsite_filter(false);
+ $this->extend('updateLinks', $old, $new);
}
/**
View
112 tests/FileLinkTrackingTest.php
@@ -1,112 +0,0 @@
-<?php
-
-/**
- * Tests link tracking to files and images.
- */
-class FileLinkTrackingTest extends SapphireTest {
- static $fixture_file = "sapphire/tests/FileLinkTrackingTest.yml";
-
- function setUp() {
- parent::setUp();
- $this->logInWithPermission('ADMIN');
-
- $fh = fopen(Director::baseFolder() . '/assets/testscript-test-file.pdf', "w");
- fwrite($fh, str_repeat('x',1000000));
- fclose($fh);
- }
- function tearDown() {
- parent::tearDown();
- $testFiles = array(
- '/assets/testscript-test-file.pdf',
- '/assets/renamed-test-file.pdf',
- '/assets/renamed-test-file-second-time.pdf',
- );
- foreach($testFiles as $file) {
- if(file_exists(Director::baseFolder().$file)) unlink(Director::baseFolder().$file);
- }
- }
-
- function testFileRenameUpdatesDraftAndPublishedPages() {
- $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());
- }
-
- function testFileLinkRewritingOnVirtualPages() {
- // 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());
- }
-
- function testLinkRewritingOnAPublishedPageDoesntMakeItEditedOnDraft() {
- // 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);
- }
-
- function testTwoFileRenamesInARowWork() {
- $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());
- }
-}
-
-?>
View
10 tests/FileLinkTrackingTest.yml
@@ -1,10 +0,0 @@
-# These need to come first so that SiteTree has the link meta-data written.
-File:
- file1:
- Filename: assets/testscript-test-file.pdf
-
-Page:
- page1:
- Title: page1
- URLSegment: page1
- Content: <p><img src="assets/testscript-test-file.pdf" /></p>

0 comments on commit d93025d

Please sign in to comment.