Skip to content

Commit

Permalink
more fixes and tests for trash handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Oct 29, 2017
1 parent 55e0736 commit 492c7b6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 30 deletions.
96 changes: 81 additions & 15 deletions Core/Executor/TrashManager.php
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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']);
}
Expand Down
13 changes: 12 additions & 1 deletion Core/Matcher/TrashMatcher.php
Expand Up @@ -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';

/**
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.yml
Expand Up @@ -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 }

Expand Down
19 changes: 5 additions & 14 deletions 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
22 changes: 22 additions & 0 deletions Tests/dsl/good/UnitTestOK026_trash.yml
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 492c7b6

Please sign in to comment.