2014-08-12 06:39:11 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTreeLinkTracking_Parser;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
|
2014-08-12 06:39:11 +02:00
|
|
|
class SiteTreeLinkTrackingTest extends SapphireTest {
|
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
protected function isBroken($content) {
|
2014-08-12 06:39:11 +02:00
|
|
|
$parser = new SiteTreeLinkTracking_Parser();
|
|
|
|
$htmlValue = Injector::inst()->create('HTMLValue', $content);
|
|
|
|
$links = $parser->process($htmlValue);
|
|
|
|
|
|
|
|
if (empty($links[0])) return false;
|
|
|
|
return $links[0]['Broken'];
|
|
|
|
}
|
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
public function testParser() {
|
2014-08-12 06:39:11 +02:00
|
|
|
$this->assertTrue($this->isBroken('<a href="[sitetree_link,id=123]">link</a>'));
|
|
|
|
$this->assertTrue($this->isBroken('<a href="[sitetree_link,id=123]#no-such-anchor">link</a>'));
|
|
|
|
$this->assertTrue($this->isBroken('<a href="[file_link,id=123]">link</a>'));
|
|
|
|
$this->assertTrue($this->isBroken('<a href="">link</a>'));
|
|
|
|
$this->assertTrue($this->isBroken('<a href="/">link</a>'));
|
|
|
|
|
|
|
|
$this->assertFalse($this->isBroken('<a name="anchor">anchor</a>'));
|
|
|
|
$this->assertFalse($this->isBroken('<a id="anchor">anchor</a>'));
|
2016-03-21 16:22:00 +01:00
|
|
|
$this->assertTrue($this->isBroken('<a href="##anchor">anchor</a>'));
|
2014-08-12 06:39:11 +02:00
|
|
|
|
|
|
|
$page = new Page();
|
|
|
|
$page->Content = '<a name="yes-name-anchor">name</a><a id="yes-id-anchor">id</a>';
|
|
|
|
$page->write();
|
|
|
|
|
|
|
|
$file = new File();
|
|
|
|
$file->write();
|
|
|
|
|
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]\">link</a>"));
|
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#yes-name-anchor\">link</a>"));
|
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#yes-id-anchor\">link</a>"));
|
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[file_link,id=$file->ID]\">link</a>"));
|
2016-01-11 15:52:31 +01:00
|
|
|
$this->assertTrue($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#http://invalid-anchor.com\"></a>"));
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
protected function highlight($content) {
|
2014-08-15 02:04:48 +02:00
|
|
|
$page = new Page();
|
|
|
|
$page->Content = $content;
|
|
|
|
$page->write();
|
|
|
|
return $page->Content;
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
public function testHighlighter() {
|
2014-08-12 06:39:11 +02:00
|
|
|
$content = $this->highlight('<a href="[sitetree_link,id=123]" class="existing-class">link</a>');
|
|
|
|
$this->assertEquals(substr_count($content, 'ss-broken'), 1, 'A ss-broken class is added to the broken link.');
|
|
|
|
$this->assertEquals(substr_count($content, 'existing-class'), 1, 'Existing class is not removed.');
|
|
|
|
|
|
|
|
$content = $this->highlight('<a href="[sitetree_link,id=123]">link</a>');
|
|
|
|
$this->assertEquals(substr_count($content, 'ss-broken'), 1, 'ss-broken class is added to the broken link.');
|
|
|
|
|
2014-08-15 02:04:48 +02:00
|
|
|
$otherPage = new Page();
|
|
|
|
$otherPage->Content = '';
|
|
|
|
$otherPage->write();
|
2014-08-12 06:39:11 +02:00
|
|
|
|
|
|
|
$content = $this->highlight(
|
2014-08-15 02:04:48 +02:00
|
|
|
"<a href=\"[sitetree_link,id=$otherPage->ID]\" class=\"existing-class ss-broken ss-broken\">link</a>"
|
2014-08-12 06:39:11 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(substr_count($content, 'ss-broken'), 0, 'All ss-broken classes are removed from good link');
|
|
|
|
$this->assertEquals(substr_count($content, 'existing-class'), 1, 'Existing class is not removed.');
|
|
|
|
}
|
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
public function testHasBrokenFile() {
|
2016-03-09 23:57:39 +01:00
|
|
|
$this->assertTrue($this->pageIsBrokenFile('[image src="someurl.jpg" id="99999999"]'));
|
|
|
|
$this->assertFalse($this->pageIsBrokenFile('[image src="someurl.jpg"]'));
|
2015-10-15 00:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function pageIsBrokenFile($content) {
|
|
|
|
$page = new Page();
|
|
|
|
$page->Content = $content;
|
|
|
|
$page->write();
|
|
|
|
return $page->HasBrokenFile;
|
|
|
|
}
|
|
|
|
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|