1c196a1d98fd01cdd6944829724af6dd05d38c8c
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / JoinTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\views_test_data\Plugin\views\join\JoinTest as JoinTestPlugin;
6 use Drupal\views\Plugin\views\join\JoinPluginBase;
7 use Drupal\views\Views;
8
9 /**
10  * Tests the join plugin.
11  *
12  * @group views
13  * @see \Drupal\views_test_data\Plugin\views\join\JoinTest
14  * @see \Drupal\views\Plugin\views\join\JoinPluginBase
15  */
16 class JoinTest extends RelationshipJoinTestBase {
17
18   /**
19    * Views used by this test.
20    *
21    * @var array
22    */
23   public static $testViews = ['test_view'];
24
25   /**
26    * A plugin manager which handlers the instances of joins.
27    *
28    * @var \Drupal\views\Plugin\ViewsPluginManager
29    */
30   protected $manager;
31
32   protected function setUp($import_test_views = TRUE) {
33     parent::setUp();
34
35     // Add a join plugin manager which can be used in all of the tests.
36     $this->manager = $this->container->get('plugin.manager.views.join');
37   }
38
39   /**
40    * Tests an example join plugin.
41    */
42   public function testExamplePlugin() {
43
44     // Setup a simple join and test the result sql.
45     $view = Views::getView('test_view');
46     $view->initDisplay();
47     $view->initQuery();
48
49     $configuration = [
50       'left_table' => 'views_test_data',
51       'left_field' => 'uid',
52       'table' => 'users_field_data',
53       'field' => 'uid',
54     ];
55     $join = $this->manager->createInstance('join_test', $configuration);
56     $this->assertTrue($join instanceof JoinTestPlugin, 'The correct join class got loaded.');
57
58     $rand_int = rand(0, 1000);
59     $join->setJoinValue($rand_int);
60
61     $query = db_select('views_test_data');
62     $table = ['alias' => 'users_field_data'];
63     $join->buildJoin($query, $table, $view->query);
64
65     $tables = $query->getTables();
66     $join_info = $tables['users_field_data'];
67     $this->assertTrue(strpos($join_info['condition'], "views_test_data.uid = $rand_int") !== FALSE, 'Make sure that the custom join plugin can extend the join base and alter the result.');
68   }
69
70   /**
71    * Tests the join plugin base.
72    */
73   public function testBasePlugin() {
74
75     // Setup a simple join and test the result sql.
76     $view = Views::getView('test_view');
77     $view->initDisplay();
78     $view->initQuery();
79
80     // First define a simple join without an extra condition.
81     // Set the various options on the join object.
82     $configuration = [
83       'left_table' => 'views_test_data',
84       'left_field' => 'uid',
85       'table' => 'users_field_data',
86       'field' => 'uid',
87       'adjusted' => TRUE,
88     ];
89     $join = $this->manager->createInstance('standard', $configuration);
90     $this->assertTrue($join instanceof JoinPluginBase, 'The correct join class got loaded.');
91     $this->assertNull($join->extra, 'The field extra was not overridden.');
92     $this->assertTrue($join->adjusted, 'The field adjusted was set correctly.');
93
94     // Build the actual join values and read them back from the dbtng query
95     // object.
96     $query = db_select('views_test_data');
97     $table = ['alias' => 'users_field_data'];
98     $join->buildJoin($query, $table, $view->query);
99
100     $tables = $query->getTables();
101     $join_info = $tables['users_field_data'];
102     $this->assertEqual($join_info['join type'], 'LEFT', 'Make sure the default join type is LEFT');
103     $this->assertEqual($join_info['table'], $configuration['table']);
104     $this->assertEqual($join_info['alias'], 'users_field_data');
105     $this->assertEqual($join_info['condition'], 'views_test_data.uid = users_field_data.uid');
106
107     // Set a different alias and make sure table info is as expected.
108     $join = $this->manager->createInstance('standard', $configuration);
109     $table = ['alias' => 'users1'];
110     $join->buildJoin($query, $table, $view->query);
111
112     $tables = $query->getTables();
113     $join_info = $tables['users1'];
114     $this->assertEqual($join_info['alias'], 'users1');
115
116     // Set a different join type (INNER) and make sure it is used.
117     $configuration['type'] = 'INNER';
118     $join = $this->manager->createInstance('standard', $configuration);
119     $table = ['alias' => 'users2'];
120     $join->buildJoin($query, $table, $view->query);
121
122     $tables = $query->getTables();
123     $join_info = $tables['users2'];
124     $this->assertEqual($join_info['join type'], 'INNER');
125
126     // Setup addition conditions and make sure it is used.
127     $random_name_1 = $this->randomMachineName();
128     $random_name_2 = $this->randomMachineName();
129     $configuration['extra'] = [
130       [
131         'field' => 'name',
132         'value' => $random_name_1
133       ],
134       [
135         'field' => 'name',
136         'value' => $random_name_2,
137         'operator' => '<>'
138       ],
139     ];
140     $join = $this->manager->createInstance('standard', $configuration);
141     $table = ['alias' => 'users3'];
142     $join->buildJoin($query, $table, $view->query);
143
144     $tables = $query->getTables();
145     $join_info = $tables['users3'];
146     $this->assertTrue(strpos($join_info['condition'], "views_test_data.uid = users3.uid") !== FALSE, 'Make sure the join condition appears in the query.');
147     $this->assertTrue(strpos($join_info['condition'], "users3.name = :views_join_condition_0") !== FALSE, 'Make sure the first extra join condition appears in the query and uses the first placeholder.');
148     $this->assertTrue(strpos($join_info['condition'], "users3.name <> :views_join_condition_1") !== FALSE, 'Make sure the second extra join condition appears in the query and uses the second placeholder.');
149     $this->assertEqual(array_values($join_info['arguments']), [$random_name_1, $random_name_2], 'Make sure the arguments are in the right order');
150
151     // Test that 'IN' conditions are properly built.
152     $random_name_1 = $this->randomMachineName();
153     $random_name_2 = $this->randomMachineName();
154     $random_name_3 = $this->randomMachineName();
155     $random_name_4 = $this->randomMachineName();
156     $configuration['extra'] = [
157       [
158         'field' => 'name',
159         'value' => $random_name_1
160       ],
161       [
162         'field' => 'name',
163         'value' => [$random_name_2, $random_name_3, $random_name_4],
164       ],
165     ];
166     $join = $this->manager->createInstance('standard', $configuration);
167     $table = ['alias' => 'users4'];
168     $join->buildJoin($query, $table, $view->query);
169
170     $tables = $query->getTables();
171     $join_info = $tables['users4'];
172     $this->assertTrue(strpos($join_info['condition'], "views_test_data.uid = users4.uid") !== FALSE, 'Make sure the join condition appears in the query.');
173     $this->assertTrue(strpos($join_info['condition'], "users4.name = :views_join_condition_2") !== FALSE, 'Make sure the first extra join condition appears in the query.');
174     $this->assertTrue(strpos($join_info['condition'], "users4.name IN ( :views_join_condition_3[] )") !== FALSE, 'The IN condition for the join is properly formed.');
175     $this->assertEqual($join_info['arguments'][':views_join_condition_3[]'], [$random_name_2, $random_name_3, $random_name_4], 'Make sure the IN arguments are still part of an array.');
176
177     // Test that all the conditions are properly built.
178     $configuration['extra'] = [
179       [
180         'field' => 'langcode',
181         'value' => 'en'
182       ],
183       [
184         'left_field' => 'status',
185         'value' => 0,
186         'numeric' => TRUE,
187       ],
188       [
189         'field' => 'name',
190         'left_field' => 'name'
191       ],
192     ];
193     $join = $this->manager->createInstance('standard', $configuration);
194     $table = ['alias' => 'users5'];
195     $join->buildJoin($query, $table, $view->query);
196
197     $tables = $query->getTables();
198     $join_info = $tables['users5'];
199     $this->assertTrue(strpos($join_info['condition'], "views_test_data.uid = users5.uid") !== FALSE, 'Make sure the join condition appears in the query.');
200     $this->assertTrue(strpos($join_info['condition'], "users5.langcode = :views_join_condition_4") !== FALSE, 'Make sure the first extra join condition appears in the query.');
201     $this->assertTrue(strpos($join_info['condition'], "views_test_data.status = :views_join_condition_5") !== FALSE, 'Make sure the second extra join condition appears in the query.');
202     $this->assertTrue(strpos($join_info['condition'], "users5.name = views_test_data.name") !== FALSE, 'Make sure the third extra join condition appears in the query.');
203     $this->assertEqual(array_values($join_info['arguments']), ['en', 0], 'Make sure the arguments are in the right order');
204   }
205
206 }