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 // The comparison will be done on the string representation of the value.
92 // For entity fields we don't have the raw value. Let's try to fetch it
93 // using the entity itself.
94 elseif (empty($value->$view_column) && isset($view->field[$expected_column]) && ($field = $view->field[$expected_column]) && $field instanceof EntityField) {
96 if (count(explode(':', $view_column)) == 2) {
97 $column = explode(':', $view_column)[1];
99 $row[$expected_column] = $field->getValue($value, $column);
102 $result[$key] = $row;
105 // Remove the columns we don't need from the expected result.
106 foreach ($expected_result as $key => $value) {
108 foreach ($column_map as $expected_column) {
109 // The comparison will be done on the string representation of the value.
110 if (is_object($value)) {
111 $row[$expected_column] = (string) $value->$expected_column;
113 // This case is about fields with multiple values.
114 elseif (is_array($value[$expected_column])) {
115 foreach (array_keys($value[$expected_column]) as $delta) {
116 $row[$expected_column][$delta] = (string) $value[$expected_column][$delta];
120 $row[$expected_column] = (string) $value[$expected_column];
123 $expected_result[$key] = $row;
126 $this->verbose('<pre style="white-space: pre-wrap;">'
127 . "\n\nQuery:\n" . $view->build_info['query']
128 . "\n\nQuery arguments:\n" . var_export($view->build_info['query']->getArguments(), TRUE)
129 . "\n\nActual result:\n" . var_export($result, TRUE)
130 . "\n\nExpected result:\n" . var_export($expected_result, TRUE));
132 // Reset the numbering of the arrays.
133 $result = array_values($result);
134 $expected_result = array_values($expected_result);
136 // Do the actual comparison.
137 if (!isset($message)) {
138 $not = (strpos($assert_method, 'Not') ? 'not' : '');
139 $message = format_string("Actual result <pre>\n@actual\n</pre> is $not identical to expected <pre>\n@expected\n</pre>", [
140 '@actual' => var_export($result, TRUE),
141 '@expected' => var_export($expected_result, TRUE),
144 return $this->$assert_method($result, $expected_result, $message);