View source
<?php
namespace Drupal\Tests\views\Kernel\Plugin;
use Drupal\views\Plugin\views\join\FieldOrLanguageJoin;
use Drupal\views\Views;
use Drupal\views\ViewExecutable;
class FieldOrLanguageJoinTest extends RelationshipJoinTestBase {
public static $testViews = [
'test_view',
];
protected $pluginId = 'field_or_language_join';
protected $manager;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp();
$this->manager = $this->container
->get('plugin.manager.views.join');
}
public function testBase() {
$view = Views::getView('test_view');
$view
->initDisplay();
$view
->initQuery();
$configuration = [
'left_table' => 'views_test_data',
'left_field' => 'uid',
'table' => 'users_field_data',
'field' => 'uid',
'adjusted' => TRUE,
];
$join = $this->manager
->createInstance($this->pluginId, $configuration);
$this
->assertInstanceOf(FieldOrLanguageJoin::class, $join);
$this
->assertNull($join->extra);
$this
->assertTrue($join->adjusted);
$join_info = $this
->buildJoin($view, $configuration, 'users_field_data');
$this
->assertSame($join_info['join type'], 'LEFT');
$this
->assertSame($join_info['table'], $configuration['table']);
$this
->assertSame($join_info['alias'], 'users_field_data');
$this
->assertSame($join_info['condition'], 'views_test_data.uid = users_field_data.uid');
$join_info = $this
->buildJoin($view, $configuration, 'users1');
$this
->assertSame($join_info['alias'], 'users1');
$configuration['type'] = 'INNER';
$join_info = $this
->buildJoin($view, $configuration, 'users2');
$this
->assertSame($join_info['join type'], 'INNER');
$random_name_1 = $this
->randomMachineName();
$random_name_2 = $this
->randomMachineName();
$configuration['extra'] = [
[
'field' => 'name',
'value' => $random_name_1,
],
[
'field' => 'name',
'value' => $random_name_2,
'operator' => '<>',
],
];
$join_info = $this
->buildJoin($view, $configuration, 'users3');
$this
->assertStringContainsString('views_test_data.uid = users3.uid', $join_info['condition']);
$this
->assertStringContainsString('users3.name = :views_join_condition_0', $join_info['condition']);
$this
->assertStringContainsString('users3.name <> :views_join_condition_1', $join_info['condition']);
$this
->assertSame(array_values($join_info['arguments']), [
$random_name_1,
$random_name_2,
]);
$random_name_1 = $this
->randomMachineName();
$random_name_2 = $this
->randomMachineName();
$random_name_3 = $this
->randomMachineName();
$random_name_4 = $this
->randomMachineName();
$configuration['extra'] = [
[
'field' => 'name',
'value' => $random_name_1,
],
[
'field' => 'name',
'value' => [
$random_name_2,
$random_name_3,
$random_name_4,
],
],
];
$join_info = $this
->buildJoin($view, $configuration, 'users4');
$this
->assertStringContainsString('views_test_data.uid = users4.uid', $join_info['condition']);
$this
->assertStringContainsString('users4.name = :views_join_condition_0', $join_info['condition']);
$this
->assertStringContainsString('users4.name IN ( :views_join_condition_1[] )', $join_info['condition']);
$this
->assertSame($join_info['arguments'][':views_join_condition_1[]'], [
$random_name_2,
$random_name_3,
$random_name_4,
]);
}
public function testLanguageBundleConditions() {
$view = Views::getView('test_view');
$view
->initDisplay();
$view
->initQuery();
$configuration = [
'table' => 'node__field_tags',
'left_table' => 'views_test_data',
'left_field' => 'nid',
'field' => 'entity_id',
'extra' => [
[
'left_field' => 'langcode',
'field' => 'langcode',
],
],
];
$join_info = $this
->buildJoin($view, $configuration, 'node__field_tags');
$this
->assertStringContainsString('AND (node__field_tags.langcode = views_test_data.langcode)', $join_info['condition']);
array_unshift($configuration['extra'], [
'field' => 'deleted',
'value' => 0,
'numeric' => TRUE,
]);
$join_info = $this
->buildJoin($view, $configuration, 'node__field_tags');
$this
->assertStringContainsString('AND (node__field_tags.langcode = views_test_data.langcode)', $join_info['condition']);
$configuration['extra'][1] = [
'field' => 'bundle',
'value' => [
'page',
],
];
$join_info = $this
->buildJoin($view, $configuration, 'node__field_tags');
$this
->assertStringContainsString('AND (node__field_tags.bundle = :views_join_condition_1)', $join_info['condition']);
$configuration['extra'][] = [
'left_field' => 'langcode',
'field' => 'langcode',
];
$join_info = $this
->buildJoin($view, $configuration, 'node__field_tags');
$this
->assertStringContainsString('AND (node__field_tags.bundle = :views_join_condition_1 OR node__field_tags.langcode = views_test_data.langcode)', $join_info['condition']);
}
protected function buildJoin(ViewExecutable $view, $configuration, $table_alias) {
$query = \Drupal::database()
->select('node');
$join = $this->manager
->createInstance('field_or_language_join', $configuration);
$this
->assertInstanceOf(FieldOrLanguageJoin::class, $join);
$table = [
'alias' => $table_alias,
];
$join
->buildJoin($query, $table, $view->query);
$tables = $query
->getTables();
$join_info = $tables[$table['alias']];
return $join_info;
}
}