|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Tests\Model\EditableFormField; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
|
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
7
|
|
|
use SilverStripe\Forms\CompositeField; |
|
8
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig; |
|
9
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField; |
|
10
|
|
|
use SilverStripe\Forms\LiteralField; |
|
11
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableLiteralField; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Tests the {@see EditableLiteralField} class |
|
15
|
|
|
*/ |
|
16
|
|
|
class EditableLiteralFieldTest extends SapphireTest |
|
17
|
|
|
{ |
|
18
|
|
|
protected function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
parent::setUp(); |
|
21
|
|
|
$cmsConfig = HTMLEditorConfig::get('cms'); |
|
22
|
|
|
HTMLEditorConfig::set_active($cmsConfig); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Tests the sanitisation of HTML content |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testSanitisation() |
|
29
|
|
|
{ |
|
30
|
|
|
$rawContent = '<h1>Welcome</h1><script>alert("Hello!");</script><p>Giant Robots!</p>'; |
|
31
|
|
|
$safeContent = '<h1>Welcome</h1><p>Giant Robots!</p>'; |
|
32
|
|
|
$field = new EditableLiteralField(); |
|
33
|
|
|
|
|
34
|
|
|
// Test with sanitisation enabled |
|
35
|
|
|
Config::modify()->set(HTMLEditorField::class, 'sanitise_server_side', true); |
|
36
|
|
|
$field->setContent($rawContent); |
|
37
|
|
|
$this->assertEquals($safeContent, $field->getContent()); |
|
38
|
|
|
|
|
39
|
|
|
// Test with sanitisation disabled |
|
40
|
|
|
Config::modify()->remove(HTMLEditorField::class, 'sanitise_server_side'); |
|
41
|
|
|
$field->setContent($rawContent); |
|
42
|
|
|
$this->assertEquals($rawContent, $field->getContent()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testHideLabel() |
|
46
|
|
|
{ |
|
47
|
|
|
$field = new EditableLiteralField([ |
|
48
|
|
|
'Title' => 'Test label' |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertContains('Test label', $field->getFormField()->FieldHolder()); |
|
52
|
|
|
$this->assertEquals('Test label', $field->getFormField()->Title()); |
|
53
|
|
|
|
|
54
|
|
|
$field->HideLabel = true; |
|
|
|
|
|
|
55
|
|
|
$this->assertNotContains('Test label', $field->getFormField()->FieldHolder()); |
|
56
|
|
|
$this->assertEmpty($field->getFormField()->Title()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testLiteralFieldHasUpdateFormFieldMethodCalled() |
|
60
|
|
|
{ |
|
61
|
|
|
$field = $this->getMockBuilder(EditableLiteralField::class) |
|
62
|
|
|
->setMethods(array('doUpdateFormField')) |
|
63
|
|
|
->getMock(); |
|
64
|
|
|
|
|
65
|
|
|
$field->expects($this->once())->method('doUpdateFormField'); |
|
66
|
|
|
|
|
67
|
|
|
$field->getFormField(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* LiteralFields do not allow field names, etc. Instead, the field is contained within a composite field. This |
|
72
|
|
|
* test ensures that this structure is correct. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testLiteralFieldIsContainedWithinCompositeField() |
|
75
|
|
|
{ |
|
76
|
|
|
$field = new EditableLiteralField; |
|
77
|
|
|
$formField = $field->getFormField(); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertInstanceOf( |
|
80
|
|
|
CompositeField::class, |
|
81
|
|
|
$formField, |
|
82
|
|
|
'Literal field is contained within a composite field' |
|
83
|
|
|
); |
|
84
|
|
|
$this->assertInstanceOf( |
|
85
|
|
|
LiteralField::class, |
|
86
|
|
|
$formField->FieldList()->first(), |
|
87
|
|
|
'Actual literal field exists in composite field children' |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|