2011-03-18 04:23:47 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
/**
|
|
|
|
* Tests {@see SiteTreeLinkTracking} broken links feature: LinkTracking
|
|
|
|
*/
|
2011-03-18 04:23:47 +01:00
|
|
|
class SiteTreeBacklinksTest extends SapphireTest {
|
2013-03-18 11:47:15 +01:00
|
|
|
protected static $fixture_file = "SiteTreeBacklinksTest.yml";
|
2011-03-18 04:23:47 +01:00
|
|
|
|
|
|
|
protected $requiredExtensions = array(
|
|
|
|
'SiteTree' => array('SiteTreeBacklinksTest_DOD'),
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function setUp() {
|
2011-03-18 04:23:47 +01:00
|
|
|
parent::setUp();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// Log in as admin so that we don't run into permission issues. That's not what we're
|
|
|
|
// testing here.
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testSavingPageWithLinkAddsBacklink() {
|
2011-03-18 04:23:47 +01:00
|
|
|
// load page 1
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert backlink to page 2 doesn't exist
|
|
|
|
$page2 = $this->objFromFixture('Page', 'page2');
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertNotContains($page2->ID, $page1->BackLinkTracking()->column('ID'), 'Assert backlink to page 2 doesn\'t exist');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// add hyperlink to page 1 on page 2
|
2012-07-12 04:51:19 +02:00
|
|
|
$page2->Content .= '<p><a href="[sitetree_link,id='.$page1->ID.']">Testing page 1 link</a></p>';
|
2011-03-18 04:23:47 +01:00
|
|
|
$page2->write();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// load page 1
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert backlink to page 2 exists
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertContains($page2->ID, $page1->BackLinkTracking()->column('ID'), 'Assert backlink to page 2 exists');
|
2011-03-18 04:23:47 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testRemovingLinkFromPageRemovesBacklink() {
|
2011-03-18 04:23:47 +01:00
|
|
|
// load page 1
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert backlink to page 3 exits
|
|
|
|
$page3 = $this->objFromFixture('Page', 'page3');
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertContains($page3->ID, $page1->BackLinkTracking()->column('ID'), 'Assert backlink to page 3 exists');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// remove hyperlink to page 1
|
|
|
|
$page3->Content = '<p>No links anymore!</p>';
|
|
|
|
$page3->write();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// load page 1
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert backlink to page 3 exists
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertNotContains($page3->ID, $page1->BackLinkTracking()->column('ID'), 'Assert backlink to page 3 doesn\'t exist');
|
2011-03-18 04:23:47 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testChangingUrlOnDraftSiteRewritesLink() {
|
2011-03-18 04:23:47 +01:00
|
|
|
// load page 1
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert backlink to page 3 exists
|
|
|
|
$page3 = $this->objFromFixture('Page', 'page3');
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertContains($page3->ID, $page1->BackLinkTracking()->column('ID'), 'Assert backlink to page 3 exists');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert hyperlink to page 1's current url exists on page 3
|
|
|
|
$links = HTTP::getLinksIn($page3->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'page1/', $links, 'Assert hyperlink to page 1\'s current url exists on page 3');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// change url of page 1
|
|
|
|
$page1->URLSegment = 'new-url-segment';
|
|
|
|
$page1->write();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// load page 3
|
|
|
|
$page3 = $this->objFromFixture('Page', 'page3');
|
|
|
|
|
|
|
|
// assert hyperlink to page 1's new url exists
|
|
|
|
$links = HTTP::getLinksIn($page3->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'new-url-segment/', $links, 'Assert hyperlink to page 1\'s new url exists on page 3');
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testChangingUrlOnLiveSiteRewritesLink() {
|
2016-01-26 06:38:42 +01:00
|
|
|
$this->markTestSkipped("Test disabled until versioned many_many implemented");
|
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// publish page 1 & 3
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
|
|
|
$page3 = $this->objFromFixture('Page', 'page3');
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page1->publishRecursive());
|
|
|
|
$this->assertTrue($page3->publishRecursive());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// load pages from live
|
|
|
|
$page1live = Versioned::get_one_by_stage('Page', 'Live', '"SiteTree"."ID" = ' . $page1->ID);
|
|
|
|
$page3live = Versioned::get_one_by_stage('Page', 'Live', '"SiteTree"."ID" = ' . $page3->ID);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert backlink to page 3 exists
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertContains($page3live->ID, $page1live->BackLinkTracking()->column('ID'), 'Assert backlink to page 3 exists');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert hyperlink to page 1's current url exists on page 3
|
|
|
|
$links = HTTP::getLinksIn($page3live->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'page1/', $links, 'Assert hyperlink to page 1\'s current url exists on page 3');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// change url of page 1
|
|
|
|
$page1live->URLSegment = 'new-url-segment';
|
|
|
|
$page1live->writeToStage('Live');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// load page 3 from live
|
|
|
|
$page3live = Versioned::get_one_by_stage('Page', 'Live', '"SiteTree"."ID" = ' . $page3->ID);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert hyperlink to page 1's new url exists
|
2016-03-17 01:02:50 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2011-03-18 04:23:47 +01:00
|
|
|
$links = HTTP::getLinksIn($page3live->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'new-url-segment/', $links, 'Assert hyperlink to page 1\'s new url exists on page 3');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testPublishingPageWithModifiedUrlRewritesLink() {
|
2016-01-26 06:38:42 +01:00
|
|
|
$this->markTestSkipped("Test disabled until versioned many_many implemented");
|
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// publish page 1 & 3
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
|
|
|
$page3 = $this->objFromFixture('Page', 'page3');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page1->publishRecursive());
|
|
|
|
$this->assertTrue($page3->publishRecursive());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// load page 3 from live
|
|
|
|
$page3live = Versioned::get_one_by_stage('Page', 'Live', '"SiteTree"."ID" = ' . $page3->ID);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert hyperlink to page 1's current url exists
|
|
|
|
$links = HTTP::getLinksIn($page3live->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'page1/', $links, 'Assert hyperlink to page 1\'s current url exists on page 3');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// rename url of page 1 on stage
|
|
|
|
$page1->URLSegment = 'new-url-segment';
|
|
|
|
$page1->write();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert hyperlink to page 1's current publish url exists
|
|
|
|
$page3live = Versioned::get_one_by_stage('Page', 'Live', '"SiteTree"."ID" = ' . $page3->ID);
|
2016-03-17 01:02:50 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2011-03-18 04:23:47 +01:00
|
|
|
$links = HTTP::getLinksIn($page3live->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'page1/', $links, 'Assert hyperlink to page 1\'s current published url exists on page 3');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// publish page 1
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page1->publishRecursive());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert hyperlink to page 1's new published url exists
|
|
|
|
$page3live = Versioned::get_one_by_stage('Page', 'Live', '"SiteTree"."ID" = ' . $page3->ID);
|
|
|
|
$links = HTTP::getLinksIn($page3live->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'new-url-segment/', $links, 'Assert hyperlink to page 1\'s new published url exists on page 3');
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testPublishingPageWithModifiedLinksRewritesLinks() {
|
2016-01-26 06:38:42 +01:00
|
|
|
$this->markTestSkipped("Test disabled until versioned many_many implemented");
|
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// publish page 1 & 3
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
|
|
|
$page3 = $this->objFromFixture('Page', 'page3');
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page1->publishRecursive());
|
|
|
|
$this->assertTrue($page3->publishRecursive());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert hyperlink to page 1's current url exists
|
|
|
|
$links = HTTP::getLinksIn($page3->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'page1/', $links, 'Assert hyperlink to page 1\'s current published url exists on page 3');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// change page 1 url on draft
|
|
|
|
$page1->URLSegment = 'new-url-segment';
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// save page 1
|
|
|
|
$page1->write();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert page 3 on draft contains new page 1 url
|
|
|
|
$page3 = $this->objFromFixture('Page', 'page3');
|
|
|
|
$links = HTTP::getLinksIn($page3->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'new-url-segment/', $links, 'Assert hyperlink to page 1\'s current draft url exists on page 3');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// publish page 3
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page3->publishRecursive());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert page 3 on published site contains old page 1 url
|
|
|
|
$page3live = Versioned::get_one_by_stage('Page', 'Live', '"SiteTree"."ID" = ' . $page3->ID);
|
2016-03-17 01:02:50 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2011-03-18 04:23:47 +01:00
|
|
|
$links = HTTP::getLinksIn($page3live->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'page1/', $links, 'Assert hyperlink to page 1\'s current published url exists on page 3');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// publish page 1
|
2016-04-01 05:17:37 +02:00
|
|
|
$this->assertTrue($page1->publishRecursive());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert page 3 on published site contains new page 1 url
|
|
|
|
$page3live = Versioned::get_one_by_stage('Page', 'Live', '"SiteTree"."ID" = ' . $page3->ID);
|
|
|
|
$links = HTTP::getLinksIn($page3live->obj('Content')->forTemplate());
|
|
|
|
$this->assertContains(Director::baseURL().'new-url-segment/', $links, 'Assert hyperlink to page 1\'s current published url exists on page 3');
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testLinkTrackingOnExtraContentFields() {
|
2011-03-18 04:23:47 +01:00
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
|
|
|
$page2 = $this->objFromFixture('Page', 'page2');
|
2016-04-01 05:17:37 +02:00
|
|
|
$page1->publishRecursive();
|
|
|
|
$page2->publishRecursive();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// assert backlink to page 2 doesn't exist
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertNotContains($page2->ID, $page1->BackLinkTracking()->column('ID'), 'Assert backlink to page 2 doesn\'t exist');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// add hyperlink to page 1 on page 2
|
2012-07-12 04:51:19 +02:00
|
|
|
$page2->ExtraContent .= '<p><a href="[sitetree_link,id='.$page1->ID.']">Testing page 1 link</a></p>';
|
2011-03-18 04:23:47 +01:00
|
|
|
$page2->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$page2->publishRecursive();
|
2011-03-18 04:23:47 +01:00
|
|
|
|
|
|
|
// assert backlink to page 2 exists
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertContains($page2->ID, $page1->BackLinkTracking()->column('ID'), 'Assert backlink to page 2 exists');
|
2011-03-18 04:23:47 +01:00
|
|
|
|
|
|
|
// update page1 url
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
|
|
|
$page1->URLSegment = "page1-new-url";
|
|
|
|
$page1->write();
|
|
|
|
|
|
|
|
// confirm that draft link on page2 has been rewritten
|
|
|
|
$page2 = $this->objFromFixture('Page', 'page2');
|
|
|
|
$this->assertEquals('<p><a href="'.Director::baseURL().'page1-new-url/">Testing page 1 link</a></p>', $page2->obj('ExtraContent')->forTemplate());
|
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
// @todo - Implement versioned many_many
|
|
|
|
$this->markTestSkipped("Test disabled until versioned many_many implemented");
|
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// confirm that published link hasn't
|
|
|
|
$page2Live = Versioned::get_one_by_stage("Page", "Live", "\"SiteTree\".\"ID\" = $page2->ID");
|
2016-03-17 01:02:50 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2011-03-18 04:23:47 +01:00
|
|
|
$this->assertEquals('<p><a href="'.Director::baseURL().'page1/">Testing page 1 link</a></p>', $page2Live->obj('ExtraContent')->forTemplate());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
// publish page1 and confirm that the link on the published page2 has now been updated
|
2016-04-01 05:17:37 +02:00
|
|
|
$page1->publishRecursive();
|
2011-03-18 04:23:47 +01:00
|
|
|
$page2Live = Versioned::get_one_by_stage("Page", "Live", "\"SiteTree\".\"ID\" = $page2->ID");
|
|
|
|
$this->assertEquals('<p><a href="'.Director::baseURL().'page1-new-url/">Testing page 1 link</a></p>', $page2Live->obj('ExtraContent')->forTemplate());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
|
|
|
|
// remove hyperlink to page 1
|
|
|
|
$page2->ExtraContent = '<p>No links anymore!</p>';
|
|
|
|
$page2->write();
|
|
|
|
|
|
|
|
// assert backlink to page 2 no longer exists
|
2011-05-03 12:26:04 +02:00
|
|
|
$this->assertNotContains($page2->ID, $page1->BackLinkTracking()->column('ID'), 'Assert backlink to page 2 has been removed');
|
2011-03-18 04:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:37:15 +02:00
|
|
|
class SiteTreeBacklinksTest_DOD extends DataExtension implements TestOnly {
|
2012-05-23 12:13:27 +02:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $db = array(
|
2012-05-23 12:13:27 +02:00
|
|
|
'ExtraContent' => 'HTMLText',
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function updateCMSFields(FieldList $fields) {
|
2011-04-15 06:35:19 +02:00
|
|
|
$fields->addFieldToTab("Root.Content", new HTMLEditorField("ExtraContent"));
|
2011-03-18 04:23:47 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-13 21:40:49 +01:00
|
|
|
|