set('alternate_base_url', 'http://www.mysite.com/'); } protected function isBroken($content) { $parser = new SiteTreeLinkTracking_Parser(); $htmlValue = HTMLValue::create($content); $links = $parser->process($htmlValue); if (empty($links[0])) { return false; } return $links[0]['Broken']; } public function testParser() { SiteTree::add_extension(Page::class, SiteTreeLinkTracking_Extension::class); // Shortcodes $this->assertTrue($this->isBroken('link')); $this->assertTrue($this->isBroken('link')); // Relative urls $this->assertTrue($this->isBroken('link')); $this->assertTrue($this->isBroken('link')); // Non-shortcodes, assume non-broken without due reason $this->assertFalse($this->isBroken('link')); $this->assertFalse($this->isBroken('link')); // Absolute urls $this->assertFalse($this->isBroken('link')); $this->assertFalse($this->isBroken('link')); // Anchors $this->assertFalse($this->isBroken('anchor')); $this->assertFalse($this->isBroken('anchor')); $this->assertTrue($this->isBroken('anchor')); $page = new Page(); $page->Content = 'nameid'; $page->write(); $this->assertFalse($this->isBroken("ID]\">link")); $this->assertFalse($this->isBroken("ID]#yes-name-anchor\">link")); $this->assertFalse($this->isBroken("ID]#yes-id-anchor\">link")); $this->assertTrue($this->isBroken("ID]#http://invalid-anchor.com\">")); // Anchors Via updateAnchorsOnPage Extension $this->assertFalse($this->isBroken("ID]#extension-anchor\">link")); $this->assertTrue($this->isBroken("ID]#no-such-anchor\">")); } protected function highlight($content) { $page = new Page(); $page->Content = $content; $page->write(); return $page->Content; } public function testHighlighter() { $content = $this->highlight('link'); $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('link'); $this->assertEquals(substr_count($content, 'ss-broken'), 1, 'ss-broken class is added to the broken link.'); $otherPage = new Page(); $otherPage->Content = ''; $otherPage->write(); $content = $this->highlight( "ID]\" class=\"existing-class ss-broken ss-broken\">link" ); $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.'); } }