|
16 | 16 | use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowChecker;
|
17 | 17 | use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
|
18 | 18 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
19 |
| -use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
| 19 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
20 | 20 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
| 21 | +use Symfony\Component\Security\Core\User\UserInterface; |
21 | 22 |
|
22 | 23 | class PublishWorkflowTest extends BaseTestCase
|
23 | 24 | {
|
24 |
| - /** |
25 |
| - * @var AuthorizationCheckerInterface |
26 |
| - */ |
27 |
| - private $publishWorkflowChecker; |
28 |
| - |
29 |
| - /** |
30 |
| - * @var TokenStorageInterface |
31 |
| - */ |
32 |
| - private $tokenStorage; |
| 25 | + private AuthorizationCheckerInterface $publishWorkflowChecker; |
| 26 | + private TokenStorageInterface $tokenStorage; |
33 | 27 |
|
34 | 28 | public function setUp(): void
|
35 | 29 | {
|
36 |
| - $this->publishWorkflowChecker = $this->getContainer()->get('cmf_core.publish_workflow.checker'); |
37 |
| - $this->tokenStorage = $this->getContainer()->get('test.service_container')->get('security.token_storage'); |
| 30 | + $this->publishWorkflowChecker = self::getContainer()->get('cmf_core.publish_workflow.checker'); |
| 31 | + $this->tokenStorage = self::getContainer()->get('test.service_container')->get('security.token_storage'); |
38 | 32 | }
|
39 | 33 |
|
40 |
| - public function testPublishable() |
| 34 | + public function testPublishable(): void |
41 | 35 | {
|
42 | 36 | $doc = $this->createMock(PublishableReadInterface::class);
|
43 | 37 | $doc
|
44 | 38 | ->method('isPublishable')
|
45 |
| - ->will($this->returnValue(true)) |
| 39 | + ->willReturn(true) |
46 | 40 | ;
|
47 | 41 |
|
48 | 42 | $this->assertTrue($this->publishWorkflowChecker->isGranted(PublishWorkflowChecker::VIEW_ATTRIBUTE, $doc));
|
49 | 43 | $this->assertTrue($this->publishWorkflowChecker->isGranted(PublishWorkflowChecker::VIEW_ANONYMOUS_ATTRIBUTE, $doc));
|
50 | 44 | }
|
51 | 45 |
|
52 |
| - public function testPublishPeriod() |
| 46 | + public function testPublishPeriod(): void |
53 | 47 | {
|
54 | 48 | $doc = $this->createMock(PublishModel::class);
|
55 | 49 | $doc
|
56 | 50 | ->method('isPublishable')
|
57 |
| - ->will($this->returnValue(true)) |
| 51 | + ->willReturn(true) |
58 | 52 | ;
|
59 | 53 | $doc
|
60 | 54 | ->method('getPublishEndDate')
|
61 |
| - ->will($this->returnValue(new \DateTime('01/01/1980'))) |
| 55 | + ->willReturn(new \DateTime('01/01/1980')) |
62 | 56 | ;
|
63 | 57 |
|
64 | 58 | $this->assertFalse($this->publishWorkflowChecker->isGranted(PublishWorkflowChecker::VIEW_ATTRIBUTE, $doc));
|
65 | 59 | $this->assertFalse($this->publishWorkflowChecker->isGranted(PublishWorkflowChecker::VIEW_ANONYMOUS_ATTRIBUTE, $doc));
|
66 | 60 | }
|
67 | 61 |
|
68 |
| - public function testIgnoreRoleHas() |
| 62 | + public function testIgnoreRoleHas(): void |
69 | 63 | {
|
70 | 64 | $doc = $this->createMock(PublishModel::class);
|
71 | 65 | $doc
|
72 | 66 | ->method('isPublishable')
|
73 |
| - ->will($this->returnValue(false)) |
| 67 | + ->willReturn(false) |
74 | 68 | ;
|
75 | 69 | $roles = [
|
76 | 70 | 'ROLE_CAN_VIEW_NON_PUBLISHED',
|
77 | 71 | ];
|
78 |
| - $token = new UsernamePasswordToken('test', 'pass', 'testprovider', $roles); |
| 72 | + $token = $this->createMock(TokenInterface::class); |
| 73 | + $token->method('getUser')->willReturn($this->createMock(UserInterface::class)); // authorization checker will ignore roles if user is null |
| 74 | + $token->method('getRoleNames')->willReturn($roles); |
79 | 75 | $this->tokenStorage->setToken($token);
|
80 | 76 |
|
81 | 77 | $this->assertTrue($this->publishWorkflowChecker->isGranted(PublishWorkflowChecker::VIEW_ATTRIBUTE, $doc));
|
82 | 78 | $this->assertFalse($this->publishWorkflowChecker->isGranted(PublishWorkflowChecker::VIEW_ANONYMOUS_ATTRIBUTE, $doc));
|
83 | 79 | }
|
84 | 80 |
|
85 |
| - public function testIgnoreRoleNotHas() |
| 81 | + public function testIgnoreRoleNotHas(): void |
86 | 82 | {
|
87 | 83 | $doc = $this->createMock(PublishModel::class);
|
88 | 84 | $doc
|
89 | 85 | ->method('isPublishable')
|
90 |
| - ->will($this->returnValue(false)) |
| 86 | + ->willReturn(false) |
91 | 87 | ;
|
92 | 88 | $roles = [
|
93 | 89 | 'OTHER_ROLE',
|
94 | 90 | ];
|
95 |
| - $token = new UsernamePasswordToken('test', 'pass', 'testprovider', $roles); |
| 91 | + $token = $this->createMock(TokenInterface::class); |
| 92 | + $token->method('getUser')->willReturn($this->createMock(UserInterface::class)); |
| 93 | + $token->method('getRoleNames')->willReturn($roles); |
96 | 94 | $this->tokenStorage->setToken($token);
|
97 | 95 |
|
98 | 96 | $this->assertFalse($this->publishWorkflowChecker->isGranted(PublishWorkflowChecker::VIEW_ATTRIBUTE, $doc));
|
|
0 commit comments