Completed
Pull Request — master (#13)
by Robbie
03:42
created

AuditLoggerTest_Logger   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 19
rs 10
1
<?php
2
3
namespace SilverStripe\Auditor\Tests;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Silverstripe\Auditor\AuditHook;
7
use SilverStripe\Auditor\Tests\AuditHookTest\Logger;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\FunctionalTest;
10
use SilverStripe\Security\Group;
11
use SilverStripe\Security\Member;
12
13
class AuditHookTest extends FunctionalTest
14
{
15
    protected $usesDatabase = true;
16
17
    protected $writer = null;
18
19
    protected function setUp()
20
    {
21
        parent::setUp();
22
23
        $this->writer = new Logger;
24
25
        // Phase singleton out, so the message log is purged.
26
        Injector::inst()->unregisterNamedObject('AuditLogger');
27
        Injector::inst()->registerService($this->writer, 'AuditLogger');
28
29
        // ensure the manipulations are being captured, normally called in {@link AuditLogger::onBeforeInit()}
30
        // but tests will reset this during setting up, so we need to set it back again.
31
        AuditHook::bind_manipulation_capture();
32
    }
33
34
    public function testLoggingIn()
35
    {
36
        $this->logInWithPermission('ADMIN');
37
38
        $message = $this->writer->getLastMessage();
39
        $this->assertContains('[email protected]', $message);
40
        $this->assertContains('successfully logged in', $message);
41
    }
42
43 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...
44
    {
45
        // Simulate an autologin by calling the extension hook directly.
46
        // Member->autoLogin() relies on session and cookie state which we can't simulate here.
47
        $this->logInWithPermission('ADMIN');
48
        $member = Member::get()->filter(array('Email' => '[email protected]'))->first();
49
        $member->extend('memberAutoLoggedIn');
50
51
        $message = $this->writer->getLastMessage();
52
        $this->assertContains('[email protected]', $message);
53
        $this->assertContains('successfully restored autologin', $message);
54
    }
55
56 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...
57
    {
58
        $this->logInWithPermission('ADMIN');
59
60
        $member = Member::get()->filter(array('Email' => '[email protected]'))->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $member is dead and can be removed.
Loading history...
61
        $this->logOut();
62
63
        $message = $this->writer->getLastMessage();
64
        $this->assertContains('[email protected]', $message);
65
        $this->assertContains('successfully logged out', $message);
66
    }
67
68
    public function testLoggingWriteDoesNotOccurWhenNotLoggedIn()
69
    {
70
        $this->logOut();
71
72
        $group = new Group(array('Title' => 'My group'));
73
        $group->write();
74
75
        $message = $this->writer->getLastMessage();
76
        $this->assertEmpty($message, 'No one is logged in, so nothing was logged');
77
    }
78
79 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...
80
    {
81
        $this->logInWithPermission('ADMIN');
82
83
        $group = new Group(array('Title' => 'My group'));
84
        $group->write();
85
86
        $message = $this->writer->getLastMessage();
87
        $this->assertContains('[email protected]', $message);
88
        $this->assertContains('modified', $message);
89
        $this->assertContains(Group::class, $message);
90
    }
91
92 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...
93
    {
94
        $this->logInWithPermission('ADMIN');
95
96
        $group = new Group(array('Title' => 'My group'));
97
        $group->write();
98
99
        $member = new Member(array('FirstName' => 'Joe', 'Email' => 'joe1'));
100
        $member->write();
101
102
        $group->Members()->add($member);
103
104
        $message = $this->writer->getLastMessage();
105
        $this->assertContains('[email protected]', $message);
106
        $this->assertContains('added Member "joe1"', $message);
107
        $this->assertContains('to Group "My group"', $message);
108
    }
109
110 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...
111
    {
112
        $this->logInWithPermission('ADMIN');
113
114
        $group = new Group(array('Title' => 'My group'));
115
        $group->write();
116
117
        $member = new Member(array('FirstName' => 'Joe', 'Email' => 'joe2'));
118
        $member->write();
119
120
        $member->Groups()->add($group);
121
122
        $message = $this->writer->getLastMessage();
123
        $this->assertContains('[email protected]', $message);
124
        $this->assertContains('added Member "joe2"', $message);
125
        $this->assertContains('to Group "My group"', $message);
126
    }
127
128 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...
129
    {
130
        $this->logInWithPermission('ADMIN');
131
132
        $group = new Group(array('Title' => 'My group'));
133
        $group->write();
134
135
        $member = new Member(array('FirstName' => 'Joe', 'Email' => 'joe3'));
136
        $member->write();
137
138
        $group->Members()->add($member);
139
        $group->Members()->remove($member);
140
141
        $message = $this->writer->getLastMessage();
142
        $this->assertContains('[email protected]', $message);
143
        $this->assertContains('removed Member "joe3"', $message);
144
        $this->assertContains('from Group "My group"', $message);
145
    }
146
147 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...
148
    {
149
        $this->logInWithPermission('ADMIN');
150
151
        $group = new Group(array('Title' => 'My group'));
152
        $group->write();
153
154
        $member = new Member(array('FirstName' => 'Joe', 'Email' => 'joe4'));
155
        $member->write();
156
157
        $member->Groups()->add($group);
158
        $member->Groups()->remove($group);
159
160
        $message = $this->writer->getLastMessage();
161
        $this->assertContains('[email protected]', $message);
162
        $this->assertContains('removed Member "joe4"', $message);
163
        $this->assertContains('from Group "My group"', $message);
164
    }
165
166
    public function testPublishPage()
167
    {
168
        $this->logInWithPermission('ADMIN');
169
170
        $page = new Page();
171
        $page->Title = 'My page';
172
        $page->Content = 'This is my page content';
173
        $page->write();
174
        $page->publishSingle();
175
176
        $message = $this->writer->getLastMessage();
177
        $this->assertContains('[email protected]', $message);
178
        $this->assertContains('published Page', $message);
179
        $this->assertContains('My page', $message);
180
    }
181
182 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...
183
    {
184
        $this->logInWithPermission('ADMIN');
185
186
        $page = new Page();
187
        $page->Title = 'My page';
188
        $page->Content = 'This is my page content';
189
        $page->write();
190
        $page->publishSingle();
191
        $page->doUnpublish();
192
193
        $message = $this->writer->getLastMessage();
194
        $this->assertContains('[email protected]', $message);
195
        $this->assertContains('unpublished Page', $message);
196
        $this->assertContains('My page', $message);
197
    }
198
199
    public function testDuplicatePage()
200
    {
201
        $this->logInWithPermission('ADMIN');
202
203
        $page = new Page();
204
        $page->Title = 'My page';
205
        $page->Content = 'This is my page content';
206
        $page->write();
207
        $page->duplicate();
208
209
        $message = $this->writer->getLastMessage();
210
        $this->assertContains('[email protected]', $message);
211
        $this->assertContains('duplicated Page', $message);
212
        $this->assertContains('My page', $message);
213
    }
214
215 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...
216
    {
217
        $this->logInWithPermission('ADMIN');
218
219
        $page = new Page();
220
        $page->Title = 'My page';
221
        $page->Content = 'This is my page content';
222
        $page->write();
223
        $page->publishSingle();
224
225
        $page->Content = 'Changed';
226
        $page->write();
227
        $page->doRevertToLive();
228
229
        $message = $this->writer->getLastMessage();
230
        $this->assertContains('[email protected]', $message);
231
        $this->assertContains('reverted Page', $message);
232
        $this->assertContains('My page', $message);
233
    }
234
235 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...
236
    {
237
        $this->logInWithPermission('ADMIN');
238
239
        $page = new Page();
240
        $page->Title = 'My page';
241
        $page->Content = 'This is my page content';
242
        $page->write();
243
        $page->publishSingle();
244
245
        $page->delete();
246
247
        $message = $this->writer->getLastMessage();
248
        $this->assertContains('[email protected]', $message);
249
        $this->assertContains('deleted Page', $message);
250
        $this->assertContains('My page', $message);
251
    }
252
253 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...
254
    {
255
        $this->logInWithPermission('ADMIN');
256
257
        $page = new Page();
258
        $page->Title = 'My page';
259
        $page->Content = 'Published';
260
        $page->write();
261
        $page->publishSingle();
262
263
        $page->Content = 'This is my page content';
264
        $page->write();
265
        $page->publishSingle();
266
        $page->delete();
267
268
        $message = $this->writer->getLastMessage();
269
        $this->assertContains('[email protected]', $message);
270
        $this->assertContains('deleted Page', $message);
271
        $this->assertContains('My page', $message);
272
    }
273
}
274