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

LogEntryTest::testDoNotAllowEditing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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