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 RelationshipTest 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 testRelationshipQuery() {
$connection = Database::getConnection();
$connection
->update('views_test_data')
->fields([
'uid' => 1,
])
->condition('id', 1)
->execute();
$connection
->update('views_test_data')
->fields([
'uid' => 2,
])
->condition('id', 1, '<>')
->execute();
$view = Views::getView('test_view');
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('relationships', [
'uid' => [
'id' => 'uid',
'table' => 'views_test_data',
'field' => 'uid',
],
]);
$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 = [
1,
];
$this
->executeView($view);
$expected_result = [
[
'name' => 'John',
'uid' => 1,
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->initHandlers();
$view->filter['uid']->value = [
3,
];
$this
->executeView($view);
$expected_result = [];
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->initHandlers();
$view->relationship['uid']->options['required'] = TRUE;
$this
->executeView($view);
$expected_result = [
[
'name' => 'John',
'uid' => 1,
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->initHandlers();
$view->relationship['uid']->options['required'] = FALSE;
$this
->executeView($view);
$expected_result = $this
->dataSet();
foreach ($expected_result as &$row) {
if ($row['name'] == 'John') {
$row['uid'] = 1;
}
else {
$row['uid'] = NULL;
}
}
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
}
public function testRelationshipRender() {
$connection = Database::getConnection();
$author1 = $this
->createUser();
$connection
->update('views_test_data')
->fields([
'uid' => $author1
->id(),
])
->condition('id', 1)
->execute();
$author2 = $this
->createUser();
$connection
->update('views_test_data')
->fields([
'uid' => $author2
->id(),
])
->condition('id', 2)
->execute();
$connection
->update('views_test_data')
->fields([
'uid' => $author2
->id() + 123,
])
->condition('id', 3)
->execute();
$view = Views::getView('test_view');
$view
->getDisplay()
->overrideOption('relationships', [
'uid' => [
'id' => 'uid',
'table' => 'views_test_data',
'field' => 'uid',
],
]);
$view
->getDisplay()
->overrideOption('fields', [
'id' => [
'id' => 'id',
'table' => 'views_test_data',
'field' => 'id',
],
'author' => [
'id' => 'author',
'table' => 'users_field_data',
'field' => 'name',
'relationship' => 'uid',
],
]);
$output = $view
->preview();
$html = $this->container
->get('renderer')
->renderRoot($output);
$this
->setRawContent($html);
$xpath = '//div[@class="views-row" and div[@class="views-field views-field-id"]=:id and div[@class="views-field views-field-author"]=:author]';
$this
->assertCount(1, $this
->xpath($xpath, [
':id' => 1,
':author' => $author1
->getAccountName(),
]));
$this
->assertCount(1, $this
->xpath($xpath, [
':id' => 2,
':author' => $author2
->getAccountName(),
]));
$this
->assertCount(1, $this
->xpath($xpath, [
':id' => 3,
':author' => '',
]));
}
}