2011-03-23 02:31:52 +01:00
|
|
|
<?php
|
2016-07-22 01:32:32 +02:00
|
|
|
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Filesystem;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2016-11-15 06:06:31 +01:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
|
|
|
|
2016-08-23 04:36:06 +02:00
|
|
|
|
2016-05-06 04:31:19 +02:00
|
|
|
class SiteTreeHTMLEditorFieldTest extends FunctionalTest {
|
|
|
|
protected static $fixture_file = 'SiteTreeHTMLEditorFieldTest.yml';
|
2011-03-23 02:31:52 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
protected static $use_draft_site = true;
|
2011-03-23 02:31:52 +01:00
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2016-11-15 06:06:31 +01:00
|
|
|
TestAssetStore::activate('SiteTreeHTMLEditorFieldTest');
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
// Write file contents
|
2016-08-23 04:36:06 +02:00
|
|
|
$files = File::get()->exclude('ClassName', 'SilverStripe\\Assets\\Folder');
|
2015-10-15 00:08:52 +02:00
|
|
|
foreach($files as $file) {
|
2016-11-15 06:06:31 +01:00
|
|
|
$destPath = TestAssetStore::getLocalPath($file);
|
2015-10-15 00:08:52 +02:00
|
|
|
Filesystem::makeFolder(dirname($destPath));
|
|
|
|
file_put_contents($destPath, str_repeat('x', 1000000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2016-11-15 06:06:31 +01:00
|
|
|
TestAssetStore::reset();
|
2015-10-15 00:08:52 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2011-03-23 02:31:52 +01:00
|
|
|
public function testLinkTracking() {
|
2016-07-22 01:32:32 +02:00
|
|
|
$sitetree = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'home');
|
2016-05-06 04:31:19 +02:00
|
|
|
$editor = new HTMLEditorField('Content');
|
2011-03-23 02:31:52 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$aboutID = $this->idFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'about');
|
|
|
|
$contactID = $this->idFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'contact');
|
2011-03-23 02:31:52 +01:00
|
|
|
|
2012-07-12 04:51:19 +02:00
|
|
|
$editor->setValue("<a href=\"[sitetree_link,id=$aboutID]\">Example Link</a>");
|
2011-03-23 02:31:52 +01:00
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2011-03-23 02:31:52 +01:00
|
|
|
$this->assertEquals(array($aboutID => $aboutID), $sitetree->LinkTracking()->getIdList(), 'Basic link tracking works.');
|
|
|
|
|
|
|
|
$editor->setValue (
|
2012-07-12 04:51:19 +02:00
|
|
|
"<a href=\"[sitetree_link,id=$aboutID]\"></a><a href=\"[sitetree_link,id=$contactID]\"></a>"
|
2011-03-23 02:31:52 +01:00
|
|
|
);
|
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2011-03-23 02:31:52 +01:00
|
|
|
$this->assertEquals (
|
|
|
|
array($aboutID => $aboutID, $contactID => $contactID),
|
|
|
|
$sitetree->LinkTracking()->getIdList(),
|
|
|
|
'Tracking works on multiple links'
|
|
|
|
);
|
|
|
|
|
|
|
|
$editor->setValue(null);
|
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2011-03-23 02:31:52 +01:00
|
|
|
$this->assertEquals(array(), $sitetree->LinkTracking()->getIdList(), 'Link tracking is removed when links are.');
|
2014-04-16 23:05:18 +02: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(
|
|
|
|
array($aboutID => $aboutID),
|
|
|
|
$sitetree->LinkTracking()->getIdList(),
|
|
|
|
'Link tracking with space instead of comma in shortcode works.'
|
|
|
|
);
|
2011-03-23 02:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileLinkTracking() {
|
2016-07-22 01:32:32 +02:00
|
|
|
$sitetree = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'home');
|
2016-05-06 04:31:19 +02:00
|
|
|
$editor = new HTMLEditorField('Content');
|
2016-08-23 04:36:06 +02:00
|
|
|
$fileID = $this->idFromFixture('SilverStripe\\Assets\\File', 'example_file');
|
2011-03-23 02:31:52 +01:00
|
|
|
|
2014-04-16 23:05:18 +02:00
|
|
|
$editor->setValue(sprintf(
|
|
|
|
'<p><a href="[file_link,id=%d]">Example File</a></p>',
|
|
|
|
$fileID
|
|
|
|
));
|
2011-03-23 02:31:52 +01:00
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2011-03-23 02:31:52 +01:00
|
|
|
$this->assertEquals (
|
|
|
|
array($fileID => $fileID), $sitetree->ImageTracking()->getIDList(), 'Links to assets are tracked.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$editor->setValue(null);
|
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2011-03-23 02:31:52 +01:00
|
|
|
$this->assertEquals(array(), $sitetree->ImageTracking()->getIdList(), 'Asset tracking is removed with links.');
|
2014-04-16 23:05:18 +02:00
|
|
|
|
|
|
|
// Legacy support - old CMS versions added link shortcodes with spaces instead of commas
|
|
|
|
$editor->setValue(sprintf(
|
|
|
|
'<p><a href="[file_link id=%d]">Example File</a></p>',
|
|
|
|
$fileID
|
|
|
|
));
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
|
|
|
$this->assertEquals(
|
|
|
|
array($fileID => $fileID),
|
|
|
|
$sitetree->ImageTracking()->getIDList(),
|
|
|
|
'Link tracking with space instead of comma in shortcode works.'
|
|
|
|
);
|
2011-03-23 02:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testImageInsertion() {
|
|
|
|
$sitetree = new SiteTree();
|
2016-05-06 04:31:19 +02:00
|
|
|
$editor = new HTMLEditorField('Content');
|
2011-03-23 02:31:52 +01:00
|
|
|
|
|
|
|
$editor->setValue('<img src="assets/example.jpg" />');
|
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2011-03-23 02:31:52 +01:00
|
|
|
|
2012-12-17 17:32:46 +01:00
|
|
|
$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);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2012-12-17 17:32:46 +01:00
|
|
|
|
|
|
|
$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.');
|
2011-03-23 02:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testImageTracking() {
|
2016-07-22 01:32:32 +02:00
|
|
|
$sitetree = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'home');
|
2016-05-06 04:31:19 +02:00
|
|
|
$editor = new HTMLEditorField('Content');
|
2016-08-23 04:36:06 +02:00
|
|
|
$file = $this->objFromFixture('SilverStripe\\Assets\\Image', 'example_image');
|
2011-03-23 02:31:52 +01:00
|
|
|
|
2016-03-09 23:57:39 +01:00
|
|
|
$editor->setValue(sprintf('[image src="%s" id="%d"]', $file->getURL(), $file->ID));
|
2011-03-23 02:31:52 +01:00
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array($file->ID => $file->ID),
|
|
|
|
$sitetree->ImageTracking()->getIDList(),
|
|
|
|
'Inserted images are tracked.'
|
2011-03-23 02:31:52 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$editor->setValue(null);
|
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->assertEmpty(
|
|
|
|
$sitetree->ImageTracking()->getIDList(),
|
|
|
|
'Tracked images are deleted when removed.'
|
2011-03-23 02:31:52 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-04-16 23:05:18 +02:00
|
|
|
public function testBrokenSiteTreeLinkTracking() {
|
2011-03-23 02:31:52 +01:00
|
|
|
$sitetree = new SiteTree();
|
2016-05-06 04:31:19 +02:00
|
|
|
$editor = new HTMLEditorField('Content');
|
2011-03-23 02:31:52 +01:00
|
|
|
|
|
|
|
$this->assertFalse((bool) $sitetree->HasBrokenLink);
|
|
|
|
|
2012-07-12 04:51:19 +02:00
|
|
|
$editor->setValue('<p><a href="[sitetree_link,id=0]">Broken Link</a></p>');
|
2011-03-23 02:31:52 +01:00
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2011-03-23 02:31:52 +01:00
|
|
|
|
|
|
|
$this->assertTrue($sitetree->HasBrokenLink);
|
|
|
|
|
|
|
|
$editor->setValue(sprintf (
|
2012-07-12 04:51:19 +02:00
|
|
|
'<p><a href="[sitetree_link,id=%d]">Working Link</a></p>',
|
2016-07-22 01:32:32 +02:00
|
|
|
$this->idFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'home')
|
2011-03-23 02:31:52 +01:00
|
|
|
));
|
|
|
|
$sitetree->HasBrokenLink = false;
|
|
|
|
$editor->saveInto($sitetree);
|
2013-08-07 01:03:15 +02:00
|
|
|
$sitetree->write();
|
2011-03-23 02:31:52 +01:00
|
|
|
|
|
|
|
$this->assertFalse((bool) $sitetree->HasBrokenLink);
|
|
|
|
}
|
|
|
|
|
2014-04-16 23:05:18 +02:00
|
|
|
public function testBrokenFileLinkTracking() {
|
|
|
|
$sitetree = new SiteTree();
|
2016-05-06 04:31:19 +02:00
|
|
|
$editor = new HTMLEditorField('Content');
|
2014-04-16 23:05:18 +02:00
|
|
|
|
|
|
|
$this->assertFalse((bool) $sitetree->HasBrokenFile);
|
|
|
|
|
|
|
|
$editor->setValue('<p><a href="[file_link,id=0]">Broken Link</a></p>');
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
|
|
|
|
|
|
|
$this->assertTrue($sitetree->HasBrokenFile);
|
|
|
|
|
|
|
|
$editor->setValue(sprintf (
|
|
|
|
'<p><a href="[file_link,id=%d]">Working Link</a></p>',
|
2016-08-23 04:36:06 +02:00
|
|
|
$this->idFromFixture('SilverStripe\\Assets\\File', 'example_file')
|
2014-04-16 23:05:18 +02:00
|
|
|
));
|
|
|
|
$sitetree->HasBrokenFile = false;
|
|
|
|
$editor->saveInto($sitetree);
|
|
|
|
$sitetree->write();
|
|
|
|
|
|
|
|
$this->assertFalse((bool) $sitetree->HasBrokenFile);
|
|
|
|
}
|
|
|
|
|
2011-03-23 02:31:52 +01:00
|
|
|
}
|