View source
<?php
namespace Drupal\Tests\views\Kernel\Plugin;
use Drupal\Core\Database\Database;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\views\Views;
class RelationshipJoinInTest extends RelationshipJoinTestBase {
use UserCreationTrait;
public static $testViews = [
'test_view',
];
protected $columnMap = [
'views_test_data_name' => 'name',
'users_field_data_views_test_data_uid' => 'uid',
];
public function testRelationshipInQuery() {
$account_k = $this
->createUser([], 'Kristiaan');
$connection = Database::getConnection();
$connection
->update('views_test_data')
->fields([
'uid' => $account_k
->id(),
])
->condition('id', [
1,
2,
], 'IN')
->execute();
$account_d = $this
->createUser([], 'Django');
$connection
->update('views_test_data')
->fields([
'uid' => $account_d
->id(),
])
->condition('id', [
3,
4,
], 'IN')
->execute();
$account_s = $this
->createUser([], 'Silvie');
$connection
->update('views_test_data')
->fields([
'uid' => $account_s
->id(),
])
->condition('id', 5)
->execute();
$view = Views::getView('test_view');
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('relationships', [
'uid' => [
'id' => 'uid',
'table' => 'views_test_data',
'field' => 'uid',
'required' => TRUE,
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'uid' => [
'id' => 'uid',
'table' => 'users_field_data',
'field' => 'uid',
'relationship' => 'uid',
],
]);
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'uid' => [
'id' => 'uid',
'table' => 'users_field_data',
'field' => 'uid',
'relationship' => 'uid',
],
]);
$view
->initHandlers();
$view->filter['uid']->value = [
$account_k
->id(),
];
$this
->executeView($view);
$expected_result = [
[
'name' => 'John',
'uid' => $account_k
->id(),
],
[
'name' => 'George',
'uid' => $account_k
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->initHandlers();
$view->filter['uid']->value = [
$account_d
->id(),
];
$this
->executeView($view);
$expected_result = [];
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->initHandlers();
$this
->executeView($view);
$expected_result = [
[
'name' => 'John',
'uid' => $account_k
->id(),
],
[
'name' => 'George',
'uid' => $account_k
->id(),
],
[
'name' => 'Meredith',
'uid' => $account_s
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
}
protected function viewsData() {
$data = parent::viewsData();
$data['views_test_data']['uid']['relationship']['extra'][] = [
'field' => 'name',
'value' => [
'Kristiaan',
'Silvie',
],
];
return $data;
}
}