Skip to content

Commit

Permalink
Merge branch '7.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Longosz committed Jan 25, 2019
2 parents 6793782 + 00d7535 commit 9a380fa
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 95 deletions.
220 changes: 217 additions & 3 deletions eZ/Publish/API/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
95 changes: 93 additions & 2 deletions eZ/Publish/Core/Persistence/Cache/Tests/TrashHandlerTest.php
Expand Up @@ -12,6 +12,8 @@
use eZ\Publish\Core\Persistence\Cache\LocationHandler;
use eZ\Publish\SPI\Persistence\Content\Location;
use eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler as TrashHandler;
use eZ\Publish\SPI\Persistence\Content\Location\Trashed;
use eZ\Publish\SPI\Persistence\Content\Relation;

/**
* Test case for Persistence\Cache\SectionHandler.
Expand All @@ -33,8 +35,6 @@ public function providerForUnCachedMethods(): array
// string $method, array $arguments, array? $tags, string? $key
return [
['loadTrashItem', [6]],
['emptyTrash', []],
['deleteTrashItem', [6]],
];
}

Expand Down Expand Up @@ -149,4 +149,95 @@ public function testTrashSubtree()
$handler = $this->persistenceCacheHandler->$handlerMethodName();
$handler->trashSubtree($locationId);
}

public function testDeleteTrashItem()
{
$trashedId = 6;
$contentId = 42;
$relationSourceContentId = 42;

$handlerMethodName = $this->getHandlerMethodName();

$innerHandler = $this->createMock($this->getHandlerClassName());

$innerHandler
->expects($this->once())
->method('loadTrashItem')
->with($trashedId)
->will($this->returnValue(new Trashed(['id' => $trashedId, 'contentId' => $contentId])));

$this->persistenceHandlerMock
->method($handlerMethodName)
->will($this->returnValue($innerHandler));

$contentHandlerMock = $this->createMock(ContentHandler::class);

$contentHandlerMock
->expects($this->once())
->method('loadReverseRelations')
->with($contentId)
->will($this->returnValue([new Relation(['sourceContentId' => $relationSourceContentId])]));

$this->persistenceHandlerMock
->method('contentHandler')
->will($this->returnValue($contentHandlerMock));

$tags = [
'content-fields-' . $relationSourceContentId,
];

$this->cacheMock
->expects($this->once())
->method('invalidateTags')
->with($tags);

/** @var \eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler $handler */
$handler = $this->persistenceCacheHandler->$handlerMethodName();
$handler->deleteTrashItem($trashedId);
}

public function testEmptyTrash()
{
$trashedId = 6;
$contentId = 42;
$relationSourceContentId = 42;

$handlerMethodName = $this->getHandlerMethodName();

$innerHandler = $this->createMock($this->getHandlerClassName());

$innerHandler
->expects($this->once())
->method('findTrashItems')
->will($this->returnValue([new Trashed(['id' => $trashedId, 'contentId' => $contentId])]));

$this->persistenceHandlerMock
->method($handlerMethodName)
->will($this->returnValue($innerHandler));

$contentHandlerMock = $this->createMock(ContentHandler::class);

$contentHandlerMock
->expects($this->once())
->method('loadReverseRelations')
->with($contentId)
->will($this->returnValue([new Relation(['sourceContentId' => $relationSourceContentId])]));

$this->persistenceHandlerMock
->method('contentHandler')
->will($this->returnValue($contentHandlerMock));

$tags = [
'content-fields-' . $relationSourceContentId,
];

$this->cacheMock
->expects($this->once())
->method('invalidateTags')
->with($tags);

/** @var \eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler $handler */
$handler = $this->persistenceCacheHandler->$handlerMethodName();
$handler->emptyTrash();
}
}

0 comments on commit 9a380fa

Please sign in to comment.