3131use OC \Federation \CloudIdManager ;
3232use OC \L10N \L10N ;
3333use OC \Security \SecureRandom ;
34+ use OCA \Circles \Model \Circle ;
3435use OCA \Deck \Activity \ActivityManager ;
36+ use OCA \Deck \BadRequestException ;
3537use OCA \Deck \Db \Acl ;
3638use OCA \Deck \Db \AclMapper ;
3739use OCA \Deck \Db \Assignment ;
@@ -104,6 +106,9 @@ class BoardServiceTest extends TestCase {
104106 /** @var IUserManager */
105107 private $ userManager ;
106108
109+ /** @var CirclesService|MockObject */
110+ private $ circlesService ;
111+
107112 public function setUp (): void {
108113 parent ::setUp ();
109114 $ this ->l10n = $ this ->createMock (L10N ::class);
@@ -125,6 +130,7 @@ public function setUp(): void {
125130 $ this ->boardServiceValidator = $ this ->createMock (BoardServiceValidator::class);
126131 $ this ->sessionMapper = $ this ->createMock (SessionMapper::class);
127132 $ this ->userManager = $ this ->createMock (IUserManager::class);
133+ $ this ->circlesService = $ this ->createMock (CirclesService::class);
128134
129135 $ this ->service = new BoardService (
130136 $ this ->boardMapper ,
@@ -151,7 +157,7 @@ public function setUp(): void {
151157 $ this ->userManager ,
152158 $ this ->createMock (SecureRandom::class),
153159 $ this ->createMock (ConfigService::class),
154- $ this ->createMock (CirclesService::class) ,
160+ $ this ->circlesService ,
155161 $ this ->userId
156162 );
157163
@@ -227,6 +233,129 @@ public function testCreateDenied() {
227233 $ b = $ this ->service ->create ('MyBoard ' , 'admin ' , '00ff00 ' );
228234 }
229235
236+ public function testCreateForTeamEmptyTeamId (): void {
237+ $ this ->expectException (BadRequestException::class);
238+ $ this ->service ->createForTeam ('Team board ' , $ this ->userId , '00ff00 ' , '' );
239+ }
240+
241+ public function testCreateForTeamCirclesDisabled (): void {
242+ $ this ->circlesService ->expects ($ this ->once ())
243+ ->method ('isCirclesEnabled ' )
244+ ->willReturn (false );
245+ $ this ->expectException (BadRequestException::class);
246+ $ this ->service ->createForTeam ('Team board ' , $ this ->userId , '00ff00 ' , 'team-a ' );
247+ }
248+
249+ public function testCreateForTeamNotFound (): void {
250+ $ this ->circlesService ->expects ($ this ->once ())
251+ ->method ('isCirclesEnabled ' )
252+ ->willReturn (true );
253+ $ this ->circlesService ->expects ($ this ->once ())
254+ ->method ('getCircle ' )
255+ ->with ('team-a ' )
256+ ->willReturn (null );
257+ $ this ->expectException (BadRequestException::class);
258+ $ this ->service ->createForTeam ('Team board ' , $ this ->userId , '00ff00 ' , 'team-a ' );
259+ }
260+
261+ public function testCreateForTeamNotMember (): void {
262+ $ this ->circlesService ->expects ($ this ->once ())
263+ ->method ('isCirclesEnabled ' )
264+ ->willReturn (true );
265+ $ this ->circlesService ->expects ($ this ->once ())
266+ ->method ('getCircle ' )
267+ ->with ('team-a ' )
268+ ->willReturn ($ this ->createMock (Circle::class));
269+ $ this ->circlesService ->expects ($ this ->once ())
270+ ->method ('isUserInCircle ' )
271+ ->with ('team-a ' , $ this ->userId )
272+ ->willReturn (false );
273+ $ this ->expectException (NoPermissionException::class);
274+ $ this ->service ->createForTeam ('Team board ' , $ this ->userId , '00ff00 ' , 'team-a ' );
275+ }
276+
277+ public function testCreateForTeamSuccess (): void {
278+ $ createdBoard = new Board ();
279+ $ createdBoard ->setId (42 );
280+ $ createdBoard ->setTitle ('Team board ' );
281+ $ createdBoard ->setOwner ($ this ->userId );
282+ $ createdBoard ->setColor ('00ff00 ' );
283+
284+ $ updatedBoard = new Board ();
285+ $ updatedBoard ->setId (42 );
286+ $ updatedBoard ->setTitle ('Team board ' );
287+ $ updatedBoard ->setOwner ($ this ->userId );
288+ $ updatedBoard ->setColor ('00ff00 ' );
289+ $ updatedBoard ->setTeamId ('team-a ' );
290+
291+ $ this ->circlesService ->expects ($ this ->once ())
292+ ->method ('isCirclesEnabled ' )
293+ ->willReturn (true );
294+ $ this ->circlesService ->expects ($ this ->once ())
295+ ->method ('getCircle ' )
296+ ->with ('team-a ' )
297+ ->willReturn ($ this ->createMock (Circle::class));
298+ $ this ->circlesService ->expects ($ this ->once ())
299+ ->method ('isUserInCircle ' )
300+ ->with ('team-a ' , $ this ->userId )
301+ ->willReturn (true );
302+
303+ /** @var BoardService|MockObject $service */
304+ $ service = $ this ->getMockBuilder (BoardService::class)
305+ ->setConstructorArgs ([
306+ $ this ->boardMapper ,
307+ $ this ->stackMapper ,
308+ $ this ->cardMapper ,
309+ $ this ->config ,
310+ $ this ->l10n ,
311+ $ this ->labelMapper ,
312+ $ this ->aclMapper ,
313+ $ this ->permissionService ,
314+ $ this ->assignmentService ,
315+ $ this ->notificationHelper ,
316+ $ this ->assignedUsersMapper ,
317+ $ this ->activityManager ,
318+ $ this ->createMock (CloudFederationProviderManager::class),
319+ $ this ->createMock (CloudIdManager::class),
320+ $ this ->createMock (CloudFederationFactory::class),
321+ $ this ->eventDispatcher ,
322+ $ this ->changeHelper ,
323+ $ this ->urlGenerator ,
324+ $ this ->connection ,
325+ $ this ->boardServiceValidator ,
326+ $ this ->sessionMapper ,
327+ $ this ->userManager ,
328+ $ this ->createMock (SecureRandom::class),
329+ $ this ->createMock (ConfigService::class),
330+ $ this ->circlesService ,
331+ $ this ->userId ,
332+ ])
333+ ->onlyMethods (['create ' , 'addAcl ' , 'find ' ])
334+ ->getMock ();
335+
336+ $ service ->expects ($ this ->once ())
337+ ->method ('create ' )
338+ ->with ('Team board ' , $ this ->userId , '00ff00 ' )
339+ ->willReturn ($ createdBoard );
340+ $ this ->boardMapper ->expects ($ this ->once ())
341+ ->method ('update ' )
342+ ->with ($ this ->callback (function (Board $ board ) {
343+ return $ board ->getId () === 42 && $ board ->getTeamId () === 'team-a ' ;
344+ }))
345+ ->willReturn ($ updatedBoard );
346+ $ service ->expects ($ this ->once ())
347+ ->method ('addAcl ' )
348+ ->with (42 , Acl::PERMISSION_TYPE_CIRCLE , 'team-a ' , true , false , false );
349+ $ service ->expects ($ this ->once ())
350+ ->method ('find ' )
351+ ->with (42 )
352+ ->willReturn ($ updatedBoard );
353+
354+ $ result = $ service ->createForTeam ('Team board ' , $ this ->userId , '00ff00 ' , 'team-a ' );
355+ $ this ->assertSame ($ updatedBoard , $ result );
356+ $ this ->assertEquals ('team-a ' , $ result ->getTeamId ());
357+ }
358+
230359 public function testUpdate () {
231360 $ board = new Board ();
232361 $ board ->setId (123 );
0 commit comments