|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Silverback API Components Bundle Project |
| 5 | + * |
| 6 | + * (c) Daniel West <daniel@silverback.is> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Silverback\ApiComponentsBundle\Tests\Security\Voter; |
| 13 | + |
| 14 | +use ApiPlatform\Metadata\IriConverterInterface; |
| 15 | +use ApiPlatform\Metadata\ResourceAccessCheckerInterface; |
| 16 | +use Doctrine\Common\Collections\ArrayCollection; |
| 17 | +use Doctrine\Persistence\ManagerRegistry; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Silverback\ApiComponentsBundle\AttributeReader\PublishableAttributeReader; |
| 20 | +use Silverback\ApiComponentsBundle\DataProvider\PageDataProvider; |
| 21 | +use Silverback\ApiComponentsBundle\Entity\Core\ComponentGroup; |
| 22 | +use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition; |
| 23 | +use Silverback\ApiComponentsBundle\Entity\Core\Page; |
| 24 | +use Silverback\ApiComponentsBundle\Entity\Core\Route; |
| 25 | +use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker; |
| 26 | +use Silverback\ApiComponentsBundle\Helper\Route\RouteReachabilityResolver; |
| 27 | +use Silverback\ApiComponentsBundle\Security\Voter\ComponentVoter; |
| 28 | +use Silverback\ApiComponentsBundle\Tests\Functional\TestBundle\Entity\DummyComponent; |
| 29 | +use Symfony\Bundle\SecurityBundle\Security; |
| 30 | +use Symfony\Component\HttpFoundation\Request; |
| 31 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 32 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 33 | +use Symfony\Component\Security\Core\Authentication\Token\NullToken; |
| 34 | +use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
| 35 | + |
| 36 | +class ComponentVoterTest extends TestCase |
| 37 | +{ |
| 38 | + public function test_a_component_on_a_routed_page_is_granted_whichever_other_pages_it_is_also_on(): void |
| 39 | + { |
| 40 | + $routedPage = $this->page(new Route()); |
| 41 | + $routelessPage = $this->page(null); |
| 42 | + |
| 43 | + self::assertSame(VoterInterface::ACCESS_GRANTED, $this->vote($this->component([$routedPage], [$routelessPage]), $routedPage)); |
| 44 | + self::assertSame(VoterInterface::ACCESS_GRANTED, $this->vote($this->component([$routelessPage], [$routedPage]), $routedPage)); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_a_component_only_on_routeless_pages_is_denied(): void |
| 48 | + { |
| 49 | + $routedPage = $this->page(new Route()); |
| 50 | + |
| 51 | + self::assertSame(VoterInterface::ACCESS_DENIED, $this->vote($this->component([$this->page(null)], [$this->page(null)]), $routedPage)); |
| 52 | + } |
| 53 | + |
| 54 | + private function vote(DummyComponent $component, Page $routedPage): int |
| 55 | + { |
| 56 | + $security = $this->createStub(Security::class); |
| 57 | + $security->method('isGranted')->willReturnCallback(static fn (string $attribute, mixed $subject) => $subject === $routedPage->getRoute()); |
| 58 | + $pageDataProvider = $this->createStub(PageDataProvider::class); |
| 59 | + $pageDataProvider->method('findPageDataComponentMetadata')->willReturn([]); |
| 60 | + $pageDataProvider->method('findPageDataResourcesByPages')->willReturn([]); |
| 61 | + $accessChecker = $this->createStub(ResourceAccessCheckerInterface::class); |
| 62 | + $accessChecker->method('isGranted')->willReturn(false); |
| 63 | + $publishableStatusChecker = $this->createStub(PublishableStatusChecker::class); |
| 64 | + $publishableStatusChecker->method('getAttributeReader')->willReturn(new PublishableAttributeReader($this->createStub(ManagerRegistry::class))); |
| 65 | + $requestStack = new RequestStack(); |
| 66 | + $requestStack->push(Request::create('/component/dummy_components/1')); |
| 67 | + |
| 68 | + $voter = new ComponentVoter( |
| 69 | + $pageDataProvider, |
| 70 | + $this->createStub(IriConverterInterface::class), |
| 71 | + $this->createStub(HttpKernelInterface::class), |
| 72 | + $requestStack, |
| 73 | + $publishableStatusChecker, |
| 74 | + $this->createStub(ManagerRegistry::class), |
| 75 | + new RouteReachabilityResolver($this->createStub(ManagerRegistry::class), $security), |
| 76 | + $accessChecker, |
| 77 | + "is_granted('ROLE_ADMIN')", |
| 78 | + ); |
| 79 | + |
| 80 | + return $voter->vote(new NullToken(), $component, [ComponentVoter::READ_COMPONENT]); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param list<Page> ...$pagesPerGroup |
| 85 | + */ |
| 86 | + private function component(array ...$pagesPerGroup): DummyComponent |
| 87 | + { |
| 88 | + $component = new DummyComponent(); |
| 89 | + foreach ($pagesPerGroup as $pages) { |
| 90 | + $group = new ComponentGroup(); |
| 91 | + $group->pages = new ArrayCollection($pages); |
| 92 | + $position = new ComponentPosition(); |
| 93 | + $position->componentGroup = $group; |
| 94 | + $component->addComponentPosition($position); |
| 95 | + } |
| 96 | + |
| 97 | + return $component; |
| 98 | + } |
| 99 | + |
| 100 | + private function page(?Route $route): Page |
| 101 | + { |
| 102 | + $page = new Page(); |
| 103 | + if ($route) { |
| 104 | + $page->setRoute($route); |
| 105 | + } |
| 106 | + |
| 107 | + return $page; |
| 108 | + } |
| 109 | +} |
0 commit comments