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;
11 * Tests the join plugin.
14 * @see \Drupal\views_test_data\Plugin\views\join\JoinTest
15 * @see \Drupal\views\Plugin\views\join\JoinPluginBase
17 class JoinTest extends RelationshipJoinTestBase {
20 * Views used by this test.
24 public static $testViews = ['test_view'];
27 * A plugin manager which handlers the instances of joins.
29 * @var \Drupal\views\Plugin\ViewsPluginManager
33 protected function setUp($import_test_views = TRUE) {
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');
41 * Tests an example join plugin.
43 public function testExamplePlugin() {
45 // Setup a simple join and test the result sql.
46 $view = Views::getView('test_view');
51 'left_table' => 'views_test_data',
52 'left_field' => 'uid',
53 'table' => 'users_field_data',
56 $join = $this->manager->createInstance('join_test', $configuration);
57 $this->assertTrue($join instanceof JoinTestPlugin, 'The correct join class got loaded.');
59 $rand_int = rand(0, 1000);
60 $join->setJoinValue($rand_int);
62 $query = db_select('views_test_data');
63 $table = ['alias' => 'users_field_data'];
64 $join->buildJoin($query, $table, $view->query);
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.');
72 * Tests the join plugin base.
74 public function testBasePlugin() {
76 // Setup a simple join and test the result sql.
77 $view = Views::getView('test_view');
81 // First define a simple join without an extra condition.
82 // Set the various options on the join object.
84 'left_table' => 'views_test_data',
85 'left_field' => 'uid',
86 'table' => 'users_field_data',
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.');
95 // Build the actual join values and read them back from the dbtng query
97 $query = db_select('views_test_data');
98 $table = ['alias' => 'users_field_data'];
99 $join->buildJoin($query, $table, $view->query);
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');
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);
113 $tables = $query->getTables();
114 $join_info = $tables['users1'];
115 $this->assertEqual($join_info['alias'], 'users1');
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);
123 $tables = $query->getTables();
124 $join_info = $tables['users2'];
125 $this->assertEqual($join_info['join type'], 'INNER');
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'] = [
133 'value' => $random_name_1
137 'value' => $random_name_2,
141 $join = $this->manager->createInstance('standard', $configuration);
142 $table = ['alias' => 'users3'];
143 $join->buildJoin($query, $table, $view->query);
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');
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'] = [
160 'value' => $random_name_1
164 'value' => [$random_name_2, $random_name_3, $random_name_4],
167 $join = $this->manager->createInstance('standard', $configuration);
168 $table = ['alias' => 'users4'];
169 $join->buildJoin($query, $table, $view->query);
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.');
178 // Test that all the conditions are properly built.
179 $configuration['extra'] = [
181 'field' => 'langcode',
185 'left_field' => 'status',
191 'left_field' => 'name'
194 $join = $this->manager->createInstance('standard', $configuration);
195 $table = ['alias' => 'users5'];
196 $join->buildJoin($query, $table, $view->query);
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');