2015-02-10 03:32:44 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-15 23:31:47 +02:00
|
|
|
namespace SilverStripe\UserForms\Tests\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2021-11-01 21:52:58 +01:00
|
|
|
protected function setUp(): void
|
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
|
|
|
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->assertStringContainsString('Test label', $field->getFormField()->FieldHolder());
|
2017-05-18 00:31:18 +02:00
|
|
|
$this->assertEquals('Test label', $field->getFormField()->Title());
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$field->HideLabel = true;
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->assertStringNotContainsString('Test label', $field->getFormField()->FieldHolder());
|
2017-05-18 00:31:18 +02:00
|
|
|
$this->assertEmpty($field->getFormField()->Title());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLiteralFieldHasUpdateFormFieldMethodCalled()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$field = $this->getMockBuilder(EditableLiteralField::class)
|
2024-09-10 01:28:45 +02:00
|
|
|
->onlyMethods(array('doUpdateFormField'))
|
2017-05-18 00:31:18 +02:00
|
|
|
->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
|
|
|
}
|