Completed
Push — migrate-files-no-interaction ( 025687...608925 )
by
unknown
46:43 queued 18:48
created

UserHandlerTest::testPublishNewRoleDraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 50
nc 1
nop 0
dl 0
loc 59
rs 9.597
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File contains Test class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache\Tests;
10
11
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
12
use eZ\Publish\SPI\Persistence\User;
13
use eZ\Publish\SPI\Persistence\User\Role;
14
use eZ\Publish\SPI\Persistence\User\RoleAssignment;
15
use eZ\Publish\SPI\Persistence\User\RoleUpdateStruct;
16
use eZ\Publish\SPI\Persistence\User\RoleCreateStruct;
17
use eZ\Publish\SPI\Persistence\User\Policy;
18
use eZ\Publish\SPI\Persistence\User\Handler as SPIUserHandler;
19
use Stash\Interfaces\ItemInterface;
20
21
/**
22
 * Test case for Persistence\Cache\UserHandler.
23
 */
24
class UserHandlerTest extends HandlerTest
25
{
26
    protected function getCacheItemMock()
27
    {
28
        return $this->createMock(ItemInterface::class);
29
    }
30
31
    protected function getSPIUserHandlerMock()
32
    {
33
        return $this->createMock(SPIUserHandler::class);
34
    }
35
36
    /**
37
     * Commented lines represent cached functions covered by specific unit tests further down.
38
     *
39
     * @return array
40
     */
41
    public function providerForUnCachedMethods()
42
    {
43
        return array(
44
            //array( 'create', array( new User ) ),
45
            array('load', array(14)),
46
            array('loadByLogin', array('admin')),
47
            array('loadByEmail', array('[email protected]')),
48
            //array( 'update', array( new User ) ),
49
            //array( 'delete', array( 14 ) ),
50
            //array( 'createRole', array( new Role ) ),
51
            //array( 'loadRole', array( 22 ) ),
52
            array('loadRoleByIdentifier', array('users')),
53
            array('loadRoles', array()),
54
            array('loadRoleAssignmentsByRoleId', array(22)),
55
            //array( 'loadRoleAssignmentsByGroupId', array( 44, true ) ),
56
            //array( 'updateRole', array( new RoleUpdateStruct ) ),
57
            //array( 'deleteRole', array( 22 ) ),
58
            //array( 'addPolicy', array( 22, new Policy ) ),
59
            //array( 'updatePolicy', array( new Policy ) ),
60
            //array( 'deletePolicy', array( 22, 66 ) ),
61
            array('loadPoliciesByUserId', array(14)),
62
            //array( 'assignRole', array( 44, 22, array( 42 ) ) ),
63
            //array( 'unassignRole', array( 44, 22 ) ),
64
        );
65
    }
66
67
    /**
68
     * @dataProvider providerForUnCachedMethods
69
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler
70
     */
71 View Code Duplication
    public function testUnCachedMethods($method, array $arguments)
72
    {
73
        $this->loggerMock->expects($this->once())->method('logCall');
74
        $this->cacheMock
75
            ->expects($this->never())
76
            ->method($this->anything());
77
78
        $innerHandler = $this->getSPIUserHandlerMock();
79
        $this->persistenceHandlerMock
80
            ->expects($this->once())
81
            ->method('userHandler')
82
            ->will($this->returnValue($innerHandler));
83
84
        $expects = $innerHandler
85
            ->expects($this->once())
86
            ->method($method);
87
88
        if (isset($arguments[2])) {
89
            $expects->with($arguments[0], $arguments[1], $arguments[2]);
90
        } elseif (isset($arguments[1])) {
91
            $expects->with($arguments[0], $arguments[1]);
92
        } elseif (isset($arguments[0])) {
93
            $expects->with($arguments[0]);
94
        }
95
96
        $expects->will($this->returnValue(null));
97
98
        $handler = $this->persistenceCacheHandler->userHandler();
99
        call_user_func_array(array($handler, $method), $arguments);
100
    }
101
102
    /**
103
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::create
104
     */
105 View Code Duplication
    public function testCreate()
106
    {
107
        $this->loggerMock->expects($this->once())->method('logCall');
108
109
        $innerHandlerMock = $this->getSPIUserHandlerMock();
110
        $this->persistenceHandlerMock
111
            ->expects($this->once())
112
            ->method('userHandler')
113
            ->will($this->returnValue($innerHandlerMock));
114
115
        $innerHandlerMock
116
            ->expects($this->once())
117
            ->method('create')
118
            ->with($this->isInstanceOf(User::class))
119
            ->will(
120
                $this->returnValue(true)
121
            );
122
123
        $this->cacheMock
124
            ->expects($this->at(0))
125
            ->method('clear')
126
            ->with('content', 42)
127
            ->will($this->returnValue(true));
128
129
        $handler = $this->persistenceCacheHandler->userHandler();
130
        $handler->create(new User(array('id' => 42)));
131
    }
132
133
    /**
134
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::update
135
     */
136 View Code Duplication
    public function testUpdate()
137
    {
138
        $this->loggerMock->expects($this->once())->method('logCall');
139
140
        $innerHandlerMock = $this->getSPIUserHandlerMock();
141
        $this->persistenceHandlerMock
142
            ->expects($this->once())
143
            ->method('userHandler')
144
            ->will($this->returnValue($innerHandlerMock));
145
146
        $innerHandlerMock
147
            ->expects($this->once())
148
            ->method('update')
149
            ->with($this->isInstanceOf(User::class))
150
            ->will(
151
                $this->returnValue(true)
152
            );
153
154
        $this->cacheMock
155
            ->expects($this->at(0))
156
            ->method('clear')
157
            ->with('content', 42)
158
            ->will($this->returnValue(true));
159
160
        $handler = $this->persistenceCacheHandler->userHandler();
161
        $handler->update(new User(array('id' => 42)));
162
    }
163
164
    /**
165
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::delete
166
     */
167 View Code Duplication
    public function testDelete()
168
    {
169
        $this->loggerMock->expects($this->once())->method('logCall');
170
171
        $innerHandlerMock = $this->getSPIUserHandlerMock();
172
        $this->persistenceHandlerMock
173
            ->expects($this->once())
174
            ->method('userHandler')
175
            ->will($this->returnValue($innerHandlerMock));
176
177
        $innerHandlerMock
178
            ->expects($this->once())
179
            ->method('delete')
180
            ->with(14)
181
            ->will(
182
                $this->returnValue(true)
183
            );
184
185
        $this->cacheMock
186
            ->expects($this->at(0))
187
            ->method('clear')
188
            ->with('content', 14)
189
            ->will($this->returnValue(true));
190
191
        $this->cacheMock
192
            ->expects($this->at(1))
193
            ->method('clear')
194
            ->with('user', 'role', 'assignments', 'byGroup', 14)
195
            ->will($this->returnValue(true));
196
197
        $this->cacheMock
198
            ->expects($this->at(2))
199
            ->method('clear')
200
            ->with('user', 'role', 'assignments', 'byGroup', 'inherited', 14)
201
            ->will($this->returnValue(true));
202
203
        $handler = $this->persistenceCacheHandler->userHandler();
204
        $handler->delete(14);
205
    }
206
207
    /**
208
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::loadRole
209
     */
210 View Code Duplication
    public function testLoadRoleIsMiss()
211
    {
212
        $this->loggerMock->expects($this->once())->method('logCall');
213
214
        $cacheItemMock = $this->getCacheItemMock();
215
        $this->cacheMock
216
            ->expects($this->once())
217
            ->method('getItem')
218
            ->with('user', 'role', 33)
219
            ->will($this->returnValue($cacheItemMock));
220
221
        $cacheItemMock
222
            ->expects($this->once())
223
            ->method('get')
224
            ->will($this->returnValue(null));
225
226
        $cacheItemMock
227
            ->expects($this->once())
228
            ->method('isMiss')
229
            ->will($this->returnValue(true));
230
231
        $innerHandlerMock = $this->getSPIUserHandlerMock();
232
        $this->persistenceHandlerMock
233
            ->expects($this->once())
234
            ->method('userHandler')
235
            ->will($this->returnValue($innerHandlerMock));
236
237
        $innerHandlerMock
238
            ->expects($this->once())
239
            ->method('loadRole')
240
            ->with(33)
241
            ->will(
242
                $this->returnValue(new Role())
243
            );
244
245
        $cacheItemMock
246
            ->expects($this->once())
247
            ->method('set')
248
            ->with($this->isInstanceOf(Role::class))
249
            ->will($this->returnValue($cacheItemMock));
250
251
        $cacheItemMock
252
            ->expects($this->once())
253
            ->method('save')
254
            ->with();
255
256
        $handler = $this->persistenceCacheHandler->userHandler();
257
        $handler->loadRole(33);
258
    }
259
260
    /**
261
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::loadRole
262
     */
263 View Code Duplication
    public function testLoadRoleHasCache()
264
    {
265
        $this->loggerMock->expects($this->never())->method('logCall');
266
267
        $cacheItemMock = $this->getCacheItemMock();
268
        $this->cacheMock
269
            ->expects($this->once())
270
            ->method('getItem')
271
            ->with('user', 'role', 33)
272
            ->will($this->returnValue($cacheItemMock));
273
274
        $cacheItemMock
275
            ->expects($this->once())
276
            ->method('get')
277
            ->will($this->returnValue(new Role()));
278
279
        $cacheItemMock
280
            ->expects($this->once())
281
            ->method('isMiss')
282
            ->will($this->returnValue(false));
283
284
        $this->persistenceHandlerMock
285
            ->expects($this->never())
286
            ->method($this->anything());
287
288
        $cacheItemMock
289
            ->expects($this->never())
290
            ->method('set');
291
292
        $handler = $this->persistenceCacheHandler->userHandler();
293
        $handler->loadRole(33);
294
    }
295
296
    /**
297
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::loadRoleAssignmentsByGroupId
298
     */
299 View Code Duplication
    public function testLoadRoleAssignmentsByGroupIdIsMiss()
300
    {
301
        $this->loggerMock->expects($this->once())->method('logCall');
302
303
        $cacheItemMock = $this->getCacheItemMock();
304
        $this->cacheMock
305
            ->expects($this->once())
306
            ->method('getItem')
307
            ->with('user', 'role', 'assignments', 'byGroup', 42)
308
            ->will($this->returnValue($cacheItemMock));
309
310
        $cacheItemMock
311
            ->expects($this->once())
312
            ->method('get')
313
            ->will($this->returnValue(null));
314
315
        $cacheItemMock
316
            ->expects($this->once())
317
            ->method('isMiss')
318
            ->will($this->returnValue(true));
319
320
        $innerHandlerMock = $this->getSPIUserHandlerMock();
321
        $this->persistenceHandlerMock
322
            ->expects($this->once())
323
            ->method('userHandler')
324
            ->will($this->returnValue($innerHandlerMock));
325
326
        $innerHandlerMock
327
            ->expects($this->once())
328
            ->method('loadRoleAssignmentsByGroupId')
329
            ->with(42, false)
330
            ->will(
331
                $this->returnValue(
332
                    array(
333
                        new RoleAssignment(array('roleId' => 33)),
334
                    )
335
                )
336
            );
337
338
        $cacheItemMock
339
            ->expects($this->once())
340
            ->method('set')
341
            ->with($this->isType('array'))
342
            ->will($this->returnValue($cacheItemMock));
343
344
        $cacheItemMock
345
            ->expects($this->once())
346
            ->method('save')
347
            ->with();
348
349
        $handler = $this->persistenceCacheHandler->userHandler();
350
        $roleAssignments = $handler->loadRoleAssignmentsByGroupId(42);
351
352
        $this->assertEquals(1, count($roleAssignments));
353
        $this->assertInstanceOf(RoleAssignment::class, $roleAssignments[0]);
354
        $this->assertEquals(33, $roleAssignments[0]->roleId);
355
    }
356
357
    /**
358
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::loadRoleAssignmentsByGroupId
359
     */
360 View Code Duplication
    public function testLoadRoleAssignmentsByGroupIdHasCache()
361
    {
362
        $this->loggerMock->expects($this->never())->method('logCall');
363
364
        $cacheItemMock = $this->getCacheItemMock();
365
        $this->cacheMock
366
            ->expects($this->once())
367
            ->method('getItem')
368
            ->with('user', 'role', 'assignments', 'byGroup', 42)
369
            ->will($this->returnValue($cacheItemMock));
370
371
        $cacheItemMock
372
            ->expects($this->once())
373
            ->method('get')
374
            ->will(
375
                $this->returnValue(array(new RoleAssignment(array('roleId' => 33))))
376
            );
377
378
        $cacheItemMock
379
            ->expects($this->once())
380
            ->method('isMiss')
381
            ->will($this->returnValue(false));
382
383
        $this->persistenceHandlerMock
384
            ->expects($this->never())
385
            ->method($this->anything());
386
387
        $cacheItemMock
388
            ->expects($this->never())
389
            ->method('set');
390
391
        $handler = $this->persistenceCacheHandler->userHandler();
392
        $roleAssignments = $handler->loadRoleAssignmentsByGroupId(42);
393
394
        $this->assertEquals(1, count($roleAssignments));
395
        $this->assertInstanceOf(RoleAssignment::class, $roleAssignments[0]);
396
        $this->assertEquals(33, $roleAssignments[0]->roleId);
397
    }
398
399
    /**
400
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::loadRoleAssignmentsByGroupId
401
     */
402 View Code Duplication
    public function testLoadRoleAssignmentsByGroupIdInheritedIsMiss()
403
    {
404
        $this->loggerMock->expects($this->once())->method('logCall');
405
406
        $cacheItemMock = $this->getCacheItemMock();
407
        $this->cacheMock
408
            ->expects($this->once())
409
            ->method('getItem')
410
            ->with('user', 'role', 'assignments', 'byGroup', 'inherited', '42')
411
            ->will($this->returnValue($cacheItemMock));
412
413
        $cacheItemMock
414
            ->expects($this->once())
415
            ->method('get')
416
            ->will($this->returnValue(null));
417
418
        $cacheItemMock
419
            ->expects($this->once())
420
            ->method('isMiss')
421
            ->will($this->returnValue(true));
422
423
        $innerHandlerMock = $this->getSPIUserHandlerMock();
424
        $this->persistenceHandlerMock
425
            ->expects($this->once())
426
            ->method('userHandler')
427
            ->will($this->returnValue($innerHandlerMock));
428
429
        $innerHandlerMock
430
            ->expects($this->once())
431
            ->method('loadRoleAssignmentsByGroupId')
432
            ->with(42, true)
433
            ->will(
434
                $this->returnValue(array(new RoleAssignment(array('roleId' => 33))))
435
            );
436
437
        $cacheItemMock
438
            ->expects($this->once())
439
            ->method('set')
440
            ->with($this->isType('array'))
441
            ->will($this->returnValue($cacheItemMock));
442
443
        $cacheItemMock
444
            ->expects($this->once())
445
            ->method('save')
446
            ->with();
447
448
        $handler = $this->persistenceCacheHandler->userHandler();
449
        $roleAssignments = $handler->loadRoleAssignmentsByGroupId(42, true);
450
451
        $this->assertEquals(1, count($roleAssignments));
452
        $this->assertInstanceOf(RoleAssignment::class, $roleAssignments[0]);
453
        $this->assertEquals(33, $roleAssignments[0]->roleId);
454
    }
455
456
    /**
457
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::loadRoleAssignmentsByGroupId
458
     */
459 View Code Duplication
    public function testLoadRoleAssignmentsByGroupIdInheritedHasCache()
460
    {
461
        $this->loggerMock->expects($this->never())->method('logCall');
462
463
        $cacheItemMock = $this->getCacheItemMock();
464
        $this->cacheMock
465
            ->expects($this->once())
466
            ->method('getItem')
467
            ->with('user', 'role', 'assignments', 'byGroup', 'inherited', '42')
468
            ->will($this->returnValue($cacheItemMock));
469
470
        $cacheItemMock
471
            ->expects($this->once())
472
            ->method('get')
473
            ->will(
474
                $this->returnValue(array(new RoleAssignment(array('roleId' => 33))))
475
            );
476
477
        $cacheItemMock
478
            ->expects($this->once())
479
            ->method('isMiss')
480
            ->will($this->returnValue(false));
481
482
        $this->persistenceHandlerMock
483
            ->expects($this->never())
484
            ->method($this->anything());
485
486
        $cacheItemMock
487
            ->expects($this->never())
488
            ->method('set');
489
490
        $handler = $this->persistenceCacheHandler->userHandler();
491
        $roleAssignments = $handler->loadRoleAssignmentsByGroupId(42, true);
492
493
        $this->assertEquals(1, count($roleAssignments));
494
        $this->assertInstanceOf(RoleAssignment::class, $roleAssignments[0]);
495
        $this->assertEquals(33, $roleAssignments[0]->roleId);
496
    }
497
498
    /**
499
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::createRole
500
     */
501 View Code Duplication
    public function testCreateRole()
502
    {
503
        $this->loggerMock->expects($this->once())->method('logCall');
504
505
        $innerHandlerMock = $this->getSPIUserHandlerMock();
506
        $this->persistenceHandlerMock
507
            ->expects($this->once())
508
            ->method('userHandler')
509
            ->will($this->returnValue($innerHandlerMock));
510
511
        $innerHandlerMock
512
            ->expects($this->once())
513
            ->method('createRole')
514
            ->with($this->isInstanceOf(RoleCreateStruct::class))
515
            ->will(
516
                $this->returnValue(
517
                    new Role(
518
                        ['id' => 33, 'name' => 'Editors', 'identifier' => 'intranet', 'status' => Role::STATUS_DEFINED]
519
                    )
520
                )
521
            );
522
523
        $cacheItemMock = $this->getCacheItemMock();
524
        $this->cacheMock
525
            ->expects($this->never())
526
            ->method('getItem');
527
528
        $cacheItemMock
529
            ->expects($this->never())
530
            ->method('set');
531
532
        $cacheItemMock
533
            ->expects($this->never())
534
            ->method('get');
535
536
        $handler = $this->persistenceCacheHandler->userHandler();
537
        $handler->createRole(new User\RoleCreateStruct());
538
    }
539
540
    /**
541
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::createRoleDraft
542
     */
543
    public function testCreateRoleDraft()
544
    {
545
        $this->loggerMock->expects($this->once())->method('logCall');
546
547
        $innerHandlerMock = $this->getSPIUserHandlerMock();
548
        $this->persistenceHandlerMock
549
            ->expects($this->once())
550
            ->method('userHandler')
551
            ->will($this->returnValue($innerHandlerMock));
552
553
        $innerHandlerMock
554
            ->expects($this->once())
555
            ->method('createRoleDraft')
556
            ->with(33)
557
            ->will(
558
                $this->returnValue(
559
                    new Role(
560
                        ['id' => 33, 'name' => 'Editors', 'identifier' => 'intranet', 'status' => Role::STATUS_DRAFT]
561
                    )
562
                )
563
            );
564
565
        $cacheItemMock = $this->getCacheItemMock();
566
        $this->cacheMock
567
            ->expects($this->never())
568
            ->method('getItem');
569
570
        $cacheItemMock
571
            ->expects($this->never())
572
            ->method('set');
573
574
        $cacheItemMock
575
            ->expects($this->never())
576
            ->method('get');
577
578
        $handler = $this->persistenceCacheHandler->userHandler();
579
        $role = new Role(['id' => 33]);
580
        $handler->createRoleDraft($role->id);
581
    }
582
583
    /**
584
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::updateRole
585
     */
586 View Code Duplication
    public function testUpdateRole()
587
    {
588
        $this->loggerMock->expects($this->once())->method('logCall');
589
590
        $innerHandler = $this->getSPIUserHandlerMock();
591
        $this->persistenceHandlerMock
592
            ->expects($this->once())
593
            ->method('userHandler')
594
            ->will($this->returnValue($innerHandler));
595
596
        $innerHandler
597
            ->expects($this->once())
598
            ->method('updateRole')
599
            ->with($this->isInstanceOf(RoleUpdateStruct::class));
600
601
        $roleUpdateStruct = new RoleUpdateStruct();
602
        $roleUpdateStruct->id = 42;
603
604
        $this->cacheMock
605
            ->expects($this->once())
606
            ->method('clear')
607
            ->with('user', 'role', $roleUpdateStruct->id)
608
            ->will($this->returnValue(true));
609
610
        $this->cacheMock
611
            ->expects($this->never())
612
            ->method('getItem');
613
614
        $handler = $this->persistenceCacheHandler->userHandler();
615
        $handler->updateRole($roleUpdateStruct);
616
    }
617
618
    /**
619
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::updateRole
620
     */
621 View Code Duplication
    public function testUpdateRoleDraft()
622
    {
623
        $this->loggerMock->expects($this->once())->method('logCall');
624
625
        $innerHandler = $this->getSPIUserHandlerMock();
626
        $this->persistenceHandlerMock
627
            ->expects($this->once())
628
            ->method('userHandler')
629
            ->will($this->returnValue($innerHandler));
630
631
        $innerHandler
632
            ->expects($this->once())
633
            ->method('updateRole')
634
            ->with($this->isInstanceOf(RoleUpdateStruct::class));
635
636
        $roleUpdateStruct = new RoleUpdateStruct();
637
        $roleUpdateStruct->id = 42;
638
639
        $this->cacheMock
640
            ->expects($this->once())
641
            ->method('clear')
642
            ->with('user', 'role', $roleUpdateStruct->id)
643
            ->will($this->returnValue(true));
644
645
        $this->cacheMock
646
            ->expects($this->never())
647
            ->method('getItem');
648
649
        $handler = $this->persistenceCacheHandler->userHandler();
650
        $handler->updateRole($roleUpdateStruct);
651
    }
652
653
    /**
654
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::deleteRole
655
     */
656 View Code Duplication
    public function testDeleteRole()
657
    {
658
        $this->loggerMock->expects($this->once())->method('logCall');
659
660
        $innerHandlerMock = $this->getSPIUserHandlerMock();
661
        $this->persistenceHandlerMock
662
            ->expects($this->once())
663
            ->method('userHandler')
664
            ->will($this->returnValue($innerHandlerMock));
665
666
        $innerHandlerMock
667
            ->expects($this->once())
668
            ->method('deleteRole')
669
            ->with(33)
670
            ->will(
671
                $this->returnValue(true)
672
            );
673
674
        $this->cacheMock
675
            ->expects($this->at(0))
676
            ->method('clear')
677
            ->with('user', 'role', 33)
678
            ->will($this->returnValue(true));
679
680
        $this->cacheMock
681
            ->expects($this->at(1))
682
            ->method('clear')
683
            ->with('user', 'role', 'assignments')
684
            ->will($this->returnValue(true));
685
686
        $handler = $this->persistenceCacheHandler->userHandler();
687
        $handler->deleteRole(33);
688
    }
689
690
    /**
691
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::deleteRole
692
     */
693
    public function testDeleteRoleDraft()
694
    {
695
        $this->loggerMock->expects($this->once())->method('logCall');
696
697
        $innerHandlerMock = $this->getSPIUserHandlerMock();
698
        $this->persistenceHandlerMock
699
            ->expects($this->once())
700
            ->method('userHandler')
701
            ->will($this->returnValue($innerHandlerMock));
702
703
        $innerHandlerMock
704
            ->expects($this->once())
705
            ->method('deleteRole')
706
            ->with(33, Role::STATUS_DRAFT)
707
            ->will(
708
                $this->returnValue(true)
709
            );
710
711
        $this->cacheMock
712
            ->expects($this->never())
713
            ->method('clear');
714
715
        $handler = $this->persistenceCacheHandler->userHandler();
716
        $handler->deleteRole(33, Role::STATUS_DRAFT);
717
    }
718
719
    public function testPublishRoleDraftFromExistingRole()
720
    {
721
        $this->loggerMock->expects($this->once())->method('logCall');
722
723
        $innerHandlerMock = $this->getSPIUserHandlerMock();
724
        $this->persistenceHandlerMock
725
            ->expects($this->once())
726
            ->method('userHandler')
727
            ->willReturn($innerHandlerMock);
728
729
        $roleDraftId = 33;
730
        $originalRoleId = 30;
731
        $innerHandlerMock
732
            ->expects($this->exactly(2))
733
            ->method('loadRole')
734
            ->willReturnMap([
735
                [$roleDraftId, Role::STATUS_DRAFT, new Role(['originalId' => $originalRoleId])],
736
                [$originalRoleId, Role::STATUS_DEFINED, new Role(['id' => $originalRoleId])],
737
            ]);
738
        $innerHandlerMock
739
            ->expects($this->once())
740
            ->method('publishRoleDraft')
741
            ->with($roleDraftId);
742
743
        $this->cacheMock
744
            ->expects($this->once())
745
            ->method('clear')
746
            ->with('user', 'role', 'assignments')
747
            ->will($this->returnValue(true));
748
749
        $cacheItemMock = $this->getCacheItemMock();
750
        $this->cacheMock
751
            ->expects($this->once())
752
            ->method('getItem')
753
            ->with('user', 'role', $originalRoleId)
754
            ->will($this->returnValue($cacheItemMock));
755
756
        $cacheItemMock
757
            ->expects($this->once())
758
            ->method('set')
759
            ->with($this->isInstanceOf(Role::class))
760
            ->will($this->returnValue($cacheItemMock));
761
762
        $cacheItemMock
763
            ->expects($this->once())
764
            ->method('save')
765
            ->with();
766
767
        $handler = $this->persistenceCacheHandler->userHandler();
768
        $handler->publishRoleDraft($roleDraftId);
769
    }
770
771
    public function testPublishNewRoleDraft()
772
    {
773
        $this->loggerMock->expects($this->once())->method('logCall');
774
775
        $innerHandlerMock = $this->getSPIUserHandlerMock();
776
        $this->persistenceHandlerMock
777
            ->expects($this->once())
778
            ->method('userHandler')
779
            ->willReturn($innerHandlerMock);
780
781
        $roleDraftId = 33;
782
        $originalRoleId = -1;
783
        $innerHandlerMock
784
            ->expects($this->at(0))
785
            ->method('loadRole')
786
            ->with($roleDraftId, Role::STATUS_DRAFT)
787
            ->willReturn(new Role(['originalId' => $originalRoleId]));
788
        $innerHandlerMock
789
            ->expects($this->at(1))
790
            ->method('publishRoleDraft')
791
            ->with($roleDraftId);
792
        $innerHandlerMock
793
            ->expects($this->at(2))
794
            ->method('loadRole')
795
            ->with($originalRoleId, Role::STATUS_DEFINED)
796
            ->willThrowException(new NotFoundException('foo', 'bar'));
797
        $innerHandlerMock
798
            ->expects($this->at(3))
799
            ->method('loadRole')
800
            ->with($roleDraftId, Role::STATUS_DEFINED)
801
            ->willReturn(new Role(['id' => $roleDraftId]));
802
803
        $this->cacheMock
804
            ->expects($this->once())
805
            ->method('clear')
806
            ->with('user', 'role', 'assignments')
807
            ->will($this->returnValue(true));
808
809
        $cacheItemMock = $this->getCacheItemMock();
810
        $this->cacheMock
811
            ->expects($this->once())
812
            ->method('getItem')
813
            ->with('user', 'role', $roleDraftId)
814
            ->will($this->returnValue($cacheItemMock));
815
816
        $cacheItemMock
817
            ->expects($this->once())
818
            ->method('set')
819
            ->with($this->isInstanceOf(Role::class))
820
            ->will($this->returnValue($cacheItemMock));
821
822
        $cacheItemMock
823
            ->expects($this->once())
824
            ->method('save')
825
            ->with();
826
827
        $handler = $this->persistenceCacheHandler->userHandler();
828
        $handler->publishRoleDraft($roleDraftId);
829
    }
830
831
    /**
832
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::addPolicy
833
     */
834
    public function testAddPolicy()
835
    {
836
        $this->loggerMock->expects($this->once())->method('logCall');
837
838
        $innerHandlerMock = $this->getSPIUserHandlerMock();
839
        $this->persistenceHandlerMock
840
            ->expects($this->once())
841
            ->method('userHandler')
842
            ->will($this->returnValue($innerHandlerMock));
843
844
        $innerHandlerMock
845
            ->expects($this->once())
846
            ->method('addPolicy')
847
            ->with(33, $this->isInstanceOf(Policy::class))
848
            ->will(
849
                $this->returnValue(new Policy())
850
            );
851
852
        $this->cacheMock
853
            ->expects($this->once())
854
            ->method('clear')
855
            ->with('user', 'role', 33)
856
            ->will($this->returnValue(true));
857
858
        $handler = $this->persistenceCacheHandler->userHandler();
859
        $handler->addPolicy(33, new Policy());
860
    }
861
862
    /**
863
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::addPolicyByRoleDraft
864
     */
865
    public function testAddPolicyByRoleDraft()
866
    {
867
        $this->loggerMock->expects($this->once())->method('logCall');
868
869
        $innerHandlerMock = $this->getSPIUserHandlerMock();
870
        $this->persistenceHandlerMock
871
            ->expects($this->once())
872
            ->method('userHandler')
873
            ->will($this->returnValue($innerHandlerMock));
874
875
        $innerHandlerMock
876
            ->expects($this->once())
877
            ->method('addPolicyByRoleDraft')
878
            ->with(33, $this->isInstanceOf(Policy::class))
879
            ->will(
880
                $this->returnValue(new Policy())
881
            );
882
883
        $this->cacheMock
884
            ->expects($this->never())
885
            ->method('clear');
886
887
        $handler = $this->persistenceCacheHandler->userHandler();
888
        $handler->addPolicyByRoleDraft(33, new Policy());
889
    }
890
891
    /**
892
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::updatePolicy
893
     */
894 View Code Duplication
    public function testUpdatePolicy()
895
    {
896
        $this->loggerMock->expects($this->once())->method('logCall');
897
898
        $innerHandlerMock = $this->getSPIUserHandlerMock();
899
        $this->persistenceHandlerMock
900
            ->expects($this->once())
901
            ->method('userHandler')
902
            ->will($this->returnValue($innerHandlerMock));
903
904
        $innerHandlerMock
905
            ->expects($this->once())
906
            ->method('updatePolicy')
907
            ->with($this->isInstanceOf(Policy::class))
908
            ->will(
909
                $this->returnValue(new Policy(array('roleId' => 33)))
910
            );
911
912
        $this->cacheMock
913
            ->expects($this->once())
914
            ->method('clear')
915
            ->with('user', 'role', 33)
916
            ->will($this->returnValue(true));
917
918
        $handler = $this->persistenceCacheHandler->userHandler();
919
        $handler->updatePolicy(new Policy(array('roleId' => 33)));
920
    }
921
922
    /**
923
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::deletePolicy
924
     */
925
    public function testDeletePolicy()
926
    {
927
        $this->loggerMock->expects($this->once())->method('logCall');
928
929
        $innerHandlerMock = $this->getSPIUserHandlerMock();
930
        $this->persistenceHandlerMock
931
            ->expects($this->once())
932
            ->method('userHandler')
933
            ->will($this->returnValue($innerHandlerMock));
934
935
        $innerHandlerMock
936
            ->expects($this->once())
937
            ->method('deletePolicy')
938
            ->with(55)
939
            ->will(
940
                $this->returnValue(true)
941
            );
942
943
        $this->cacheMock
944
            ->expects($this->once())
945
            ->method('clear')
946
            ->with('user', 'role')
947
            ->will($this->returnValue(true));
948
949
        $handler = $this->persistenceCacheHandler->userHandler();
950
        $handler->deletePolicy(55);
951
    }
952
953
    /**
954
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::assignRole
955
     */
956 View Code Duplication
    public function testAssignRole()
957
    {
958
        $this->loggerMock->expects($this->once())->method('logCall');
959
960
        $innerHandlerMock = $this->getSPIUserHandlerMock();
961
        $this->persistenceHandlerMock
962
            ->expects($this->once())
963
            ->method('userHandler')
964
            ->will($this->returnValue($innerHandlerMock));
965
966
        $innerHandlerMock
967
            ->expects($this->once())
968
            ->method('assignRole')
969
            ->with(33, 22)
970
            ->will(
971
                $this->returnValue(true)
972
            );
973
974
        $this->cacheMock
975
            ->expects($this->at(0))
976
            ->method('clear')
977
            ->with('user', 'role', 22)
978
            ->will($this->returnValue(true));
979
980
        $this->cacheMock
981
            ->expects($this->at(1))
982
            ->method('clear')
983
            ->with('user', 'role', 'assignments', 'byGroup', 33)
984
            ->will($this->returnValue(true));
985
986
        $this->cacheMock
987
            ->expects($this->at(2))
988
            ->method('clear')
989
            ->with('user', 'role', 'assignments', 'byGroup', 'inherited')
990
            ->will($this->returnValue(true));
991
992
        $handler = $this->persistenceCacheHandler->userHandler();
993
        $handler->assignRole(33, 22);
994
    }
995
996
    /**
997
     * @covers \eZ\Publish\Core\Persistence\Cache\UserHandler::unassignRole
998
     */
999 View Code Duplication
    public function testUnassignRole()
1000
    {
1001
        $this->loggerMock->expects($this->once())->method('logCall');
1002
1003
        $innerHandlerMock = $this->getSPIUserHandlerMock();
1004
        $this->persistenceHandlerMock
1005
            ->expects($this->once())
1006
            ->method('userHandler')
1007
            ->will($this->returnValue($innerHandlerMock));
1008
1009
        $innerHandlerMock
1010
            ->expects($this->once())
1011
            ->method('unassignRole')
1012
            ->with(33, 22)
1013
            ->will(
1014
                $this->returnValue(true)
1015
            );
1016
1017
        $this->cacheMock
1018
            ->expects($this->at(0))
1019
            ->method('clear')
1020
            ->with('user', 'role', 22)
1021
            ->will($this->returnValue(true));
1022
1023
        $this->cacheMock
1024
            ->expects($this->at(1))
1025
            ->method('clear')
1026
            ->with('user', 'role', 'assignments', 'byGroup', 33)
1027
            ->will($this->returnValue(true));
1028
1029
        $this->cacheMock
1030
            ->expects($this->at(2))
1031
            ->method('clear')
1032
            ->with('user', 'role', 'assignments', 'byGroup', 'inherited')
1033
            ->will($this->returnValue(true));
1034
1035
        $handler = $this->persistenceCacheHandler->userHandler();
1036
        $handler->unassignRole(33, 22);
1037
    }
1038
}
1039