2015-02-10 03:32:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the {@see EditableLiteralField} class
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableLiteralFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
HtmlEditorConfig::set_active('cms');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
Config::inst()->update('HtmlEditorField', 'sanitise_server_side', true);
|
|
|
|
$field->setContent($rawContent);
|
|
|
|
$this->assertEquals($safeContent, $field->getContent());
|
|
|
|
|
|
|
|
// Test with sanitisation disabled
|
|
|
|
Config::inst()->remove('HtmlEditorField', 'sanitise_server_side');
|
|
|
|
$field->setContent($rawContent);
|
|
|
|
$this->assertEquals($rawContent, $field->getContent());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHideLabel()
|
|
|
|
{
|
|
|
|
$field = new EditableLiteralField(array(
|
|
|
|
'Title' => 'Test label'
|
|
|
|
));
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$field = $this->getMockBuilder('EditableLiteralField')
|
|
|
|
->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();
|
|
|
|
|
|
|
|
$this->assertInstanceOf('CompositeField', $formField, 'Literal field is contained within a composite field');
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
'LiteralField',
|
|
|
|
$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
|
|
|
}
|