34e4f93000dda44e33d0747b91f1127ad01dcc3d
[yaffs-website] / FieldOrLanguageJoinTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\views\Plugin\views\join\FieldOrLanguageJoin;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the "field OR language" join plugin.
10  *
11  * @group views
12  * @see \Drupal\views\Plugin\views\join\FieldOrLanguageJoin
13  */
14 class FieldOrLanguageJoinTest extends RelationshipJoinTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $testViews = ['test_view'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected $pluginId = 'field_or_language_join';
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   /**
34    * {@inheritdoc}
35    */
36   protected function setUp($import_test_views = TRUE) {
37     parent::setUp();
38
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');
41   }
42
43   /**
44    * Tests base join functionality.
45    *
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.
49    */
50   public function testBase() {
51     // Setup a simple join and test the result sql.
52     $view = Views::getView('test_view');
53     $view->initDisplay();
54     $view->initQuery();
55
56     // First define a simple join without an extra condition.
57     // Set the various options on the join object.
58     $configuration = [
59       'left_table' => 'views_test_data',
60       'left_field' => 'uid',
61       'table' => 'users_field_data',
62       'field' => 'uid',
63       'adjusted' => TRUE,
64     ];
65     $join = $this->manager->createInstance($this->pluginId, $configuration);
66     $this->assertTrue($join instanceof FieldOrLanguageJoin);
67     $this->assertNull($join->extra);
68     $this->assertTrue($join->adjusted);
69
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');
75
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');
79
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');
84
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'] = [
89       [
90         'field' => 'name',
91         'value' => $random_name_1,
92       ],
93       [
94         'field' => 'name',
95         'value' => $random_name_2,
96         'operator' => '<>',
97       ],
98     ];
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]);
104
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'] = [
111       [
112         'field' => 'name',
113         'value' => $random_name_1,
114       ],
115       [
116         'field' => 'name',
117         'value' => [$random_name_2, $random_name_3, $random_name_4],
118       ],
119     ];
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]);
125   }
126
127   /**
128    * Tests the adding of conditions by the join plugin.
129    */
130   public function testLanguageBundleConditions() {
131     // Setup a simple join and test the result sql.
132     $view = Views::getView('test_view');
133     $view->initDisplay();
134     $view->initQuery();
135
136     // Set the various options on the join object with only a langcode
137     // condition.
138     $configuration = [
139       'table' => 'node__field_tags',
140       'left_table' => 'node',
141       'left_field' => 'nid',
142       'field' => 'entity_id',
143       'extra' => [
144         [
145           'left_field' => 'langcode',
146           'field' => 'langcode',
147         ],
148       ],
149     ];
150     $join_info = $this->buildJoin($view, $configuration, 'node__field_tags');
151     $this->assertContains('AND (node__field_tags.langcode = .langcode)', $join_info['condition']);
152
153     array_unshift($configuration['extra'], [
154       'field' => 'deleted',
155       'value' => 0,
156       'numeric' => TRUE,
157     ]);
158     $join_info = $this->buildJoin($view, $configuration, 'node__field_tags');
159     $this->assertContains('AND (node__field_tags.langcode = .langcode)', $join_info['condition']);
160
161     // Replace the language condition with a bundle condition.
162     $configuration['extra'][1] = [
163       'field' => 'bundle',
164       'value' => ['page'],
165     ];
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']);
168
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',
174     ];
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']);
177   }
178
179   /**
180    * Builds a join using the given configuration.
181    *
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.
188    *
189    * @return array
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.
193    */
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');
197
198     $join = $this->manager->createInstance('field_or_language_join', $configuration);
199     $this->assertInstanceOf(FieldOrLanguageJoin::class, $join, 'The correct join class got loaded.');
200
201     $table = ['alias' => $table_alias];
202     $join->buildJoin($query, $table, $view->query);
203
204     $tables = $query->getTables();
205     $join_info = $tables[$table['alias']];
206     return $join_info;
207   }
208
209 }