Skip to content

Commit

Permalink
Merge branch '7.3' into 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Longosz committed Jan 25, 2019
2 parents 1c2d04f + e37b307 commit 71ca773
Showing 1 changed file with 217 additions and 3 deletions.
220 changes: 217 additions & 3 deletions Repository/Tests/LocationServiceTest.php
Expand Up @@ -8,15 +8,17 @@
*/
namespace eZ\Publish\API\Repository\Tests;

use Exception;
use eZ\Publish\API\Repository\Exceptions\BadStateException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
use eZ\Publish\API\Repository\Values\Content\LocationList;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use Exception;
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;

/**
* Test case for operations in the LocationService using in memory storage.
Expand Down Expand Up @@ -1405,6 +1407,218 @@ public function testSwapLocation()
);
}

/**
* Test swapping secondary Location with main Location.
*
* @covers \eZ\Publish\API\Repository\LocationService::swapLocation
*
* @see https://jira.ez.no/browse/EZP-28663
*
* @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*
* @return int[]
*/
public function testSwapLocationForMainAndSecondaryLocation(): array
{
$repository = $this->getRepository();
$locationService = $repository->getLocationService();
$contentService = $repository->getContentService();

$folder1 = $this->createFolder(['eng-GB' => 'Folder1'], 2);
$folder2 = $this->createFolder(['eng-GB' => 'Folder2'], 2);
$folder3 = $this->createFolder(['eng-GB' => 'Folder3'], 2);

$primaryLocation = $locationService->loadLocation($folder1->contentInfo->mainLocationId);
$parentLocation = $locationService->loadLocation($folder2->contentInfo->mainLocationId);
$secondaryLocation = $locationService->createLocation(
$folder1->contentInfo,
$locationService->newLocationCreateStruct($parentLocation->id)
);

$targetLocation = $locationService->loadLocation($folder3->contentInfo->mainLocationId);

// perform sanity checks
$this->assertContentHasExpectedLocations([$primaryLocation, $secondaryLocation], $folder1);

// begin use case
$locationService->swapLocation($secondaryLocation, $targetLocation);

// test results
$primaryLocation = $locationService->loadLocation($primaryLocation->id);
$secondaryLocation = $locationService->loadLocation($secondaryLocation->id);
$targetLocation = $locationService->loadLocation($targetLocation->id);

self::assertEquals($folder1->id, $primaryLocation->contentInfo->id);
self::assertEquals($folder1->id, $targetLocation->contentInfo->id);
self::assertEquals($folder3->id, $secondaryLocation->contentInfo->id);

$this->assertContentHasExpectedLocations([$primaryLocation, $targetLocation], $folder1);

self::assertEquals(
$folder1,
$contentService->loadContent($folder1->id)
);

self::assertEquals(
$folder2,
$contentService->loadContent($folder2->id)
);

// only in case of Folder 3, main location id changed due to swap
self::assertEquals(
$secondaryLocation->id,
$contentService->loadContent($folder3->id)->contentInfo->mainLocationId
);

return [$folder1, $folder2, $folder3];
}

/**
* Compare Ids of expected and loaded Locations for the given Content.
*
* @param \eZ\Publish\API\Repository\Values\Content\Location[] $expectedLocations
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
*/
private function assertContentHasExpectedLocations(array $expectedLocations, Content $content)
{
$repository = $this->getRepository(false);
$locationService = $repository->getLocationService();

$expectedLocationIds = array_map(
function (Location $location) {
return (int)$location->id;
},
$expectedLocations
);

$actualLocationsIds = array_map(
function (Location $location) {
return $location->id;
},
$locationService->loadLocations($content->contentInfo)
);
self::assertCount(count($expectedLocations), $actualLocationsIds);

// perform unordered equality assertion
self::assertEquals(
$expectedLocationIds,
$actualLocationsIds,
sprintf(
'Content %d contains Locations %s, but expected: %s',
$content->id,
implode(', ', $actualLocationsIds),
implode(', ', $expectedLocationIds)
),
0.0,
10,
true
);
}

/**
* @depends testSwapLocationForMainAndSecondaryLocation
*
* @param \eZ\Publish\API\Repository\Values\Content\Content[] $contentItems Content items created by testSwapLocationForSecondaryLocation
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function testSwapLocationDoesNotCorruptSearchResults(array $contentItems)
{
$repository = $this->getRepository(false);
$searchService = $repository->getSearchService();

$this->refreshSearch($repository);

$contentIds = array_map(
function (Content $content) {
return $content->id;
},
$contentItems
);

$query = new Query();
$query->filter = new Query\Criterion\ContentId($contentIds);

$searchResult = $searchService->findContent($query);

self::assertEquals(count($contentItems), $searchResult->totalCount);
self::assertEquals(
$searchResult->totalCount,
count($searchResult->searchHits),
'Total count of search result hits does not match the actual number of found results'
);
$foundContentIds = array_map(
function (SearchHit $searchHit) {
return $searchHit->valueObject->id;
},
$searchResult->searchHits
);
sort($contentIds);
sort($foundContentIds);
self::assertSame(
$contentIds,
$foundContentIds,
'Got different than expected Content item Ids'
);
}

/**
* Test swapping two secondary (non-main) Locations.
*
* @covers \eZ\Publish\API\Repository\LocationService::swapLocation
*
* @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testSwapLocationForSecondaryLocations()
{
$repository = $this->getRepository();
$locationService = $repository->getLocationService();
$contentService = $repository->getContentService();

$folder1 = $this->createFolder(['eng-GB' => 'Folder1'], 2);
$folder2 = $this->createFolder(['eng-GB' => 'Folder2'], 2);
$parentFolder1 = $this->createFolder(['eng-GB' => 'Parent1'], 2);
$parentFolder2 = $this->createFolder(['eng-GB' => 'Parent2'], 2);

$parentLocation1 = $locationService->loadLocation($parentFolder1->contentInfo->mainLocationId);
$parentLocation2 = $locationService->loadLocation($parentFolder2->contentInfo->mainLocationId);
$secondaryLocation1 = $locationService->createLocation(
$folder1->contentInfo,
$locationService->newLocationCreateStruct($parentLocation1->id)
);
$secondaryLocation2 = $locationService->createLocation(
$folder2->contentInfo,
$locationService->newLocationCreateStruct($parentLocation2->id)
);

// begin use case
$locationService->swapLocation($secondaryLocation1, $secondaryLocation2);

// test results
$secondaryLocation1 = $locationService->loadLocation($secondaryLocation1->id);
$secondaryLocation2 = $locationService->loadLocation($secondaryLocation2->id);

self::assertEquals($folder2->id, $secondaryLocation1->contentInfo->id);
self::assertEquals($folder1->id, $secondaryLocation2->contentInfo->id);

self::assertEquals(
$folder1,
$contentService->loadContent($folder1->id)
);

self::assertEquals(
$folder2,
$contentService->loadContent($folder2->id)
);
}

/**
* Test swapping Main Location of a Content with another one updates Content item Main Location.
*
Expand Down

0 comments on commit 71ca773

Please sign in to comment.