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