3 namespace Drupal\Tests\views\Kernel\Plugin;
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;
10 * Tests the join plugin.
13 * @see \Drupal\views_test_data\Plugin\views\join\JoinTest
14 * @see \Drupal\views\Plugin\views\join\JoinPluginBase
16 class JoinTest extends RelationshipJoinTestBase {
19 * Views used by this test.
23 public static $testViews = ['test_view'];
26 * A plugin manager which handlers the instances of joins.
28 * @var \Drupal\views\Plugin\ViewsPluginManager
32 protected function setUp($import_test_views = TRUE) {
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');
40 * Tests an example join plugin.
42 public function testExamplePlugin() {
44 // Setup a simple join and test the result sql.
45 $view = Views::getView('test_view');
50 'left_table' => 'views_test_data',
51 'left_field' => 'uid',
52 'table' => 'users_field_data',
55 $join = $this->manager->createInstance('join_test', $configuration);
56 $this->assertTrue($join instanceof JoinTestPlugin, 'The correct join class got loaded.');
58 $rand_int = rand(0, 1000);
59 $join->setJoinValue($rand_int);
61 $query = db_select('views_test_data');
62 $table = ['alias' => 'users_field_data'];
63 $join->buildJoin($query, $table, $view->query);
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.');
71 * Tests the join plugin base.
73 public function testBasePlugin() {
75 // Setup a simple join and test the result sql.
76 $view = Views::getView('test_view');
80 // First define a simple join without an extra condition.
81 // Set the various options on the join object.
83 'left_table' => 'views_test_data',
84 'left_field' => 'uid',
85 'table' => 'users_field_data',
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.');
94 // Build the actual join values and read them back from the dbtng query
96 $query = db_select('views_test_data');
97 $table = ['alias' => 'users_field_data'];
98 $join->buildJoin($query, $table, $view->query);
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');
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);
112 $tables = $query->getTables();
113 $join_info = $tables['users1'];
114 $this->assertEqual($join_info['alias'], 'users1');
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);
122 $tables = $query->getTables();
123 $join_info = $tables['users2'];
124 $this->assertEqual($join_info['join type'], 'INNER');
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'] = [
132 'value' => $random_name_1,
136 'value' => $random_name_2,
140 $join = $this->manager->createInstance('standard', $configuration);
141 $table = ['alias' => 'users3'];
142 $join->buildJoin($query, $table, $view->query);
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');
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'] = [
159 'value' => $random_name_1,
163 'value' => [$random_name_2, $random_name_3, $random_name_4],
166 $join = $this->manager->createInstance('standard', $configuration);
167 $table = ['alias' => 'users4'];
168 $join->buildJoin($query, $table, $view->query);
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.');
177 // Test that all the conditions are properly built.
178 $configuration['extra'] = [
180 'field' => 'langcode',
184 'left_field' => 'status',
190 'left_field' => 'name',
193 $join = $this->manager->createInstance('standard', $configuration);
194 $table = ['alias' => 'users5'];
195 $join->buildJoin($query, $table, $view->query);
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');