Completed
Pull Request — master (#14)
by Simon
02:03
created

LogEntryTest::testGridfieldSummary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Tests\Model;
4
5
use ReflectionClass;
6
use SilverLeague\LogViewer\Model\LogEntry;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\Security\Member;
12
13
/**
14
 * @package silverstripe-logviewer
15
 * @author  Robbie Averill <[email protected]>
16
 * @author  Simon Erkelens <[email protected]>
17
 */
18
class LogEntryTest extends SapphireTest
19
{
20
21
    /**
22
     * Test the gridfield Summary method
23
     */
24
    public function testGridfieldSummary()
25
    {
26
        $shortString = 'abcdefghijklmnopqrstuvwxyz';
27
        $longString = '';
28
        // Generate a string of 350 characters. Because we don't care about the actual content
29
        // A set of a's is good enough;
30
        for ($i = 0; $i <= 350; $i++) {
31
            $longString .= 'a';
32
        }
33
        $logEntry = LogEntry::create();
34
        $logEntry->Entry = $shortString;
35
        $this->assertEquals($shortString, $logEntry->getGridfieldSummary());
36
        $logEntry->Entry = $longString;
37
        $elipsisString = substr($longString, 0, 300) . '...';
38
        $this->assertEquals($elipsisString, $logEntry->getGridfieldSummary());
39
        $this->assertEquals($longString, $logEntry->getGridfieldSummary(351));
40
    }
41
42
    /**
43
     * Test if the fields are in the fieldlist
44
     * A literal field is returning null in getCMSFields.
45
     */
46
    public function testGetCMSFieldsFormattedEntry()
47
    {
48
        $logEntry = LogEntry::create(array(
49
            'Level' => 'INFO',
50
            'Entry' => Convert::array2json(array('Test' => 'Message'))
51
        ));
52
        $fields = $logEntry->getCMSFields();
53
        $levelField = $fields->dataFieldByName('Level');
54
        $this->assertEquals("SilverStripe\\Forms\\TextField", $levelField->class);
55
    }
56
57
    /**
58
     * Test if the field shows when not formatting
59
     */
60
    public function testGetCMSFieldsUnformattedEntry()
61
    {
62
        Config::inst()->update('SilverLeague\LogViewer\Model\LogEntry', 'format_entry', false);
63
        $logEntry = LogEntry::create(array(
64
            'Level' => 'INFO',
65
            'Entry' => Convert::array2json(array('Test' => 'Message'))
66
        ));
67
        $fields = $logEntry->getCMSFields();
68
        $entryField = $fields->dataFieldByName('Entry');
69
        $this->assertEquals("SilverStripe\\Forms\\TextareaField", $entryField->class);
70
    }
71
72
    /**
73
     * Test the entire build of a formatted entry
74
     */
75
    public function testBuildEntry()
76
    {
77
        $entry = array(
78
            'Message'  => 'Something went wrong',
79
            'DateTime' => '2016-01-22 03:14:23'
80
        );
81
        $logEntry = LogEntry::create(array(
82
            'Level' => 'INFO',
83
            'Entry' => Convert::array2json($entry)
84
        ));
85
        $result = $this->invokeMethod($logEntry, 'buildEntry', array());
86
        $expectedResult = "<pre class='form-control-static logentry-entry' style='white-space: normal; max-width: 85%;'>".
87
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">Message</span>'.
88
            '<span style="color: #007700">: </span><span style="color: #DD0000">Something went wrong</span></li>'.
89
            '<li class="list-unstyled"><span style="color: #0000BB">DateTime</span>'.
90
            '<span style="color: #007700">: </span><span style="color: #DD0000">2016-01-22 03:14:23</span></li>'.
91
            '</ul></pre>';
92
        $this->assertEquals($expectedResult, $result);
93
    }
94
95
    /**
96
     * There's no reason to manually create, so don't allow manual creation
97
     */
98 View Code Duplication
    public function testAllowCreate()
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...
99
    {
100
        $member = Member::default_admin();
101
        $createTrue = LogEntry::create()->canCreate($member);
102
        $this->assertFalse($createTrue);
103
        $createFalse = LogEntry::create()->canCreate(null);
104
        $this->assertFalse($createFalse);
105
    }
106
107
    /**
108
     * Test that LogEntry classes can not be edited
109
     */
110
    public function testtAllowEditing()
111
    {
112
        $this->assertFalse(LogEntry::create()->canEdit());
113
    }
114
115
    /**
116
     * We can view if we're logged in as admin. Otherwise, no.
117
     */
118 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...
119
    {
120
        $member = Member::default_admin();
121
        $viewTrue = LogEntry::create()->canView($member);
122
        $this->assertTrue($viewTrue);
123
        $viewFalse = LogEntry::create()->canView(null);
124
        $this->assertFalse($viewFalse);
125
    }
126
127
    /**
128
     * We can Delete if we're logged in as admin. Otherwise, no.
129
     */
130 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...
131
    {
132
        $member = Member::default_admin();
133
        $deleteTrue = LogEntry::create()->canDelete($member);
134
        $this->assertTrue($deleteTrue);
135
        $deleteFalse = LogEntry::create()->canDelete(null);
136
        $this->assertFalse($deleteFalse);
137
    }
138
139
    /**
140
     * Call protected/private method of a class.
141
     *
142
     * @param DataObject &$object Instantiated object that we will run method on.
143
     * @param string $methodName Method name to call
144
     * @param array $parameters Array of parameters to pass into method.
145
     *
146
     * @return mixed Method return.
147
     */
148 View Code Duplication
    public function invokeMethod(&$object, $methodName, array $parameters = array())
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...
149
    {
150
        $reflection = new ReflectionClass(get_class($object));
151
        $method = $reflection->getMethod($methodName);
152
        $method->setAccessible(true);
153
154
        return $method->invokeArgs($object, $parameters);
155
    }
156
}
157