Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / RelationshipJoinInTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\simpletest\UserCreationTrait;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the base relationship handler.
10  *
11  * @group views
12  * @see \Drupal\views\Plugin\views\relationship\RelationshipPluginBase
13  */
14 class RelationshipJoinInTest extends RelationshipJoinTestBase {
15
16   use UserCreationTrait;
17
18   /**
19    * Views used by this test.
20    *
21    * @var array
22    */
23   public static $testViews = ['test_view'];
24
25   /**
26    * Maps between the key in the expected result and the query result.
27    *
28    * @var array
29    */
30   protected $columnMap = [
31     'views_test_data_name' => 'name',
32     'users_field_data_views_test_data_uid' => 'uid',
33   ];
34
35   /**
36    * Tests the query result of a view with a relationship with an IN condition.
37    */
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()]);
42
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()]);
46
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()]);
50
51     $view = Views::getView('test_view');
52     $view->setDisplay();
53
54     $view->displayHandlers->get('default')->overrideOption('relationships', [
55       'uid' => [
56         'id' => 'uid',
57         'table' => 'views_test_data',
58         'field' => 'uid',
59         'required' => TRUE,
60       ],
61     ]);
62
63     $view->displayHandlers->get('default')->overrideOption('filters', [
64       'uid' => [
65         'id' => 'uid',
66         'table' => 'users_field_data',
67         'field' => 'uid',
68         'relationship' => 'uid',
69       ],
70     ]);
71
72     $fields = $view->displayHandlers->get('default')->getOption('fields');
73     $view->displayHandlers->get('default')->overrideOption('fields', $fields + [
74       'uid' => [
75         'id' => 'uid',
76         'table' => 'users_field_data',
77         'field' => 'uid',
78         'relationship' => 'uid',
79       ],
80     ]);
81
82     // Check for all beatles created by Kristiaan.
83     $view->initHandlers();
84     $view->filter['uid']->value = [$account_k->id()];
85     $this->executeView($view);
86
87     $expected_result = [
88       ['name' => 'John', 'uid' => $account_k->id()],
89       ['name' => 'George', 'uid' => $account_k->id()],
90     ];
91     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
92     $view->destroy();
93
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);
100
101     $expected_result = [];
102     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
103     $view->destroy();
104
105     // Check for all people created by anyone.
106     $view->initHandlers();
107     $this->executeView($view);
108     $expected_result = [
109       ['name' => 'John', 'uid' => $account_k->id()],
110       ['name' => 'George', 'uid' => $account_k->id()],
111       ['name' => 'Meredith', 'uid' => $account_s->id()],
112     ];
113     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
114     $view->destroy();
115   }
116
117   /**
118    * Adds an IN condition for the user name.
119    */
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'][] = [
124       'field' => 'name',
125       'value' => ['Kristiaan', 'Silvie'],
126     ];
127     return $data;
128   }
129
130 }