177 lines
6.2 KiB
PHP
177 lines
6.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ElixirAudit\Tests\DataSetup\Traits\App\Helpers;
|
|
|
|
/**
|
|
* Tests provide a small per-language config; this trait expands it into the object-structure
|
|
* used by existing tests ($page->menuItem, $page->gM->pageTitle, etc.).
|
|
*/
|
|
trait pageDataTrait
|
|
{
|
|
private array $uoDefaultTableColumns = [
|
|
'h_checkbox',
|
|
'h_id',
|
|
'h_a_title',
|
|
'h_a_desc',
|
|
'h_cl_name',
|
|
'h_a_created',
|
|
'h_a_valid_from',
|
|
'h_a_valid_till',
|
|
'h_status',
|
|
'h_buttons',
|
|
];
|
|
|
|
private array $uoDefaultTableColumnsToSkip = [
|
|
'h_checkbox',
|
|
'h_status',
|
|
'h_buttons',
|
|
];
|
|
|
|
/**
|
|
* Build expanded page data for Unified Objects tests.
|
|
*
|
|
* Input config (per language):
|
|
* [
|
|
* LANG_CODE => [
|
|
* 'url' => '...',
|
|
* 'menuItem' => '...',
|
|
* 'pageTitle' => '...',
|
|
* 'objectItem' => '...',
|
|
* 'messagesExist' => ['mod' => '...', 'sub' => '...'], // partial ok
|
|
*
|
|
* 'tableColumns' => [
|
|
* 'remove' => ['h_cl_name'],
|
|
* 'add' => ['h_custom' => 'Custom label'], // adds to order + adds label
|
|
* ],
|
|
* 'rename' => ['h_a_title' => 'Title'], // label override for existing column
|
|
*
|
|
* 'tableColumnsToSkip' => [
|
|
* 'add' => ['h_custom'],
|
|
* 'remove' => ['h_status'],
|
|
* ],
|
|
* ],
|
|
* ]
|
|
*
|
|
* Extra supported keys (optional):
|
|
* - 'dropdownActions' => ['inew' => '...', ...]
|
|
* - 'tableColumnsOrder' => ['h_checkbox', 'h_id', ...] // overrides default order for this language/test
|
|
*
|
|
* @return array<string, object> languageCode => pageData object
|
|
*/
|
|
protected function getPageData(array $config): array
|
|
{
|
|
$out = [];
|
|
|
|
foreach ($config as $langCode => $cfg) {
|
|
if (!is_array($cfg)) {
|
|
continue;
|
|
}
|
|
|
|
// TODO: Table columns and skip columns should be simplified.
|
|
$tableCfg = is_array($cfg['tableColumns'] ?? null) ? $cfg['tableColumns'] : [];
|
|
$rename = is_array($tableCfg['rename'] ?? null) ? $tableCfg['rename'] : [];
|
|
$removeCols = is_array($tableCfg['remove'] ?? null) ? $tableCfg['remove'] : [];
|
|
$addCols = is_array($tableCfg['add'] ?? null) ? $tableCfg['add'] : [];
|
|
|
|
$skipCfg = is_array($cfg['tableColumnsToSkip'] ?? null) ? $cfg['tableColumnsToSkip'] : [];
|
|
$skipAdd = is_array($skipCfg['add'] ?? null) ? $skipCfg['add'] : [];
|
|
$skipRemove = is_array($skipCfg['remove'] ?? null) ? $skipCfg['remove'] : [];
|
|
|
|
$baseOrder = is_array($cfg['tableColumnsOrder'] ?? null)
|
|
? $cfg['tableColumnsOrder']
|
|
: $this->uoDefaultTableColumns;
|
|
|
|
$tableColumns = $this->buildTableColumnsFromBase($baseOrder, $removeCols, array_keys($addCols));
|
|
$tableColumnsToSkip = $this->buildTableColumnsToSkip($removeCols, $skipAdd, $skipRemove);
|
|
|
|
// Labels map:
|
|
// - rename overrides existing labels
|
|
// - add provides labels for new columns (and can also override if same key)
|
|
$labels = [];
|
|
foreach ($rename as $id => $label) {
|
|
$labels[(string)$id] = (string)$label;
|
|
}
|
|
foreach ($addCols as $id => $label) {
|
|
$labels[(string)$id] = (string)$label;
|
|
}
|
|
|
|
$expanded = [
|
|
'url' => isset($cfg['url']) ? (string)$cfg['url'] : '',
|
|
'menuItem' => isset($cfg['menuItem']) ? (string)$cfg['menuItem'] : '',
|
|
'menuParentItem' => isset($cfg['menuItem']) ? (string)$cfg['menuParentItem'] : '',
|
|
'gM' => (object)[
|
|
'pageTitle' => isset($cfg['pageTitle']) ? (string)$cfg['pageTitle'] : '',
|
|
'objectItem' => isset($cfg['objectItem']) ? (string)$cfg['objectItem'] : '',
|
|
'messagesExist' => is_array($cfg['messagesExist'] ?? null) ? $cfg['messagesExist'] : [],
|
|
'tableColumns' => $labels,
|
|
'dropdownActions' => is_array($cfg['dropdownActions'] ?? null) ? $cfg['dropdownActions'] : [],
|
|
'validationErrors' => is_array($cfg['validationErrors'] ?? null) ? $cfg['validationErrors'] : [],
|
|
'rowActions' => is_array($cfg['rowActions'] ?? null) ? $cfg['rowActions'] : [],
|
|
'dialog' => is_array($cfg['dialog'] ?? null) ? $cfg['dialog'] : [],
|
|
],
|
|
'tableColumns' => $tableColumns,
|
|
'tableColumnsToSkip' => $tableColumnsToSkip,
|
|
];
|
|
|
|
$out[(string)$langCode] = (object) $expanded;
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
/**
|
|
* @param string[] $baseOrder
|
|
* @param string[] $remove
|
|
* @param string[] $addIds
|
|
* @return string[]
|
|
*/
|
|
private function buildTableColumnsFromBase(array $baseOrder, array $remove, array $addIds): array
|
|
{
|
|
$cols = array_merge(array_values(array_diff($baseOrder, $remove)), $addIds);
|
|
|
|
// Keep order, drop duplicates.
|
|
$seen = [];
|
|
$out = [];
|
|
foreach ($cols as $id) {
|
|
$id = (string) $id;
|
|
if (isset($seen[$id])) {
|
|
continue;
|
|
}
|
|
$seen[$id] = true;
|
|
$out[] = $id;
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
/**
|
|
* @param string[] $removedColumns
|
|
* @param string[] $skipAdd
|
|
* @param string[] $skipRemove
|
|
* @return string[]
|
|
*/
|
|
private function buildTableColumnsToSkip(array $removedColumns, array $skipAdd, array $skipRemove): array
|
|
{
|
|
$allRemove = array_merge($removedColumns, $skipRemove);
|
|
|
|
$skip = array_diff($this->uoDefaultTableColumnsToSkip, $allRemove);
|
|
$skip = array_merge(array_values($skip), $skipAdd);
|
|
|
|
// Keep order, drop duplicates.
|
|
$seen = [];
|
|
$out = [];
|
|
foreach ($skip as $id) {
|
|
$id = (string) $id;
|
|
if (isset($seen[$id])) {
|
|
continue;
|
|
}
|
|
$seen[$id] = true;
|
|
$out[] = $id;
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
}
|