Version 1
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / RelationshipTest.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 RelationshipTest extends RelationshipJoinTestBase {
15   use UserCreationTrait;
16
17   /**
18    * Views used by this test.
19    *
20    * @var array
21    */
22   public static $testViews = ['test_view'];
23
24   /**
25    * Maps between the key in the expected result and the query result.
26    *
27    * @var array
28    */
29   protected $columnMap = [
30     'views_test_data_name' => 'name',
31     'users_field_data_views_test_data_uid' => 'uid',
32   ];
33
34   /**
35    * Tests the query result of a view with a relationship.
36    */
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");
41
42     $view = Views::getView('test_view');
43     $view->setDisplay();
44
45     $view->displayHandlers->get('default')->overrideOption('relationships', [
46       'uid' => [
47         'id' => 'uid',
48         'table' => 'views_test_data',
49         'field' => 'uid',
50       ],
51     ]);
52
53     $view->displayHandlers->get('default')->overrideOption('filters', [
54       'uid' => [
55         'id' => 'uid',
56         'table' => 'users_field_data',
57         'field' => 'uid',
58         'relationship' => 'uid',
59       ],
60     ]);
61
62     $fields = $view->displayHandlers->get('default')->getOption('fields');
63     $view->displayHandlers->get('default')->overrideOption('fields', $fields + [
64       'uid' => [
65         'id' => 'uid',
66         'table' => 'users_field_data',
67         'field' => 'uid',
68         'relationship' => 'uid',
69       ],
70     ]);
71
72     $view->initHandlers();
73
74     // Check for all beatles created by admin.
75     $view->filter['uid']->value = [1];
76     $this->executeView($view);
77
78     $expected_result = [
79       [
80         'name' => 'John',
81         'uid' => 1
82       ]
83     ];
84     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
85     $view->destroy();
86
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);
93     $view->destroy();
94
95     // Set the relationship to required, so only results authored by the admin
96     // should return.
97     $view->initHandlers();
98     $view->relationship['uid']->options['required'] = TRUE;
99     $this->executeView($view);
100
101     $expected_result = [
102       [
103         'name' => 'John',
104         'uid' => 1
105       ]
106     ];
107     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
108     $view->destroy();
109
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);
114
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') {
120         $row['uid'] = 1;
121       }
122       else {
123         // The LEFT join should set an empty {users}.uid field.
124         $row['uid'] = NULL;
125       }
126     }
127
128     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
129   }
130
131   /**
132    * Tests rendering of a view with a relationship.
133    */
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]);
141
142     $view = Views::getView('test_view');
143     // Add a relationship for authors.
144     $view->getDisplay()->overrideOption('relationships', [
145       'uid' => [
146         'id' => 'uid',
147         'table' => 'views_test_data',
148         'field' => 'uid',
149       ],
150     ]);
151     // Add fields for {views_test_data}.id and author name.
152     $view->getDisplay()->overrideOption('fields', [
153       'id' => [
154         'id' => 'id',
155         'table' => 'views_test_data',
156         'field' => 'id',
157       ],
158       'author' => [
159         'id' => 'author',
160         'table' => 'users_field_data',
161         'field' => 'name',
162         'relationship' => 'uid',
163       ],
164     ]);
165
166     // Render the view.
167     $output = $view->preview();
168     $html = $this->container->get('renderer')->renderRoot($output);
169     $this->setRawContent($html);
170
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' => ''])));
176   }
177
178 }