View source
<?php
namespace Drupal\Tests\views\Functional\Handler;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Entity\View;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\HandlerBase;
use Drupal\views\Views;
class HandlerTest extends ViewTestBase {
use CommentTestTrait;
public static $testViews = [
'test_view',
'test_view_handler_weight',
'test_handler_relationships',
'test_handler_test_access',
'test_filter_in_operator_ui',
];
public static $modules = [
'views_ui',
'comment',
'node',
];
protected $defaultTheme = 'stark';
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this
->addDefaultCommentField('node', 'page');
$this
->enableViewsTestModule();
}
protected function viewsData() {
$data = parent::viewsData();
$data['views_test_data']['name']['field']['id'] = 'test_field';
$data['views_test_data']['access_callback'] = $data['views_test_data']['id'];
$data['views_test_data']['access_callback_arguments'] = $data['views_test_data']['id'];
foreach (ViewExecutable::getHandlerTypes() as $type => $info) {
if (isset($data['views_test_data']['access_callback'][$type]['id'])) {
$data['views_test_data']['access_callback'][$type]['access callback'] = 'views_test_data_handler_test_access_callback';
$data['views_test_data']['access_callback_arguments'][$type]['access callback'] = 'views_test_data_handler_test_access_callback_argument';
$data['views_test_data']['access_callback_arguments'][$type]['access arguments'] = [
TRUE,
];
}
}
return $data;
}
public function testBreakString() {
$this
->assertEqual((object) [
'value' => [],
'operator' => NULL,
], HandlerBase::breakString(''));
$handler = HandlerBase::breakString('word1 word2+word');
$this
->assertEqualValue([
'word1',
'word2',
'word',
], $handler);
$this
->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakString('word1+word2+word');
$this
->assertEqualValue([
'word1',
'word2',
'word',
], $handler);
$this
->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakString('word1 word2 word');
$this
->assertEqualValue([
'word1',
'word2',
'word',
], $handler);
$this
->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakString('word-1+word-2+word');
$this
->assertEqualValue([
'word-1',
'word-2',
'word',
], $handler);
$this
->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakString('wõrd1+wõrd2+wõrd');
$this
->assertEqualValue([
'wõrd1',
'wõrd2',
'wõrd',
], $handler);
$this
->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakString('word1,word2,word');
$this
->assertEqualValue([
'word1',
'word2',
'word',
], $handler);
$this
->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakString('word1 word2,word');
$this
->assertEqualValue([
'word1 word2',
'word',
], $handler);
$this
->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakString('word1,word2 word');
$this
->assertEqualValue([
'word1',
'word2 word',
], $handler);
$this
->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakString('word-1,word-2,word');
$this
->assertEqualValue([
'word-1',
'word-2',
'word',
], $handler);
$this
->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakString('wõrd1,wõrd2,wõrd');
$this
->assertEqualValue([
'wõrd1',
'wõrd2',
'wõrd',
], $handler);
$this
->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakString('word');
$this
->assertEqualValue([
'word',
], $handler);
$this
->assertEqual('and', $handler->operator);
$s1 = $this
->randomMachineName();
$n1 = rand(0, 100);
$n2 = rand(0, 100);
$n3 = rand(0, 100);
$handlerBase = HandlerBase::breakString("{$s1} {$n2}+{$n3}");
$this
->assertEqualValue([
$s1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1}+{$n2}+{$n3}");
$this
->assertEqualValue([
$s1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1} {$n2} {$n3}");
$this
->assertEqualValue([
$s1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1} {$n2}++{$n3}");
$this
->assertEqualValue([
$s1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1},{$n2},{$n3}");
$this
->assertEqualValue([
$s1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('and', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1},,{$n2},{$n3}");
$this
->assertEqualValue([
$s1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('and', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$n1},{$n2},{$n3}", TRUE);
$this
->assertEqualValue([
$n1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('and', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$n1}+{$n2}+{$n3}", TRUE);
$this
->assertEqualValue([
$n1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1},{$n2},{$n3}", TRUE);
$this
->assertEqualValue([
(int) $s1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('and', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1}+{$n2}+{$n3}", TRUE);
$this
->assertEqualValue([
(int) $s1,
$n2,
$n3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$d1 = rand(0, 10) / 10;
$d2 = rand(0, 10) / 10;
$d3 = rand(0, 10) / 10;
$handlerBase = HandlerBase::breakString("{$s1} {$d1}+{$d2}");
$this
->assertEqualValue([
$s1,
$d1,
$d2,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1}+{$d1}+{$d3}");
$this
->assertEqualValue([
$s1,
$d1,
$d3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1} {$d2} {$d3}");
$this
->assertEqualValue([
$s1,
$d2,
$d3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1} {$d2}++{$d3}");
$this
->assertEqualValue([
$s1,
$d2,
$d3,
], $handlerBase);
$this
->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1},{$d2},{$d3}");
$this
->assertEqualValue([
$s1,
$d2,
$d3,
], $handlerBase);
$this
->assertEqual('and', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("{$s1},,{$d2},{$d3}");
$this
->assertEqualValue([
$s1,
$d2,
$d3,
], $handlerBase);
$this
->assertEqual('and', $handlerBase->operator);
}
public function testHandlerWeights() {
$handler_types = [
'fields',
'filters',
'sorts',
];
$view = Views::getView('test_view_handler_weight');
$view
->initDisplay();
$original_order = [];
foreach ($handler_types as $type) {
$original_order[$type] = array_keys($view->display_handler
->getOption($type));
}
$view
->save();
$view = views::getView('test_view_handler_weight');
$view
->initDisplay();
foreach ($handler_types as $type) {
$loaded_order = array_keys($view->display_handler
->getOption($type));
$this
->assertIdentical($original_order[$type], $loaded_order);
}
}
protected function assertEqualValue($expected, $handler, $message = '', $group = 'Other') {
if (empty($message)) {
$message = t('Comparing @first and @second', [
'@first' => implode(',', $expected),
'@second' => implode(',', $handler->value),
]);
}
return $this
->assert($expected == $handler->value, $message, $group);
}
public function testRelationshipUI() {
$views_admin = $this
->drupalCreateUser([
'administer views',
]);
$this
->drupalLogin($views_admin);
$handler_options_path = 'admin/structure/views/nojs/handler/test_handler_relationships/default/field/title';
$view_edit_path = 'admin/structure/views/view/test_handler_relationships/edit';
$this
->drupalGet($view_edit_path);
$this
->assertLinkByHref($handler_options_path);
$this
->drupalGet($handler_options_path);
$relationship_name = 'options[relationship]';
$this
->assertFieldByName($relationship_name);
$fields = $this
->getSession()
->getPage()
->findAll('named_exact', [
'field',
$relationship_name,
]);
$options = [];
foreach ($fields as $field) {
$items = $field
->findAll('css', 'option');
foreach ($items as $item) {
$options[] = $item
->getAttribute('value');
}
}
$expected_options = [
'none',
'nid',
];
$this
->assertEqual($options, $expected_options);
$this
->drupalPostForm('admin/structure/views/nojs/handler/test_handler_relationships/default/relationship/nid', [], t('Remove'));
$this
->drupalGet($handler_options_path);
$this
->assertNoFieldByName($relationship_name, NULL, 'Make sure that no relationship option is available');
View::create([
'base_table' => 'comment_field_data',
'id' => 'test_get_entity_type',
])
->save();
$this
->drupalPostForm('admin/structure/views/nojs/add-handler/test_get_entity_type/default/relationship', [
'name[comment_field_data.node]' => 'comment_field_data.node',
], t('Add and configure relationships'));
$this
->drupalPostForm(NULL, [], t('Apply'));
$this
->drupalPostForm('admin/structure/views/nojs/add-handler/test_get_entity_type/default/filter', [
'name[node_field_data.type]' => 'node_field_data.type',
], t('Add and configure filter criteria'));
$this
->assertOptionSelected('edit-options-relationship', 'node');
$this
->drupalPostForm(NULL, [
'options[value][page]' => 'page',
], t('Apply'));
$this
->drupalGet('admin/structure/views/nojs/handler/test_get_entity_type/default/filter/type');
$this
->assertOptionSelected('edit-options-relationship', 'node');
$this
->assertFieldChecked('edit-options-value-page');
}
public function testSetRelationship() {
$view = Views::getView('test_handler_relationships');
$view
->setDisplay();
$view
->addHandler('default', 'relationship', $this
->randomMachineName(), $this
->randomMachineName(), [], 'broken_relationship');
$view
->addHandler('default', 'relationship', 'comment_field_data', 'node', [
'relationship' => 'cid',
], 'valid_relationship');
$view
->initHandlers();
$field = $view->field['title'];
$field->options['relationship'] = NULL;
$field
->setRelationship();
$this
->assertNull($field->relationship, 'Make sure that an empty relationship does not create a relationship on the field.');
$field->options['relationship'] = $this
->randomMachineName();
$field
->setRelationship();
$this
->assertNull($field->relationship, 'Make sure that a random relationship does not create a relationship on the field.');
$field->options['relationship'] = 'broken_relationship';
$field
->setRelationship();
$this
->assertNull($field->relationship, 'Make sure that a broken relationship does not create a relationship on the field.');
$field->options['relationship'] = 'valid_relationship';
$field
->setRelationship();
$this
->assertFalse(!empty($field->relationship), 'Make sure that the relationship alias was not set without building a views query before.');
unset($view->relationship['broken_relationship']);
$view
->build();
$field
->setRelationship();
$this
->assertEqual($field->relationship, $view->relationship['valid_relationship']->alias, 'Make sure that a valid relationship does create the right relationship query alias.');
}
public function testPlaceholder() {
$view = Views::getView('test_view');
$view
->initHandlers();
$view
->initQuery();
$handler = $view->field['name'];
$table = $handler->table;
$field = $handler->field;
$string = ':' . $table . '_' . $field;
$this
->assertEqual($handler
->getPlaceholder(), $string);
$this
->assertEqual($handler
->getPlaceholder(), $string . 1);
$this
->assertEqual($handler
->getPlaceholder(), $string . 2);
$table = $handler->table = $this
->randomMachineName();
$field = $handler->field = $this
->randomMachineName();
$string = ':' . $table . '_' . $field;
$this
->assertEqual($handler
->getPlaceholder(), $string);
$this
->assertEqual($handler
->getPlaceholder(), $string . 1);
$this
->assertEqual($handler
->getPlaceholder(), $string . 2);
}
public function testAccess() {
$view = Views::getView('test_handler_test_access');
$views_data = $this
->viewsData();
$views_data = $views_data['views_test_data'];
$this
->config('views_test_data.tests')
->set('handler_access_callback', TRUE)
->save();
$this
->config('views_test_data.tests')
->set('handler_access_callback_argument', FALSE)
->save();
$view
->initDisplay();
$view
->initHandlers();
foreach ($views_data['access_callback'] as $type => $info) {
if (!in_array($type, [
'title',
'help',
])) {
$this
->assertInstanceOf(HandlerBase::class, $view->field['access_callback']);
$this
->assertFalse(isset($view->field['access_callback_arguments']), 'Make sure the user got no access to the access_callback_arguments field ');
}
}
$this
->config('views_test_data.tests')
->set('handler_access_callback', FALSE)
->save();
$this
->config('views_test_data.tests')
->set('handler_access_callback_argument', TRUE)
->save();
$view
->destroy();
$view
->initDisplay();
$view
->initHandlers();
foreach ($views_data['access_callback'] as $type => $info) {
if (!in_array($type, [
'title',
'help',
])) {
$this
->assertFalse(isset($view->field['access_callback']), 'Make sure the user got no access to the access_callback field ');
$this
->assertInstanceOf(HandlerBase::class, $view->field['access_callback_arguments']);
}
}
}
}