annotate core/modules/views/src/Tests/ViewResultAssertionTrait.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views\Tests;
Chris@0 4
Chris@0 5 use Drupal\views\Plugin\views\field\EntityField;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Provides a class for assertions to check for the expected result of a View.
Chris@0 9 */
Chris@0 10 trait ViewResultAssertionTrait {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Verifies that a result set returned by a View matches expected values.
Chris@0 14 *
Chris@0 15 * The comparison is done on the string representation of the columns of the
Chris@0 16 * column map, taking the order of the rows into account, but not the order
Chris@0 17 * of the columns.
Chris@0 18 *
Chris@0 19 * @param \Drupal\views\ViewExecutable $view
Chris@0 20 * An executed View.
Chris@0 21 * @param array $expected_result
Chris@0 22 * An expected result set.
Chris@0 23 * @param array $column_map
Chris@0 24 * (optional) An associative array mapping the columns of the result set
Chris@0 25 * from the view (as keys) and the expected result set (as values).
Chris@0 26 * @param string $message
Chris@0 27 * (optional) A custom message to display with the assertion. Defaults to
Chris@0 28 * 'Identical result set.'
Chris@0 29 *
Chris@0 30 * @return bool
Chris@0 31 * TRUE if the assertion succeeded, or FALSE otherwise.
Chris@0 32 */
Chris@0 33 protected function assertIdenticalResultset($view, $expected_result, $column_map = [], $message = NULL) {
Chris@0 34 return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, 'assertIdentical', $message);
Chris@0 35 }
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Verifies that a result set returned by a View differs from certain values.
Chris@0 39 *
Chris@0 40 * Inverse of ViewsTestCase::assertIdenticalResultset().
Chris@0 41 *
Chris@0 42 * @param \Drupal\views\ViewExecutable $view
Chris@0 43 * An executed View.
Chris@0 44 * @param array $expected_result
Chris@0 45 * An expected result set.
Chris@0 46 * @param array $column_map
Chris@0 47 * (optional) An associative array mapping the columns of the result set
Chris@0 48 * from the view (as keys) and the expected result set (as values).
Chris@0 49 * @param string $message
Chris@0 50 * (optional) A custom message to display with the assertion. Defaults to
Chris@0 51 * 'Non-identical result set.'
Chris@0 52 *
Chris@0 53 * @return bool
Chris@0 54 * TRUE if the assertion succeeded, or FALSE otherwise.
Chris@0 55 */
Chris@0 56 protected function assertNotIdenticalResultset($view, $expected_result, $column_map = [], $message = NULL) {
Chris@0 57 return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, 'assertNotIdentical', $message);
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Performs View result assertions.
Chris@0 62 *
Chris@0 63 * This is a helper method for ViewTestBase::assertIdenticalResultset() and
Chris@0 64 * ViewTestBase::assertNotIdenticalResultset().
Chris@0 65 *
Chris@0 66 * @param \Drupal\views\ViewExecutable $view
Chris@0 67 * An executed View.
Chris@0 68 * @param array $expected_result
Chris@0 69 * An expected result set.
Chris@0 70 * @param array $column_map
Chris@0 71 * An associative array mapping the columns of the result set
Chris@0 72 * from the view (as keys) and the expected result set (as values).
Chris@0 73 * @param string $assert_method
Chris@0 74 * The TestBase assertion method to use (either 'assertIdentical' or
Chris@0 75 * 'assertNotIdentical').
Chris@0 76 * @param string $message
Chris@0 77 * (optional) The message to display with the assertion.
Chris@0 78 *
Chris@0 79 * @return bool
Chris@0 80 * TRUE if the assertion succeeded, or FALSE otherwise.
Chris@0 81 */
Chris@0 82 protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $assert_method, $message = NULL) {
Chris@0 83 // Convert $view->result to an array of arrays.
Chris@0 84 $result = [];
Chris@0 85 foreach ($view->result as $key => $value) {
Chris@0 86 $row = [];
Chris@0 87 foreach ($column_map as $view_column => $expected_column) {
Chris@0 88 if (property_exists($value, $view_column)) {
Chris@0 89 $row[$expected_column] = (string) $value->$view_column;
Chris@0 90 }
Chris@0 91 // The comparison will be done on the string representation of the value.
Chris@0 92 // For entity fields we don't have the raw value. Let's try to fetch it
Chris@0 93 // using the entity itself.
Chris@0 94 elseif (empty($value->$view_column) && isset($view->field[$expected_column]) && ($field = $view->field[$expected_column]) && $field instanceof EntityField) {
Chris@0 95 $column = NULL;
Chris@0 96 if (count(explode(':', $view_column)) == 2) {
Chris@0 97 $column = explode(':', $view_column)[1];
Chris@0 98 }
Chris@0 99 $row[$expected_column] = $field->getValue($value, $column);
Chris@0 100 }
Chris@0 101 }
Chris@0 102 $result[$key] = $row;
Chris@0 103 }
Chris@0 104
Chris@0 105 // Remove the columns we don't need from the expected result.
Chris@0 106 foreach ($expected_result as $key => $value) {
Chris@0 107 $row = [];
Chris@0 108 foreach ($column_map as $expected_column) {
Chris@0 109 // The comparison will be done on the string representation of the value.
Chris@0 110 if (is_object($value)) {
Chris@0 111 $row[$expected_column] = (string) $value->$expected_column;
Chris@0 112 }
Chris@0 113 // This case is about fields with multiple values.
Chris@0 114 elseif (is_array($value[$expected_column])) {
Chris@0 115 foreach (array_keys($value[$expected_column]) as $delta) {
Chris@0 116 $row[$expected_column][$delta] = (string) $value[$expected_column][$delta];
Chris@0 117 }
Chris@0 118 }
Chris@0 119 else {
Chris@0 120 $row[$expected_column] = (string) $value[$expected_column];
Chris@0 121 }
Chris@0 122 }
Chris@0 123 $expected_result[$key] = $row;
Chris@0 124 }
Chris@0 125
Chris@0 126 $this->verbose('<pre style="white-space: pre-wrap;">'
Chris@0 127 . "\n\nQuery:\n" . $view->build_info['query']
Chris@0 128 . "\n\nQuery arguments:\n" . var_export($view->build_info['query']->getArguments(), TRUE)
Chris@0 129 . "\n\nActual result:\n" . var_export($result, TRUE)
Chris@0 130 . "\n\nExpected result:\n" . var_export($expected_result, TRUE));
Chris@0 131
Chris@0 132 // Reset the numbering of the arrays.
Chris@0 133 $result = array_values($result);
Chris@0 134 $expected_result = array_values($expected_result);
Chris@0 135
Chris@0 136 // Do the actual comparison.
Chris@0 137 if (!isset($message)) {
Chris@0 138 $not = (strpos($assert_method, 'Not') ? 'not' : '');
Chris@0 139 $message = format_string("Actual result <pre>\n@actual\n</pre> is $not identical to expected <pre>\n@expected\n</pre>", [
Chris@0 140 '@actual' => var_export($result, TRUE),
Chris@0 141 '@expected' => var_export($expected_result, TRUE),
Chris@0 142 ]);
Chris@0 143 }
Chris@0 144 return $this->$assert_method($result, $expected_result, $message);
Chris@0 145 }
Chris@0 146
Chris@0 147 }