2015-02-10 03:32:44 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Test\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\CompositeField;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
|
2017-08-13 23:26:53 +02:00
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableLiteralField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2015-02-10 03:32:44 +01:00
|
|
|
/**
|
|
|
|
* Tests the {@see EditableLiteralField} class
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableLiteralFieldTest extends SapphireTest
|
|
|
|
{
|
2017-08-11 02:20:12 +02:00
|
|
|
protected function setUp()
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2017-08-13 23:26:53 +02:00
|
|
|
$cmsConfig = HTMLEditorConfig::get('cms');
|
|
|
|
HTMLEditorConfig::set_active($cmsConfig);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the sanitisation of HTML content
|
|
|
|
*/
|
|
|
|
public function testSanitisation()
|
|
|
|
{
|
|
|
|
$rawContent = '<h1>Welcome</h1><script>alert("Hello!");</script><p>Giant Robots!</p>';
|
|
|
|
$safeContent = '<h1>Welcome</h1><p>Giant Robots!</p>';
|
|
|
|
$field = new EditableLiteralField();
|
|
|
|
|
|
|
|
// Test with sanitisation enabled
|
2017-08-13 23:26:53 +02:00
|
|
|
Config::modify()->set(HTMLEditorField::class, 'sanitise_server_side', true);
|
2016-07-21 07:53:59 +02:00
|
|
|
$field->setContent($rawContent);
|
|
|
|
$this->assertEquals($safeContent, $field->getContent());
|
|
|
|
|
|
|
|
// Test with sanitisation disabled
|
2017-08-13 23:26:53 +02:00
|
|
|
Config::modify()->remove(HTMLEditorField::class, 'sanitise_server_side');
|
2016-07-21 07:53:59 +02:00
|
|
|
$field->setContent($rawContent);
|
|
|
|
$this->assertEquals($rawContent, $field->getContent());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHideLabel()
|
|
|
|
{
|
2017-08-11 02:20:12 +02:00
|
|
|
$field = new EditableLiteralField([
|
2016-07-21 07:53:59 +02:00
|
|
|
'Title' => 'Test label'
|
2017-08-11 02:20:12 +02:00
|
|
|
]);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-05-18 00:31:18 +02:00
|
|
|
$this->assertContains('Test label', $field->getFormField()->FieldHolder());
|
|
|
|
$this->assertEquals('Test label', $field->getFormField()->Title());
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$field->HideLabel = true;
|
2017-05-18 00:31:18 +02:00
|
|
|
$this->assertNotContains('Test label', $field->getFormField()->FieldHolder());
|
|
|
|
$this->assertEmpty($field->getFormField()->Title());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLiteralFieldHasUpdateFormFieldMethodCalled()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$field = $this->getMockBuilder(EditableLiteralField::class)
|
2017-05-18 00:31:18 +02:00
|
|
|
->setMethods(array('doUpdateFormField'))
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$field->expects($this->once())->method('doUpdateFormField');
|
|
|
|
|
|
|
|
$field->getFormField();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LiteralFields do not allow field names, etc. Instead, the field is contained within a composite field. This
|
|
|
|
* test ensures that this structure is correct.
|
|
|
|
*/
|
|
|
|
public function testLiteralFieldIsContainedWithinCompositeField()
|
|
|
|
{
|
|
|
|
$field = new EditableLiteralField;
|
|
|
|
$formField = $field->getFormField();
|
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$this->assertInstanceOf(
|
|
|
|
CompositeField::class,
|
|
|
|
$formField,
|
|
|
|
'Literal field is contained within a composite field'
|
|
|
|
);
|
2017-05-18 00:31:18 +02:00
|
|
|
$this->assertInstanceOf(
|
2017-08-09 01:55:09 +02:00
|
|
|
LiteralField::class,
|
2017-05-18 00:31:18 +02:00
|
|
|
$formField->FieldList()->first(),
|
|
|
|
'Actual literal field exists in composite field children'
|
|
|
|
);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-02-10 03:32:44 +01:00
|
|
|
}
|