forked from ezsystems/repository-forms
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-27370: Missing validation of the language create/update form (ezs…
…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
Showing
5 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
36 changes: 36 additions & 0 deletions
36
tests/RepositoryForms/Validator/Constraints/UniqueLanguageCodeTest.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,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()); | ||
} | ||
} |
170 changes: 170 additions & 0 deletions
170
tests/RepositoryForms/Validator/Constraints/UniqueLanguageCodeValidatorTest.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,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); | ||
} | ||
} |