Completed
Push — master ( 2357ce...f9bf40 )
by Robbie
43:41 queued 08:38
created

testLiteralFieldIsContainedWithinCompositeField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/**
4
 * Tests the {@see EditableLiteralField} class
5
 */
6
class EditableLiteralFieldTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<EditableLiteralFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
29
        // Test with sanitisation disabled
30
        Config::inst()->remove('HtmlEditorField', 'sanitise_server_side');
31
        $field->setContent($rawContent);
32
        $this->assertEquals($rawContent, $field->getContent());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<EditableLiteralFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<EditableLiteralFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        $field->HideLabel = true;
0 ignored issues
show
Documentation introduced by
The property HideLabel does not exist on object<EditableLiteralField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
        $this->assertNotContains('Test label', $field->getFormField()->FieldHolder());
46
        $this->assertEmpty($field->getFormField()->Title());
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<EditableLiteralFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
    }
48
49
    public function testLiteralFieldHasUpdateFormFieldMethodCalled()
50
    {
51
        $field = $this->getMockBuilder('EditableLiteralField')
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<EditableLiteralFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
            ->setMethods(array('doUpdateFormField'))
53
            ->getMock();
54
55
        $field->expects($this->once())->method('doUpdateFormField');
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableLiteralFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<EditableLiteralFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
        $this->assertInstanceOf(
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<EditableLiteralFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            'LiteralField',
72
            $formField->FieldList()->first(),
73
            'Actual literal field exists in composite field children'
74
        );
75
    }
76
}
77