Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-29849: Allow user to remove location in subtree/location limitation
- Loading branch information
Showing
7 changed files
with
403 additions
and
44 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
lib/Limitation/DataTransformer/UDWBasedValueModelTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\RepositoryForms\Limitation\DataTransformer; | ||
|
||
use eZ\Publish\API\Repository\Exceptions\NotFoundException; | ||
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; | ||
use eZ\Publish\API\Repository\LocationService; | ||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
/** | ||
* DataTransformer for UDWBasedMapper. | ||
* | ||
* Needed to display the form field correctly and transform it back to an appropriate value object. | ||
*/ | ||
class UDWBasedValueModelTransformer implements DataTransformerInterface | ||
{ | ||
/** @var \eZ\Publish\API\Repository\LocationService */ | ||
private $locationService; | ||
|
||
/** | ||
* @param \eZ\Publish\API\Repository\LocationService $locationService | ||
*/ | ||
public function __construct(LocationService $locationService) | ||
{ | ||
$this->locationService = $locationService; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transform($value) | ||
{ | ||
if (!is_array($value)) { | ||
return null; | ||
} | ||
|
||
try { | ||
return array_map(function (string $path) { | ||
return $this->locationService->loadLocation( | ||
$this->extractLocationIdFromPath($path) | ||
); | ||
}, $value); | ||
} catch (NotFoundException | UnauthorizedException $e) { | ||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (!is_array($value)) { | ||
return null; | ||
} | ||
|
||
return array_map(function(Location $location) { | ||
return $location->id; | ||
}, $value); | ||
} | ||
|
||
/** | ||
* Extracts and returns an item id from a path, e.g. /1/2/58/ => 58. | ||
* | ||
* @param string $path | ||
* | ||
* @return string|null | ||
*/ | ||
private function extractLocationIdFromPath(string $path): ?string | ||
{ | ||
$pathParts = explode('/', trim($path, '/')); | ||
|
||
return array_pop($pathParts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
lib/Limitation/DataTransformer/UDWBasedValueViewTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\RepositoryForms\Limitation\DataTransformer; | ||
|
||
use eZ\Publish\API\Repository\Exceptions\NotFoundException; | ||
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; | ||
use eZ\Publish\API\Repository\LocationService; | ||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
class UDWBasedValueViewTransformer implements DataTransformerInterface | ||
{ | ||
const DELIMITER = ','; | ||
|
||
/** @var \eZ\Publish\API\Repository\LocationService */ | ||
private $locationService; | ||
|
||
/** | ||
* @param \eZ\Publish\API\Repository\LocationService $locationService | ||
*/ | ||
public function __construct(LocationService $locationService) | ||
{ | ||
$this->locationService = $locationService; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transform($value) | ||
{ | ||
if (!is_array($value)) { | ||
return null; | ||
} | ||
|
||
return implode(self::DELIMITER, array_map(function (Location $location) { | ||
return $location->id; | ||
}, $value)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (!is_string($value) || $value === '') { | ||
return $value; | ||
} | ||
|
||
try { | ||
return array_map(function ($id) { | ||
return $this->locationService->loadLocation($id); | ||
}, explode(self::DELIMITER, $value)); | ||
} catch (NotFoundException | UnauthorizedException $e) { | ||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
tests/RepositoryForms/Limitation/DataTransformer/UDWBasedValueModelTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\RepositoryForms\Tests\Limitation\DataTransformer; | ||
|
||
use eZ\Publish\API\Repository\Exceptions\NotFoundException; | ||
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; | ||
use eZ\Publish\API\Repository\LocationService; | ||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
use EzSystems\RepositoryForms\Limitation\DataTransformer\UDWBasedValueModelTransformer; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
class UDWBasedValueModelTransformerTest extends TestCase | ||
{ | ||
/** @var \eZ\Publish\API\Repository\LocationService|\PHPUnit\Framework\MockObject\MockObject */ | ||
private $locationService; | ||
|
||
/** @var \EzSystems\RepositoryForms\Limitation\DataTransformer\UDWBasedValueModelTransformer */ | ||
private $transformer; | ||
|
||
protected function setUp() | ||
{ | ||
$this->locationService = $this->createMock(LocationService::class); | ||
$this->transformer = new UDWBasedValueModelTransformer( | ||
$this->locationService | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForTransform | ||
*/ | ||
public function testTransform(?array $given, ?array $expected) | ||
{ | ||
$this->locationService | ||
->method('loadLocation') | ||
->willReturnCallback(function($id) { | ||
return $this->createLocation($id); | ||
}); | ||
|
||
$this->assertEquals($expected, $this->transformer->transform($given)); | ||
} | ||
|
||
public function dataProviderForTransform(): array | ||
{ | ||
return [ | ||
[ null, null ], | ||
[ | ||
[ | ||
'/1/2/54/', | ||
'/1/2/54/56/', | ||
'/1/2/54/58/' | ||
], | ||
[ | ||
$this->createLocation(54), | ||
$this->createLocation(56), | ||
$this->createLocation(58), | ||
] | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForTransformThrowsTransformationFailedException | ||
*/ | ||
public function testTransformThrowsTransformationFailedException(string $exceptionClass) | ||
{ | ||
$this->expectException(TransformationFailedException::class); | ||
|
||
$this->locationService | ||
->expects($this->any()) | ||
->method('loadLocation') | ||
->willThrowException( | ||
$this->createMock($exceptionClass) | ||
); | ||
|
||
$this->transformer->transform(['/1/2/54']); | ||
} | ||
|
||
public function dataProviderForTransformThrowsTransformationFailedException(): array | ||
{ | ||
return [ | ||
[NotFoundException::class], | ||
[UnauthorizedException::class], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForReverseTransform | ||
*/ | ||
public function testReverseTransform(?array $given, ?array $expected) | ||
{ | ||
$this->assertEquals($expected, $this->transformer->reverseTransform($given)); | ||
} | ||
|
||
public function dataProviderForReverseTransform(): array | ||
{ | ||
return [ | ||
[ null, null ], | ||
[ | ||
[ | ||
$this->createLocation(54), | ||
$this->createLocation(56), | ||
$this->createLocation(58), | ||
], | ||
[ 54, 56, 58 ], | ||
] | ||
]; | ||
} | ||
|
||
private function createLocation($id): Location | ||
{ | ||
$location = $this->createMock(Location::class); | ||
$location | ||
->method('__get') | ||
->with('id') | ||
->willReturn($id); | ||
|
||
return $location; | ||
} | ||
} |
Oops, something went wrong.