2014-08-12 06:39:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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>'));
|
|
|
|
|
|
|
|
$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() {
|
|
|
|
$this->assertTrue($this->pageIsBrokenFile('<img src="someurl.jpg" data-fileid="99999999" />'));
|
|
|
|
$this->assertFalse($this->pageIsBrokenFile('<img src="someurl.jpg" />'));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function pageIsBrokenFile($content) {
|
|
|
|
$page = new Page();
|
|
|
|
$page->Content = $content;
|
|
|
|
$page->write();
|
|
|
|
return $page->HasBrokenFile;
|
|
|
|
}
|
|
|
|
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|