Skip to content

Commit

Permalink
EZP-28742: Add unit tests to RepositoryForms
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Jan 15, 2018
1 parent 84fb261 commit f7e7fff
Show file tree
Hide file tree
Showing 4 changed files with 804 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/lib/Tests/RepositoryForms/Data/ContentTranslationDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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.
*/
namespace EzSystems\EzPlatformAdminUi\Tests\RepositoryForms\Data;

use EzSystems\EzPlatformAdminUi\RepositoryForms\Data\ContentTranslationData;
use PHPUnit\Framework\TestCase;
use EzSystems\RepositoryForms\Data\Content\FieldData;
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;

class ContentTranslationDataTest extends TestCase
{
/** @var ContentTranslationData */
private $contentTranslationData;

public function setUp()
{
$this->contentTranslationData = new ContentTranslationData();
}

public function testAddFieldData()
{
$refObject = new \ReflectionObject($this->contentTranslationData);
$refProperty = $refObject->getProperty('fieldsData');
$refProperty->setAccessible(true);
$refProperty->setValue($this->contentTranslationData, []);

$this->contentTranslationData->addFieldData(new FieldData([
'fieldDefinition' => $this->getFieldDefinition(),
]));

$this->assertCount(1, $refProperty->getValue($this->contentTranslationData));

// Add another field with same identifier
$this->contentTranslationData->addFieldData(new FieldData([
'fieldDefinition' => $this->getFieldDefinition(),
]));
$this->assertCount(1, $refProperty->getValue($this->contentTranslationData));

// Add field with another identifier
$this->contentTranslationData->addFieldData(new FieldData([
'fieldDefinition' => $this->getFieldDefinition('another_identifier'),
]));
$this->assertCount(2, $refProperty->getValue($this->contentTranslationData));
}

/**
* @param string $identifier
*
* @return FieldDefinition
*/
private function getFieldDefinition(string $identifier = 'identifier'): FieldDefinition
{
return new FieldDefinition([
'identifier' => $identifier,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
<?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.
*/
namespace EzSystems\EzPlatformAdminUi\Tests\RepositoryForms\Data\Mapper;

use eZ\Publish\API\Repository\Values\ContentType\ContentType as ApiContentType;
use eZ\Publish\Core\Repository\Values\Content\Content;
use EzSystems\EzPlatformAdminUi\RepositoryForms\Data\ContentTranslationData;
use EzSystems\EzPlatformAdminUi\RepositoryForms\Data\Mapper\ContentTranslationMapper;
use PHPUnit\Framework\TestCase;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\API\Repository\Values\Content\Field;
use EzSystems\RepositoryForms\Data\Content\FieldData;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;

class ContentTranslationMapperTest extends TestCase
{
public const LANGUAGE_CODE = 'cyb-CY';

/** @var ContentTranslationMapper */
private $mapper;

public function setUp()
{
$this->mapper = new ContentTranslationMapper();
}

/**
* @dataProvider paramsProvider
*
* @param $content
* @param array $params
* @param $expectedData
*/
public function testMapToFormData(Content $content, array $params, ContentTranslationData $expectedData)
{
$actualData = $this->mapper->mapToFormData($content, $params);

$this->assertEquals($expectedData, $actualData);
}

public function paramsProvider(): array
{
$language = new Language(['languageCode' => self::LANGUAGE_CODE]);

$field1 = $this->getField();
$field2 = $this->getField('identifier2');
$field3 = $this->getField('identifier3');

$content_with_1_field = $this->getCompleteContent([$field1]);
$content_with_3_fields = $this->getCompleteContent([$field1, $field2, $field3]);

return [
'no_base_language' => [
$content_with_1_field,
[
'language' => $language,
'contentType' => $this->getContentType([$this->getFieldDefinition($field1->fieldDefIdentifier, true)]),
'baseLanguage' => null,
],
new ContentTranslationData([
'content' => $content_with_1_field,
'initialLanguageCode' => self::LANGUAGE_CODE,
'fieldsData' => [
$field1->fieldDefIdentifier => new FieldData([
'field' => $field1,
'fieldDefinition' => $this->getFieldDefinition($field1->fieldDefIdentifier, true),
'value' => 'default_value',
]),
],
]),
],
'one_field' => [
$content_with_1_field,
[
'language' => $language,
'contentType' => $this->getContentType([$this->getFieldDefinition()]),
'baseLanguage' => $language,
],
new ContentTranslationData([
'content' => $content_with_1_field,
'initialLanguageCode' => self::LANGUAGE_CODE,
'fieldsData' => [
$field1->fieldDefIdentifier => new FieldData([
'field' => $field1,
'fieldDefinition' => $this->getFieldDefinition(),
'value' => 'string_value',
]),
],
]),
],
'tree_fields' => [
$content_with_3_fields,
[
'language' => $language,
'contentType' => $this->getContentType([
$this->getFieldDefinition($field1->fieldDefIdentifier),
$this->getFieldDefinition($field2->fieldDefIdentifier),
$this->getFieldDefinition($field3->fieldDefIdentifier),
]),
'baseLanguage' => $language,
],
new ContentTranslationData([
'content' => $content_with_3_fields,
'initialLanguageCode' => self::LANGUAGE_CODE,
'fieldsData' => [
$field1->fieldDefIdentifier => new FieldData([
'field' => $field1,
'fieldDefinition' => $this->getFieldDefinition($field1->fieldDefIdentifier),
'value' => 'string_value',
]),
$field2->fieldDefIdentifier => new FieldData([
'field' => $field2,
'fieldDefinition' => $this->getFieldDefinition($field2->fieldDefIdentifier),
'value' => 'string_value',
]),
$field3->fieldDefIdentifier => new FieldData([
'field' => $field3,
'fieldDefinition' => $this->getFieldDefinition($field3->fieldDefIdentifier),
'value' => 'string_value',
]),
],
]),
],
];
}

/**
* @dataProvider wrongParamsProvider
*
* @param $content
* @param array $params
* @param array $exception
*/
public function testMapToFormDataWithoutRequiredParameter($content, array $params, array $exception)
{
$this->expectException($exception['class']);
$this->expectExceptionMessage($exception['message']);

$this->mapper->mapToFormData($content, $params);
}

public function wrongParamsProvider(): array
{
return [
'missing_language' => [
$this->getCompleteContent(),
[
'contentType' => 'contentType',
'baseLanguage' => 'baseLanguage',
],
[
'class' => MissingOptionsException::class,
'message' => 'The required option "language" is missing.',
],
],
'missing_content_type' => [
$this->getCompleteContent(),
[
'language' => 'language',
'baseLanguage' => null,
],
[
'class' => MissingOptionsException::class,
'message' => 'The required option "contentType" is missing.',
],
],
'wrong_type_of_language' => [
$this->getCompleteContent(),
[
'language' => 'language',
'contentType' => new ContentType(),
'baseLanguage' => null,
],
[
'class' => InvalidOptionsException::class,
'message' => sprintf('The option "language" with value "language" is expected to be of type "%s", but is of type "string".', Language::class),
],
],
'wrong_type_of_content_type' => [
$this->getCompleteContent(),
[
'language' => new Language(),
'contentType' => 'content_type',
'baseLanguage' => null,
],
[
'class' => InvalidOptionsException::class,
'message' => sprintf('The option "contentType" with value "content_type" is expected to be of type "%s", but is of type "string".', ApiContentType::class),
],
],
'wrong_type_of_base_language' => [
$this->getCompleteContent(),
[
'language' => new Language(),
'contentType' => new ContentType(),
'baseLanguage' => 'base_language',
],
[
'class' => InvalidOptionsException::class,
'message' => sprintf('The option "baseLanguage" with value "base_language" is expected to be of type "null" or "%s", but is of type "string".', Language::class),
],
],
];
}

/**
* @param Field[] $fields
*
* @return Content
*/
private function getCompleteContent(array $fields = []): Content
{
return new Content([
'internalFields' => $fields,
'versionInfo' => new VersionInfo([
'contentInfo' => new ContentInfo(['mainLanguageCode' => self::LANGUAGE_CODE]),
]),
]);
}

/**
* @param string $fieldDefIdentifier
* @param string $languageCode
* @param string $value
*
* @return Field
*/
private function getField($fieldDefIdentifier = 'identifier', $languageCode = self::LANGUAGE_CODE, $value = 'string_value'): Field
{
return new Field([
'fieldDefIdentifier' => $fieldDefIdentifier,
'languageCode' => $languageCode,
'value' => $value,
]);
}

/**
* @param FieldDefinition[] $fieldDefs
*
* @return ContentType
*/
private function getContentType(array $fieldDefs = []): ContentType
{
return new ContentType([
'fieldDefinitions' => $fieldDefs,
]);
}

/**
* @param string $identifier
* @param bool $isTranslatable
*
* @return FieldDefinition
*/
private function getFieldDefinition(string $identifier = 'identifier', bool $isTranslatable = false): FieldDefinition
{
return new FieldDefinition([
'identifier' => $identifier,
'defaultValue' => 'default_value',
'isTranslatable' => $isTranslatable,
]);
}
}
Loading

0 comments on commit f7e7fff

Please sign in to comment.