Completed
Push — master ( e36c0e...593867 )
by Damian
03:12 queued 01:16
created

AuditHookTest::testRestoreToStage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Auditor\Tests;
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
        \Silverstripe\Auditor\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\Tests\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 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...
174
    {
175
        $this->logInWithPermission('ADMIN');
176
177
        $page = new \Page();
178
        $page->Title = 'My page';
179
        $page->Content = 'This is my page content';
180
        $page->doPublish();
181
        $page->doUnpublish();
182
183
        $message = $this->writer->getLastMessage();
184
        $this->assertContains('[email protected]', $message);
185
        $this->assertContains('unpublished Page', $message);
186
        $this->assertContains('My page', $message);
187
    }
188
189 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...
190
    {
191
        $this->logInWithPermission('ADMIN');
192
193
        $page = new \Page();
194
        $page->Title = 'My page';
195
        $page->Content = 'This is my page content';
196
        $page->write();
197
        $page->duplicate();
198
199
        $message = $this->writer->getLastMessage();
200
        $this->assertContains('[email protected]', $message);
201
        $this->assertContains('duplicated Page', $message);
202
        $this->assertContains('My page', $message);
203
    }
204
205 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...
206
    {
207
        $this->logInWithPermission('ADMIN');
208
209
        $page = new \Page();
210
        $page->Title = 'My page';
211
        $page->Content = 'This is my page content';
212
        $page->doPublish();
213
214
        $page->Content = 'Changed';
215
        $page->write();
216
        $page->doRevertToLive();
217
218
        $message = $this->writer->getLastMessage();
219
        $this->assertContains('[email protected]', $message);
220
        $this->assertContains('reverted Page', $message);
221
        $this->assertContains('My page', $message);
222
    }
223
224 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...
225
    {
226
        $this->logInWithPermission('ADMIN');
227
228
        $page = new \Page();
229
        $page->Title = 'My page';
230
        $page->Content = 'This is my page content';
231
        $page->doPublish();
232
233
        $page->delete();
234
235
        $message = $this->writer->getLastMessage();
236
        $this->assertContains('[email protected]', $message);
237
        $this->assertContains('deleted Page', $message);
238
        $this->assertContains('My page', $message);
239
    }
240
241 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...
242
    {
243
        $this->logInWithPermission('ADMIN');
244
245
        $page = new \Page();
246
        $page->Title = 'My page';
247
        $page->Content = 'Published';
248
        $page->doPublish();
249
250
        $page->Content = 'This is my page content';
251
        $page->doPublish();
252
        $page->delete();
253
254
        $message = $this->writer->getLastMessage();
255
        $this->assertContains('[email protected]', $message);
256
        $this->assertContains('deleted Page', $message);
257
        $this->assertContains('My page', $message);
258
    }
259
260
    public function tearDown()
261
    {
262
        parent::tearDown();
263
264
        \SS_Log::remove_writer($this->writer);
265
        unset($this->writer);
266
    }
267
}
268
269
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...
270
{
271
    protected $messages = array();
272
273
	public function log($level, $message, array $context = array())
274
    {
275
        array_push($this->messages, $message);
276
	}
277
278
    public function getLastMessage()
279
    {
280
        return end($this->messages);
281
    }
282
283
    public function getMessages()
284
    {
285
        return $this->messages;
286
    }
287
}
288