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 RelationshipJoinInTest extends RelationshipJoinTestBase {
16 use UserCreationTrait;
19 * Views used by this test.
23 public static $testViews = ['test_view'];
26 * Maps between the key in the expected result and the query result.
30 protected $columnMap = [
31 'views_test_data_name' => 'name',
32 'users_field_data_views_test_data_uid' => 'uid',
36 * Tests the query result of a view with a relationship with an IN condition.
38 public function testRelationshipInQuery() {
39 // Update the first two Beatles to be authored by Kristiaan.
40 $account_k = $this->createUser([], 'Kristiaan');
41 db_query("UPDATE {views_test_data} SET uid = :uid WHERE id IN (1,2)", [':uid' => $account_k->id()]);
43 // Update the other two Beatles to be authored by Django.
44 $account_d = $this->createUser([], 'Django');
45 db_query("UPDATE {views_test_data} SET uid = :uid WHERE id IN (3,4)", [':uid' => $account_d->id()]);
47 // Update Meredith to be authored by Silvie.
48 $account_s = $this->createUser([], 'Silvie');
49 db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 5", [':uid' => $account_s->id()]);
51 $view = Views::getView('test_view');
54 $view->displayHandlers->get('default')->overrideOption('relationships', [
57 'table' => 'views_test_data',
63 $view->displayHandlers->get('default')->overrideOption('filters', [
66 'table' => 'users_field_data',
68 'relationship' => 'uid',
72 $fields = $view->displayHandlers->get('default')->getOption('fields');
73 $view->displayHandlers->get('default')->overrideOption('fields', $fields + [
76 'table' => 'users_field_data',
78 'relationship' => 'uid',
82 // Check for all beatles created by Kristiaan.
83 $view->initHandlers();
84 $view->filter['uid']->value = [$account_k->id()];
85 $this->executeView($view);
88 ['name' => 'John', 'uid' => $account_k->id()],
89 ['name' => 'George', 'uid' => $account_k->id()],
91 $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
94 // Check for all beatles created by Django. This should not return anything
95 // as the 'extra' option on the join prohibits relating to any authors but
96 // Kristiaan or Silvie.
97 $view->initHandlers();
98 $view->filter['uid']->value = [$account_d->id()];
99 $this->executeView($view);
101 $expected_result = [];
102 $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
105 // Check for all people created by anyone.
106 $view->initHandlers();
107 $this->executeView($view);
109 ['name' => 'John', 'uid' => $account_k->id()],
110 ['name' => 'George', 'uid' => $account_k->id()],
111 ['name' => 'Meredith', 'uid' => $account_s->id()],
113 $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
118 * Adds an IN condition for the user name.
120 protected function viewsData() {
121 $data = parent::viewsData();
122 // Only relate if the author's name is Kristiaan or Silvie.
123 $data['views_test_data']['uid']['relationship']['extra'][] = [
125 'value' => ['Kristiaan', 'Silvie'],