@@ -20,201 +20,201 @@ |
||
20 | 20 | use Test\TestCase; |
21 | 21 | |
22 | 22 | class BackupCodeStorageTest extends TestCase { |
23 | - private BackupCodeMapper&MockObject $mapper; |
|
24 | - private ISecureRandom&MockObject $random; |
|
25 | - private IHasher&MockObject $hasher; |
|
26 | - private IEventDispatcher&MockObject $eventDispatcher; |
|
27 | - private BackupCodeStorage $storage; |
|
28 | - |
|
29 | - protected function setUp(): void { |
|
30 | - parent::setUp(); |
|
31 | - |
|
32 | - $this->mapper = $this->createMock(BackupCodeMapper::class); |
|
33 | - $this->random = $this->createMock(ISecureRandom::class); |
|
34 | - $this->hasher = $this->createMock(IHasher::class); |
|
35 | - $this->eventDispatcher = $this->createMock(IEventDispatcher::class); |
|
36 | - |
|
37 | - $this->storage = new BackupCodeStorage($this->mapper, $this->random, $this->hasher, $this->eventDispatcher); |
|
38 | - } |
|
39 | - |
|
40 | - public function testCreateCodes(): void { |
|
41 | - $user = $this->createMock(IUser::class); |
|
42 | - $number = 5; |
|
43 | - $user->method('getUID')->willReturn('fritz'); |
|
44 | - $this->random->expects($this->exactly($number)) |
|
45 | - ->method('generate') |
|
46 | - ->with(16, ISecureRandom::CHAR_HUMAN_READABLE) |
|
47 | - ->willReturn('CODEABCDEF'); |
|
48 | - $this->hasher->expects($this->exactly($number)) |
|
49 | - ->method('hash') |
|
50 | - ->with('CODEABCDEF') |
|
51 | - ->willReturn('HASHEDCODE'); |
|
52 | - $row = new BackupCode(); |
|
53 | - $row->setUserId('fritz'); |
|
54 | - $row->setCode('HASHEDCODE'); |
|
55 | - $row->setUsed(0); |
|
56 | - $this->mapper->expects($this->exactly($number)) |
|
57 | - ->method('insert') |
|
58 | - ->with($this->equalTo($row)); |
|
59 | - $this->eventDispatcher->expects($this->once()) |
|
60 | - ->method('dispatchTyped') |
|
61 | - ->with( |
|
62 | - $this->equalTo(new CodesGenerated($user)) |
|
63 | - ); |
|
64 | - |
|
65 | - $codes = $this->storage->createCodes($user, $number); |
|
66 | - $this->assertCount($number, $codes); |
|
67 | - foreach ($codes as $code) { |
|
68 | - $this->assertEquals('CODEABCDEF', $code); |
|
69 | - } |
|
70 | - } |
|
71 | - |
|
72 | - public function testHasBackupCodes(): void { |
|
73 | - $user = $this->createMock(IUser::class); |
|
74 | - $codes = [ |
|
75 | - new BackupCode(), |
|
76 | - new BackupCode(), |
|
77 | - ]; |
|
78 | - |
|
79 | - $this->mapper->expects($this->once()) |
|
80 | - ->method('getBackupCodes') |
|
81 | - ->with($user) |
|
82 | - ->willReturn($codes); |
|
83 | - |
|
84 | - $this->assertTrue($this->storage->hasBackupCodes($user)); |
|
85 | - } |
|
86 | - |
|
87 | - public function testHasBackupCodesNoCodes(): void { |
|
88 | - $user = $this->createMock(IUser::class); |
|
89 | - $codes = []; |
|
90 | - |
|
91 | - $this->mapper->expects($this->once()) |
|
92 | - ->method('getBackupCodes') |
|
93 | - ->with($user) |
|
94 | - ->willReturn($codes); |
|
95 | - |
|
96 | - $this->assertFalse($this->storage->hasBackupCodes($user)); |
|
97 | - } |
|
98 | - |
|
99 | - public function testGetBackupCodeState(): void { |
|
100 | - $user = $this->createMock(IUser::class); |
|
101 | - |
|
102 | - $code1 = new BackupCode(); |
|
103 | - $code1->setUsed(1); |
|
104 | - $code2 = new BackupCode(); |
|
105 | - $code2->setUsed('0'); |
|
106 | - $codes = [ |
|
107 | - $code1, |
|
108 | - $code2, |
|
109 | - ]; |
|
110 | - |
|
111 | - $this->mapper->expects($this->once()) |
|
112 | - ->method('getBackupCodes') |
|
113 | - ->with($user) |
|
114 | - ->willReturn($codes); |
|
115 | - |
|
116 | - $expected = [ |
|
117 | - 'enabled' => true, |
|
118 | - 'total' => 2, |
|
119 | - 'used' => 1, |
|
120 | - ]; |
|
121 | - $this->assertEquals($expected, $this->storage->getBackupCodesState($user)); |
|
122 | - } |
|
123 | - |
|
124 | - public function testGetBackupCodeDisabled(): void { |
|
125 | - $user = $this->createMock(IUser::class); |
|
126 | - |
|
127 | - $codes = []; |
|
128 | - |
|
129 | - $this->mapper->expects($this->once()) |
|
130 | - ->method('getBackupCodes') |
|
131 | - ->with($user) |
|
132 | - ->willReturn($codes); |
|
133 | - |
|
134 | - $expected = [ |
|
135 | - 'enabled' => false, |
|
136 | - 'total' => 0, |
|
137 | - 'used' => 0, |
|
138 | - ]; |
|
139 | - $this->assertEquals($expected, $this->storage->getBackupCodesState($user)); |
|
140 | - } |
|
141 | - |
|
142 | - public function testValidateCode(): void { |
|
143 | - $user = $this->createMock(IUser::class); |
|
144 | - $code = new BackupCode(); |
|
145 | - $code->setUsed(0); |
|
146 | - $code->setCode('HASHEDVALUE'); |
|
147 | - $codes = [ |
|
148 | - $code, |
|
149 | - ]; |
|
150 | - |
|
151 | - $this->mapper->expects($this->once()) |
|
152 | - ->method('getBackupCodes') |
|
153 | - ->with($user) |
|
154 | - ->willReturn($codes); |
|
155 | - $this->hasher->expects($this->once()) |
|
156 | - ->method('verify') |
|
157 | - ->with('CHALLENGE', 'HASHEDVALUE', $this->anything()) |
|
158 | - ->willReturn(true); |
|
159 | - $this->mapper->expects($this->once()) |
|
160 | - ->method('update') |
|
161 | - ->with($code); |
|
162 | - |
|
163 | - $this->assertTrue($this->storage->validateCode($user, 'CHALLENGE')); |
|
164 | - |
|
165 | - $this->assertEquals(1, $code->getUsed()); |
|
166 | - } |
|
167 | - |
|
168 | - public function testValidateUsedCode(): void { |
|
169 | - $user = $this->createMock(IUser::class); |
|
170 | - $code = new BackupCode(); |
|
171 | - $code->setUsed('1'); |
|
172 | - $code->setCode('HASHEDVALUE'); |
|
173 | - $codes = [ |
|
174 | - $code, |
|
175 | - ]; |
|
176 | - |
|
177 | - $this->mapper->expects($this->once()) |
|
178 | - ->method('getBackupCodes') |
|
179 | - ->with($user) |
|
180 | - ->willReturn($codes); |
|
181 | - $this->hasher->expects($this->never()) |
|
182 | - ->method('verify'); |
|
183 | - $this->mapper->expects($this->never()) |
|
184 | - ->method('update'); |
|
185 | - |
|
186 | - $this->assertFalse($this->storage->validateCode($user, 'CHALLENGE')); |
|
187 | - } |
|
188 | - |
|
189 | - public function testValidateCodeWithWrongHash(): void { |
|
190 | - $user = $this->createMock(IUser::class); |
|
191 | - $code = new BackupCode(); |
|
192 | - $code->setUsed(0); |
|
193 | - $code->setCode('HASHEDVALUE'); |
|
194 | - $codes = [ |
|
195 | - $code, |
|
196 | - ]; |
|
197 | - |
|
198 | - $this->mapper->expects($this->once()) |
|
199 | - ->method('getBackupCodes') |
|
200 | - ->with($user) |
|
201 | - ->willReturn($codes); |
|
202 | - $this->hasher->expects($this->once()) |
|
203 | - ->method('verify') |
|
204 | - ->with('CHALLENGE', 'HASHEDVALUE') |
|
205 | - ->willReturn(false); |
|
206 | - $this->mapper->expects($this->never()) |
|
207 | - ->method('update'); |
|
208 | - |
|
209 | - $this->assertFalse($this->storage->validateCode($user, 'CHALLENGE')); |
|
210 | - } |
|
211 | - |
|
212 | - public function testDeleteCodes(): void { |
|
213 | - $user = $this->createMock(IUser::class); |
|
214 | - $this->mapper->expects($this->once()) |
|
215 | - ->method('deleteCodes') |
|
216 | - ->with($user); |
|
217 | - |
|
218 | - $this->storage->deleteCodes($user); |
|
219 | - } |
|
23 | + private BackupCodeMapper&MockObject $mapper; |
|
24 | + private ISecureRandom&MockObject $random; |
|
25 | + private IHasher&MockObject $hasher; |
|
26 | + private IEventDispatcher&MockObject $eventDispatcher; |
|
27 | + private BackupCodeStorage $storage; |
|
28 | + |
|
29 | + protected function setUp(): void { |
|
30 | + parent::setUp(); |
|
31 | + |
|
32 | + $this->mapper = $this->createMock(BackupCodeMapper::class); |
|
33 | + $this->random = $this->createMock(ISecureRandom::class); |
|
34 | + $this->hasher = $this->createMock(IHasher::class); |
|
35 | + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); |
|
36 | + |
|
37 | + $this->storage = new BackupCodeStorage($this->mapper, $this->random, $this->hasher, $this->eventDispatcher); |
|
38 | + } |
|
39 | + |
|
40 | + public function testCreateCodes(): void { |
|
41 | + $user = $this->createMock(IUser::class); |
|
42 | + $number = 5; |
|
43 | + $user->method('getUID')->willReturn('fritz'); |
|
44 | + $this->random->expects($this->exactly($number)) |
|
45 | + ->method('generate') |
|
46 | + ->with(16, ISecureRandom::CHAR_HUMAN_READABLE) |
|
47 | + ->willReturn('CODEABCDEF'); |
|
48 | + $this->hasher->expects($this->exactly($number)) |
|
49 | + ->method('hash') |
|
50 | + ->with('CODEABCDEF') |
|
51 | + ->willReturn('HASHEDCODE'); |
|
52 | + $row = new BackupCode(); |
|
53 | + $row->setUserId('fritz'); |
|
54 | + $row->setCode('HASHEDCODE'); |
|
55 | + $row->setUsed(0); |
|
56 | + $this->mapper->expects($this->exactly($number)) |
|
57 | + ->method('insert') |
|
58 | + ->with($this->equalTo($row)); |
|
59 | + $this->eventDispatcher->expects($this->once()) |
|
60 | + ->method('dispatchTyped') |
|
61 | + ->with( |
|
62 | + $this->equalTo(new CodesGenerated($user)) |
|
63 | + ); |
|
64 | + |
|
65 | + $codes = $this->storage->createCodes($user, $number); |
|
66 | + $this->assertCount($number, $codes); |
|
67 | + foreach ($codes as $code) { |
|
68 | + $this->assertEquals('CODEABCDEF', $code); |
|
69 | + } |
|
70 | + } |
|
71 | + |
|
72 | + public function testHasBackupCodes(): void { |
|
73 | + $user = $this->createMock(IUser::class); |
|
74 | + $codes = [ |
|
75 | + new BackupCode(), |
|
76 | + new BackupCode(), |
|
77 | + ]; |
|
78 | + |
|
79 | + $this->mapper->expects($this->once()) |
|
80 | + ->method('getBackupCodes') |
|
81 | + ->with($user) |
|
82 | + ->willReturn($codes); |
|
83 | + |
|
84 | + $this->assertTrue($this->storage->hasBackupCodes($user)); |
|
85 | + } |
|
86 | + |
|
87 | + public function testHasBackupCodesNoCodes(): void { |
|
88 | + $user = $this->createMock(IUser::class); |
|
89 | + $codes = []; |
|
90 | + |
|
91 | + $this->mapper->expects($this->once()) |
|
92 | + ->method('getBackupCodes') |
|
93 | + ->with($user) |
|
94 | + ->willReturn($codes); |
|
95 | + |
|
96 | + $this->assertFalse($this->storage->hasBackupCodes($user)); |
|
97 | + } |
|
98 | + |
|
99 | + public function testGetBackupCodeState(): void { |
|
100 | + $user = $this->createMock(IUser::class); |
|
101 | + |
|
102 | + $code1 = new BackupCode(); |
|
103 | + $code1->setUsed(1); |
|
104 | + $code2 = new BackupCode(); |
|
105 | + $code2->setUsed('0'); |
|
106 | + $codes = [ |
|
107 | + $code1, |
|
108 | + $code2, |
|
109 | + ]; |
|
110 | + |
|
111 | + $this->mapper->expects($this->once()) |
|
112 | + ->method('getBackupCodes') |
|
113 | + ->with($user) |
|
114 | + ->willReturn($codes); |
|
115 | + |
|
116 | + $expected = [ |
|
117 | + 'enabled' => true, |
|
118 | + 'total' => 2, |
|
119 | + 'used' => 1, |
|
120 | + ]; |
|
121 | + $this->assertEquals($expected, $this->storage->getBackupCodesState($user)); |
|
122 | + } |
|
123 | + |
|
124 | + public function testGetBackupCodeDisabled(): void { |
|
125 | + $user = $this->createMock(IUser::class); |
|
126 | + |
|
127 | + $codes = []; |
|
128 | + |
|
129 | + $this->mapper->expects($this->once()) |
|
130 | + ->method('getBackupCodes') |
|
131 | + ->with($user) |
|
132 | + ->willReturn($codes); |
|
133 | + |
|
134 | + $expected = [ |
|
135 | + 'enabled' => false, |
|
136 | + 'total' => 0, |
|
137 | + 'used' => 0, |
|
138 | + ]; |
|
139 | + $this->assertEquals($expected, $this->storage->getBackupCodesState($user)); |
|
140 | + } |
|
141 | + |
|
142 | + public function testValidateCode(): void { |
|
143 | + $user = $this->createMock(IUser::class); |
|
144 | + $code = new BackupCode(); |
|
145 | + $code->setUsed(0); |
|
146 | + $code->setCode('HASHEDVALUE'); |
|
147 | + $codes = [ |
|
148 | + $code, |
|
149 | + ]; |
|
150 | + |
|
151 | + $this->mapper->expects($this->once()) |
|
152 | + ->method('getBackupCodes') |
|
153 | + ->with($user) |
|
154 | + ->willReturn($codes); |
|
155 | + $this->hasher->expects($this->once()) |
|
156 | + ->method('verify') |
|
157 | + ->with('CHALLENGE', 'HASHEDVALUE', $this->anything()) |
|
158 | + ->willReturn(true); |
|
159 | + $this->mapper->expects($this->once()) |
|
160 | + ->method('update') |
|
161 | + ->with($code); |
|
162 | + |
|
163 | + $this->assertTrue($this->storage->validateCode($user, 'CHALLENGE')); |
|
164 | + |
|
165 | + $this->assertEquals(1, $code->getUsed()); |
|
166 | + } |
|
167 | + |
|
168 | + public function testValidateUsedCode(): void { |
|
169 | + $user = $this->createMock(IUser::class); |
|
170 | + $code = new BackupCode(); |
|
171 | + $code->setUsed('1'); |
|
172 | + $code->setCode('HASHEDVALUE'); |
|
173 | + $codes = [ |
|
174 | + $code, |
|
175 | + ]; |
|
176 | + |
|
177 | + $this->mapper->expects($this->once()) |
|
178 | + ->method('getBackupCodes') |
|
179 | + ->with($user) |
|
180 | + ->willReturn($codes); |
|
181 | + $this->hasher->expects($this->never()) |
|
182 | + ->method('verify'); |
|
183 | + $this->mapper->expects($this->never()) |
|
184 | + ->method('update'); |
|
185 | + |
|
186 | + $this->assertFalse($this->storage->validateCode($user, 'CHALLENGE')); |
|
187 | + } |
|
188 | + |
|
189 | + public function testValidateCodeWithWrongHash(): void { |
|
190 | + $user = $this->createMock(IUser::class); |
|
191 | + $code = new BackupCode(); |
|
192 | + $code->setUsed(0); |
|
193 | + $code->setCode('HASHEDVALUE'); |
|
194 | + $codes = [ |
|
195 | + $code, |
|
196 | + ]; |
|
197 | + |
|
198 | + $this->mapper->expects($this->once()) |
|
199 | + ->method('getBackupCodes') |
|
200 | + ->with($user) |
|
201 | + ->willReturn($codes); |
|
202 | + $this->hasher->expects($this->once()) |
|
203 | + ->method('verify') |
|
204 | + ->with('CHALLENGE', 'HASHEDVALUE') |
|
205 | + ->willReturn(false); |
|
206 | + $this->mapper->expects($this->never()) |
|
207 | + ->method('update'); |
|
208 | + |
|
209 | + $this->assertFalse($this->storage->validateCode($user, 'CHALLENGE')); |
|
210 | + } |
|
211 | + |
|
212 | + public function testDeleteCodes(): void { |
|
213 | + $user = $this->createMock(IUser::class); |
|
214 | + $this->mapper->expects($this->once()) |
|
215 | + ->method('deleteCodes') |
|
216 | + ->with($user); |
|
217 | + |
|
218 | + $this->storage->deleteCodes($user); |
|
219 | + } |
|
220 | 220 | } |
@@ -15,27 +15,27 @@ |
||
15 | 15 | use Test\TestCase; |
16 | 16 | |
17 | 17 | class CheckBackupCodeTest extends TestCase { |
18 | - private IJobList&MockObject $jobList; |
|
19 | - private CheckBackupCodes $checkBackupsCodes; |
|
18 | + private IJobList&MockObject $jobList; |
|
19 | + private CheckBackupCodes $checkBackupsCodes; |
|
20 | 20 | |
21 | - protected function setUp(): void { |
|
22 | - parent::setUp(); |
|
21 | + protected function setUp(): void { |
|
22 | + parent::setUp(); |
|
23 | 23 | |
24 | - $this->jobList = $this->createMock(IJobList::class); |
|
25 | - $this->checkBackupsCodes = new CheckBackupCodes($this->jobList); |
|
26 | - } |
|
24 | + $this->jobList = $this->createMock(IJobList::class); |
|
25 | + $this->checkBackupsCodes = new CheckBackupCodes($this->jobList); |
|
26 | + } |
|
27 | 27 | |
28 | - public function testGetName(): void { |
|
29 | - $this->assertSame('Add background job to check for backup codes', $this->checkBackupsCodes->getName()); |
|
30 | - } |
|
28 | + public function testGetName(): void { |
|
29 | + $this->assertSame('Add background job to check for backup codes', $this->checkBackupsCodes->getName()); |
|
30 | + } |
|
31 | 31 | |
32 | - public function testRun(): void { |
|
33 | - $this->jobList->expects($this->once()) |
|
34 | - ->method('add') |
|
35 | - ->with( |
|
36 | - $this->equalTo(\OCA\TwoFactorBackupCodes\BackgroundJob\CheckBackupCodes::class) |
|
37 | - ); |
|
32 | + public function testRun(): void { |
|
33 | + $this->jobList->expects($this->once()) |
|
34 | + ->method('add') |
|
35 | + ->with( |
|
36 | + $this->equalTo(\OCA\TwoFactorBackupCodes\BackgroundJob\CheckBackupCodes::class) |
|
37 | + ); |
|
38 | 38 | |
39 | - $this->checkBackupsCodes->run($this->createMock(IOutput::class)); |
|
40 | - } |
|
39 | + $this->checkBackupsCodes->run($this->createMock(IOutput::class)); |
|
40 | + } |
|
41 | 41 | } |
@@ -19,54 +19,54 @@ |
||
19 | 19 | use Test\TestCase; |
20 | 20 | |
21 | 21 | class ActivityPublisherTest extends TestCase { |
22 | - private IManager&MockObject $activityManager; |
|
23 | - private LoggerInterface&MockObject $logger; |
|
24 | - private ActivityPublisher $listener; |
|
22 | + private IManager&MockObject $activityManager; |
|
23 | + private LoggerInterface&MockObject $logger; |
|
24 | + private ActivityPublisher $listener; |
|
25 | 25 | |
26 | - protected function setUp(): void { |
|
27 | - parent::setUp(); |
|
26 | + protected function setUp(): void { |
|
27 | + parent::setUp(); |
|
28 | 28 | |
29 | - $this->activityManager = $this->createMock(IManager::class); |
|
30 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
29 | + $this->activityManager = $this->createMock(IManager::class); |
|
30 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
31 | 31 | |
32 | - $this->listener = new ActivityPublisher($this->activityManager, $this->logger); |
|
33 | - } |
|
32 | + $this->listener = new ActivityPublisher($this->activityManager, $this->logger); |
|
33 | + } |
|
34 | 34 | |
35 | - public function testHandleGenericEvent(): void { |
|
36 | - $event = $this->createMock(Event::class); |
|
37 | - $this->activityManager->expects($this->never()) |
|
38 | - ->method('publish'); |
|
35 | + public function testHandleGenericEvent(): void { |
|
36 | + $event = $this->createMock(Event::class); |
|
37 | + $this->activityManager->expects($this->never()) |
|
38 | + ->method('publish'); |
|
39 | 39 | |
40 | - $this->listener->handle($event); |
|
41 | - } |
|
40 | + $this->listener->handle($event); |
|
41 | + } |
|
42 | 42 | |
43 | - public function testHandleCodesGeneratedEvent(): void { |
|
44 | - $user = $this->createMock(IUser::class); |
|
45 | - $user->method('getUID')->willReturn('fritz'); |
|
46 | - $event = new CodesGenerated($user); |
|
47 | - $activityEvent = $this->createMock(IEvent::class); |
|
48 | - $this->activityManager->expects($this->once()) |
|
49 | - ->method('generateEvent') |
|
50 | - ->willReturn($activityEvent); |
|
51 | - $activityEvent->expects($this->once()) |
|
52 | - ->method('setApp') |
|
53 | - ->with('twofactor_backupcodes') |
|
54 | - ->willReturnSelf(); |
|
55 | - $activityEvent->expects($this->once()) |
|
56 | - ->method('setType') |
|
57 | - ->with('security') |
|
58 | - ->willReturnSelf(); |
|
59 | - $activityEvent->expects($this->once()) |
|
60 | - ->method('setAuthor') |
|
61 | - ->with('fritz') |
|
62 | - ->willReturnSelf(); |
|
63 | - $activityEvent->expects($this->once()) |
|
64 | - ->method('setAffectedUser') |
|
65 | - ->with('fritz') |
|
66 | - ->willReturnSelf(); |
|
67 | - $this->activityManager->expects($this->once()) |
|
68 | - ->method('publish'); |
|
43 | + public function testHandleCodesGeneratedEvent(): void { |
|
44 | + $user = $this->createMock(IUser::class); |
|
45 | + $user->method('getUID')->willReturn('fritz'); |
|
46 | + $event = new CodesGenerated($user); |
|
47 | + $activityEvent = $this->createMock(IEvent::class); |
|
48 | + $this->activityManager->expects($this->once()) |
|
49 | + ->method('generateEvent') |
|
50 | + ->willReturn($activityEvent); |
|
51 | + $activityEvent->expects($this->once()) |
|
52 | + ->method('setApp') |
|
53 | + ->with('twofactor_backupcodes') |
|
54 | + ->willReturnSelf(); |
|
55 | + $activityEvent->expects($this->once()) |
|
56 | + ->method('setType') |
|
57 | + ->with('security') |
|
58 | + ->willReturnSelf(); |
|
59 | + $activityEvent->expects($this->once()) |
|
60 | + ->method('setAuthor') |
|
61 | + ->with('fritz') |
|
62 | + ->willReturnSelf(); |
|
63 | + $activityEvent->expects($this->once()) |
|
64 | + ->method('setAffectedUser') |
|
65 | + ->with('fritz') |
|
66 | + ->willReturnSelf(); |
|
67 | + $this->activityManager->expects($this->once()) |
|
68 | + ->method('publish'); |
|
69 | 69 | |
70 | - $this->listener->handle($event); |
|
71 | - } |
|
70 | + $this->listener->handle($event); |
|
71 | + } |
|
72 | 72 | } |
@@ -19,70 +19,70 @@ |
||
19 | 19 | use Test\TestCase; |
20 | 20 | |
21 | 21 | class ProviderDisabledTest extends TestCase { |
22 | - private IRegistry&MockObject $registy; |
|
23 | - private IJobList&MockObject $jobList; |
|
24 | - private ProviderDisabled $listener; |
|
25 | - |
|
26 | - protected function setUp(): void { |
|
27 | - parent::setUp(); |
|
28 | - |
|
29 | - $this->registy = $this->createMock(IRegistry::class); |
|
30 | - $this->jobList = $this->createMock(IJobList::class); |
|
31 | - |
|
32 | - $this->listener = new ProviderDisabled($this->registy, $this->jobList); |
|
33 | - } |
|
34 | - |
|
35 | - public function testHandleGenericEvent(): void { |
|
36 | - $event = $this->createMock(Event::class); |
|
37 | - $this->jobList->expects($this->never()) |
|
38 | - ->method($this->anything()); |
|
39 | - |
|
40 | - $this->listener->handle($event); |
|
41 | - } |
|
42 | - |
|
43 | - public function testHandleStillActiveProvider(): void { |
|
44 | - $user = $this->createMock(IUser::class); |
|
45 | - $user->method('getUID') |
|
46 | - ->willReturn('myUID'); |
|
47 | - $event = $this->createMock(TwoFactorProviderForUserUnregistered::class); |
|
48 | - $event->method('getUser') |
|
49 | - ->willReturn($user); |
|
50 | - |
|
51 | - $this->registy->method('getProviderStates') |
|
52 | - ->with($user) |
|
53 | - ->willReturn([ |
|
54 | - 'backup_codes' => false, |
|
55 | - 'foo' => true, |
|
56 | - ]); |
|
57 | - |
|
58 | - $this->jobList->expects($this->never()) |
|
59 | - ->method($this->anything()); |
|
60 | - |
|
61 | - $this->listener->handle($event); |
|
62 | - } |
|
63 | - |
|
64 | - public function testHandleNoActiveProvider(): void { |
|
65 | - $user = $this->createMock(IUser::class); |
|
66 | - $user->method('getUID') |
|
67 | - ->willReturn('myUID'); |
|
68 | - $event = $this->createMock(TwoFactorProviderForUserUnregistered::class); |
|
69 | - $event->method('getUser') |
|
70 | - ->willReturn($user); |
|
71 | - |
|
72 | - $this->registy->method('getProviderStates') |
|
73 | - ->with($user) |
|
74 | - ->willReturn([ |
|
75 | - 'backup_codes' => false, |
|
76 | - 'foo' => false, |
|
77 | - ]); |
|
78 | - |
|
79 | - $this->jobList->expects($this->once()) |
|
80 | - ->method('remove') |
|
81 | - ->with( |
|
82 | - $this->equalTo(RememberBackupCodesJob::class), |
|
83 | - $this->equalTo(['uid' => 'myUID']) |
|
84 | - ); |
|
85 | - |
|
86 | - $this->listener->handle($event); |
|
87 | - } |
|
22 | + private IRegistry&MockObject $registy; |
|
23 | + private IJobList&MockObject $jobList; |
|
24 | + private ProviderDisabled $listener; |
|
25 | + |
|
26 | + protected function setUp(): void { |
|
27 | + parent::setUp(); |
|
28 | + |
|
29 | + $this->registy = $this->createMock(IRegistry::class); |
|
30 | + $this->jobList = $this->createMock(IJobList::class); |
|
31 | + |
|
32 | + $this->listener = new ProviderDisabled($this->registy, $this->jobList); |
|
33 | + } |
|
34 | + |
|
35 | + public function testHandleGenericEvent(): void { |
|
36 | + $event = $this->createMock(Event::class); |
|
37 | + $this->jobList->expects($this->never()) |
|
38 | + ->method($this->anything()); |
|
39 | + |
|
40 | + $this->listener->handle($event); |
|
41 | + } |
|
42 | + |
|
43 | + public function testHandleStillActiveProvider(): void { |
|
44 | + $user = $this->createMock(IUser::class); |
|
45 | + $user->method('getUID') |
|
46 | + ->willReturn('myUID'); |
|
47 | + $event = $this->createMock(TwoFactorProviderForUserUnregistered::class); |
|
48 | + $event->method('getUser') |
|
49 | + ->willReturn($user); |
|
50 | + |
|
51 | + $this->registy->method('getProviderStates') |
|
52 | + ->with($user) |
|
53 | + ->willReturn([ |
|
54 | + 'backup_codes' => false, |
|
55 | + 'foo' => true, |
|
56 | + ]); |
|
57 | + |
|
58 | + $this->jobList->expects($this->never()) |
|
59 | + ->method($this->anything()); |
|
60 | + |
|
61 | + $this->listener->handle($event); |
|
62 | + } |
|
63 | + |
|
64 | + public function testHandleNoActiveProvider(): void { |
|
65 | + $user = $this->createMock(IUser::class); |
|
66 | + $user->method('getUID') |
|
67 | + ->willReturn('myUID'); |
|
68 | + $event = $this->createMock(TwoFactorProviderForUserUnregistered::class); |
|
69 | + $event->method('getUser') |
|
70 | + ->willReturn($user); |
|
71 | + |
|
72 | + $this->registy->method('getProviderStates') |
|
73 | + ->with($user) |
|
74 | + ->willReturn([ |
|
75 | + 'backup_codes' => false, |
|
76 | + 'foo' => false, |
|
77 | + ]); |
|
78 | + |
|
79 | + $this->jobList->expects($this->once()) |
|
80 | + ->method('remove') |
|
81 | + ->with( |
|
82 | + $this->equalTo(RememberBackupCodesJob::class), |
|
83 | + $this->equalTo(['uid' => 'myUID']) |
|
84 | + ); |
|
85 | + |
|
86 | + $this->listener->handle($event); |
|
87 | + } |
|
88 | 88 | } |
@@ -18,37 +18,37 @@ |
||
18 | 18 | use Test\TestCase; |
19 | 19 | |
20 | 20 | class RegistryUpdaterTest extends TestCase { |
21 | - private IRegistry&MockObject $registry; |
|
22 | - private BackupCodesProvider&MockObject $provider; |
|
23 | - private RegistryUpdater $listener; |
|
24 | - |
|
25 | - protected function setUp(): void { |
|
26 | - parent::setUp(); |
|
27 | - |
|
28 | - $this->registry = $this->createMock(IRegistry::class); |
|
29 | - $this->provider = $this->createMock(BackupCodesProvider::class); |
|
30 | - |
|
31 | - $this->listener = new RegistryUpdater($this->registry, $this->provider); |
|
32 | - } |
|
33 | - |
|
34 | - public function testHandleGenericEvent(): void { |
|
35 | - $event = $this->createMock(Event::class); |
|
36 | - $this->registry->expects($this->never()) |
|
37 | - ->method('enableProviderFor'); |
|
38 | - |
|
39 | - $this->listener->handle($event); |
|
40 | - } |
|
41 | - |
|
42 | - public function testHandleCodesGeneratedEvent(): void { |
|
43 | - $user = $this->createMock(IUser::class); |
|
44 | - $event = new CodesGenerated($user); |
|
45 | - $this->registry->expects($this->once()) |
|
46 | - ->method('enableProviderFor') |
|
47 | - ->with( |
|
48 | - $this->provider, |
|
49 | - $user |
|
50 | - ); |
|
51 | - |
|
52 | - $this->listener->handle($event); |
|
53 | - } |
|
21 | + private IRegistry&MockObject $registry; |
|
22 | + private BackupCodesProvider&MockObject $provider; |
|
23 | + private RegistryUpdater $listener; |
|
24 | + |
|
25 | + protected function setUp(): void { |
|
26 | + parent::setUp(); |
|
27 | + |
|
28 | + $this->registry = $this->createMock(IRegistry::class); |
|
29 | + $this->provider = $this->createMock(BackupCodesProvider::class); |
|
30 | + |
|
31 | + $this->listener = new RegistryUpdater($this->registry, $this->provider); |
|
32 | + } |
|
33 | + |
|
34 | + public function testHandleGenericEvent(): void { |
|
35 | + $event = $this->createMock(Event::class); |
|
36 | + $this->registry->expects($this->never()) |
|
37 | + ->method('enableProviderFor'); |
|
38 | + |
|
39 | + $this->listener->handle($event); |
|
40 | + } |
|
41 | + |
|
42 | + public function testHandleCodesGeneratedEvent(): void { |
|
43 | + $user = $this->createMock(IUser::class); |
|
44 | + $event = new CodesGenerated($user); |
|
45 | + $this->registry->expects($this->once()) |
|
46 | + ->method('enableProviderFor') |
|
47 | + ->with( |
|
48 | + $this->provider, |
|
49 | + $user |
|
50 | + ); |
|
51 | + |
|
52 | + $this->listener->handle($event); |
|
53 | + } |
|
54 | 54 | } |
@@ -19,68 +19,68 @@ |
||
19 | 19 | use Test\TestCase; |
20 | 20 | |
21 | 21 | class ProviderEnabledTest extends TestCase { |
22 | - private IRegistry&MockObject $registy; |
|
23 | - private IJobList&MockObject $jobList; |
|
24 | - private ProviderEnabled $listener; |
|
25 | - |
|
26 | - protected function setUp(): void { |
|
27 | - parent::setUp(); |
|
28 | - |
|
29 | - $this->registy = $this->createMock(IRegistry::class); |
|
30 | - $this->jobList = $this->createMock(IJobList::class); |
|
31 | - |
|
32 | - $this->listener = new ProviderEnabled($this->registy, $this->jobList); |
|
33 | - } |
|
34 | - |
|
35 | - public function testHandleGenericEvent(): void { |
|
36 | - $event = $this->createMock(Event::class); |
|
37 | - $this->jobList->expects($this->never()) |
|
38 | - ->method($this->anything()); |
|
39 | - |
|
40 | - $this->listener->handle($event); |
|
41 | - } |
|
42 | - |
|
43 | - public function testHandleCodesGeneratedEventAlraedyBackupcodes(): void { |
|
44 | - $user = $this->createMock(IUser::class); |
|
45 | - $user->method('getUID') |
|
46 | - ->willReturn('myUID'); |
|
47 | - $event = $this->createMock(TwoFactorProviderForUserRegistered::class); |
|
48 | - $event->method('getUser') |
|
49 | - ->willReturn($user); |
|
50 | - |
|
51 | - $this->registy->method('getProviderStates') |
|
52 | - ->with($user) |
|
53 | - ->willReturn([ |
|
54 | - 'backup_codes' => true, |
|
55 | - ]); |
|
56 | - |
|
57 | - $this->jobList->expects($this->never()) |
|
58 | - ->method($this->anything()); |
|
59 | - |
|
60 | - $this->listener->handle($event); |
|
61 | - } |
|
62 | - |
|
63 | - public function testHandleCodesGeneratedEventNoBackupcodes(): void { |
|
64 | - $user = $this->createMock(IUser::class); |
|
65 | - $user->method('getUID') |
|
66 | - ->willReturn('myUID'); |
|
67 | - $event = $this->createMock(TwoFactorProviderForUserRegistered::class); |
|
68 | - $event->method('getUser') |
|
69 | - ->willReturn($user); |
|
70 | - |
|
71 | - $this->registy->method('getProviderStates') |
|
72 | - ->with($user) |
|
73 | - ->willReturn([ |
|
74 | - 'backup_codes' => false, |
|
75 | - ]); |
|
76 | - |
|
77 | - $this->jobList->expects($this->once()) |
|
78 | - ->method('add') |
|
79 | - ->with( |
|
80 | - $this->equalTo(RememberBackupCodesJob::class), |
|
81 | - $this->equalTo(['uid' => 'myUID']) |
|
82 | - ); |
|
83 | - |
|
84 | - $this->listener->handle($event); |
|
85 | - } |
|
22 | + private IRegistry&MockObject $registy; |
|
23 | + private IJobList&MockObject $jobList; |
|
24 | + private ProviderEnabled $listener; |
|
25 | + |
|
26 | + protected function setUp(): void { |
|
27 | + parent::setUp(); |
|
28 | + |
|
29 | + $this->registy = $this->createMock(IRegistry::class); |
|
30 | + $this->jobList = $this->createMock(IJobList::class); |
|
31 | + |
|
32 | + $this->listener = new ProviderEnabled($this->registy, $this->jobList); |
|
33 | + } |
|
34 | + |
|
35 | + public function testHandleGenericEvent(): void { |
|
36 | + $event = $this->createMock(Event::class); |
|
37 | + $this->jobList->expects($this->never()) |
|
38 | + ->method($this->anything()); |
|
39 | + |
|
40 | + $this->listener->handle($event); |
|
41 | + } |
|
42 | + |
|
43 | + public function testHandleCodesGeneratedEventAlraedyBackupcodes(): void { |
|
44 | + $user = $this->createMock(IUser::class); |
|
45 | + $user->method('getUID') |
|
46 | + ->willReturn('myUID'); |
|
47 | + $event = $this->createMock(TwoFactorProviderForUserRegistered::class); |
|
48 | + $event->method('getUser') |
|
49 | + ->willReturn($user); |
|
50 | + |
|
51 | + $this->registy->method('getProviderStates') |
|
52 | + ->with($user) |
|
53 | + ->willReturn([ |
|
54 | + 'backup_codes' => true, |
|
55 | + ]); |
|
56 | + |
|
57 | + $this->jobList->expects($this->never()) |
|
58 | + ->method($this->anything()); |
|
59 | + |
|
60 | + $this->listener->handle($event); |
|
61 | + } |
|
62 | + |
|
63 | + public function testHandleCodesGeneratedEventNoBackupcodes(): void { |
|
64 | + $user = $this->createMock(IUser::class); |
|
65 | + $user->method('getUID') |
|
66 | + ->willReturn('myUID'); |
|
67 | + $event = $this->createMock(TwoFactorProviderForUserRegistered::class); |
|
68 | + $event->method('getUser') |
|
69 | + ->willReturn($user); |
|
70 | + |
|
71 | + $this->registy->method('getProviderStates') |
|
72 | + ->with($user) |
|
73 | + ->willReturn([ |
|
74 | + 'backup_codes' => false, |
|
75 | + ]); |
|
76 | + |
|
77 | + $this->jobList->expects($this->once()) |
|
78 | + ->method('add') |
|
79 | + ->with( |
|
80 | + $this->equalTo(RememberBackupCodesJob::class), |
|
81 | + $this->equalTo(['uid' => 'myUID']) |
|
82 | + ); |
|
83 | + |
|
84 | + $this->listener->handle($event); |
|
85 | + } |
|
86 | 86 | } |
@@ -19,41 +19,41 @@ |
||
19 | 19 | use Test\TestCase; |
20 | 20 | |
21 | 21 | class ClearNotificationsTest extends TestCase { |
22 | - private IManager&MockObject $notificationManager; |
|
23 | - private ClearNotifications $listener; |
|
24 | - |
|
25 | - protected function setUp(): void { |
|
26 | - parent::setUp(); |
|
27 | - |
|
28 | - $this->notificationManager = $this->createMock(IManager::class); |
|
29 | - $this->notificationManager->method('createNotification') |
|
30 | - ->willReturn(Server::get(IManager::class)->createNotification()); |
|
31 | - |
|
32 | - $this->listener = new ClearNotifications($this->notificationManager); |
|
33 | - } |
|
34 | - |
|
35 | - public function testHandleGenericEvent(): void { |
|
36 | - $event = $this->createMock(Event::class); |
|
37 | - $this->notificationManager->expects($this->never()) |
|
38 | - ->method($this->anything()); |
|
39 | - |
|
40 | - $this->listener->handle($event); |
|
41 | - } |
|
42 | - |
|
43 | - public function testHandleCodesGeneratedEvent(): void { |
|
44 | - $user = $this->createMock(IUser::class); |
|
45 | - $user->method('getUID')->willReturn('fritz'); |
|
46 | - $event = new CodesGenerated($user); |
|
47 | - |
|
48 | - $this->notificationManager->expects($this->once()) |
|
49 | - ->method('markProcessed') |
|
50 | - ->with($this->callback(function (INotification $n) { |
|
51 | - return $n->getUser() === 'fritz' && |
|
52 | - $n->getApp() === 'twofactor_backupcodes' && |
|
53 | - $n->getObjectType() === 'create' && |
|
54 | - $n->getObjectId() === 'codes'; |
|
55 | - })); |
|
56 | - |
|
57 | - $this->listener->handle($event); |
|
58 | - } |
|
22 | + private IManager&MockObject $notificationManager; |
|
23 | + private ClearNotifications $listener; |
|
24 | + |
|
25 | + protected function setUp(): void { |
|
26 | + parent::setUp(); |
|
27 | + |
|
28 | + $this->notificationManager = $this->createMock(IManager::class); |
|
29 | + $this->notificationManager->method('createNotification') |
|
30 | + ->willReturn(Server::get(IManager::class)->createNotification()); |
|
31 | + |
|
32 | + $this->listener = new ClearNotifications($this->notificationManager); |
|
33 | + } |
|
34 | + |
|
35 | + public function testHandleGenericEvent(): void { |
|
36 | + $event = $this->createMock(Event::class); |
|
37 | + $this->notificationManager->expects($this->never()) |
|
38 | + ->method($this->anything()); |
|
39 | + |
|
40 | + $this->listener->handle($event); |
|
41 | + } |
|
42 | + |
|
43 | + public function testHandleCodesGeneratedEvent(): void { |
|
44 | + $user = $this->createMock(IUser::class); |
|
45 | + $user->method('getUID')->willReturn('fritz'); |
|
46 | + $event = new CodesGenerated($user); |
|
47 | + |
|
48 | + $this->notificationManager->expects($this->once()) |
|
49 | + ->method('markProcessed') |
|
50 | + ->with($this->callback(function (INotification $n) { |
|
51 | + return $n->getUser() === 'fritz' && |
|
52 | + $n->getApp() === 'twofactor_backupcodes' && |
|
53 | + $n->getObjectType() === 'create' && |
|
54 | + $n->getObjectId() === 'codes'; |
|
55 | + })); |
|
56 | + |
|
57 | + $this->listener->handle($event); |
|
58 | + } |
|
59 | 59 | } |
@@ -17,98 +17,98 @@ |
||
17 | 17 | use Test\TestCase; |
18 | 18 | |
19 | 19 | class NotifierTest extends TestCase { |
20 | - protected IFactory&MockObject $factory; |
|
21 | - protected IURLGenerator&MockObject $url; |
|
22 | - protected IL10N&MockObject $l; |
|
23 | - protected Notifier $notifier; |
|
24 | - |
|
25 | - protected function setUp(): void { |
|
26 | - parent::setUp(); |
|
27 | - |
|
28 | - $this->l = $this->createMock(IL10N::class); |
|
29 | - $this->l->expects($this->any()) |
|
30 | - ->method('t') |
|
31 | - ->willReturnCallback(function ($string, $args) { |
|
32 | - return vsprintf($string, $args); |
|
33 | - }); |
|
34 | - $this->factory = $this->createMock(IFactory::class); |
|
35 | - $this->url = $this->createMock(IURLGenerator::class); |
|
36 | - $this->factory->expects($this->any()) |
|
37 | - ->method('get') |
|
38 | - ->willReturn($this->l); |
|
39 | - |
|
40 | - $this->notifier = new Notifier( |
|
41 | - $this->factory, |
|
42 | - $this->url |
|
43 | - ); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - public function testPrepareWrongApp(): void { |
|
48 | - $this->expectException(\InvalidArgumentException::class); |
|
49 | - |
|
50 | - /** @var INotification|MockObject $notification */ |
|
51 | - $notification = $this->createMock(INotification::class); |
|
52 | - $notification->expects($this->once()) |
|
53 | - ->method('getApp') |
|
54 | - ->willReturn('notifications'); |
|
55 | - $notification->expects($this->never()) |
|
56 | - ->method('getSubject'); |
|
57 | - |
|
58 | - $this->notifier->prepare($notification, 'en'); |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - public function testPrepareWrongSubject(): void { |
|
63 | - $this->expectException(\InvalidArgumentException::class); |
|
64 | - |
|
65 | - /** @var INotification|MockObject $notification */ |
|
66 | - $notification = $this->createMock(INotification::class); |
|
67 | - $notification->expects($this->once()) |
|
68 | - ->method('getApp') |
|
69 | - ->willReturn('twofactor_backupcodes'); |
|
70 | - $notification->expects($this->once()) |
|
71 | - ->method('getSubject') |
|
72 | - ->willReturn('wrong subject'); |
|
73 | - |
|
74 | - $this->notifier->prepare($notification, 'en'); |
|
75 | - } |
|
76 | - |
|
77 | - public function testPrepare(): void { |
|
78 | - /** @var INotification&MockObject $notification */ |
|
79 | - $notification = $this->createMock(INotification::class); |
|
80 | - |
|
81 | - $notification->expects($this->once()) |
|
82 | - ->method('getApp') |
|
83 | - ->willReturn('twofactor_backupcodes'); |
|
84 | - $notification->expects($this->once()) |
|
85 | - ->method('getSubject') |
|
86 | - ->willReturn('create_backupcodes'); |
|
87 | - |
|
88 | - $this->factory->expects($this->once()) |
|
89 | - ->method('get') |
|
90 | - ->with('twofactor_backupcodes', 'nl') |
|
91 | - ->willReturn($this->l); |
|
92 | - |
|
93 | - $notification->expects($this->once()) |
|
94 | - ->method('setParsedSubject') |
|
95 | - ->with('Generate backup codes') |
|
96 | - ->willReturnSelf(); |
|
97 | - $notification->expects($this->once()) |
|
98 | - ->method('setParsedMessage') |
|
99 | - ->with('You enabled two-factor authentication but did not generate backup codes yet. They are needed to restore access to your account in case you lose your second factor.') |
|
100 | - ->willReturnSelf(); |
|
101 | - |
|
102 | - $this->url->expects($this->once()) |
|
103 | - ->method('linkToRouteAbsolute') |
|
104 | - ->with('settings.PersonalSettings.index', ['section' => 'security']) |
|
105 | - ->willReturn('linkToRouteAbsolute'); |
|
106 | - $notification->expects($this->once()) |
|
107 | - ->method('setLink') |
|
108 | - ->with('linkToRouteAbsolute') |
|
109 | - ->willReturnSelf(); |
|
110 | - |
|
111 | - $return = $this->notifier->prepare($notification, 'nl'); |
|
112 | - $this->assertEquals($notification, $return); |
|
113 | - } |
|
20 | + protected IFactory&MockObject $factory; |
|
21 | + protected IURLGenerator&MockObject $url; |
|
22 | + protected IL10N&MockObject $l; |
|
23 | + protected Notifier $notifier; |
|
24 | + |
|
25 | + protected function setUp(): void { |
|
26 | + parent::setUp(); |
|
27 | + |
|
28 | + $this->l = $this->createMock(IL10N::class); |
|
29 | + $this->l->expects($this->any()) |
|
30 | + ->method('t') |
|
31 | + ->willReturnCallback(function ($string, $args) { |
|
32 | + return vsprintf($string, $args); |
|
33 | + }); |
|
34 | + $this->factory = $this->createMock(IFactory::class); |
|
35 | + $this->url = $this->createMock(IURLGenerator::class); |
|
36 | + $this->factory->expects($this->any()) |
|
37 | + ->method('get') |
|
38 | + ->willReturn($this->l); |
|
39 | + |
|
40 | + $this->notifier = new Notifier( |
|
41 | + $this->factory, |
|
42 | + $this->url |
|
43 | + ); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + public function testPrepareWrongApp(): void { |
|
48 | + $this->expectException(\InvalidArgumentException::class); |
|
49 | + |
|
50 | + /** @var INotification|MockObject $notification */ |
|
51 | + $notification = $this->createMock(INotification::class); |
|
52 | + $notification->expects($this->once()) |
|
53 | + ->method('getApp') |
|
54 | + ->willReturn('notifications'); |
|
55 | + $notification->expects($this->never()) |
|
56 | + ->method('getSubject'); |
|
57 | + |
|
58 | + $this->notifier->prepare($notification, 'en'); |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + public function testPrepareWrongSubject(): void { |
|
63 | + $this->expectException(\InvalidArgumentException::class); |
|
64 | + |
|
65 | + /** @var INotification|MockObject $notification */ |
|
66 | + $notification = $this->createMock(INotification::class); |
|
67 | + $notification->expects($this->once()) |
|
68 | + ->method('getApp') |
|
69 | + ->willReturn('twofactor_backupcodes'); |
|
70 | + $notification->expects($this->once()) |
|
71 | + ->method('getSubject') |
|
72 | + ->willReturn('wrong subject'); |
|
73 | + |
|
74 | + $this->notifier->prepare($notification, 'en'); |
|
75 | + } |
|
76 | + |
|
77 | + public function testPrepare(): void { |
|
78 | + /** @var INotification&MockObject $notification */ |
|
79 | + $notification = $this->createMock(INotification::class); |
|
80 | + |
|
81 | + $notification->expects($this->once()) |
|
82 | + ->method('getApp') |
|
83 | + ->willReturn('twofactor_backupcodes'); |
|
84 | + $notification->expects($this->once()) |
|
85 | + ->method('getSubject') |
|
86 | + ->willReturn('create_backupcodes'); |
|
87 | + |
|
88 | + $this->factory->expects($this->once()) |
|
89 | + ->method('get') |
|
90 | + ->with('twofactor_backupcodes', 'nl') |
|
91 | + ->willReturn($this->l); |
|
92 | + |
|
93 | + $notification->expects($this->once()) |
|
94 | + ->method('setParsedSubject') |
|
95 | + ->with('Generate backup codes') |
|
96 | + ->willReturnSelf(); |
|
97 | + $notification->expects($this->once()) |
|
98 | + ->method('setParsedMessage') |
|
99 | + ->with('You enabled two-factor authentication but did not generate backup codes yet. They are needed to restore access to your account in case you lose your second factor.') |
|
100 | + ->willReturnSelf(); |
|
101 | + |
|
102 | + $this->url->expects($this->once()) |
|
103 | + ->method('linkToRouteAbsolute') |
|
104 | + ->with('settings.PersonalSettings.index', ['section' => 'security']) |
|
105 | + ->willReturn('linkToRouteAbsolute'); |
|
106 | + $notification->expects($this->once()) |
|
107 | + ->method('setLink') |
|
108 | + ->with('linkToRouteAbsolute') |
|
109 | + ->willReturnSelf(); |
|
110 | + |
|
111 | + $return = $this->notifier->prepare($notification, 'nl'); |
|
112 | + $this->assertEquals($notification, $return); |
|
113 | + } |
|
114 | 114 | } |
@@ -19,90 +19,90 @@ |
||
19 | 19 | use Test\TestCase; |
20 | 20 | |
21 | 21 | class ProviderTest extends TestCase { |
22 | - private IFactory&MockObject $l10n; |
|
23 | - private IURLGenerator&MockObject $urlGenerator; |
|
24 | - private IManager&MockObject $activityManager; |
|
25 | - private Provider $provider; |
|
26 | - |
|
27 | - protected function setUp(): void { |
|
28 | - parent::setUp(); |
|
29 | - |
|
30 | - $this->l10n = $this->createMock(IFactory::class); |
|
31 | - $this->urlGenerator = $this->createMock(IURLGenerator::class); |
|
32 | - $this->activityManager = $this->createMock(IManager::class); |
|
33 | - |
|
34 | - $this->provider = new Provider($this->l10n, $this->urlGenerator, $this->activityManager); |
|
35 | - } |
|
36 | - |
|
37 | - public function testParseUnrelated(): void { |
|
38 | - $lang = 'ru'; |
|
39 | - $event = $this->createMock(IEvent::class); |
|
40 | - $event->expects($this->once()) |
|
41 | - ->method('getApp') |
|
42 | - ->willReturn('comments'); |
|
43 | - $this->expectException(UnknownActivityException::class); |
|
44 | - |
|
45 | - $this->provider->parse($lang, $event); |
|
46 | - } |
|
47 | - |
|
48 | - public static function subjectData(): array { |
|
49 | - return [ |
|
50 | - ['codes_generated'], |
|
51 | - ]; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @dataProvider subjectData |
|
56 | - */ |
|
57 | - public function testParse(string $subject): void { |
|
58 | - $lang = 'ru'; |
|
59 | - $event = $this->createMock(IEvent::class); |
|
60 | - $l = $this->createMock(IL10N::class); |
|
61 | - |
|
62 | - $event->expects($this->once()) |
|
63 | - ->method('getApp') |
|
64 | - ->willReturn('twofactor_backupcodes'); |
|
65 | - $this->l10n->expects($this->once()) |
|
66 | - ->method('get') |
|
67 | - ->with('twofactor_backupcodes', $lang) |
|
68 | - ->willReturn($l); |
|
69 | - $this->urlGenerator->expects($this->once()) |
|
70 | - ->method('imagePath') |
|
71 | - ->with('core', 'actions/password.svg') |
|
72 | - ->willReturn('path/to/image'); |
|
73 | - $this->urlGenerator->expects($this->once()) |
|
74 | - ->method('getAbsoluteURL') |
|
75 | - ->with('path/to/image') |
|
76 | - ->willReturn('absolute/path/to/image'); |
|
77 | - $event->expects($this->once()) |
|
78 | - ->method('setIcon') |
|
79 | - ->with('absolute/path/to/image'); |
|
80 | - $event->expects($this->once()) |
|
81 | - ->method('getSubject') |
|
82 | - ->willReturn($subject); |
|
83 | - $event->expects($this->once()) |
|
84 | - ->method('setParsedSubject'); |
|
85 | - |
|
86 | - $this->provider->parse($lang, $event); |
|
87 | - } |
|
88 | - |
|
89 | - public function testParseInvalidSubject(): void { |
|
90 | - $lang = 'ru'; |
|
91 | - $l = $this->createMock(IL10N::class); |
|
92 | - $event = $this->createMock(IEvent::class); |
|
93 | - |
|
94 | - $event->expects($this->once()) |
|
95 | - ->method('getApp') |
|
96 | - ->willReturn('twofactor_backupcodes'); |
|
97 | - $this->l10n->expects($this->once()) |
|
98 | - ->method('get') |
|
99 | - ->with('twofactor_backupcodes', $lang) |
|
100 | - ->willReturn($l); |
|
101 | - $event->expects($this->once()) |
|
102 | - ->method('getSubject') |
|
103 | - ->willReturn('unrelated'); |
|
104 | - |
|
105 | - $this->expectException(UnknownActivityException::class); |
|
106 | - $this->provider->parse($lang, $event); |
|
107 | - } |
|
22 | + private IFactory&MockObject $l10n; |
|
23 | + private IURLGenerator&MockObject $urlGenerator; |
|
24 | + private IManager&MockObject $activityManager; |
|
25 | + private Provider $provider; |
|
26 | + |
|
27 | + protected function setUp(): void { |
|
28 | + parent::setUp(); |
|
29 | + |
|
30 | + $this->l10n = $this->createMock(IFactory::class); |
|
31 | + $this->urlGenerator = $this->createMock(IURLGenerator::class); |
|
32 | + $this->activityManager = $this->createMock(IManager::class); |
|
33 | + |
|
34 | + $this->provider = new Provider($this->l10n, $this->urlGenerator, $this->activityManager); |
|
35 | + } |
|
36 | + |
|
37 | + public function testParseUnrelated(): void { |
|
38 | + $lang = 'ru'; |
|
39 | + $event = $this->createMock(IEvent::class); |
|
40 | + $event->expects($this->once()) |
|
41 | + ->method('getApp') |
|
42 | + ->willReturn('comments'); |
|
43 | + $this->expectException(UnknownActivityException::class); |
|
44 | + |
|
45 | + $this->provider->parse($lang, $event); |
|
46 | + } |
|
47 | + |
|
48 | + public static function subjectData(): array { |
|
49 | + return [ |
|
50 | + ['codes_generated'], |
|
51 | + ]; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @dataProvider subjectData |
|
56 | + */ |
|
57 | + public function testParse(string $subject): void { |
|
58 | + $lang = 'ru'; |
|
59 | + $event = $this->createMock(IEvent::class); |
|
60 | + $l = $this->createMock(IL10N::class); |
|
61 | + |
|
62 | + $event->expects($this->once()) |
|
63 | + ->method('getApp') |
|
64 | + ->willReturn('twofactor_backupcodes'); |
|
65 | + $this->l10n->expects($this->once()) |
|
66 | + ->method('get') |
|
67 | + ->with('twofactor_backupcodes', $lang) |
|
68 | + ->willReturn($l); |
|
69 | + $this->urlGenerator->expects($this->once()) |
|
70 | + ->method('imagePath') |
|
71 | + ->with('core', 'actions/password.svg') |
|
72 | + ->willReturn('path/to/image'); |
|
73 | + $this->urlGenerator->expects($this->once()) |
|
74 | + ->method('getAbsoluteURL') |
|
75 | + ->with('path/to/image') |
|
76 | + ->willReturn('absolute/path/to/image'); |
|
77 | + $event->expects($this->once()) |
|
78 | + ->method('setIcon') |
|
79 | + ->with('absolute/path/to/image'); |
|
80 | + $event->expects($this->once()) |
|
81 | + ->method('getSubject') |
|
82 | + ->willReturn($subject); |
|
83 | + $event->expects($this->once()) |
|
84 | + ->method('setParsedSubject'); |
|
85 | + |
|
86 | + $this->provider->parse($lang, $event); |
|
87 | + } |
|
88 | + |
|
89 | + public function testParseInvalidSubject(): void { |
|
90 | + $lang = 'ru'; |
|
91 | + $l = $this->createMock(IL10N::class); |
|
92 | + $event = $this->createMock(IEvent::class); |
|
93 | + |
|
94 | + $event->expects($this->once()) |
|
95 | + ->method('getApp') |
|
96 | + ->willReturn('twofactor_backupcodes'); |
|
97 | + $this->l10n->expects($this->once()) |
|
98 | + ->method('get') |
|
99 | + ->with('twofactor_backupcodes', $lang) |
|
100 | + ->willReturn($l); |
|
101 | + $event->expects($this->once()) |
|
102 | + ->method('getSubject') |
|
103 | + ->willReturn('unrelated'); |
|
104 | + |
|
105 | + $this->expectException(UnknownActivityException::class); |
|
106 | + $this->provider->parse($lang, $event); |
|
107 | + } |
|
108 | 108 | } |