logInWithPermission('ADMIN'); // Write file contents $files = File::get()->exclude('ClassName', Folder::class); foreach ($files as $file) { $destPath = TestAssetStore::getLocalPath($file); Filesystem::makeFolder(dirname($destPath ?? '')); file_put_contents($destPath ?? '', str_repeat('x', 1000000)); } // Ensure all pages are published /** @var Page $page */ foreach (Page::get() as $page) { $page->publishSingle(); } } protected function tearDown(): void { TestAssetStore::reset(); parent::tearDown(); } public function testLinkTracking() { /** @var SiteTree $sitetree */ $sitetree = $this->objFromFixture(SiteTree::class, 'home'); $editor = new HTMLEditorField('Content'); $aboutID = $this->idFromFixture(SiteTree::class, 'about'); $contactID = $this->idFromFixture(SiteTree::class, 'contact'); $editor->setValue("Example Link"); $editor->saveInto($sitetree); $sitetree->write(); $this->assertEquals([$aboutID => $aboutID], $sitetree->LinkTracking()->getIdList(), 'Basic link tracking works.'); $editor->setValue( "" ); $editor->saveInto($sitetree); $sitetree->write(); $this->assertEquals( [$aboutID => $aboutID, $contactID => $contactID], $sitetree->LinkTracking()->getIdList(), 'Tracking works on multiple links' ); $editor->setValue(null); $editor->saveInto($sitetree); $sitetree->write(); $this->assertEquals([], $sitetree->LinkTracking()->getIdList(), 'Link tracking is removed when links are.'); // Legacy support - old CMS versions added link shortcodes with spaces instead of commas $editor->setValue("Example Link"); $editor->saveInto($sitetree); $sitetree->write(); $this->assertEquals( [$aboutID => $aboutID], $sitetree->LinkTracking()->getIdList(), 'Link tracking with space instead of comma in shortcode works.' ); } public function testImageInsertion() { $sitetree = new SiteTree(); $editor = new HTMLEditorField('Content'); $editor->setValue(''); $editor->saveInto($sitetree); $sitetree->write(); $parser = new CSSContentParser($sitetree->Content); $xml = $parser->getByXpath('//img'); $this->assertEquals('', (string)$xml[0]['alt'], 'Alt tags are added by default.'); $this->assertEquals('', (string)$xml[0]['title'], 'Title tags are added by default.'); $editor->setValue(''); $editor->saveInto($sitetree); $sitetree->write(); $parser = new CSSContentParser($sitetree->Content); $xml = $parser->getByXpath('//img'); $this->assertEquals('foo', (string)$xml[0]['alt'], 'Alt tags are preserved.'); $this->assertEquals('bar', (string)$xml[0]['title'], 'Title tags are preserved.'); } public function testBrokenSiteTreeLinkTracking() { $sitetree = new SiteTree(); $editor = new HTMLEditorField('Content'); $this->assertFalse((bool) $sitetree->HasBrokenLink); $editor->setValue('
'); $editor->saveInto($sitetree); $sitetree->write(); $this->assertTrue($sitetree->HasBrokenLink); $editor->setValue(sprintf( '', $this->idFromFixture(SiteTree::class, 'home') )); $sitetree->HasBrokenLink = false; $editor->saveInto($sitetree); $sitetree->write(); $this->assertFalse((bool) $sitetree->HasBrokenLink); } }