silverstripe-framework/tests/php/Forms/HTMLEditor/HTMLEditorFieldToolbarTest.php

91 lines
2.7 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\Forms\Tests\HTMLEditor;
use SilverStripe;
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;
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-10-14 03:30:05 +02:00
class HTMLEditorFieldToolbarTest extends SapphireTest
{
protected static $fixture_file = 'HTMLEditorFieldToolbarTest.yml';
/**
2016-10-14 03:30:05 +02:00
* @return Toolbar
*/
2016-10-14 03:30:05 +02:00
protected function getToolbar()
{
return new Toolbar(null, '/');
}
2016-10-14 03:30:05 +02:00
public function setUp()
{
parent::setUp();
HTMLEditorField_Toolbar::config()->update('fileurl_scheme_whitelist', array('http'));
HTMLEditorField_Toolbar::config()->update('fileurl_domain_whitelist', array('example.com'));
// Filesystem mock
2016-10-14 03:30:05 +02:00
TestAssetStore::activate(__CLASS__);
// Load up files
/** @var File $file1 */
2016-10-14 03:30:05 +02:00
$file1 = $this->objFromFixture(File::class, 'example_file');
$file1->setFromString(str_repeat('x', 1000), $file1->Name);
$file1->write();
/** @var Image $image1 */
2016-10-14 03:30:05 +02:00
$image1 = $this->objFromFixture(Image::class, 'example_image');
$image1->setFromLocalFile(
2016-10-14 03:30:05 +02:00
__DIR__ . '/HTMLEditorFieldTest/images/example.jpg',
'folder/subfolder/example.jpg'
);
$image1->write();
}
2016-10-14 03:30:05 +02:00
public function testValidLocalReference()
{
/** @var File $exampleFile */
2016-10-14 03:30:05 +02:00
$exampleFile = $this->objFromFixture(File::class, 'example_file');
$expectedUrl = $exampleFile->AbsoluteLink();
HTMLEditorField_Toolbar::config()->update('fileurl_domain_whitelist', array(
'example.com',
strtolower(parse_url($expectedUrl, PHP_URL_HOST))
));
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL($exampleFile->AbsoluteLink());
$this->assertEquals($expectedUrl, $url);
}
2016-10-14 03:30:05 +02:00
public function testValidScheme()
{
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('http://example.com/test.pdf');
$this->assertEquals($url, 'http://example.com/test.pdf');
}
2016-09-09 08:43:05 +02:00
/** @expectedException SilverStripe\Control\HTTPResponse_Exception */
2016-10-14 03:30:05 +02:00
public function testInvalidScheme()
{
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('nosuchscheme://example.com/test.pdf');
}
2016-10-14 03:30:05 +02:00
public function testValidDomain()
{
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('http://example.com/test.pdf');
$this->assertEquals($url, 'http://example.com/test.pdf');
}
2016-09-09 08:43:05 +02:00
/** @expectedException SilverStripe\Control\HTTPResponse_Exception */
2016-10-14 03:30:05 +02:00
public function testInvalidDomain()
{
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('http://evil.com/test.pdf');
}
}