Commit 892c0f3c authored by catch's avatar catch
Browse files

Issue #2922343 by amateescu, dawehner:...

Issue #2922343 by amateescu, dawehner: \Drupal\views\Tests\ViewResultAssertionTrait::assertIdenticalResultsetHelper() does not cast entity field values to strings
parent 5bba892a
......@@ -88,7 +88,6 @@ protected function assertIdenticalResultsetHelper($view, $expected_result, $colu
if (property_exists($value, $view_column)) {
$row[$expected_column] = (string) $value->$view_column;
}
// The comparison will be done on the string representation of the value.
// For entity fields we don't have the raw value. Let's try to fetch it
// using the entity itself.
elseif (empty($value->$view_column) && isset($view->field[$expected_column]) && ($field = $view->field[$expected_column]) && $field instanceof EntityField) {
......@@ -96,7 +95,10 @@ protected function assertIdenticalResultsetHelper($view, $expected_result, $colu
if (count(explode(':', $view_column)) == 2) {
$column = explode(':', $view_column)[1];
}
$row[$expected_column] = $field->getValue($value, $column);
// The comparison will be done on the string representation of the
// value.
$field_value = $field->getValue($value, $column);
$row[$expected_column] = is_array($field_value) ? array_map('strval', $field_value) : (string) $field_value;
}
}
$result[$key] = $row;
......
......@@ -45,3 +45,20 @@ function views_entity_test_entity_field_access($operation, FieldDefinitionInterf
// No opinion.
return AccessResult::neutral();
}
/**
* Implements hook_entity_load().
*
* @see \Drupal\Tests\views\Kernel\Handler\FieldFieldTest::testSimpleExecute()
*/
function views_entity_test_entity_load(array $entities, $entity_type_id) {
if ($entity_type_id === 'entity_test') {
// Cast the value of an entity field to be something else than a string so
// we can check that
// \Drupal\views\Tests\ViewResultAssertionTrait::assertIdenticalResultsetHelper()
// takes care of converting all field values to strings.
foreach ($entities as $entity) {
$entity->user_id->target_id = (int) $entity->user_id->target_id;
}
}
}
......@@ -24,7 +24,7 @@ class FieldFieldTest extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['field', 'entity_test', 'user', 'views_test_formatter'];
public static $modules = ['field', 'entity_test', 'user', 'views_test_formatter', 'views_entity_test'];
/**
* {@inheritdoc}
......@@ -234,13 +234,13 @@ public function testSimpleExecute() {
$this->assertIdenticalResultset($executable,
[
['id' => 1, 'field_test' => 3],
['id' => 2, 'field_test' => 0],
['id' => 3, 'field_test' => 8],
['id' => 4, 'field_test' => 5],
['id' => 5, 'field_test' => 6],
['id' => 1, 'field_test' => 3, 'user_id' => 2],
['id' => 2, 'field_test' => 0, 'user_id' => 3],
['id' => 3, 'field_test' => 8, 'user_id' => 4],
['id' => 4, 'field_test' => 5, 'user_id' => 5],
['id' => 5, 'field_test' => 6, 'user_id' => 6],
],
['id' => 'id', 'field_test' => 'field_test']
['id' => 'id', 'field_test' => 'field_test', 'user_id' => 'user_id']
);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment