Skip to content

Commit

Permalink
Merge branch '1.5' into 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
glye committed Jul 28, 2017
2 parents d3427e0 + 691aba5 commit 422fba2
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 5 deletions.
7 changes: 6 additions & 1 deletion bundle/Resources/config/validation.yml
Expand Up @@ -60,9 +60,14 @@ EzSystems\RepositoryForms\Data\FieldDefinitionData:
- Type:
type: integer

EzSystems\RepositoryForms\Data\RoleData:
EzSystems\RepositoryForms\Data\Role\RoleData:
constraints:
- EzSystems\RepositoryForms\Validator\Constraints\UniqueRoleIdentifier: ~
properties:
identifier:
- NotBlank: ~
- Length:
max: 255

eZ\Publish\API\Repository\Values\Content\SectionStruct:
constraints:
Expand Down
5 changes: 5 additions & 0 deletions bundle/Resources/translations/validators.en.xlf
Expand Up @@ -36,6 +36,11 @@
<target>Section identifier may only contain letters from "a" to "z", numbers and underscores.</target>
<note>key: ez.section.identifier.format</note>
</trans-unit>
<trans-unit id="963a415cd08151ecf8726e651a7a36140e05f4b5" resname="ez.role.identifier.unique">
<source>The role identifier "%identifier%" is used by another role. You must enter a unique identifier.</source>
<target>The role identifier "%identifier%" is used by another role. You must enter a unique identifier.</target>
<note>key: ez.role.identifier.unique</note>
</trans-unit>
</body>
</file>
</xliff>
Expand Up @@ -9,9 +9,9 @@
>{{ "role.policy.limitation.location.udw_button"|trans({}, "ezrepoforms_role") }}</button>
<div>
<ul id="{{form.limitationValues.vars.id}}-selected-location">
{% for limitationValue in form.limitationValues.vars.value %}
{% for limitationValue in form.limitationValues.vars.value|split(',') %}
<li>
{{ render( controller( "ez_content:view", {'contentId': limitationValue, 'viewType': '_platformui_content_name'} ) ) }}
{{ render( controller( "ez_content:viewAction", {'locationId': limitationValue, 'viewType': '_platformui_content_name'} ) ) }}
</li>
{% endfor %}
</ul>
Expand Down
21 changes: 20 additions & 1 deletion lib/Limitation/DataTransformer/UDWBasedValueTransformer.php
Expand Up @@ -24,7 +24,12 @@ public function transform($value)
return null;
}

return implode(',', $value);
$locations = [];
foreach ($value as $key => $path) {
$locations[] = $this->extractLocationIdFromPath($path);
}

return implode(',', $locations);
}

public function reverseTransform($value)
Expand All @@ -35,4 +40,18 @@ public function reverseTransform($value)

return explode(',', $value);
}

/**
* Extracts and returns an item id from a path, e.g. /1/2/58 => 58.
*
* @param string $path
*
* @return mixed
*/
private function extractLocationIdFromPath($path)
{
$pathParts = explode('/', trim($path, '/'));

return array_pop($pathParts);
}
}
Expand Up @@ -36,7 +36,7 @@ public function __construct(RoleService $roleService)
*/
public function validate($value, Constraint $constraint)
{
if (!$value instanceof RoleData) {
if (!$value instanceof RoleData || $value->identifier === null) {
return;
}

Expand Down
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace EzSystems\RepositoryForms\Tests\Validator\Constraints;

use EzSystems\RepositoryForms\Validator\Constraints\UniqueRoleIdentifier;
use PHPUnit_Framework_TestCase;
use Symfony\Component\Validator\Constraint;

class UniqueRoleIdentifierTest extends PHPUnit_Framework_TestCase
{
public function testConstruct()
{
$constraint = new UniqueRoleIdentifier();
self::assertSame('ez.role.identifier.unique', $constraint->message);
}

public function testValidateBy()
{
$constraint = new UniqueRoleIdentifier();
self::assertSame('ezrepoforms.validator.unique_role_identifier', $constraint->validatedBy());
}

public function testGetTargets()
{
$constraint = new UniqueRoleIdentifier();
self::assertSame(Constraint::CLASS_CONSTRAINT, $constraint->getTargets());
}
}
@@ -0,0 +1,160 @@
<?php

/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace EzSystems\RepositoryForms\Tests\Validator\Constraints;

use eZ\Publish\API\Repository\Values\User\Role;
use eZ\Publish\API\Repository\Values\User\RoleDraft;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\RoleService;
use EzSystems\RepositoryForms\Data\Role\RoleData;
use EzSystems\RepositoryForms\Validator\Constraints\UniqueRoleIdentifier;
use EzSystems\RepositoryForms\Validator\Constraints\UniqueRoleIdentifierValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
use PHPUnit_Framework_TestCase;
use stdClass;

class UniqueRoleIdentifierValidatorTest extends PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $roleService;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $executionContext;

/**
* @var UniqueRoleIdentifierValidator
*/
private $validator;

protected function setUp()
{
parent::setUp();
$this->roleService = $this->getMock(RoleService::class);
$this->executionContext = $this->getMock(ExecutionContextInterface::class);
$this->validator = new UniqueRoleIdentifierValidator($this->roleService);
$this->validator->initialize($this->executionContext);
}

public function testNotRoleData()
{
$value = new stdClass();
$this->roleService
->expects($this->never())
->method('loadRoleByIdentifier');
$this->executionContext
->expects($this->never())
->method('buildViolation');

$this->validator->validate($value, new UniqueRoleIdentifier());
}

public function testNullRoleIdentifier()
{
$value = new RoleData(['identifier' => null]);
$this->roleService
->expects($this->never())
->method('loadRoleByIdentifier');
$this->executionContext
->expects($this->never())
->method('buildViolation');

$this->validator->validate($value, new UniqueRoleIdentifier());
}

public function testValid()
{
$identifier = 'foo_identifier';
$value = new RoleData(['identifier' => $identifier]);
$this->roleService
->expects($this->once())
->method('loadRoleByIdentifier')
->with($identifier)
->willThrowException(new NotFoundException('foo', 'bar'));
$this->executionContext
->expects($this->never())
->method('buildVioloation');

$this->validator->validate($value, new UniqueRoleIdentifier());
}

public function testEditingRoleDraftFromExistingRoleIsValid()
{
$identifier = 'foo_identifier';
$roleId = 123;
$roleDraft = $this->getMockBuilder(RoleDraft::class)
->setConstructorArgs([['id' => $roleId]])
->getMockForAbstractClass();
$value = new RoleData([
'identifier' => $identifier,
'roleDraft' => $roleDraft,
]);
$returnedRole = $this->getMockBuilder(Role::class)
->setConstructorArgs([['id' => $roleId]])
->getMockForAbstractClass();
$this->roleService
->expects($this->once())
->method('loadRoleByIdentifier')
->with($identifier)
->willReturn($returnedRole);
$this->executionContext
->expects($this->never())
->method('buildVioloation');

$this->validator->validate($value, new UniqueRoleIdentifier());
}

public function testInvalid()
{
$identifier = 'foo_identifier';
$roleDraft = $this->getMockBuilder(RoleDraft::class)
->setConstructorArgs([['id' => 456]])
->getMockForAbstractClass();
$value = new RoleData([
'identifier' => $identifier,
'roleDraft' => $roleDraft,
]);
$constraint = new UniqueRoleIdentifier();
$constraintViolationBuilder = $this->getMock(ConstraintViolationBuilderInterface::class);
$returnedRole = $this->getMockBuilder(Role::class)
->setConstructorArgs([['id' => 123]])
->getMockForAbstractClass();
$this->roleService
->expects($this->once())
->method('loadRoleByIdentifier')
->with($identifier)
->willReturn($returnedRole);
$this->executionContext
->expects($this->once())
->method('buildViolation')
->with($constraint->message)
->willReturn($constraintViolationBuilder);
$constraintViolationBuilder
->expects($this->once())
->method('atPath')
->with('identifier')
->willReturn($constraintViolationBuilder);
$constraintViolationBuilder
->expects($this->once())
->method('setParameter')
->with('%identifier%', $identifier)
->willReturn($constraintViolationBuilder);
$constraintViolationBuilder
->expects($this->once())
->method('addViolation');

$this->validator->validate($value, $constraint);
}
}

0 comments on commit 422fba2

Please sign in to comment.