Skip to content

Commit

Permalink
[TASK] Improve coverage of PageProvider test case
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Aug 23, 2023
1 parent a039129 commit a911d67
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
23 changes: 14 additions & 9 deletions Classes/Provider/PageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,13 @@ public function processTableConfiguration(array $row, array $configuration): arr
$inheritedConfiguration = [];
foreach ([self::FIELD_NAME_MAIN, self::FIELD_NAME_SUB] as $field) {
if (!empty($currentPageRecord[$field])) {
$currentPageRecord[$field] = GeneralUtility::xml2array($currentPageRecord[$field]);
$currentPageRecord[$field] = $this->convertXmlToArray($currentPageRecord[$field]) ?? [];
}
/** @var Form&Form $form */
$form = $this->getForm($row, $field);
foreach ($tree as $branch) {
if (!empty($branch[$field])) {
/** @var array $branchData */
$branchData = GeneralUtility::xml2array($branch[$field] ?? '');
$branchData = $this->convertXmlToArray($branch[$field] ?? '') ?? [];
$inheritedConfiguration[$field] = RecursiveArrayUtility::mergeRecursiveOverrule(
$inheritedConfiguration[$field] ?? [],
$branchData
Expand Down Expand Up @@ -322,22 +321,21 @@ public function processTableConfiguration(array $row, array $configuration): arr
if (is_array($configuration['databaseRow'][self::FIELD_NAME_MAIN])) {
$currentData = $configuration['databaseRow'][self::FIELD_NAME_MAIN];
} else {
/** @var array $currentData */
$currentData = GeneralUtility::xml2array($configuration['databaseRow'][self::FIELD_NAME_MAIN]);
$currentData = $this->convertXmlToArray($configuration['databaseRow'][self::FIELD_NAME_MAIN]) ?? [];
}
$configuration['databaseRow'][self::FIELD_NAME_MAIN] = RecursiveArrayUtility::mergeRecursiveOverrule(
$inheritedConfigurationForMainField,
$currentData
);
}
}
$subData = [];

if (is_array($configuration['databaseRow'][self::FIELD_NAME_SUB])) {
$currentData = $configuration['databaseRow'][self::FIELD_NAME_SUB];
$subData = $configuration['databaseRow'][self::FIELD_NAME_SUB];
} else {
/** @var array $currentData */
$currentData = GeneralUtility::xml2array($configuration['databaseRow'][self::FIELD_NAME_SUB]);
$subData = $this->convertXmlToArray($configuration['databaseRow'][self::FIELD_NAME_SUB]) ?? [];
}

$configuration['databaseRow'][self::FIELD_NAME_SUB] = RecursiveArrayUtility::mergeRecursiveOverrule(
$inheritedConfigurationForSubField,
$subData
Expand Down Expand Up @@ -433,6 +431,13 @@ private function extractDataStorageMirrorWithInheritableFields(
}
}

protected function convertXmlToArray(string $xml): ?array
{
/** @var string|array $converted */
$converted = GeneralUtility::xml2array($xml);
return is_string($converted) ? null : $converted;
}

/**
* Gets an inheritance tree (ordered first parent -> ... -> root record)
* of record arrays containing raw values, stopping at the first parent
Expand Down
59 changes: 59 additions & 0 deletions Tests/Unit/Provider/PageProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,65 @@ protected function getConstructorArguments(): array
];
}

public function testProcessTableConfigurationReturnsEarlyWithoutRecord(): void
{
$this->recordService->method('getSingle')->willReturn(null);
$instance = new PageProvider(...$this->getConstructorArguments());
$configuration = ['vanillaUid' => 123];
self::assertSame($configuration, $instance->processTableConfiguration(['uid' => 123], $configuration));
}

/**
* @param string|array $data
* @dataProvider getProcessTableConfigurationTestValues
*/
public function testProcessTableConfiguration($data): void
{
/** @var PageProvider|MockObject $instance */
$instance = $this->getMockBuilder(PageProvider::class)
->setConstructorArgs($this->getConstructorArguments())
->onlyMethods(['getForm', 'getInheritanceTree', 'convertXmlToArray'])
->getMock();
$instance->method('convertXmlToArray')->willReturn(['data' => ['foo' => 'bar']]);

$treeData = '<data />';

$tree = [
['uid' => 1, PageProvider::FIELD_NAME_MAIN => $treeData, PageProvider::FIELD_NAME_SUB => $treeData],
['uid' => 2, PageProvider::FIELD_NAME_MAIN => $treeData, PageProvider::FIELD_NAME_SUB => $treeData],
];

$instance->method('getInheritanceTree')->willReturn($tree);
$instance->method('getForm')->willReturn(Form::create());

$this->recordService->method('getSingle')
->willReturn(['uid' => 123, PageProvider::FIELD_NAME_MAIN => '<data />']);

$configuration = [
'vanillaUid' => 123,
'recordTypeValue' => 'content',
'databaseRow' => [
'uid' => 123,
PageProvider::FIELD_NAME_MAIN => $data,
PageProvider::FIELD_NAME_SUB => $data,
],
];

$expected = $configuration;
$expected['databaseRow'][PageProvider::FIELD_NAME_MAIN] = ['data' => ['foo' => 'bar']];
$expected['databaseRow'][PageProvider::FIELD_NAME_SUB] = ['data' => ['foo' => 'bar']];

self::assertSame($expected, $instance->processTableConfiguration(['uid' => 123], $configuration));
}

public function getProcessTableConfigurationTestValues(): array
{
return [
'With already unpacked FF data' => ['<data />'],
'With packed FF data' => [['data' => ['foo' => 'bar']]],
];
}

public function testGetExtensionKey(): void
{
/** @var PageProvider|MockObject $instance */
Expand Down

0 comments on commit a911d67

Please sign in to comment.