From 492c7b62d46f820301200f33c91082d3af5ca393 Mon Sep 17 00:00:00 2001 From: gggeek Date: Sun, 29 Oct 2017 01:11:08 +0100 Subject: [PATCH] more fixes and tests for trash handling --- Core/Executor/TrashManager.php | 96 ++++++++++++++++++++++---- Core/Matcher/TrashMatcher.php | 13 +++- Resources/config/services.yml | 1 + Resources/doc/DSL/Trash.yml | 19 ++--- Tests/dsl/good/UnitTestOK026_trash.yml | 22 ++++++ 5 files changed, 121 insertions(+), 30 deletions(-) diff --git a/Core/Executor/TrashManager.php b/Core/Executor/TrashManager.php index 320be300..b78c4c3e 100644 --- a/Core/Executor/TrashManager.php +++ b/Core/Executor/TrashManager.php @@ -2,8 +2,10 @@ namespace Kaliop\eZMigrationBundle\Core\Executor; +use Kaliop\eZMigrationBundle\API\Collection\LocationCollection; use Kaliop\eZMigrationBundle\API\Collection\TrashedItemCollection; use Kaliop\eZMigrationBundle\Core\Matcher\TrashMatcher; +use Kaliop\eZMigrationBundle\Core\Helper\SortConverter; /** * Handles trash migrations. @@ -16,12 +18,15 @@ class TrashManager extends RepositoryExecutor /** @var TrashMatcher $trashMatcher */ protected $trashMatcher; + protected $sortConverter; + /** * @param TrashMatcher $trashMatcher */ - public function __construct(TrashMatcher $trashMatcher) + public function __construct(TrashMatcher $trashMatcher, SortConverter $sortConverter) { $this->trashMatcher = $trashMatcher; + $this->sortConverter = $sortConverter; } /** @@ -38,6 +43,8 @@ protected function purge($step) /** * Handles the trash-restore migration action + * + * @todo support handling of restoration to custom locations */ protected function recover($step) { @@ -47,19 +54,19 @@ protected function recover($step) throw new \Exception("Can not execute Trash restore because multiple types match, and a references section is specified in the dsl. References can be set when only 1 section matches"); } + $locations = array(); $trashService = $this->repository->getTrashService(); foreach ($itemsCollection as $key => $item) { - /// @todo support handling of custom restoration locations - $trashService->recover($item); + $locations[] = $trashService->recover($item); } - $this->setReferences($itemsCollection, $step); + $this->setReferences(new LocationCollection($locations), $step); return $itemsCollection; } /** - * Handles the trash-restore migration action + * Handles the trash-delete migration action */ protected function delete($step) { @@ -101,7 +108,7 @@ protected function matchItems($action, $step) /** * Sets references to certain trashed-item attributes. * - * @param \eZ\Publish\API\Repository\Values\Content\TrashItem|TrashedItemCollection $item + * @param \eZ\Publish\API\Repository\Values\Content\TrashItem|TrashedItemCollection|\eZ\Publish\API\Repository\Values\Content\Location|LocationCollection $item * @param $step * @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute * @return boolean @@ -117,19 +124,78 @@ protected function setReferences($item, $step) foreach ($references as $reference) { switch ($reference['attribute']) { - /// @todo a trashed item extends a location, so in theory everything 'location' here should work - /*case 'section_id': + // a trashed item extends a location, so in theory everything 'location' here should work + case 'location_id': case 'id': - $value = $section->id; + $value = $item->id; break; - case 'section_identifier': - case 'identifier': - $value = $section->identifier; + case 'remote_id': + case 'location_remote_id': + $value = $item->remoteId; + break; + case 'always_available': + $value = $item->contentInfo->alwaysAvailable; + break; + case 'content_id': + $value = $item->contentId; + break; + case 'content_type_id': + $value = $item->contentInfo->contentTypeId; + break; + case 'content_type_identifier': + $contentTypeService = $this->repository->getContentTypeService(); + $value = $contentTypeService->loadContentType($item->contentInfo->contentTypeId)->identifier; + break; + case 'current_version': + case 'current_version_no': + $value = $item->contentInfo->currentVersionNo; + break; + case 'depth': + $value = $item->depth; + break; + case 'is_hidden': + $value = $item->hidden; + break; + case 'main_location_id': + $value = $item->contentInfo->mainLocationId; + break; + case 'main_language_code': + $value = $item->contentInfo->mainLanguageCode; + break; + case 'modification_date': + $value = $item->contentInfo->modificationDate->getTimestamp(); break; - case 'section_name': case 'name': - $value = $section->name; - break;*/ + $value = $item->contentInfo->name; + break; + case 'owner_id': + $value = $item->contentInfo->ownerId; + break; + case 'parent_location_id': + $value = $item->parentLocationId; + break; + case 'path': + $value = $item->pathString; + break; + case 'priority': + $value = $item->priority; + break; + case 'publication_date': + $value = $item->contentInfo->publishedDate->getTimestamp(); + break; + case 'section_id': + $value = $item->contentInfo->sectionId; + break; + case 'section_identifier': + $sectionService = $this->repository->getSectionService(); + $value = $sectionService->loadSection($item->contentInfo->sectionId)->identifier; + break; + case 'sort_field': + $value = $this->sortConverter->sortField2Hash($item->sortField); + break; + case 'sort_order': + $value = $this->sortConverter->sortOrder2Hash($item->sortOrder); + break; default: throw new \InvalidArgumentException('Trash Manager does not support setting references for attribute ' . $reference['attribute']); } diff --git a/Core/Matcher/TrashMatcher.php b/Core/Matcher/TrashMatcher.php index adcf444a..aa9ed8ca 100644 --- a/Core/Matcher/TrashMatcher.php +++ b/Core/Matcher/TrashMatcher.php @@ -5,11 +5,22 @@ use Kaliop\eZMigrationBundle\API\Collection\TrashedItemCollection; use \eZ\Publish\API\Repository\Values\Content\Query; -/// q: is it better to extends Content or Location Matcher ? +/// q: is it better to extend Content or Location Matcher ? class TrashMatcher extends ContentMatcher { const MATCH_ITEM_ID = 'item_id'; + protected $allowedConditions = array( + self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, + self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, + self::MATCH_ATTRIBUTE, self::MATCH_CONTENT_TYPE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, self::MATCH_GROUP, + self::MATCH_CREATION_DATE, self::MATCH_MODIFICATION_DATE, self::MATCH_OBJECT_STATE, self::MATCH_OWNER, + self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_SECTION, self::MATCH_SUBTREE, + self::MATCH_VISIBILITY, + // aliases + 'content_type', 'content_type_id', 'content_type_identifier', + ); + protected $returns = 'Trashed-Item'; /** diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 8504a761..f95e8f4c 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -365,6 +365,7 @@ services: class: '%ez_migration_bundle.executor.trash_manager.class%' arguments: - '@ez_migration_bundle.trash_matcher' + - '@ez_migration_bundle.helper.sort_converter' tags: - { name: ez_migration_bundle.executor } diff --git a/Resources/doc/DSL/Trash.yml b/Resources/doc/DSL/Trash.yml index cb6c9d25..83686015 100644 --- a/Resources/doc/DSL/Trash.yml +++ b/Resources/doc/DSL/Trash.yml @@ -1,32 +1,23 @@ +# Empties the trash - type: trash mode: purge +# Restores a Location that is in the trash - type: trash mode: recover match: - xxx - parent_location: [x, y, z] # Optional: the new parent location for the recovered trashed item. Multiple locations can be added in a single step using an array + xxx # See the conditions for content/update references: # Optional - identifier: referenceId # A string used to identify the reference attribute: attribute # An attribute to get the value of for the reference. # Supports: ... +# Deletes definitively a Location that is already in the trash - type: trash mode: delete match: - xxx - -#- -# type: trash -# mode: load -# match: -# xxx -# references: # Optional -# - -# identifier: referenceId # A string used to identify the reference -# attribute: attribute # An attribute to get the value of for the reference. -# # Supports: ... + xxx # See the conditions for content/update diff --git a/Tests/dsl/good/UnitTestOK026_trash.yml b/Tests/dsl/good/UnitTestOK026_trash.yml index 68590a0b..1fe90455 100644 --- a/Tests/dsl/good/UnitTestOK026_trash.yml +++ b/Tests/dsl/good/UnitTestOK026_trash.yml @@ -60,6 +60,10 @@ parent_location: reference:kmb_test_026_1_loc match: content_id: reference:kmb_test_026_3 + references: + - + identifier: kmb_test_026_3_loc2 + attribute: location_id # check that obj 3 has 2 locations @@ -111,6 +115,10 @@ mode: recover match: location_id: reference:kmb_test_026_2_loc + references: + - + identifier: kmb_test_026_2_recovered_loc + attribute: location_id # check that obj 2 has again 1 locations @@ -131,6 +139,20 @@ test: equals: 1 +# trash then delete an item + +- + type: location + mode: trash + match: + location_id: reference:kmb_test_026_2_recovered_loc + +- + type: trash + mode: delete + match: + location_id: reference:kmb_test_026_2_recovered_loc + - type: trash mode: purge