Completed
Push — master ( 05d8f1...d6e5c2 )
by
unknown
01:53 queued 34s
created

LogEntryTest::testLogEntriesAreFormattedAsJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Tests\Model;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Forms\LiteralField;
8
use SilverLeague\LogViewer\Model\LogEntry;
9
10
/**
11
 * @package silverstripe-logviewer
12
 * @author  Robbie Averill <[email protected]>
13
 */
14
class LogEntryTest extends SapphireTest
15
{
16
    /**
17
     * Test if the Permissions are an array and contain the view and delete permissions
18
     */
19
    public function testProvidePermissions()
20
    {
21
        $permissions = LogEntry::create()->providePermissions();
22
        $this->assertTrue(is_array($permissions));
23
        $this->assertTrue(array_key_exists('DELETE_ENTRY', $permissions));
24
        $this->assertTrue(array_key_exists('VIEW_ENTRY', $permissions));
25
    }
26
27
    /**
28
     * There's no reason to manually create, so don't allow manual creation
29
     */
30
    public function testAllowCreate()
31
    {
32
        $createFalse = LogEntry::create()->canCreate(null);
33
        $this->assertFalse($createFalse);
34
        $this->logInWithPermission('ADMIN');
35
        $createFalse = LogEntry::create()->canCreate();
36
        $this->assertFalse($createFalse);
37
    }
38
39
    /**
40
     * Test that LogEntry classes can not be edited
41
     */
42
    public function testAllowEditing()
43
    {
44
        $this->assertFalse(LogEntry::create()->canEdit());
45
    }
46
47
    /**
48
     * We can view if we're logged in as admin. Otherwise, no.
49
     */
50 View Code Duplication
    public function testAllowView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $this->logOut();
53
        $viewFalse = LogEntry::create()->canView(null);
54
        $this->assertFalse($viewFalse);
55
56
        $this->logInWithPermission('ADMIN');
57
        $viewTrue = LogEntry::create()->canView();
58
        $this->assertTrue($viewTrue);
59
    }
60
61
    /**
62
     * We can Delete if we're logged in as admin. Otherwise, no.
63
     */
64 View Code Duplication
    public function testAllowDelete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $this->logOut();
67
        $deleteFalse = LogEntry::create()->canDelete(null);
68
        $this->assertFalse($deleteFalse);
69
70
        $this->logInWithPermission('ADMIN');
71
        $deleteTrue = LogEntry::create()->canDelete();
72
        $this->assertTrue($deleteTrue);
73
    }
74
75
    /**
76
     * Ensure that the contents are JSON encoded and pretty printed, and that the CSS class is correct
77
     */
78
    public function testLogEntriesAreFormattedAsJson()
79
    {
80
        $data = [
81
            'foo' => [
82
                'bar' => 'baz'
83
            ],
84
            'boo'
85
        ];
86
87
        $entry = LogEntry::create();
88
        $entry->Entry = Convert::raw2json($data);
0 ignored issues
show
Documentation introduced by
The property Entry does not exist on object<SilverLeague\LogViewer\Model\LogEntry>. 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...
89
90
        $fields = $entry->getCMSFields();
91
        $field = $fields->fieldByName('Root.Main.Entry');
92
93
        $this->assertInstanceOf(LiteralField::class, $field);
94
        $this->assertContains(Convert::raw2json($data, JSON_PRETTY_PRINT), $field->getContent());
95
        $this->assertContains('<pre class="logviewer-logentry-entry"><code>', $field->getContent());
96
    }
97
}
98