3 namespace Drupal\Tests\views\Kernel\Plugin;
5 use Drupal\views\Plugin\views\join\FieldOrLanguageJoin;
6 use Drupal\views\Views;
9 * Tests the "field OR language" join plugin.
12 * @see \Drupal\views\Plugin\views\join\FieldOrLanguageJoin
14 class FieldOrLanguageJoinTest extends RelationshipJoinTestBase {
19 public static $testViews = ['test_view'];
24 protected $pluginId = 'field_or_language_join';
27 * A plugin manager which handlers the instances of joins.
29 * @var \Drupal\views\Plugin\ViewsPluginManager
36 protected function setUp($import_test_views = TRUE) {
39 // Add a join plugin manager which can be used in all of the tests.
40 $this->manager = $this->container->get('plugin.manager.views.join');
44 * Tests base join functionality.
46 * This duplicates parts of
47 * \Drupal\Tests\views\Kernel\Plugin\JoinTest::testBasePlugin() to ensure that
48 * no functionality provided by the base join plugin is broken.
50 public function testBase() {
51 // Setup a simple join and test the result sql.
52 $view = Views::getView('test_view');
56 // First define a simple join without an extra condition.
57 // Set the various options on the join object.
59 'left_table' => 'views_test_data',
60 'left_field' => 'uid',
61 'table' => 'users_field_data',
65 $join = $this->manager->createInstance($this->pluginId, $configuration);
66 $this->assertTrue($join instanceof FieldOrLanguageJoin);
67 $this->assertNull($join->extra);
68 $this->assertTrue($join->adjusted);
70 $join_info = $this->buildJoin($view, $configuration, 'users_field_data');
71 $this->assertSame($join_info['join type'], 'LEFT');
72 $this->assertSame($join_info['table'], $configuration['table']);
73 $this->assertSame($join_info['alias'], 'users_field_data');
74 $this->assertSame($join_info['condition'], 'views_test_data.uid = users_field_data.uid');
76 // Set a different alias and make sure table info is as expected.
77 $join_info = $this->buildJoin($view, $configuration, 'users1');
78 $this->assertSame($join_info['alias'], 'users1');
80 // Set a different join type (INNER) and make sure it is used.
81 $configuration['type'] = 'INNER';
82 $join_info = $this->buildJoin($view, $configuration, 'users2');
83 $this->assertSame($join_info['join type'], 'INNER');
85 // Setup addition conditions and make sure it is used.
86 $random_name_1 = $this->randomMachineName();
87 $random_name_2 = $this->randomMachineName();
88 $configuration['extra'] = [
91 'value' => $random_name_1,
95 'value' => $random_name_2,
99 $join_info = $this->buildJoin($view, $configuration, 'users3');
100 $this->assertContains('views_test_data.uid = users3.uid', $join_info['condition']);
101 $this->assertContains('users3.name = :views_join_condition_0', $join_info['condition']);
102 $this->assertContains('users3.name <> :views_join_condition_1', $join_info['condition']);
103 $this->assertSame(array_values($join_info['arguments']), [$random_name_1, $random_name_2]);
105 // Test that 'IN' conditions are properly built.
106 $random_name_1 = $this->randomMachineName();
107 $random_name_2 = $this->randomMachineName();
108 $random_name_3 = $this->randomMachineName();
109 $random_name_4 = $this->randomMachineName();
110 $configuration['extra'] = [
113 'value' => $random_name_1,
117 'value' => [$random_name_2, $random_name_3, $random_name_4],
120 $join_info = $this->buildJoin($view, $configuration, 'users4');
121 $this->assertContains('views_test_data.uid = users4.uid', $join_info['condition']);
122 $this->assertContains('users4.name = :views_join_condition_0', $join_info['condition']);
123 $this->assertContains('users4.name IN ( :views_join_condition_1[] )', $join_info['condition']);
124 $this->assertSame($join_info['arguments'][':views_join_condition_1[]'], [$random_name_2, $random_name_3, $random_name_4]);
128 * Tests the adding of conditions by the join plugin.
130 public function testLanguageBundleConditions() {
131 // Setup a simple join and test the result sql.
132 $view = Views::getView('test_view');
133 $view->initDisplay();
136 // Set the various options on the join object with only a langcode
139 'table' => 'node__field_tags',
140 'left_table' => 'node',
141 'left_field' => 'nid',
142 'field' => 'entity_id',
145 'left_field' => 'langcode',
146 'field' => 'langcode',
150 $join_info = $this->buildJoin($view, $configuration, 'node__field_tags');
151 $this->assertContains('AND (node__field_tags.langcode = .langcode)', $join_info['condition']);
153 array_unshift($configuration['extra'], [
154 'field' => 'deleted',
158 $join_info = $this->buildJoin($view, $configuration, 'node__field_tags');
159 $this->assertContains('AND (node__field_tags.langcode = .langcode)', $join_info['condition']);
161 // Replace the language condition with a bundle condition.
162 $configuration['extra'][1] = [
166 $join_info = $this->buildJoin($view, $configuration, 'node__field_tags');
167 $this->assertContains('AND (node__field_tags.bundle = :views_join_condition_1)', $join_info['condition']);
169 // Now re-add a language condition to make sure the bundle and language
170 // conditions are combined with an OR.
171 $configuration['extra'][] = [
172 'left_field' => 'langcode',
173 'field' => 'langcode',
175 $join_info = $this->buildJoin($view, $configuration, 'node__field_tags');
176 $this->assertContains('AND (node__field_tags.bundle = :views_join_condition_1 OR node__field_tags.langcode = .langcode)', $join_info['condition']);
180 * Builds a join using the given configuration.
182 * @param \Drupal\views\ViewExecutable $view
183 * The view used in this test.
184 * @param $configuration
185 * The join plugin configuration.
186 * @param $table_alias
187 * The table alias to use for the join.
190 * The join information for the joined table. See
191 * \Drupal\Core\Database\Query\Select::$tables for more information on the
192 * structure of the array.
194 protected function buildJoin($view, $configuration, $table_alias) {
195 // Build the actual join values and read them back from the query object.
196 $query = \Drupal::database()->select('node');
198 $join = $this->manager->createInstance('field_or_language_join', $configuration);
199 $this->assertInstanceOf(FieldOrLanguageJoin::class, $join, 'The correct join class got loaded.');
201 $table = ['alias' => $table_alias];
202 $join->buildJoin($query, $table, $view->query);
204 $tables = $query->getTables();
205 $join_info = $tables[$table['alias']];