Skip to content

Commit

Permalink
Merge branch '1.2' into 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed May 17, 2021
2 parents ee679d6 + 6fb537e commit 606a517
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 3 deletions.
61 changes: 61 additions & 0 deletions eZ/Publish/API/Repository/Tests/ContentTypeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use eZ\Publish\API\Repository\Exceptions\ContentTypeFieldDefinitionValidationException;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCollection as APIFieldDefinitionCollection;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
use eZ\Publish\API\Repository\Values\Translation\Message;
use Exception;
use eZ\Publish\Core\FieldType\TextLine\Value as TextLineValue;
Expand Down Expand Up @@ -2019,6 +2020,66 @@ public function testRemoveFieldDefinitionRemovesFieldFromContentRemoved($data)
);
}

/**
* @covers \eZ\Publish\API\Repository\ContentTypeService::removeFieldDefinition()
*/
public function testRemoveFieldDefinitionRemovesOrphanedRelations(): void
{
$repository = $this->getRepository();

$contentTypeService = $repository->getContentTypeService();
$contentService = $repository->getContentService();

// Create ContentType
$contentTypeDraft = $this->createContentTypeDraft([$this->getRelationFieldDefinition()]);
$contentTypeService->publishContentTypeDraft($contentTypeDraft);
$publishedType = $contentTypeService->loadContentType($contentTypeDraft->id);

// Create Content with Relation
$contentDraft = $this->createContentDraft();
$publishedVersion = $contentService->publishVersion($contentDraft->versionInfo);

$newDraft = $contentService->createContentDraft($publishedVersion->contentInfo);
$updateStruct = $contentService->newContentUpdateStruct();
$updateStruct->setField('relation', 14, 'eng-US');
$contentDraft = $contentService->updateContent($newDraft->versionInfo, $updateStruct);
$publishedContent = $contentService->publishVersion($contentDraft->versionInfo);

// Remove field definition from ContentType
$contentTypeDraft = $contentTypeService->createContentTypeDraft($publishedType);
$relationField = $contentTypeDraft->getFieldDefinition('relation');
$contentTypeService->removeFieldDefinition($contentTypeDraft, $relationField);
$contentTypeService->publishContentTypeDraft($contentTypeDraft);

// Load Content
$content = $contentService->loadContent($publishedContent->contentInfo->id);

$this->assertCount(0, $contentService->loadRelations($content->versionInfo));
}

private function getRelationFieldDefinition(): FieldDefinitionCreateStruct
{
$repository = $this->getRepository();

$contentTypeService = $repository->getContentTypeService();

$relationFieldCreate = $contentTypeService->newFieldDefinitionCreateStruct(
'relation',
'ezobjectrelation'
);
$relationFieldCreate->names = ['eng-US' => 'Relation'];
$relationFieldCreate->descriptions = ['eng-US' => 'Relation to any Content'];
$relationFieldCreate->fieldGroup = 'blog-content';
$relationFieldCreate->position = 3;
$relationFieldCreate->isTranslatable = false;
$relationFieldCreate->isRequired = false;
$relationFieldCreate->isInfoCollector = false;
$relationFieldCreate->validatorConfiguration = [];
$relationFieldCreate->isSearchable = false;

return $relationFieldCreate;
}

/**
* Test for the addFieldDefinition() method.
*
Expand Down
32 changes: 32 additions & 0 deletions eZ/Publish/API/Repository/Tests/UserServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,38 @@ public function testUpdateUserWithStrongPassword()
$this->assertInstanceOf(User::class, $user);
}

/**
* @covers \eZ\Publish\API\Repository\UserService::updateUser
*/
public function testUpdateUserByUserWithLimitations(): void
{
$repository = $this->getRepository();
$userService = $repository->getUserService();

$user = $this->createTestUserWithPassword('H@xxxiR!_1', $this->createUserContentTypeWithStrongPassword());

$currentUser = $this->createUserWithPolicies(
'user',
[
['module' => 'content', 'function' => 'edit'],
['module' => 'content', 'function' => 'read'],
['module' => 'content', 'function' => 'versionread'],
['module' => 'content', 'function' => 'publish'],
['module' => 'user', 'function' => 'password'],
],
new SubtreeLimitation(['limitationValues' => ['/1/5']])
);
$repository->getPermissionResolver()->setCurrentUserReference($currentUser);

// Create a new update struct instance
$userUpdate = $userService->newUserUpdateStruct();
$userUpdate->password = 'H@xxxiR!_2';

$user = $userService->updateUser($user, $userUpdate);

self::assertInstanceOf(User::class, $user);
}

/**
* @covers \eZ\Publish\API\Repository\UserService::updateUserPassword
*/
Expand Down
5 changes: 5 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ abstract public function deleteRelations(int $contentId, ?int $versionNo = null)
*/
abstract public function removeReverseFieldRelations(int $contentId): void;

/**
* Removes orphaned relations resulting from deleted relation fieldtype.
*/
abstract public function removeRelationsByFieldDefinitionId(int $fieldDefinitionId): void;

/**
* Delete the field with the given $fieldId.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,16 @@ public function removeReverseFieldRelations(int $contentId): void
}
}

public function removeRelationsByFieldDefinitionId(int $fieldDefinitionId): void
{
$query = $this->connection->createQueryBuilder();
$query->delete(self::CONTENT_RELATION_TABLE)
->where('contentclassattribute_id = :field_definition_id')
->setParameter('field_definition_id', $fieldDefinitionId, ParameterType::INTEGER);

$query->execute();
}

/**
* Update field value of RelationList field type identified by given $row data,
* removing relations toward given $contentId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ public function removeReverseFieldRelations(int $contentId): void
}
}

public function removeRelationsByFieldDefinitionId(int $fieldDefinitionId): void
{
try {
$this->innerGateway->removeRelationsByFieldDefinitionId($fieldDefinitionId);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
}

public function deleteField(int $fieldId): void
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ function ($versionNo) use ($contentId) {
);
}

// Delete from relations storage
$this->contentGateway->removeRelationsByFieldDefinitionId($this->fieldDefinition->id);

// Delete from internal storage -- field is always deleted from _all_ versions
foreach (array_keys($fieldIdSet) as $fieldId) {
$this->contentGateway->deleteField($fieldId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ public function testApplyMultipleVersionsMultipleTranslations()
->will($this->returnValue([$content2]));

$this->getContentGatewayMock()
->expects($this->at(4))
->expects($this->at(5))
->method('deleteField')
->with($this->equalTo($fieldId1));

$this->getContentGatewayMock()
->expects($this->at(5))
->expects($this->at(6))
->method('deleteField')
->with($this->equalTo($fieldId2));

Expand All @@ -244,6 +244,11 @@ public function testApplyMultipleVersionsMultipleTranslations()
$this->equalTo([$fieldId1, $fieldId2])
);

$this->getContentGatewayMock()
->expects($this->at(4))
->method('removeRelationsByFieldDefinitionId')
->with($this->equalTo(42));

$action->apply($contentId);
}

Expand Down
2 changes: 1 addition & 1 deletion eZ/Publish/Core/Repository/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ public function updateUser(APIUser $user, UserUpdateStruct $userUpdateStruct): A

if (!empty($userUpdateStruct->password) &&
!$canEditContent &&
!$this->permissionResolver->canUser('user', 'password', $loadedUser)
!$this->permissionResolver->canUser('user', 'password', $loadedUser, [$loadedUser])
) {
throw new UnauthorizedException('user', 'password');
}
Expand Down

0 comments on commit 606a517

Please sign in to comment.