Skip to content

Commit

Permalink
IBX-5888: Verification of whether the given landingPage field value i…
Browse files Browse the repository at this point in the history
…s correct has been changed (#29)

* IBX-5888: Verification of whether the given landingPage field value is correct has been changed

* IBX-5888: Corrected tests/CS

* IBX-5888: Corrected CS

* Corrected CS

* Changed is_subclass_of to is_a
  • Loading branch information
mateuszdebinski authored Aug 1, 2023
1 parent de0cc66 commit 13b3089
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"autoload-dev": {
"psr-4": {
"Ibexa\\Tests\\AutomatedTranslation\\": "tests/lib/",
"EzSystems\\EzPlatformAutomatedTranslation\\Tests\\": "tests/lib/",
"EzSystems\\EzPlatformAutomatedTranslationBundle\\Tests\\": "tests/bundle/"
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function decode(string $xml, Content $sourceContent): array
));
}

if (get_class($fieldValue) !== $type) {
if (!is_a($type, get_class($fieldValue), true)) {
throw new InvalidArgumentException(sprintf(
'Decoded field class mismatch: expected %s, actual: %s', $type, get_class($fieldValue)
));
Expand Down
4 changes: 1 addition & 3 deletions lib/Encoder/Field/PageBuilderFieldEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ final class PageBuilderFieldEncoder implements FieldEncoderInterface
{
private const CDATA_FAKER_TAG = 'fake_blocks_cdata';

private const XML_MARKUP = '<?xml version="1.0" encoding="UTF-8"?>';

/** @var \EzSystems\EzPlatformAutomatedTranslation\Encoder\BlockAttribute\BlockAttributeEncoderManager */
private $blockAttributeEncoderManager;

Expand All @@ -44,7 +42,7 @@ public function canEncode(Field $field): bool

public function canDecode(string $type): bool
{
return class_exists(Value::class) && Value::class === $type;
return class_exists(Value::class) && is_a($type, Value::class, true);
}

public function encode(Field $field): string
Expand Down
178 changes: 178 additions & 0 deletions tests/lib/Encoder/Field/PageBuilderFieldEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\AutomatedTranslation\Encoder\Field;

use eZ\Publish\API\Repository\Values\Content\Field;
use EzSystems\EzPlatformAutomatedTranslation\Encoder\BlockAttribute\BlockAttributeEncoderManager;
use EzSystems\EzPlatformAutomatedTranslation\Encoder\Field\PageBuilderFieldEncoder;
use EzSystems\EzPlatformPageFieldType\FieldType\LandingPage\Model\Attribute;
use EzSystems\EzPlatformPageFieldType\FieldType\LandingPage\Model\BlockValue;
use EzSystems\EzPlatformPageFieldType\FieldType\LandingPage\Model\Page;
use EzSystems\EzPlatformPageFieldType\FieldType\LandingPage\Model\Zone;
use EzSystems\EzPlatformPageFieldType\FieldType\LandingPage\Value;
use EzSystems\EzPlatformPageFieldType\FieldType\Page\Block\Definition\BlockAttributeDefinition;
use EzSystems\EzPlatformPageFieldType\FieldType\Page\Block\Definition\BlockDefinition;
use EzSystems\EzPlatformPageFieldType\FieldType\Page\Block\Definition\BlockDefinitionFactory;
use PHPUnit\Framework\TestCase;

final class PageBuilderFieldEncoderTest extends TestCase
{
public const ATTRIBUTE_VALUE = 'ibexa';

/** @var \EzSystems\EzPlatformAutomatedTranslation\Encoder\BlockAttribute\BlockAttributeEncoderManager */
private $blockAttributeEncoderManagerMock;

/** @var \EzSystems\EzPlatformPageFieldType\FieldType\Page\Block\Definition\BlockDefinitionFactory */
private $blockDefinitionFactoryMock;

public function setUp(): void {
$this->blockAttributeEncoderManagerMock = $this->createMock(BlockAttributeEncoderManager::class);
$this->blockDefinitionFactoryMock = $this->createMock(BlockDefinitionFactory::class);
}

public function testEncode(): void
{
$this->blockDefinitionFactoryMock
->method('getBlockDefinition')
->withAnyParameters()
->willReturn($this->getBlockDefinition());

$this->blockAttributeEncoderManagerMock
->method('encode')
->withAnyParameters()
->willReturn(self::ATTRIBUTE_VALUE);

$field = $this->getLandingPageField();
$subject = new PageBuilderFieldEncoder(
$this->blockAttributeEncoderManagerMock,
$this->blockDefinitionFactoryMock
);

$result = $subject->encode($field);

self::assertEquals($this->getEncodeResult(), $result);
}

public function testCanEncode(): void
{
$field = $this->getLandingPageField();
$subject = new PageBuilderFieldEncoder(
$this->blockAttributeEncoderManagerMock,
$this->blockDefinitionFactoryMock
);

self::assertTrue($subject->canEncode($field));
}

public function testDecode(): void
{
$this->blockAttributeEncoderManagerMock
->expects(self::atLeastOnce())
->method('decode')
->withAnyParameters()
->willReturn(self::ATTRIBUTE_VALUE);

$field = $this->getLandingPageField();
$subject = new PageBuilderFieldEncoder(
$this->blockAttributeEncoderManagerMock,
$this->blockDefinitionFactoryMock
);

$result = $subject->decode(
$this->getEncodeResult(),
$field->value
);

self::assertInstanceOf(Value::class, $result);
self::assertEquals(new Value($this->getPage()), $result);
}

public function testCanDecode(): void
{
$field = $this->getLandingPageField();
$subject = new PageBuilderFieldEncoder(
$this->blockAttributeEncoderManagerMock,
$this->blockDefinitionFactoryMock
);

self::assertTrue($subject->canDecode(get_class($field->value)));
}

private function getLandingPageField(): Field
{
return new Field([
'fieldDefIdentifier' => 'field_landing_page',
'value' => new Value($this->getPage()),
]);
}

private function getPage(): Page
{
return new Page('default', [$this->createZone()]);
}

private function createZone(): Zone
{
return new Zone('1', 'Foo', [
new BlockValue(
'1',
'tag',
'Code',
'default',
null,
null,
'',
null,
null,
[
new Attribute(
'1',
'content',
self::ATTRIBUTE_VALUE
),
]
),
]);
}

private function getBlockDefinition(): BlockDefinition
{
$blockDefinition = new BlockDefinition();
$blockDefinition->setIdentifier('tag');
$blockDefinition->setName('Code');
$blockDefinition->setCategory('default');
$blockDefinition->setThumbnail('fake_thumbnail');
$blockDefinition->setVisible(true);
$blockDefinition->setConfigurationTemplate('fake_configuration_template');
$blockDefinition->setViews([]);

$attributeDefinitions = [];
$blockAttributeDefinition = new BlockAttributeDefinition();
$blockAttributeDefinition->setIdentifier('1');
$blockAttributeDefinition->setName('content');
$blockAttributeDefinition->setType('string');
$blockAttributeDefinition->setConstraints([]);
$blockAttributeDefinition->setValue(self::ATTRIBUTE_VALUE);
$blockAttributeDefinition->setCategory('default');
$blockAttributeDefinition->setOptions([]);

$attributeDefinitions['content'] = $blockAttributeDefinition;

$blockDefinition->setAttributes($attributeDefinitions);

return $blockDefinition;
}

private function getEncodeResult(): string
{
return '<blocks><item key="1"><name>Code</name><attributes><content type="string">' .
self::ATTRIBUTE_VALUE . '</content></attributes></item></blocks>
';
}
}

0 comments on commit 13b3089

Please sign in to comment.