Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Aug 9, 2023
1 parent eb54e89 commit dda439d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 186 deletions.
48 changes: 48 additions & 0 deletions src/lib/Repository/Helper/NameSchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,54 @@ public function resolveUrlAliasSchema(Content $content, ContentType $contentType
$content->versionInfo->languageCodes
);
}

public function resolveNameSchema(
Content $content,
array $fieldMap = [],
array $languageCodes = [],
ContentType $contentType = null
): array {
$contentType ??= $content->getContentType();

$languageCodes = $languageCodes ?: $content->versionInfo->languageCodes;

return $this->resolve(
$contentType->nameSchema,
$contentType,
$this->mergeFieldMap(
$content,
$fieldMap,
$languageCodes
),
$languageCodes
);
}

public function resolve(string $nameSchema, ContentType $contentType, array $fieldMap, array $languageCodes): array
{
[$filteredNameSchema, $groupLookupTable] = $this->filterNameSchema($nameSchema);
$tokens = $this->extractTokens($filteredNameSchema);
$schemaIdentifiers = $this->getIdentifiers($nameSchema);

$names = [];

foreach ($languageCodes as $languageCode) {
// Fetch titles for language code
$titles = $this->getFieldTitles($schemaIdentifiers, $contentType, $fieldMap, $languageCode);
$name = $filteredNameSchema;

// Replace tokens with real values
foreach ($tokens as $token) {
$string = $this->resolveToken($token, $titles, $groupLookupTable);
$name = str_replace($token, $string, $name);
}
$name = $this->validateNameLength($name);

$names[$languageCode] = $name;
}

return $names;
}
}

class_alias(NameSchemaService::class, 'eZ\Publish\Core\Repository\Helper\NameSchemaService');
52 changes: 0 additions & 52 deletions src/lib/Repository/NameSchema/NameSchemaFilter.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/Repository/NameSchema/NameSchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class NameSchemaService implements NameSchemaServiceInterface
*/
protected array $settings;

private EventDispatcherInterface $eventDispatcher;
protected EventDispatcherInterface $eventDispatcher;

private SchemaIdentifierExtractorInterface $schemaIdentifierExtractor;
protected SchemaIdentifierExtractorInterface $schemaIdentifierExtractor;

/**
* @param array{limit?: integer, sequence?: string} $settings
Expand Down
99 changes: 0 additions & 99 deletions src/lib/Repository/NameSchema/TokenHandler.php

This file was deleted.

77 changes: 46 additions & 31 deletions tests/lib/Repository/NameSchema/NameSchemaServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Ibexa\Tests\Core\Repository\NameSchema;

use Ibexa\Contracts\Core\Event\NameSchema\AbstractSchemaEvent;
use Ibexa\Contracts\Core\Event\NameSchema\ResolveContentNameSchemaEvent;
use Ibexa\Contracts\Core\Event\NameSchema\ResolveNameSchemaEvent;
use Ibexa\Contracts\Core\Event\NameSchema\ResolveUrlAliasSchemaEvent;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinitionCollection as APIFieldDefinitionCollection;
Expand Down Expand Up @@ -36,11 +39,10 @@ public function testResolveUrlAliasSchema(): void
$content = $this->buildTestContentObject();
$contentType = $this->buildTestContentTypeStub();

$nameSchemaService = $this->buildNameSchemaService(
['field' => ['<url_alias_schema>']],
$content,
['eng-GB' => ['url_alias_schema' => 'foo']]
);
$event = new ResolveUrlAliasSchemaEvent(['field' => ['<url_alias_schema>']], $content);
$event->setTokenValues(['eng-GB' => ['url_alias_schema' => 'foo']]);

$nameSchemaService = $this->buildNameSchemaService($event);

$result = $nameSchemaService->resolveUrlAliasSchema($content, $contentType);

Expand All @@ -52,12 +54,10 @@ public function testResolveUrlAliasSchemaFallbackToNameSchema(): void
$content = $this->buildTestContentObject();
$contentType = $this->buildTestContentTypeStub(self::NAME_SCHEMA, '');

$nameSchemaService = $this->buildNameSchemaService(
['field' => [self::NAME_SCHEMA]],
$content,
['eng-GB' => ['name_schema' => 'bar']]
);
$event = new ResolveUrlAliasSchemaEvent(['field' => [self::NAME_SCHEMA]], $content);
$event->setTokenValues(['eng-GB' => ['name_schema' => 'bar']]);

$nameSchemaService = $this->buildNameSchemaService($event);
$result = $nameSchemaService->resolveUrlAliasSchema($content, $contentType);

self::assertEquals(['eng-GB' => 'bar'], $result);
Expand All @@ -70,6 +70,10 @@ public static function getDataForTestResolveNameSchema(): iterable
{
yield 'Default: Field Map and Languages taken from Content Version' => [
[],
[
'eng-GB' => ['text2' => 'two'],
'cro-HR' => ['text2' => 'dva'],
],
[],
[
'eng-GB' => 'two',
Expand All @@ -83,6 +87,10 @@ public static function getDataForTestResolveNameSchema(): iterable
'text2' => ['cro-HR' => new TextLineValue('Dva'), 'eng-GB' => new TextLineValue('two')],
'text3' => ['eng-GB' => new TextLineValue('three')],
],
[
'eng-GB' => ['text2' => 'two', 'text3' => 'three'],
'cro-HR' => ['text2' => 'Dva'],
],
['eng-GB', 'cro-HR'],
[
'eng-GB' => 'three',
Expand All @@ -95,19 +103,27 @@ public static function getDataForTestResolveNameSchema(): iterable
* @dataProvider getDataForTestResolveNameSchema
*
* @param array<string, array<string, string>> $fieldMap A map of Field Definition Identifier and Language Code to Field Value
* @param array<string, array<string, string>> $tokenValues
* @param array<string> $languageCodes
* @param array<string, string> $expectedNames
*/
public function testResolveNameSchema(array $fieldMap, array $languageCodes, array $expectedNames): void
public function testResolveNameSchema(array $fieldMap, array $tokenValues, array $languageCodes, array $expectedNames): void
{
$content = $this->buildTestContentObject();
$nameSchema = '<text3|text2>';
$nameSchemaService = $this->buildNameSchemaService(
['field' => [$nameSchema]],
$contentType = $this->buildTestContentTypeStub($nameSchema, $nameSchema);
$event = new ResolveContentNameSchemaEvent(
$content,
[]
['field' => ['text3', 'text2']],
$contentType,
$fieldMap,
$languageCodes
);
$event->setTokenValues($tokenValues);

$nameSchemaService = $this->buildNameSchemaService(
$event
);
$contentType = $this->buildTestContentTypeStub($nameSchema, $nameSchema);

$result = $nameSchemaService->resolveNameSchema($content, $fieldMap, $languageCodes, $contentType);

Expand Down Expand Up @@ -184,13 +200,21 @@ public function testResolve(
array $settings = []
): void {
$content = $this->buildTestContentObject();
$contentType = $this->buildTestContentTypeStub($nameSchema, $nameSchema);

$event = new ResolveNameSchemaEvent(
$schemaIdentifiers,
$contentType,
$content->fields,
$content->versionInfo->languageCodes
);

$event->setTokenValues($fieldTitles);

$nameSchemaService = $this->buildNameSchemaService(
['field' => [$nameSchema]],
$content,
[],
$event,
$settings
);
$contentType = $this->buildTestContentTypeStub($nameSchema, $nameSchema);

$result = $nameSchemaService->resolve(
$nameSchema,
Expand Down Expand Up @@ -299,13 +323,8 @@ protected function buildTestContentTypeStub(
* @param array<string, array<string, string>> $schemaIdentifiers
*/
protected function getEventDispatcherMock(
array $schemaIdentifiers,
Content $content,
array $tokenValues
AbstractSchemaEvent $event
): EventDispatcherInterface {
$event = new ResolveUrlAliasSchemaEvent($schemaIdentifiers, $content);
$event->setTokenValues($tokenValues);

$eventDispatcherMock = $this->getEventDispatcher();
$eventDispatcherMock->method('dispatch')
->willReturn($event);
Expand All @@ -314,14 +333,10 @@ protected function getEventDispatcherMock(
}

/**
* @param array<string, array<string>> $schemaIdentifiers
* @param array<string, array<string, string>> $tokenValues
* @param array{limit?: integer, sequence?: string} $settings
*/
private function buildNameSchemaService(
array $schemaIdentifiers,
Content $content,
array $tokenValues,
AbstractSchemaEvent $event,
array $settings = []
): NameSchemaService {
$fieldTypeRegistryMock = $this->getFieldTypeRegistryMock();
Expand All @@ -333,7 +348,7 @@ private function buildNameSchemaService(
return new NameSchemaService(
$fieldTypeRegistryMock,
new SchemaIdentifierExtractor(),
$this->getEventDispatcherMock($schemaIdentifiers, $content, $tokenValues),
$this->getEventDispatcherMock($event),
$settings
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public function getDataForTestExtract(): iterable
yield $schemaString => [
$schemaString,
[
'field' => ['description'],
'attribute' => ['mouse_type', 'mouse_weight'],
'field' => ['abc', 'xyz', 'name'],
'attribute' => ['color'],
],
];
}
Expand Down

0 comments on commit dda439d

Please sign in to comment.