Skip to content

Commit a590144

Browse files
committed
Add tests for user/group assignment checks
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent ba384d9 commit a590144

2 files changed

Lines changed: 242 additions & 24 deletions

File tree

tests/integration/database/AssignedUsersMapperTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,44 @@ public function testMapParticipant() {
189189
$this->assertEquals('invalid-username', $assignment->resolveParticipant());
190190
}
191191

192+
public function testIsUserAssigned() {
193+
$assignment = new AssignedUsers();
194+
$assignment->setCardId($this->cards[1]->getId());
195+
$assignment->setParticipant(self::TEST_USER4);
196+
$assignment->setType(AssignedUsers::TYPE_USER);
197+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER4));
198+
199+
$assignment = $this->assignedUsersMapper->insert($assignment);
200+
$actual = $this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER4);
201+
$this->assignedUsersMapper->delete($assignment);
202+
$this->assertTrue($actual);
203+
204+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER4));
205+
}
206+
207+
public function testIsUserAssignedGroup() {
208+
$assignment = new AssignedUsers();
209+
$assignment->setCardId($this->cards[1]->getId());
210+
$assignment->setParticipant('group');
211+
$assignment->setType(AssignedUsers::TYPE_GROUP);
212+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER1));
213+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER2));
214+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER3));
215+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER4));
216+
217+
$assignment = $this->assignedUsersMapper->insert($assignment);
218+
$this->assertTrue($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER1));
219+
$this->assertTrue($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER2));
220+
$this->assertTrue($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER3));
221+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER4));
222+
$this->assignedUsersMapper->delete($assignment);
223+
224+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER1));
225+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER2));
226+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER3));
227+
$this->assertFalse($this->assignedUsersMapper->isUserAssigned($this->cards[1]->getId(), self::TEST_USER4));
228+
}
229+
192230
public function tearDown(): void {
193231
$this->boardService->deleteForce($this->board->getId());
194232
parent::tearDown();

tests/unit/Notification/NotificationHelperTest.php

Lines changed: 204 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@
4141
use OCP\Notification\INotification;
4242
use PHPUnit\Framework\MockObject\MockObject;
4343

44+
class DummyUser extends \OC\User\User {
45+
46+
private $uid;
47+
48+
public function __construct($uid) {
49+
$this->uid = $uid;
50+
}
51+
52+
public function getUID() {
53+
return $this->uid;
54+
}
55+
}
56+
4457
class NotificationHelperTest extends \Test\TestCase {
4558

4659
/** @var CardMapper|MockObject */
@@ -115,34 +128,19 @@ public function testSendCardDuedate() {
115128
->with('asd', 'deck', 'board:234:notify-due', ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED)
116129
->willReturn(ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ALL);
117130

118-
$card = $this->createMock(Card::class);
119-
$card->expects($this->at(0))
120-
->method('__call')
121-
->with('getNotified', [])
122-
->willReturn(false);
123-
$card->expects($this->at(1))
124-
->method('__call')
125-
->with('getId', [])
126-
->willReturn(123);
127-
for ($i=0; $i<3; $i++) {
128-
$card->expects($this->at($i*3+2))
129-
->method('__call')
130-
->with('getId', [])
131-
->willReturn(123);
132-
$card->expects($this->at($i*3+3))
133-
->method('__call', [])
134-
->with('getTitle')
135-
->willReturn('MyCardTitle');
136-
}
131+
$card = Card::fromParams([
132+
'notified' => false,
133+
'id' => 123,
134+
'title' => 'MyCardTitle'
135+
]);
137136
$this->cardMapper->expects($this->once())
138137
->method('findBoardId')
139138
->with(123)
140139
->willReturn(234);
141-
$board = $this->createMock(Board::class);
142-
$board->expects($this->any())
143-
->method('__call')
144-
->with('getTitle', [])
145-
->willReturn('MyBoardTitle');
140+
$board = Board::fromParams([
141+
'id' => 123,
142+
'title' => 'MyBoardTitle'
143+
]);
146144
$this->boardMapper->expects($this->once())
147145
->method('find')
148146
->with(234)
@@ -208,6 +206,188 @@ public function testSendCardDuedate() {
208206
$this->notificationHelper->sendCardDuedate($card);
209207
}
210208

209+
public function testSendCardDuedateAssigned() {
210+
$this->config->expects($this->at(0))
211+
->method('getUserValue')
212+
->with('foo', 'deck', 'board:234:notify-due', ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED)
213+
->willReturn(ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED);
214+
$this->config->expects($this->at(1))
215+
->method('getUserValue')
216+
->with('bar', 'deck', 'board:234:notify-due', ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED)
217+
->willReturn(ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED);
218+
$this->config->expects($this->at(2))
219+
->method('getUserValue')
220+
->with('asd', 'deck', 'board:234:notify-due', ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED)
221+
->willReturn(ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED);
222+
223+
$users = [
224+
new DummyUser('foo'), new DummyUser('bar'), new DummyUser('asd')
225+
];
226+
$card = Card::fromParams([
227+
'notified' => false,
228+
'id' => 123,
229+
'title' => 'MyCardTitle'
230+
]);
231+
$card->setAssignedUsers([
232+
new User($users[0])
233+
]);
234+
$this->cardMapper->expects($this->once())
235+
->method('findBoardId')
236+
->with(123)
237+
->willReturn(234);
238+
$board = Board::fromParams([
239+
'id' => 123,
240+
'title' => 'MyBoardTitle'
241+
]);
242+
$this->boardMapper->expects($this->once())
243+
->method('find')
244+
->with(234)
245+
->willReturn($board);
246+
247+
248+
$this->permissionService->expects($this->once())
249+
->method('findUsers')
250+
->with(234)
251+
->willReturn($users);
252+
253+
$this->assignedUsersMapper->expects($this->exactly(3))
254+
->method('isUserAssigned')
255+
->willReturn(true);
256+
257+
258+
$n1 = $this->createMock(INotification::class);
259+
$n2 = $this->createMock(INotification::class);
260+
$n3 = $this->createMock(INotification::class);
261+
262+
$n1->expects($this->once())->method('setApp')->with('deck')->willReturn($n1);
263+
$n1->expects($this->once())->method('setUser')->with('foo')->willReturn($n1);
264+
$n1->expects($this->once())->method('setObject')->with('card', 123)->willReturn($n1);
265+
$n1->expects($this->once())->method('setSubject')->with('card-overdue', ['MyCardTitle', 'MyBoardTitle'])->willReturn($n1);
266+
$n1->expects($this->once())->method('setDateTime')->willReturn($n1);
267+
268+
$n2->expects($this->once())->method('setApp')->with('deck')->willReturn($n2);
269+
$n2->expects($this->once())->method('setUser')->with('bar')->willReturn($n2);
270+
$n2->expects($this->once())->method('setObject')->with('card', 123)->willReturn($n2);
271+
$n2->expects($this->once())->method('setSubject')->with('card-overdue', ['MyCardTitle', 'MyBoardTitle'])->willReturn($n2);
272+
$n2->expects($this->once())->method('setDateTime')->willReturn($n2);
273+
274+
$n3->expects($this->once())->method('setApp')->with('deck')->willReturn($n3);
275+
$n3->expects($this->once())->method('setUser')->with('asd')->willReturn($n3);
276+
$n3->expects($this->once())->method('setObject')->with('card', 123)->willReturn($n3);
277+
$n3->expects($this->once())->method('setSubject')->with('card-overdue', ['MyCardTitle', 'MyBoardTitle'])->willReturn($n3);
278+
$n3->expects($this->once())->method('setDateTime')->willReturn($n3);
279+
280+
$this->notificationManager->expects($this->at(0))
281+
->method('createNotification')
282+
->willReturn($n1);
283+
$this->notificationManager->expects($this->at(1))
284+
->method('notify')
285+
->with($n1);
286+
$this->notificationManager->expects($this->at(2))
287+
->method('createNotification')
288+
->willReturn($n2);
289+
$this->notificationManager->expects($this->at(3))
290+
->method('notify')
291+
->with($n2);
292+
$this->notificationManager->expects($this->at(4))
293+
->method('createNotification')
294+
->willReturn($n3);
295+
$this->notificationManager->expects($this->at(5))
296+
->method('notify')
297+
->with($n3);
298+
299+
$this->cardMapper->expects($this->once())
300+
->method('markNotified')
301+
->with($card);
302+
303+
$this->notificationHelper->sendCardDuedate($card);
304+
}
305+
306+
307+
public function testSendCardDuedateNever() {
308+
$this->config->expects($this->at(0))
309+
->method('getUserValue')
310+
->with('foo', 'deck', 'board:234:notify-due', ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED)
311+
->willReturn(ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED);
312+
$this->config->expects($this->at(1))
313+
->method('getUserValue')
314+
->with('bar', 'deck', 'board:234:notify-due', ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED)
315+
->willReturn(ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED);
316+
$this->config->expects($this->at(2))
317+
->method('getUserValue')
318+
->with('asd', 'deck', 'board:234:notify-due', ConfigService::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED)
319+
->willReturn(ConfigService::SETTING_BOARD_NOTIFICATION_DUE_OFF);
320+
321+
$users = [
322+
new DummyUser('foo'), new DummyUser('bar'), new DummyUser('asd')
323+
];
324+
$card = Card::fromParams([
325+
'notified' => false,
326+
'id' => 123,
327+
'title' => 'MyCardTitle'
328+
]);
329+
$card->setAssignedUsers([
330+
new User($users[0])
331+
]);
332+
$this->cardMapper->expects($this->once())
333+
->method('findBoardId')
334+
->with(123)
335+
->willReturn(234);
336+
$board = Board::fromParams([
337+
'id' => 123,
338+
'title' => 'MyBoardTitle'
339+
]);
340+
$this->boardMapper->expects($this->once())
341+
->method('find')
342+
->with(234)
343+
->willReturn($board);
344+
345+
346+
$this->permissionService->expects($this->once())
347+
->method('findUsers')
348+
->with(234)
349+
->willReturn($users);
350+
351+
$this->assignedUsersMapper->expects($this->exactly(2))
352+
->method('isUserAssigned')
353+
->willReturn(true);
354+
355+
356+
$n1 = $this->createMock(INotification::class);
357+
$n2 = $this->createMock(INotification::class);
358+
359+
$n1->expects($this->once())->method('setApp')->with('deck')->willReturn($n1);
360+
$n1->expects($this->once())->method('setUser')->with('foo')->willReturn($n1);
361+
$n1->expects($this->once())->method('setObject')->with('card', 123)->willReturn($n1);
362+
$n1->expects($this->once())->method('setSubject')->with('card-overdue', ['MyCardTitle', 'MyBoardTitle'])->willReturn($n1);
363+
$n1->expects($this->once())->method('setDateTime')->willReturn($n1);
364+
365+
$n2->expects($this->once())->method('setApp')->with('deck')->willReturn($n2);
366+
$n2->expects($this->once())->method('setUser')->with('bar')->willReturn($n2);
367+
$n2->expects($this->once())->method('setObject')->with('card', 123)->willReturn($n2);
368+
$n2->expects($this->once())->method('setSubject')->with('card-overdue', ['MyCardTitle', 'MyBoardTitle'])->willReturn($n2);
369+
$n2->expects($this->once())->method('setDateTime')->willReturn($n2);
370+
371+
$this->notificationManager->expects($this->at(0))
372+
->method('createNotification')
373+
->willReturn($n1);
374+
$this->notificationManager->expects($this->at(1))
375+
->method('notify')
376+
->with($n1);
377+
$this->notificationManager->expects($this->at(2))
378+
->method('createNotification')
379+
->willReturn($n2);
380+
$this->notificationManager->expects($this->at(3))
381+
->method('notify')
382+
->with($n2);
383+
384+
$this->cardMapper->expects($this->once())
385+
->method('markNotified')
386+
->with($card);
387+
388+
$this->notificationHelper->sendCardDuedate($card);
389+
}
390+
211391
public function testSendCardAssignedUser() {
212392
$board = new Board();
213393
$board->setId(123);

0 commit comments

Comments
 (0)