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