3 namespace Drupal\views\Tests;
5 use Drupal\views\Plugin\views\field\EntityField;
8 * Provides a class for assertions to check for the expected result of a View.
10 trait ViewResultAssertionTrait {
13 * Verifies that a result set returned by a View matches expected values.
15 * The comparison is done on the string representation of the columns of the
16 * column map, taking the order of the rows into account, but not the order
19 * @param \Drupal\views\ViewExecutable $view
21 * @param array $expected_result
22 * An expected result set.
23 * @param array $column_map
24 * (optional) An associative array mapping the columns of the result set
25 * from the view (as keys) and the expected result set (as values).
26 * @param string $message
27 * (optional) A custom message to display with the assertion. Defaults to
28 * 'Identical result set.'
31 * TRUE if the assertion succeeded, or FALSE otherwise.
33 protected function assertIdenticalResultset($view, $expected_result, $column_map = [], $message = NULL) {
34 return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, 'assertIdentical', $message);
38 * Verifies that a result set returned by a View differs from certain values.
40 * Inverse of ViewsTestCase::assertIdenticalResultset().
42 * @param \Drupal\views\ViewExecutable $view
44 * @param array $expected_result
45 * An expected result set.
46 * @param array $column_map
47 * (optional) An associative array mapping the columns of the result set
48 * from the view (as keys) and the expected result set (as values).
49 * @param string $message
50 * (optional) A custom message to display with the assertion. Defaults to
51 * 'Non-identical result set.'
54 * TRUE if the assertion succeeded, or FALSE otherwise.
56 protected function assertNotIdenticalResultset($view, $expected_result, $column_map = [], $message = NULL) {
57 return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, 'assertNotIdentical', $message);
61 * Performs View result assertions.
63 * This is a helper method for ViewTestBase::assertIdenticalResultset() and
64 * ViewTestBase::assertNotIdenticalResultset().
66 * @param \Drupal\views\ViewExecutable $view
68 * @param array $expected_result
69 * An expected result set.
70 * @param array $column_map
71 * An associative array mapping the columns of the result set
72 * from the view (as keys) and the expected result set (as values).
73 * @param string $assert_method
74 * The TestBase assertion method to use (either 'assertIdentical' or
75 * 'assertNotIdentical').
76 * @param string $message
77 * (optional) The message to display with the assertion.
80 * TRUE if the assertion succeeded, or FALSE otherwise.
82 protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $assert_method, $message = NULL) {
83 // Convert $view->result to an array of arrays.
85 foreach ($view->result as $key => $value) {
87 foreach ($column_map as $view_column => $expected_column) {
88 if (property_exists($value, $view_column)) {
89 $row[$expected_column] = (string) $value->$view_column;
91 // For entity fields we don't have the raw value. Let's try to fetch it
92 // using the entity itself.
93 elseif (empty($value->$view_column) && isset($view->field[$expected_column]) && ($field = $view->field[$expected_column]) && $field instanceof EntityField) {
95 if (count(explode(':', $view_column)) == 2) {
96 $column = explode(':', $view_column)[1];
98 // The comparison will be done on the string representation of the
100 $field_value = $field->getValue($value, $column);
101 $row[$expected_column] = is_array($field_value) ? array_map('strval', $field_value) : (string) $field_value;
104 $result[$key] = $row;
107 // Remove the columns we don't need from the expected result.
108 foreach ($expected_result as $key => $value) {
110 foreach ($column_map as $expected_column) {
111 // The comparison will be done on the string representation of the value.
112 if (is_object($value)) {
113 $row[$expected_column] = (string) $value->$expected_column;
115 // This case is about fields with multiple values.
116 elseif (is_array($value[$expected_column])) {
117 foreach (array_keys($value[$expected_column]) as $delta) {
118 $row[$expected_column][$delta] = (string) $value[$expected_column][$delta];
122 $row[$expected_column] = (string) $value[$expected_column];
125 $expected_result[$key] = $row;
128 $this->verbose('<pre style="white-space: pre-wrap;">'
129 . "\n\nQuery:\n" . $view->build_info['query']
130 . "\n\nQuery arguments:\n" . var_export($view->build_info['query']->getArguments(), TRUE)
131 . "\n\nActual result:\n" . var_export($result, TRUE)
132 . "\n\nExpected result:\n" . var_export($expected_result, TRUE));
134 // Reset the numbering of the arrays.
135 $result = array_values($result);
136 $expected_result = array_values($expected_result);
138 // Do the actual comparison.
139 if (!isset($message)) {
140 $not = (strpos($assert_method, 'Not') ? 'not' : '');
141 $message = format_string("Actual result <pre>\n@actual\n</pre> is $not identical to expected <pre>\n@expected\n</pre>", [
142 '@actual' => var_export($result, TRUE),
143 '@expected' => var_export($expected_result, TRUE),
146 return $this->$assert_method($result, $expected_result, $message);