|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +namespace Ibexa\Tests\Rest\Server\Output\ValueObjectVisitor; |
| 8 | + |
| 9 | +use eZ\Publish\Core\Repository\Values\Content\Location; |
| 10 | +use Ibexa\Contracts\Core\Repository\ContentService; |
| 11 | +use Ibexa\Contracts\Core\Repository\LocationService; |
| 12 | +use Ibexa\Contracts\Core\Repository\PermissionResolver; |
| 13 | +use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; |
| 14 | +use Ibexa\Contracts\Core\Repository\Values\Content\Location as ApiLocation; |
| 15 | +use Ibexa\Core\Base\Exceptions\UnauthorizedException; |
| 16 | +use Ibexa\Core\Repository\Values\Content\Content; |
| 17 | +use Ibexa\Core\Repository\Values\Content\VersionInfo; |
| 18 | +use Ibexa\Core\Repository\Values\ContentType\ContentType; |
| 19 | +use Ibexa\Rest\Server\Output\ValueObjectVisitor; |
| 20 | +use Ibexa\Tests\Rest\Output\ValueObjectVisitorBaseTest; |
| 21 | + |
| 22 | +final class LocationTest extends ValueObjectVisitorBaseTest |
| 23 | +{ |
| 24 | + private const MAIN_LOCATION_ID = 78; |
| 25 | + |
| 26 | + private const UNAUTHORIZED_MAIN_LOCATION_ID = 111; |
| 27 | + |
| 28 | + private const LOCATION_ID = 55; |
| 29 | + |
| 30 | + /** @var \Ibexa\Contracts\Core\Repository\LocationService&\PHPUnit\Framework\MockObject\MockObject */ |
| 31 | + private LocationService $locationServiceMock; |
| 32 | + |
| 33 | + /** @var \Ibexa\Contracts\Core\Repository\ContentService&\PHPUnit\Framework\MockObject\MockObject */ |
| 34 | + private ContentService $contentServiceMock; |
| 35 | + |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + $this->permissionResolverMock = $this->createMock(PermissionResolver::class); |
| 39 | + $this->locationServiceMock = $this->createMock(LocationService::class); |
| 40 | + $this->contentServiceMock = $this->createMock(ContentService::class); |
| 41 | + |
| 42 | + parent::setUp(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @dataProvider getDataForTestVisitLocationAttributesResolvesMainLocation |
| 47 | + */ |
| 48 | + public function testVisitLocationAttributesResolvesMainLocation( |
| 49 | + ?int $mainLocationId, |
| 50 | + int $locationId |
| 51 | + ): void { |
| 52 | + $visitor = $this->getVisitor(); |
| 53 | + $generator = $this->getGenerator(); |
| 54 | + |
| 55 | + $generator->startDocument(null); |
| 56 | + |
| 57 | + $contentId = 7; |
| 58 | + $versionInfo = new VersionInfo(); |
| 59 | + |
| 60 | + $location = new Location([ |
| 61 | + 'id' => $locationId, |
| 62 | + 'path' => ['1', '25', '42'], |
| 63 | + 'priority' => 1, |
| 64 | + 'sortField' => ApiLocation::SORT_FIELD_DEPTH, |
| 65 | + 'sortOrder' => ApiLocation::SORT_ORDER_ASC, |
| 66 | + 'parentLocationId' => 42, |
| 67 | + 'contentInfo' => new ContentInfo([ |
| 68 | + 'id' => $contentId, |
| 69 | + 'mainLocationId' => $mainLocationId, |
| 70 | + ]), |
| 71 | + 'content' => new Content([ |
| 72 | + 'id' => $contentId, |
| 73 | + 'contentType' => new ContentType(), |
| 74 | + 'versionInfo' => $versionInfo, |
| 75 | + ]), |
| 76 | + ]); |
| 77 | + |
| 78 | + $this->mockLoadLocation($location); |
| 79 | + |
| 80 | + $this->contentServiceMock->expects(self::once()) |
| 81 | + ->method('loadRelations') |
| 82 | + ->with($versionInfo) |
| 83 | + ->willReturn([]); |
| 84 | + |
| 85 | + $visitor->visit( |
| 86 | + $this->getVisitorMock(), |
| 87 | + $generator, |
| 88 | + $location |
| 89 | + ); |
| 90 | + |
| 91 | + $result = $generator->endDocument(null); |
| 92 | + |
| 93 | + self::assertNotNull($result); |
| 94 | + |
| 95 | + $this->assertXMLTag( |
| 96 | + [ |
| 97 | + 'tag' => 'Location', |
| 98 | + 'content' => $location->id . 1 . 'false' . 'false', |
| 99 | + ], |
| 100 | + $result, |
| 101 | + 'Invalid <Location> element.', |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private function mockLoadLocation(Location $location): void |
| 106 | + { |
| 107 | + $mainLocationId = $location->getContentInfo()->getMainLocationId(); |
| 108 | + |
| 109 | + switch ($mainLocationId) { |
| 110 | + case $location->id: |
| 111 | + case null: |
| 112 | + $this->locationServiceMock->expects(self::never()) |
| 113 | + ->method('loadLocation'); |
| 114 | + break; |
| 115 | + case self::UNAUTHORIZED_MAIN_LOCATION_ID: |
| 116 | + $this->locationServiceMock->expects(self::once()) |
| 117 | + ->method('loadLocation') |
| 118 | + ->with($mainLocationId) |
| 119 | + ->willThrowException(new UnauthorizedException('', '')); |
| 120 | + break; |
| 121 | + default: |
| 122 | + $this->locationServiceMock->expects(self::once()) |
| 123 | + ->method('loadLocation') |
| 124 | + ->with($mainLocationId) |
| 125 | + ->willReturn(new Location(['id' => $mainLocationId])); |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public function getDataForTestVisitLocationAttributesResolvesMainLocation(): iterable |
| 131 | + { |
| 132 | + yield 'same' => [self::MAIN_LOCATION_ID, self::MAIN_LOCATION_ID]; |
| 133 | + |
| 134 | + yield 'empty-main-location' => [null, self::LOCATION_ID]; |
| 135 | + |
| 136 | + yield 'different' => [999, self::LOCATION_ID]; |
| 137 | + |
| 138 | + yield 'unauthorized' => [self::UNAUTHORIZED_MAIN_LOCATION_ID, self::LOCATION_ID]; |
| 139 | + } |
| 140 | + |
| 141 | + protected function internalGetVisitor(): ValueObjectVisitor\Location |
| 142 | + { |
| 143 | + return new ValueObjectVisitor\Location($this->locationServiceMock, $this->contentServiceMock); |
| 144 | + } |
| 145 | +} |
0 commit comments