View source
<?php
namespace Drupal\views\Tests\Handler;
use Drupal\simpletest\UserCreationTrait;
use Drupal\views\Views;
use Drupal\views\Tests\Plugin\RelationshipJoinTestBase;
class RelationshipTest extends RelationshipJoinTestBase {
use UserCreationTrait;
public static $testViews = array(
'test_view',
);
protected $columnMap = array(
'views_test_data_name' => 'name',
'users_field_data_views_test_data_uid' => 'uid',
);
public function testRelationshipQuery() {
db_query("UPDATE {views_test_data} SET uid = 1 WHERE id = 1");
db_query("UPDATE {views_test_data} SET uid = 2 WHERE id <> 1");
$view = Views::getView('test_view');
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('relationships', array(
'uid' => array(
'id' => 'uid',
'table' => 'views_test_data',
'field' => 'uid',
),
));
$view->displayHandlers
->get('default')
->overrideOption('filters', array(
'uid' => array(
'id' => 'uid',
'table' => 'users_field_data',
'field' => 'uid',
'relationship' => 'uid',
),
));
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + array(
'uid' => array(
'id' => 'uid',
'table' => 'users_field_data',
'field' => 'uid',
'relationship' => 'uid',
),
));
$view
->initHandlers();
$view->filter['uid']->value = array(
1,
);
$this
->executeView($view);
$expected_result = array(
array(
'name' => 'John',
'uid' => 1,
),
);
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->initHandlers();
$view->filter['uid']->value = array(
3,
);
$this
->executeView($view);
$expected_result = array();
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->initHandlers();
$view->relationship['uid']->options['required'] = TRUE;
$this
->executeView($view);
$expected_result = array(
array(
'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() {
$author1 = $this
->createUser();
db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 1", [
':uid' => $author1
->id(),
]);
$author2 = $this
->createUser();
db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 2", [
':uid' => $author2
->id(),
]);
db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 3", [
':uid' => $author2
->id() + 123,
]);
$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
->assertEqual(1, count($this
->xpath($xpath, [
':id' => 1,
':author' => $author1
->getUsername(),
])));
$this
->assertEqual(1, count($this
->xpath($xpath, [
':id' => 2,
':author' => $author2
->getUsername(),
])));
$this
->assertEqual(1, count($this
->xpath($xpath, [
':id' => 3,
':author' => '',
])));
}
}