3 namespace Drupal\Tests\views\Kernel\Plugin;
5 use Drupal\simpletest\UserCreationTrait;
6 use Drupal\views\Views;
9 * Tests the base relationship handler.
12 * @see \Drupal\views\Plugin\views\relationship\RelationshipPluginBase
14 class RelationshipTest extends RelationshipJoinTestBase {
15 use UserCreationTrait;
18 * Views used by this test.
22 public static $testViews = ['test_view'];
25 * Maps between the key in the expected result and the query result.
29 protected $columnMap = [
30 'views_test_data_name' => 'name',
31 'users_field_data_views_test_data_uid' => 'uid',
35 * Tests the query result of a view with a relationship.
37 public function testRelationshipQuery() {
38 // Set the first entry to have the admin as author.
39 db_query("UPDATE {views_test_data} SET uid = 1 WHERE id = 1");
40 db_query("UPDATE {views_test_data} SET uid = 2 WHERE id <> 1");
42 $view = Views::getView('test_view');
45 $view->displayHandlers->get('default')->overrideOption('relationships', [
48 'table' => 'views_test_data',
53 $view->displayHandlers->get('default')->overrideOption('filters', [
56 'table' => 'users_field_data',
58 'relationship' => 'uid',
62 $fields = $view->displayHandlers->get('default')->getOption('fields');
63 $view->displayHandlers->get('default')->overrideOption('fields', $fields + [
66 'table' => 'users_field_data',
68 'relationship' => 'uid',
72 $view->initHandlers();
74 // Check for all beatles created by admin.
75 $view->filter['uid']->value = [1];
76 $this->executeView($view);
84 $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
87 // Check for all beatles created by another user, which so doesn't exist.
88 $view->initHandlers();
89 $view->filter['uid']->value = [3];
90 $this->executeView($view);
91 $expected_result = [];
92 $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
95 // Set the relationship to required, so only results authored by the admin
97 $view->initHandlers();
98 $view->relationship['uid']->options['required'] = TRUE;
99 $this->executeView($view);
107 $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
110 // Set the relationship to optional should cause to return all beatles.
111 $view->initHandlers();
112 $view->relationship['uid']->options['required'] = FALSE;
113 $this->executeView($view);
115 $expected_result = $this->dataSet();
116 // Alter the expected result to contain the right uids.
117 foreach ($expected_result as &$row) {
118 // Only John has an existing author.
119 if ($row['name'] == 'John') {
123 // The LEFT join should set an empty {users}.uid field.
128 $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
132 * Tests rendering of a view with a relationship.
134 public function testRelationshipRender() {
135 $author1 = $this->createUser();
136 db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 1", [':uid' => $author1->id()]);
137 $author2 = $this->createUser();
138 db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 2", [':uid' => $author2->id()]);
139 // Set uid to non-existing author uid for row 3.
140 db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 3", [':uid' => $author2->id() + 123]);
142 $view = Views::getView('test_view');
143 // Add a relationship for authors.
144 $view->getDisplay()->overrideOption('relationships', [
147 'table' => 'views_test_data',
151 // Add fields for {views_test_data}.id and author name.
152 $view->getDisplay()->overrideOption('fields', [
155 'table' => 'views_test_data',
160 'table' => 'users_field_data',
162 'relationship' => 'uid',
167 $output = $view->preview();
168 $html = $this->container->get('renderer')->renderRoot($output);
169 $this->setRawContent($html);
171 // Check that the output contains correct values.
172 $xpath = '//div[@class="views-row" and div[@class="views-field views-field-id"]=:id and div[@class="views-field views-field-author"]=:author]';
173 $this->assertEqual(1, count($this->xpath($xpath, [':id' => 1, ':author' => $author1->getUsername()])));
174 $this->assertEqual(1, count($this->xpath($xpath, [':id' => 2, ':author' => $author2->getUsername()])));
175 $this->assertEqual(1, count($this->xpath($xpath, [':id' => 3, ':author' => ''])));