2016-05-03 04:16:05 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests\HTMLEditor;
|
|
|
|
|
|
|
|
use SilverStripe;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Image;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField_Toolbar;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\HTMLEditor\HTMLEditorFieldToolbarTest\Toolbar;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
class HTMLEditorFieldToolbarTest extends SapphireTest
|
|
|
|
{
|
2016-05-03 04:16:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected static $fixture_file = 'HTMLEditorFieldToolbarTest.yml';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Toolbar
|
|
|
|
*/
|
|
|
|
protected function getToolbar()
|
|
|
|
{
|
|
|
|
return new Toolbar(null, '/');
|
|
|
|
}
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
HTMLEditorField_Toolbar::config()->set('fileurl_scheme_whitelist', array('http'));
|
|
|
|
HTMLEditorField_Toolbar::config()->set('fileurl_domain_whitelist', array('example.com'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Filesystem mock
|
|
|
|
TestAssetStore::activate(__CLASS__);
|
|
|
|
|
|
|
|
// Load up files
|
2017-02-22 04:15:08 +01:00
|
|
|
/** @var File $file1 */
|
2016-12-16 05:34:21 +01:00
|
|
|
$file1 = $this->objFromFixture(File::class, 'example_file');
|
|
|
|
$file1->setFromString(str_repeat('x', 1000), $file1->Name);
|
|
|
|
$file1->write();
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
/** @var Image $image1 */
|
2016-12-16 05:34:21 +01:00
|
|
|
$image1 = $this->objFromFixture(Image::class, 'example_image');
|
|
|
|
$image1->setFromLocalFile(
|
|
|
|
__DIR__ . '/HTMLEditorFieldTest/images/example.jpg',
|
|
|
|
'folder/subfolder/example.jpg'
|
|
|
|
);
|
|
|
|
$image1->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidLocalReference()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
/** @var File $exampleFile */
|
2016-12-16 05:34:21 +01:00
|
|
|
$exampleFile = $this->objFromFixture(File::class, 'example_file');
|
|
|
|
$expectedUrl = $exampleFile->AbsoluteLink();
|
2017-02-22 04:15:08 +01:00
|
|
|
HTMLEditorField_Toolbar::config()->set(
|
2016-12-16 05:34:21 +01:00
|
|
|
'fileurl_domain_whitelist',
|
2017-02-22 04:15:08 +01:00
|
|
|
[
|
|
|
|
'example.com',
|
|
|
|
strtolower(parse_url($expectedUrl, PHP_URL_HOST))
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
list(, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL($exampleFile->AbsoluteLink());
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expectedUrl, $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidScheme()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
list(, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('http://example.com/test.pdf');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($url, 'http://example.com/test.pdf');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidScheme()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
$this->setExpectedException(HTTPResponse_Exception::class);
|
|
|
|
$this->getToolbar()->viewfile_getRemoteFileByURL('nosuchscheme://example.com/test.pdf');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidDomain()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
list(, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('http://example.com/test.pdf');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($url, 'http://example.com/test.pdf');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidDomain()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
$this->setExpectedException(HTTPResponse_Exception::class);
|
|
|
|
$this->getToolbar()->viewfile_getRemoteFileByURL('http://evil.com/test.pdf');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-05-03 04:16:05 +02:00
|
|
|
}
|