Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 63 additions & 6 deletions tests/unit/Database/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ protected function createTestColumn(int $tableId, array $data = []) {
'mandatory' => false,
'order_weight' => 0,
'number_prefix' => '',
'number_suffix' => ''
'number_suffix' => '',
'text_default' => '',
'number_default' => null,
'datetime_default' => '',
'selection_default' => '',
'usergroup_default' => '',
];

$testIdent = $data['test_ident'] ?? null;
Expand Down Expand Up @@ -318,27 +323,32 @@ protected function addCellsToRow(int $rowId, array $cellsData, array $columnMapp
*/
protected function insertCellData(int $rowId, int $columnId, $value): void {
$qb = $this->connection->getQueryBuilder();
$result = $qb->select('type')
$result = $qb->select('type', 'subtype')
->from('tables_columns')
->where($qb->expr()->eq('id', $qb->createNamedParameter($columnId)))
->executeQuery();

$columnType = $result->fetchOne();
$column = $result->fetch();
$result->closeCursor();

if (!$columnType) {
if (!$column) {
throw new \InvalidArgumentException("Column with ID $columnId not found");
}

$this->insertCellIntoTypeTable($rowId, $columnId, $value, $columnType);
$this->insertCellIntoTypeTable($rowId, $columnId, $value, $column['type'], $column['subtype']);
}

/**
* Inserts cell data into the appropriate type-specific table
*/
protected function insertCellIntoTypeTable(int $rowId, int $columnId, $value, string $columnType): void {
protected function insertCellIntoTypeTable(int $rowId, int $columnId, $value, string $columnType, string $columnSubtype): void {
$tableName = 'tables_row_cells_' . $columnType;

// Handle selection type - convert values to IDs based on selection_options
if ($columnType === 'selection' && $columnSubtype !== 'check') {
$value = $this->convertSelectionValuesToIds($columnId, $value);
}

$qb = $this->connection->getQueryBuilder();
$qb->insert($tableName)
->setValue('row_id', $qb->createNamedParameter($rowId))
Expand Down Expand Up @@ -403,6 +413,53 @@ protected function extractTestIdentMapping(array $results): array {
return $mapping;
}

/**
* Converts selection values to IDs based on selection_options
*/
protected function convertSelectionValuesToIds(int $columnId, $value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this still works with #1887. Gonna merge that shortly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a rebase, it works

// Get column configuration to find selection_options
$qb = $this->connection->getQueryBuilder();
$result = $qb->select('selection_options')
->from('tables_columns')
->where($qb->expr()->eq('id', $qb->createNamedParameter($columnId)))
->executeQuery();

$selectionOptions = $result->fetchOne();
$result->closeCursor();

if (!$selectionOptions) {
throw new \InvalidArgumentException("Column with ID $columnId not found");
}

$selectionOptions = json_decode($selectionOptions, true);

// Create mapping from label to id
$optionMapping = [];
foreach ($selectionOptions as $option) {
if (isset($option['label']) && isset($option['id'])) {
$optionMapping[$option['label']] = $option['id'];
}
}

// Convert single value or array of values
if (is_array($value)) {
// Multiple selection - convert each value to ID and return as JSON
$convertedValues = [];
foreach ($value as $optionText) {
if (isset($optionMapping[$optionText])) {
$convertedValues[] = $optionMapping[$optionText];
}
}
return json_encode($convertedValues);
} else {
// Single selection - convert to ID
if (isset($optionMapping[$value])) {
return $optionMapping[$value];
}
return null;
}
}

/**
* Gets ID by test_ident from creation results
* @param array $results Array of creation results
Expand Down
Loading
Loading