Skip to content

Commit

Permalink
EZP-27370: Missing validation of the language create/update form (ezs…
Browse files Browse the repository at this point in the history
…ystems#123)

* EZP-27370: Validation on create/edit language form is not working

* EZP-27370: Missing translation for validation message with "ez.language.code.unique" key

* EZP-27370: Unit tests for EzSystems\RepositoryForms\Validator\Constraints\{UniqueLanguageCodeTest,UniqueLanguageCodeValidatorTest}
  • Loading branch information
adamwojs authored and andrerom committed May 18, 2017
1 parent 7c8b706 commit 060cccb
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bundle/Resources/config/validation.yml
Expand Up @@ -99,3 +99,29 @@ EzSystems\RepositoryForms\Data\Content\ContentUpdateData:
EzSystems\RepositoryForms\Data\Content\FieldData:
constraints:
- EzSystems\RepositoryForms\Validator\Constraints\FieldValue: ~

EzSystems\RepositoryForms\Data\Language\LanguageCreateData:
constraints:
- EzSystems\RepositoryForms\Validator\Constraints\UniqueLanguageCode: ~
properties:
languageCode:
- NotBlank: ~
- Length:
max: 20
name:
- NotBlank: ~
- Length:
max: 255

EzSystems\RepositoryForms\Data\Language\LanguageUpdateData:
constraints:
- EzSystems\RepositoryForms\Validator\Constraints\UniqueLanguageCode: ~
properties:
languageCode:
- NotBlank: ~
- Length:
max: 20
name:
- NotBlank: ~
- Length:
max: 255
5 changes: 5 additions & 0 deletions bundle/Resources/translations/validators.en.xlf
Expand Up @@ -41,6 +41,11 @@
<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>
<trans-unit id="e7e08263e46325665a4d7f85d5f3465c37d49bb7" resname="ez.language.code.unique">
<source>The language code "%language_code%" is used by another language. You must enter a unique language code.</source>
<target>The language code "%language_code%" is used by another language. You must enter a unique language code.</target>
<note>key: ez.language.code.unique</note>
</trans-unit>
</body>
</file>
</xliff>
3 changes: 3 additions & 0 deletions lib/Validator/Constraints/UniqueLanguageCodeValidator.php
Expand Up @@ -11,6 +11,7 @@
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

Expand Down Expand Up @@ -55,6 +56,8 @@ public function validate($value, Constraint $constraint)
->addViolation();
} catch (NotFoundException $e) {
// Do nothing
} catch (InvalidArgumentException $e) {
// Do nothing
}
}
}
@@ -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\UniqueLanguageCode;
use PHPUnit_Framework_TestCase;
use Symfony\Component\Validator\Constraint;

class UniqueLanguageCodeTest extends PHPUnit_Framework_TestCase
{
public function testConstruct()
{
$constraint = new UniqueLanguageCode();
self::assertSame('ez.language.code.unique', $constraint->message);
}

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

public function testGetTargets()
{
$constraint = new UniqueLanguageCode();
self::assertSame(Constraint::CLASS_CONSTRAINT, $constraint->getTargets());
}
}
@@ -0,0 +1,170 @@
<?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\LanguageService;
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use EzSystems\RepositoryForms\Data\Language\LanguageCreateData;
use EzSystems\RepositoryForms\Validator\Constraints\UniqueLanguageCode;
use EzSystems\RepositoryForms\Validator\Constraints\UniqueLanguageCodeValidator;
use stdClass;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
use PHPUnit_Framework_TestCase;

class UniqueLanguageCodeValidatorTest extends PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $languageService;

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

/**
* @var \EzSystems\RepositoryForms\Validator\Constraints\UniqueLanguageCodeValidator
*/
private $validator;

protected function setUp()
{
$this->languageService = $this->getMock(LanguageService::class);
$this->executionContext = $this->getMock(ExecutionContextInterface::class);
$this->validator = new UniqueLanguageCodeValidator($this->languageService);
$this->validator->initialize($this->executionContext);
}

public function testUnsupportedValueType()
{
$value = new stdClass();
$this->languageService
->expects($this->never())
->method('loadLanguage');
$this->executionContext
->expects($this->never())
->method('buildViolation');

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

public function testInvalidLanguageCode()
{
$languageCode = '';
$value = new LanguageCreateData([
'languageCode' => $languageCode,
]);

$this->languageService
->expects($this->once())
->method('loadLanguage')
->with($languageCode)
->willThrowException(new InvalidArgumentException('languageCode', 'language code has an invalid value'));
$this->executionContext
->expects($this->never())
->method('buildVioloation');

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

public function testValid()
{
$languageCode = 'eng-GB';
$value = new LanguageCreateData([
'languageCode' => $languageCode,
]);

$this->languageService
->expects($this->once())
->method('loadLanguage')
->with($languageCode)
->willThrowException(new NotFoundException('Language', $languageCode));
$this->executionContext
->expects($this->never())
->method('buildVioloation');

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

public function testEditingLanguageIsValid()
{
$languageCode = 'eng-GB';
$languageId = 123;

$language = $this->getMockBuilder(Language::class)
->setConstructorArgs([['id' => $languageId]])
->getMockForAbstractClass();
$value = new LanguageCreateData([
'languageCode' => $languageCode,
'language' => $language,
]);
$returnedLanguage = $this->getMockBuilder(Language::class)
->setConstructorArgs([['id' => $languageId]])
->getMockForAbstractClass();
$this->languageService
->expects($this->once())
->method('loadLanguage')
->with($languageCode)
->willReturn($returnedLanguage);
$this->executionContext
->expects($this->never())
->method('buildVioloation');

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

public function testInvalid()
{
$languageCode = 'eng-GB';

$language = $this->getMockBuilder(Language::class)
->setConstructorArgs([['id' => 123]])
->getMockForAbstractClass();
$value = new LanguageCreateData([
'languageCode' => $languageCode,
'language' => $language,
]);
$returnedLanguage = $this->getMockBuilder(Language::class)
->setConstructorArgs([['id' => 456]])
->getMockForAbstractClass();
$this->languageService
->expects($this->once())
->method('loadLanguage')
->with($languageCode)
->willReturn($returnedLanguage);
$constraint = new UniqueLanguageCode();
$constraintViolationBuilder = $this->getMock(ConstraintViolationBuilderInterface::class);
$this->executionContext
->expects($this->once())
->method('buildViolation')
->with($constraint->message)
->willReturn($constraintViolationBuilder);
$constraintViolationBuilder
->expects($this->once())
->method('atPath')
->with('language_code')
->willReturn($constraintViolationBuilder);
$constraintViolationBuilder
->expects($this->once())
->method('setParameter')
->with('%language_code%', $languageCode)
->willReturn($constraintViolationBuilder);
$constraintViolationBuilder
->expects($this->once())
->method('addViolation');

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

0 comments on commit 060cccb

Please sign in to comment.