|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Tests the {@see EditableLiteralField} class |
|
5
|
|
|
*/ |
|
6
|
|
|
class EditableLiteralFieldTest extends SapphireTest |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
public function setUp() |
|
10
|
|
|
{ |
|
11
|
|
|
parent::setUp(); |
|
12
|
|
|
HtmlEditorConfig::set_active('cms'); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Tests the sanitisation of HTML content |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testSanitisation() |
|
19
|
|
|
{ |
|
20
|
|
|
$rawContent = '<h1>Welcome</h1><script>alert("Hello!");</script><p>Giant Robots!</p>'; |
|
21
|
|
|
$safeContent = '<h1>Welcome</h1><p>Giant Robots!</p>'; |
|
22
|
|
|
$field = new EditableLiteralField(); |
|
23
|
|
|
|
|
24
|
|
|
// Test with sanitisation enabled |
|
25
|
|
|
Config::inst()->update('HtmlEditorField', 'sanitise_server_side', true); |
|
26
|
|
|
$field->setContent($rawContent); |
|
27
|
|
|
$this->assertEquals($safeContent, $field->getContent()); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// Test with sanitisation disabled |
|
30
|
|
|
Config::inst()->remove('HtmlEditorField', 'sanitise_server_side'); |
|
31
|
|
|
$field->setContent($rawContent); |
|
32
|
|
|
$this->assertEquals($rawContent, $field->getContent()); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testHideLabel() |
|
36
|
|
|
{ |
|
37
|
|
|
$field = new EditableLiteralField(array( |
|
38
|
|
|
'Title' => 'Test label' |
|
39
|
|
|
)); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertContains('Test label', $field->getFormField()->FieldHolder()); |
|
42
|
|
|
$this->assertEquals('Test label', $field->getFormField()->Title()); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$field->HideLabel = true; |
|
|
|
|
|
|
45
|
|
|
$this->assertNotContains('Test label', $field->getFormField()->FieldHolder()); |
|
46
|
|
|
$this->assertEmpty($field->getFormField()->Title()); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testLiteralFieldHasUpdateFormFieldMethodCalled() |
|
50
|
|
|
{ |
|
51
|
|
|
$field = $this->getMockBuilder('EditableLiteralField') |
|
|
|
|
|
|
52
|
|
|
->setMethods(array('doUpdateFormField')) |
|
53
|
|
|
->getMock(); |
|
54
|
|
|
|
|
55
|
|
|
$field->expects($this->once())->method('doUpdateFormField'); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$field->getFormField(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* LiteralFields do not allow field names, etc. Instead, the field is contained within a composite field. This |
|
62
|
|
|
* test ensures that this structure is correct. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testLiteralFieldIsContainedWithinCompositeField() |
|
65
|
|
|
{ |
|
66
|
|
|
$field = new EditableLiteralField; |
|
67
|
|
|
$formField = $field->getFormField(); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertInstanceOf('CompositeField', $formField, 'Literal field is contained within a composite field'); |
|
|
|
|
|
|
70
|
|
|
$this->assertInstanceOf( |
|
|
|
|
|
|
71
|
|
|
'LiteralField', |
|
72
|
|
|
$formField->FieldList()->first(), |
|
73
|
|
|
'Actual literal field exists in composite field children' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.