Completed
Push — master ( b537e6...b70c66 )
by Daniel
35:35
created

EditableLiteralFieldTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 1
f 0
cc 1
eloc 3
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
	public function setUp() {
9
		parent::setUp();
10
		HtmlEditorConfig::set_active('cms');
11
	}
12
13
	/**
14
	 * Tests the sanitisation of HTML content
15
	 */
16
	public function testSanitisation() {
17
		$rawContent = '<h1>Welcome</h1><script>alert("Hello!");</script><p>Giant Robots!</p>';
18
		$safeContent = '<h1>Welcome</h1><p>Giant Robots!</p>';
19
		$field = new EditableLiteralField();
20
21
		// Test with sanitisation enabled
22
		Config::inst()->update('HtmlEditorField', 'sanitise_server_side', true);
23
		$field->setContent($rawContent);
24
		$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...
25
26
		// Test with sanitisation disabled
27
		Config::inst()->remove('HtmlEditorField', 'sanitise_server_side');
28
		$field->setContent($rawContent);
29
		$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...
30
	}
31
32
	public function testHideLabel() {
33
		$field = new EditableLiteralField(array(
34
			'Title' => 'Test label'
35
		));
36
37
		$this->assertContains('Test label', $field->getFormField()->Field());
38
39
		$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...
40
		$this->assertNotContains('Test label', $field->getFormField()->Field());
41
	}
42
}
43