Skip to content

Commit

Permalink
Merge pull request #8054 from ping-yee/231018_generate_table
Browse files Browse the repository at this point in the history
fix: [Table] field named `data` will produce bugged output
  • Loading branch information
kenjis authored Oct 26, 2023
2 parents 6c788de + 4c0eab5 commit 407c108
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/View/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ protected function _prepArgs(array $args)
// If there is no $args[0], skip this and treat as an associative array
// This can happen if there is only a single key, for example this is passed to table->generate
// array(array('foo'=>'bar'))
if (isset($args[0]) && count($args) === 1 && is_array($args[0]) && ! isset($args[0]['data'])) {
if (isset($args[0]) && count($args) === 1 && is_array($args[0])) {
$args = $args[0];
}

Expand Down
58 changes: 58 additions & 0 deletions tests/system/View/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,64 @@ public static function orderedColumnUsecases(): iterable
],
];
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8051
*/
public function testGenerateTableWithHeadingContainFieldNamedData(): void
{
$table = new Table();
$table->setHeading([
'codigo' => 'Codigo Orçamento',
'data' => 'Data do Orçamento',
'tipo_desconto' => 'Tipo de Desconto',
'valor_desconto' => 'Valor do Desconto',
])->setSyncRowsWithHeading(true);

$sampleData = [
[
'id' => 1,
'id_cliente' => 1,
'codigo' => 'codigo1',
'data' => '2023-10-16 21:53:25',
'tipo_desconto' => 'NENHUM',
'valor_desconto' => '',
'created_at' => '2023-10-16 21:53:25',
'updated_at' => '2023-10-16 21:53:25',
'deleted_at' => '',
],
[
'id' => 2,
'id_cliente' => 2,
'codigo' => 'codigo2',
'data' => '2023-10-16 21:53:25',
'tipo_desconto' => 'REAL',
'valor_desconto' => 10.00,
'created_at' => '2023-10-16 21:53:25',
'updated_at' => '2023-10-16 21:53:25',
'deleted_at' => '',
],
[
'id' => 3,
'id_cliente' => 3,
'codigo' => 'codigo3',
'data' => '2023-10-16 21:53:25',
'tipo_desconto' => 'PERCENTUAL',
'valor_desconto' => 10.00,
'created_at' => '2023-10-16 21:53:25',
'updated_at' => '2023-10-16 21:53:25',
'deleted_at' => '',
],
];

$generated = $table->generate($sampleData);

$this->assertStringContainsString('<th>Codigo Orçamento</th><th>Data do Orçamento</th><th>Tipo de Desconto</th><th>Valor do Desconto</th>', $generated);

$this->assertStringContainsString('<td>codigo1</td><td>2023-10-16 21:53:25</td><td>NENHUM</td><td></td>', $generated);
$this->assertStringContainsString('<td>codigo2</td><td>2023-10-16 21:53:25</td><td>REAL</td><td>10</td>', $generated);
$this->assertStringContainsString('<td>codigo3</td><td>2023-10-16 21:53:25</td><td>PERCENTUAL</td><td>10</td>', $generated);
}
}

// We need this for the _set_from_db_result() test
Expand Down

0 comments on commit 407c108

Please sign in to comment.