MDL-75081 behat: correct tests of table contents asserting one column.

Prior to this change, assertions of "should exist" and "should not
exist" in table content, where the step provided only a single column,
could give false positives and pass (when they shouldn't).
This commit is contained in:
Paul Holden 2023-11-22 14:28:37 +00:00 committed by Jun Pataleta
parent 560f4d895b
commit 3a8a8c8463
No known key found for this signature in database
GPG Key ID: F83510526D99E2C7

View File

@ -1466,12 +1466,19 @@ EOF;
$datahash = $data->getHash();
foreach ($datahash as $row) {
$firstcell = null;
foreach ($row as $column => $value) {
if ($firstcell === null) {
$firstcell = $value;
} else {
$this->row_column_of_table_should_contain($firstcell, $column, $table, $value);
// Row contains only a single column, just assert it's present in the table.
if (count($row) === 1) {
$this->execute('behat_general::assert_element_contains_text', [reset($row), $table, 'table']);
} else {
// Iterate over all columns.
$firstcell = null;
foreach ($row as $column => $value) {
if ($firstcell === null) {
$firstcell = $value;
} else {
$this->row_column_of_table_should_contain($firstcell, $column, $table, $value);
}
}
}
}
@ -1491,18 +1498,25 @@ EOF;
$datahash = $data->getHash();
foreach ($datahash as $value) {
$row = array_shift($value);
foreach ($value as $column => $value) {
try {
$this->row_column_of_table_should_contain($row, $column, $table, $value);
// Throw exception if found.
} catch (ElementNotFoundException $e) {
// Table row/column doesn't contain this value. Nothing to do.
continue;
// Row contains only a single column, just assert it's not present in the table.
if (count($value) === 1) {
$this->execute('behat_general::assert_element_not_contains_text', [reset($value), $table, 'table']);
} else {
// Iterate over all columns.
$row = array_shift($value);
foreach ($value as $column => $value) {
try {
$this->row_column_of_table_should_contain($row, $column, $table, $value);
// Throw exception if found.
} catch (ElementNotFoundException $e) {
// Table row/column doesn't contain this value. Nothing to do.
continue;
}
throw new ExpectationException('"' . $column . '" with value "' . $value . '" is present in "' .
$row . '" row for table "' . $table . '"', $this->getSession()
);
}
throw new ExpectationException('"' . $column . '" with value "' . $value . '" is present in "' .
$row . '" row for table "' . $table . '"', $this->getSession()
);
}
}
}