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

LogEntryTest::testEntryToUl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Tests\Model;
4
5
use SilverLeague\LogViewer\Model\LogEntry;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\Dev\SapphireTest;
9
10
/**
11
 * @package silverstripe-logviewer
12
 * @author  Robbie Averill <[email protected]>
13
 * @author  Simon Erkelens <[email protected]>
14
 */
15
class LogEntryTest extends SapphireTest
16
{
17
    /**
18
     * Test that LogEntry classes can not be edited
19
     */
20
    public function testDoNotAllowEditing()
21
    {
22
        static::assertFalse(LogEntry::create()->canEdit());
23
    }
24
25
    /**
26
     * Test the gridfield Summary method
27
     */
28
    public function testGridfieldSummary()
29
    {
30
        $shortString = 'abcdefghijklmnopqrstuvwxyz';
31
        $longString = '';
32
        // Generate a string of 350 characters. Because we don't care about the actual content
33
        // A set of a's is good enough;
34
        for ($i = 0; $i <= 350; $i++) {
35
            $longString .= 'a';
36
        }
37
        $logEntry = LogEntry::create();
38
        $logEntry->Entry = $shortString;
39
        static::assertEquals($shortString, $logEntry->getGridfieldSummary());
40
        $logEntry->Entry = $longString;
41
        $elipsisString = substr($longString, 0, 300) . '...';
42
        static::assertEquals($elipsisString, $logEntry->getGridfieldSummary());
43
        static::assertEquals($longString, $logEntry->getGridfieldSummary(351));
44
    }
45
46
    public function testEntryToUl()
47
    {
48
        $entry = array(
49
            'Message'  => 'Something went wrong',
50
            'DateTime' => '2016-01-22 03:14:23'
51
        );
52
        $logEntry = LogEntry::create(array(
53
            'Level' => 'INFO',
54
            'Entry' => Convert::array2json($entry)
55
        ));
56
        $result = $this->invokeMethod($logEntry, 'entryToUl', array($entry));
57
        $expectedString = '[<ul style="margin-bottom: 0"><li  class="list-unstyled"><span>Message => Something went' .
58
            ' wrong</span></li><li  class="list-unstyled"><span>DateTime => 2016-01-22 03:14:23</span></li></ul>]';
59
60
        static::assertEquals($expectedString, $result);
61
    }
62
63
    public function testFormatLegacyEntry()
64
    {
65
        $arrayEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied' .
66
            'for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
67
        $logEntry = LogEntry::create(array(
68
            'Level' => 'INFO',
69
            'Entry' => $arrayEntry
70
        ));
71
72
        $result = $this->invokeMethod($logEntry, 'formatLegacyEntry', array($arrayEntry));
73
        $expectedString = '<h2>Entry</h2>E_WARNING: Invalid argument supplied for foreach() => [<ul style="margin-bottom:' .
74
            ' 0"><li  class="list-unstyled"><span>code => 2</span></li><li  class="list-unstyled"><span>message =>' .
75
            ' Invalid argument suppliedfor foreach()</span></li><li  class="list-unstyled"><span>file =>' .
76
            ' /data/site/docroot/logviewer/src/Model/LogEntry.php</span></li><li  class="list-unstyled"><span>line' .
77
            " => 89</span></li></ul>]\n" .
78
            'Other => [<ul style="margin-bottom: 0"></ul>]';
79
        static::assertEquals($expectedString, $result);
80
81
        $stringEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied' .
82
            'for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} OOPS';
83
        $logEntry->Entry = $stringEntry;
84
85
        $result = $this->invokeMethod($logEntry, 'formatLegacyEntry', array($stringEntry));
86
        $expectedString = '<h2>Entry</h2>E_WARNING: Invalid argument supplied for foreach() => [<ul style="margin-bottom:' .
87
            ' 0"><li  class="list-unstyled"><span>code => 2</span></li><li  class="list-unstyled"><span>message =>' .
88
            ' Invalid argument suppliedfor foreach()</span></li><li  class="list-unstyled"><span>file =>' .
89
            ' /data/site/docroot/logviewer/src/Model/LogEntry.php</span></li><li  class="list-unstyled"><span>line' .
90
            " => 89</span></li></ul>]\n" .
91
            "Other:\n OOPS";
92
        static::assertEquals($result, $expectedString);
93
    }
94
95
    /**
96
     * A literal field is returning null in getCMSFields.
97
     */
98
    public function testGetCMSFieldsFormattedEntry()
99
    {
100
        $logEntry = LogEntry::create(array(
101
            'Level' => 'INFO',
102
            'Entry' => Convert::array2json(array('Test' => 'Message'))
103
        ));
104
        $fields = $logEntry->getCMSFields();
105
        $levelField = $fields->dataFieldByName('Level');
106
        static::assertEquals("SilverStripe\\Forms\\TextField", $levelField->class);
107
    }
108
109
110
    public function testGetCMSFieldsUnformattedEntry()
111
    {
112
        Config::inst()->update('SilverLeague\LogViewer\Model\LogEntry', 'format_entry', false);
113
        $logEntry = LogEntry::create(array(
114
            'Level' => 'INFO',
115
            'Entry' => Convert::array2json(array('Test' => 'Message'))
116
        ));
117
        $fields = $logEntry->getCMSFields();
118
        $entryField = $fields->dataFieldByName('Entry');
119
        static::assertEquals("SilverStripe\\Forms\\TextareaField", $entryField->class);
120
    }
121
122
    /**
123
     * Call protected/private method of a class.
124
     *
125
     * @param object &$object Instantiated object that we will run method on.
126
     * @param string $methodName Method name to call
127
     * @param array $parameters Array of parameters to pass into method.
128
     *
129
     * @return mixed Method return.
130
     */
131
    public function invokeMethod(&$object, $methodName, array $parameters = array())
132
    {
133
        $reflection = new \ReflectionClass(get_class($object));
134
        $method = $reflection->getMethod($methodName);
135
        $method->setAccessible(true);
136
137
        return $method->invokeArgs($object, $parameters);
138
    }
139
}
140