2011-03-23 02:31:52 +01:00
|
|
|
<?php
|
2016-07-22 01:32:32 +02:00
|
|
|
|
2017-08-09 04:53:38 +02:00
|
|
|
namespace SilverStripe\CMS\Tests\Model;
|
2017-08-09 03:25:12 +02:00
|
|
|
|
2018-03-21 05:44:24 +01:00
|
|
|
use Page;
|
2018-04-06 05:53:57 +02:00
|
|
|
use SilverStripe\Assets\Dev\TestAssetStore;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Filesystem;
|
2018-03-21 05:44:24 +01:00
|
|
|
use SilverStripe\Assets\Folder;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2018-03-21 05:44:24 +01:00
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
2016-11-15 06:06:31 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
class SiteTreeHTMLEditorFieldTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'SiteTreeHTMLEditorFieldTest.yml';
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
TestAssetStore::activate('SiteTreeHTMLEditorFieldTest');
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
// Write file contents
|
2018-03-21 05:44:24 +01:00
|
|
|
$files = File::get()->exclude('ClassName', Folder::class);
|
2017-01-25 21:59:25 +01:00
|
|
|
foreach ($files as $file) {
|
|
|
|
$destPath = TestAssetStore::getLocalPath($file);
|
|
|
|
Filesystem::makeFolder(dirname($destPath));
|
|
|
|
file_put_contents($destPath, str_repeat('x', 1000000));
|
|
|
|
}
|
2018-03-21 05:44:24 +01:00
|
|
|
|
|
|
|
// Ensure all pages are published
|
|
|
|
/** @var Page $page */
|
|
|
|
foreach (Page::get() as $page) {
|
|
|
|
$page->publishSingle();
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
TestAssetStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLinkTracking()
|
|
|
|
{
|
2018-04-06 05:53:57 +02:00
|
|
|
/** @var SiteTree $sitetree */
|
2017-08-09 04:53:38 +02:00
|
|
|
$sitetree = $this->objFromFixture(SiteTree::class, 'home');
|
2017-01-25 21:59:25 +01:00
|
|
|
$editor = new HTMLEditorField('Content');
|
|
|
|
|
2017-08-09 04:53:38 +02:00
|
|
|
$aboutID = $this->idFromFixture(SiteTree::class, 'about');
|
|
|
|
$contactID = $this->idFromFixture(SiteTree::class, 'contact');
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
$editor->setValue("<a href=\"[sitetree_link,id=$aboutID]\">Example Link</a>");
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
2020-04-19 06:18:01 +02:00
|
|
|
$this->assertEquals([$aboutID => $aboutID], $sitetree->LinkTracking()->getIdList(), 'Basic link tracking works.');
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
$editor->setValue(
|
|
|
|
"<a href=\"[sitetree_link,id=$aboutID]\"></a><a href=\"[sitetree_link,id=$contactID]\"></a>"
|
|
|
|
);
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
|
|
|
$this->assertEquals(
|
2020-04-19 06:18:01 +02:00
|
|
|
[$aboutID => $aboutID, $contactID => $contactID],
|
2017-01-25 21:59:25 +01:00
|
|
|
$sitetree->LinkTracking()->getIdList(),
|
|
|
|
'Tracking works on multiple links'
|
|
|
|
);
|
|
|
|
|
|
|
|
$editor->setValue(null);
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
2020-04-19 06:18:01 +02:00
|
|
|
$this->assertEquals([], $sitetree->LinkTracking()->getIdList(), 'Link tracking is removed when links are.');
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
// Legacy support - old CMS versions added link shortcodes with spaces instead of commas
|
|
|
|
$editor->setValue("<a href=\"[sitetree_link id=$aboutID]\">Example Link</a>");
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
|
|
|
$this->assertEquals(
|
2020-04-19 06:18:01 +02:00
|
|
|
[$aboutID => $aboutID],
|
2017-01-25 21:59:25 +01:00
|
|
|
$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('<img src="assets/example.jpg" />');
|
|
|
|
$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('<img src="assets/example.jpg" alt="foo" title="bar" />');
|
|
|
|
$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('<p><a href="[sitetree_link,id=0]">Broken Link</a></p>');
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
|
|
|
|
|
|
|
$this->assertTrue($sitetree->HasBrokenLink);
|
|
|
|
|
|
|
|
$editor->setValue(sprintf(
|
|
|
|
'<p><a href="[sitetree_link,id=%d]">Working Link</a></p>',
|
2017-08-09 04:53:38 +02:00
|
|
|
$this->idFromFixture(SiteTree::class, 'home')
|
2017-01-25 21:59:25 +01:00
|
|
|
));
|
|
|
|
$sitetree->HasBrokenLink = false;
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
|
|
|
|
|
|
|
$this->assertFalse((bool) $sitetree->HasBrokenLink);
|
|
|
|
}
|
2011-03-23 02:31:52 +01:00
|
|
|
}
|