Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [Table] field named data will produce bugged output #8054

Merged
merged 4 commits into from
Oct 26, 2023
Merged
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
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'])) {
Copy link
Member

Choose a reason for hiding this comment

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

Why was the data excluded?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will cause the setHeading function to be executed incorrectly when the heading field contain named data.

Copy link
Member

Choose a reason for hiding this comment

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

The && ! isset($args[0]['data']) came from this commit: bcit-ci/CodeIgniter@f0b69a85
Are you absolutely sure that just removing it does not cause another issue?
Frankly, I don't get the code yet.

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 am not sure but the all of the test cases get passed.
And I also don't get where will use this condition and the data processing of the array field contains data so far.

Or we can add one more process to check that whether the heading array has been handled correctly when the table is being generated.

Copy link
Member

Choose a reason for hiding this comment

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

the all of the test cases get passed.

Yes. But we cannot see the coverage in Coveralls.

coverage/coveralls — Target branch is ahead of PR base. Rebase the PR to fix.

Can you rebase the PR branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you rebase the PR branch?

Done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Based on my usage of the table class (quite extensive) I have not noticed any issues thus far with the patch applied.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you all!

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
Loading