Completed
Pull Request — master (#7)
by Mateusz
03:04
created

AuditLoggerTest_Logger   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 19
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 4 1
A getLastMessage() 0 4 1
A getMessages() 0 4 1
1
<?php
2
3
namespace SilverStripe\Auditor;
4
5
class AuditHookTest extends \FunctionalTest
6
{
7
8
    protected $usesDatabase = true;
9
10
    protected $writer = null;
11
12
    public function setUp()
13
    {
14
        parent::setUp();
15
16
        $this->writer = new AuditLoggerTest_Logger;
17
		// Phase singleton out, so the message log is purged.
18
		\Injector::inst()->unregisterNamedObject('AuditLogger');
19
		\Injector::inst()->registerService($this->writer, 'AuditLogger');
0 ignored issues
show
Documentation introduced by
$this->writer is of type object<SilverStripe\Audi...AuditLoggerTest_Logger>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
20
21
        // ensure the manipulations are being captured, normally called in {@link AuditLogger::onBeforeInit()}
22
        // but tests will reset this during setting up, so we need to set it back again.
23
        AuditHook::bind_manipulation_capture();
24
    }
25
26
    public function testLoggingIn()
27
    {
28
        $this->logInWithPermission('ADMIN');
29
30
        $message = $this->writer->getLastMessage();
31
        $this->assertContains('[email protected]', $message);
32
        $this->assertContains('successfully logged in', $message);
33
    }
34
35 View Code Duplication
    public function testAutoLoggingIn()
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...
36
    {
37
        // Simulate an autologin by calling the extension hook directly.
38
        // Member->autoLogin() relies on session and cookie state which we can't simulate here.
39
        $this->logInWithPermission('ADMIN');
40
        $member = \Member::get()->filter(array('Email' => '[email protected]'))->first();
41
        $member->extend('memberAutoLoggedIn');
42
43
        $message = $this->writer->getLastMessage();
44
        $this->assertContains('[email protected]', $message);
45
        $this->assertContains('successfully restored autologin', $message);
46
    }
47
48 View Code Duplication
    public function testLoggingOut()
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...
49
    {
50
        $this->logInWithPermission('ADMIN');
51
52
        $member = \Member::get()->filter(array('Email' => '[email protected]'))->first();
53
        $member->logOut();
54
55
        $message = $this->writer->getLastMessage();
56
        $this->assertContains('[email protected]', $message);
57
        $this->assertContains('successfully logged out', $message);
58
    }
59
60
    public function testLoggingWriteDoesNotOccurWhenNotLoggedIn()
61
    {
62
        $this->session()->inst_set('loggedInAs', null);
63
64
        $group = new \Group(array('Title' => 'My group'));
65
        $group->write();
66
67
        $message = $this->writer->getLastMessage();
68
        $this->assertEmpty($message, 'No one is logged in, so nothing was logged');
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<SilverStripe\Auditor\AuditHookTest>.

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...
69
    }
70
71 View Code Duplication
    public function testLoggingWriteWhenLoggedIn()
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...
72
    {
73
        $this->logInWithPermission('ADMIN');
74
75
        $group = new \Group(array('Title' => 'My group'));
76
        $group->write();
77
78
        $message = $this->writer->getLastMessage();
79
        $this->assertContains('[email protected]', $message);
80
        $this->assertContains('modified', $message);
81
        $this->assertContains('Group', $message);
82
    }
83
84 View Code Duplication
    public function testAddMemberToGroupUsingGroupMembersRelation()
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...
85
    {
86
        $this->logInWithPermission('ADMIN');
87
88
        $group = new \Group(array('Title' => 'My group'));
89
        $group->write();
90
91
        $member = new \Member(array('FirstName' => 'Joe', 'Email' => 'joe1'));
92
        $member->write();
93
94
        $group->Members()->add($member);
95
96
        $message = $this->writer->getLastMessage();
97
        $this->assertContains('[email protected]', $message);
98
        $this->assertContains('added Member "joe1"', $message);
99
        $this->assertContains('to Group "My group"', $message);
100
    }
101
102 View Code Duplication
    public function testAddMemberToGroupUsingMemberGroupsRelation()
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...
103
    {
104
        $this->logInWithPermission('ADMIN');
105
106
        $group = new \Group(array('Title' => 'My group'));
107
        $group->write();
108
109
        $member = new \Member(array('FirstName' => 'Joe', 'Email' => 'joe2'));
110
        $member->write();
111
112
        $member->Groups()->add($group);
113
114
        $message = $this->writer->getLastMessage();
115
        $this->assertContains('[email protected]', $message);
116
        $this->assertContains('added Member "joe2"', $message);
117
        $this->assertContains('to Group "My group"', $message);
118
    }
119
120 View Code Duplication
    public function testRemoveMemberFromGroupUsingGroupMembersRelation()
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...
121
    {
122
        $this->logInWithPermission('ADMIN');
123
124
        $group = new \Group(array('Title' => 'My group'));
125
        $group->write();
126
127
        $member = new \Member(array('FirstName' => 'Joe', 'Email' => 'joe3'));
128
        $member->write();
129
130
        $group->Members()->add($member);
131
        $group->Members()->remove($member);
132
133
        $message = $this->writer->getLastMessage();
134
        $this->assertContains('[email protected]', $message);
135
        $this->assertContains('removed Member "joe3"', $message);
136
        $this->assertContains('from Group "My group"', $message);
137
    }
138
139 View Code Duplication
    public function testRemoveMemberFromGroupUsingMemberGroupsRelation()
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...
140
    {
141
        $this->logInWithPermission('ADMIN');
142
143
        $group = new \Group(array('Title' => 'My group'));
144
        $group->write();
145
146
        $member = new \Member(array('FirstName' => 'Joe', 'Email' => 'joe4'));
147
        $member->write();
148
149
        $member->Groups()->add($group);
150
        $member->Groups()->remove($group);
151
152
        $message = $this->writer->getLastMessage();
153
        $this->assertContains('[email protected]', $message);
154
        $this->assertContains('removed Member "joe4"', $message);
155
        $this->assertContains('from Group "My group"', $message);
156
    }
157
158
    public function testPublishPage()
159
    {
160
        $this->logInWithPermission('ADMIN');
161
162
        $page = new \Page();
163
        $page->Title = 'My page';
164
        $page->Content = 'This is my page content';
165
        $page->doPublish();
166
167
        $message = $this->writer->getLastMessage();
168
        $this->assertContains('[email protected]', $message);
169
        $this->assertContains('published Page', $message);
170
        $this->assertContains('My page', $message);
171
    }
172
173
    /**
174
     * Test log message sanitisation.
175
     */
176
    public function testSanitisation()
177
    {
178
        $this->logInWithPermission('ADMIN');
179
180
        $page = new \Page();
181
        $page->Title = "My\npage\r\nYour Page";
182
        $page->Content = 'This is my page content';
183
        $page->doPublish();
184
185
        $message = $this->writer->getLastMessage();
186
        $this->assertContains('[email protected]', $message);
187
        $this->assertContains('published Page', $message);
188
        $this->assertContains('My page Your Page', $message);
189
    }
190
191 View Code Duplication
    public function testUnpublishPage()
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...
192
    {
193
        $this->logInWithPermission('ADMIN');
194
195
        $page = new \Page();
196
        $page->Title = 'My page';
197
        $page->Content = 'This is my page content';
198
        $page->doPublish();
199
        $page->doUnpublish();
200
201
        $message = $this->writer->getLastMessage();
202
        $this->assertContains('[email protected]', $message);
203
        $this->assertContains('unpublished Page', $message);
204
        $this->assertContains('My page', $message);
205
    }
206
207 View Code Duplication
    public function testDuplicatePage()
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...
208
    {
209
        $this->logInWithPermission('ADMIN');
210
211
        $page = new \Page();
212
        $page->Title = 'My page';
213
        $page->Content = 'This is my page content';
214
        $page->write();
215
        $page->duplicate();
216
217
        $message = $this->writer->getLastMessage();
218
        $this->assertContains('[email protected]', $message);
219
        $this->assertContains('duplicated Page', $message);
220
        $this->assertContains('My page', $message);
221
    }
222
223 View Code Duplication
    public function testRevertToLive()
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...
224
    {
225
        $this->logInWithPermission('ADMIN');
226
227
        $page = new \Page();
228
        $page->Title = 'My page';
229
        $page->Content = 'This is my page content';
230
        $page->doPublish();
231
232
        $page->Content = 'Changed';
233
        $page->write();
234
        $page->doRevertToLive();
235
236
        $message = $this->writer->getLastMessage();
237
        $this->assertContains('[email protected]', $message);
238
        $this->assertContains('reverted Page', $message);
239
        $this->assertContains('My page', $message);
240
    }
241
242 View Code Duplication
    public function testDelete()
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...
243
    {
244
        $this->logInWithPermission('ADMIN');
245
246
        $page = new \Page();
247
        $page->Title = 'My page';
248
        $page->Content = 'This is my page content';
249
        $page->doPublish();
250
251
        $page->delete();
252
253
        $message = $this->writer->getLastMessage();
254
        $this->assertContains('[email protected]', $message);
255
        $this->assertContains('deleted Page', $message);
256
        $this->assertContains('My page', $message);
257
    }
258
259 View Code Duplication
    public function testRestoreToStage()
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...
260
    {
261
        $this->logInWithPermission('ADMIN');
262
263
        $page = new \Page();
264
        $page->Title = 'My page';
265
        $page->Content = 'Published';
266
        $page->doPublish();
267
268
        $page->Content = 'This is my page content';
269
        $page->doPublish();
270
        $page->delete();
271
272
        $message = $this->writer->getLastMessage();
273
        $this->assertContains('[email protected]', $message);
274
        $this->assertContains('deleted Page', $message);
275
        $this->assertContains('My page', $message);
276
    }
277
278
    public function tearDown()
279
    {
280
        parent::tearDown();
281
282
        \SS_Log::remove_writer($this->writer);
283
        unset($this->writer);
284
    }
285
}
286
287
class AuditLoggerTest_Logger extends \Psr\Log\AbstractLogger
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
288
{
289
    protected $messages = array();
290
291
	public function log($level, $message, array $context = array())
292
    {
293
        array_push($this->messages, $message);
294
	}
295
296
    public function getLastMessage()
297
    {
298
        return end($this->messages);
299
    }
300
301
    public function getMessages()
302
    {
303
        return $this->messages;
304
    }
305
}
306