Commit 92537b39 authored by dawehner's avatar dawehner Committed by tim.plunkett

Issue #1785770 by dawehner: Improve the handler base test.

parent 39de66cc
...@@ -496,9 +496,12 @@ public function postExecute(&$values) { } ...@@ -496,9 +496,12 @@ public function postExecute(&$values) { }
/** /**
* Provides a unique placeholders for handlers. * Provides a unique placeholders for handlers.
*
* @return string
* A placeholder which contains the table and the fieldname.
*/ */
protected function placeholder() { protected function placeholder() {
return $this->query->placeholder($this->options['table'] . '_' . $this->options['field']); return $this->query->placeholder($this->table . '_' . $this->field);
} }
/** /**
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\views\Tests\Handler; namespace Drupal\views\Tests\Handler;
use Drupal\views\ViewExecutable;
use Drupal\views\Tests\ViewTestBase; use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Plugin\views\HandlerBase; use Drupal\views\Plugin\views\HandlerBase;
...@@ -20,13 +21,13 @@ class HandlerTest extends ViewTestBase { ...@@ -20,13 +21,13 @@ class HandlerTest extends ViewTestBase {
* *
* @var array * @var array
*/ */
public static $modules = array('views_ui'); public static $modules = array('views_ui', 'comment', 'node');
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'Handlers tests', 'name' => 'Handler: Base',
'description' => 'Tests abstract handler definitions.', 'description' => 'Tests abstract handler definitions.',
'group' => 'Views', 'group' => 'Views Handlers',
); );
} }
...@@ -36,6 +37,34 @@ protected function setUp() { ...@@ -36,6 +37,34 @@ protected function setUp() {
$this->enableViewsTestModule(); $this->enableViewsTestModule();
} }
/**
* Overrides Drupal\views\Tests\ViewTestBase::viewsData().
*/
protected function viewsData() {
$data = parent::viewsData();
// Override the name handler to be able to call placeholder() from outside.
$data['views_test_data']['name']['field']['id'] = 'test_field';
// Setup one field with an access callback and one with an access callback
// and arguments.
$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::viewsHandlerTypes() 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'] = array(TRUE);
}
}
return $data;
}
/**
* @todo
* This should probably moved to a filter related test.
*/
function testFilterInOperatorUi() { function testFilterInOperatorUi() {
$admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration')); $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
$this->drupalLogin($admin_user); $this->drupalLogin($admin_user);
...@@ -109,7 +138,7 @@ function testBreakPhraseString() { ...@@ -109,7 +138,7 @@ function testBreakPhraseString() {
} }
/** /**
* Tests views_break_phrase function. * Tests Drupal\views\Plugin\views\HandlerBase::breakPhrase() function.
*/ */
function testBreakPhrase() { function testBreakPhrase() {
$empty_stdclass = new \stdClass(); $empty_stdclass = new \stdClass();
...@@ -145,11 +174,12 @@ function testBreakPhrase() { ...@@ -145,11 +174,12 @@ function testBreakPhrase() {
} }
/** /**
* Check to see if two values are equal. * Check to see if a value is the same as the value on a certain handler.
* *
* @param $first * @param $first
* The first value to check. * The first value to check.
* @param views_handler $handler * @param Drupal\views\Plugin\views\HandlerBase $handler
* The handler that has the $handler->value property to compare with first.
* @param string $message * @param string $message
* The message to display along with the assertion. * The message to display along with the assertion.
* @param string $group * @param string $group
...@@ -162,4 +192,149 @@ protected function assertEqualValue($first, $handler, $message = '', $group = 'O ...@@ -162,4 +192,149 @@ protected function assertEqualValue($first, $handler, $message = '', $group = 'O
return $this->assert($first == $handler->value, $message ? $message : t('First value is equal to second value'), $group); return $this->assert($first == $handler->value, $message ? $message : t('First value is equal to second value'), $group);
} }
/**
* Tests the relationship ui for field/filter/argument/relationship.
*/
public function testRelationshipUI() {
$views_admin = $this->drupalCreateUser(array('administer views'));
$this->drupalLogin($views_admin);
// Make sure the link to the field options exists.
$handler_options_path = 'admin/structure/views/nojs/config-item/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);
// The test view has a relationship to node_revision so the field should
// show a relationship selection.
$this->drupalGet($handler_options_path);
$relationship_name = 'options[relationship]';
$this->assertFieldByName($relationship_name);
// Check for available options.
$xpath = $this->constructFieldXpath('name', $relationship_name);
$fields = $this->xpath($xpath);
$options = array();
foreach ($fields as $field) {
$items = $this->getAllOptions($field);
foreach ($items as $item) {
$options[] = $item->attributes()->value;
}
}
$expected_options = array('none', 'nid');
$this->assertEqual($options, $expected_options);
// Remove the relationship and take sure no relationship option appears.
$this->drupalPost('admin/structure/views/nojs/config-item/test_handler_relationships/default/relationship/nid', array(), t('Remove'));
$this->drupalGet($handler_options_path);
$this->assertNoFieldByName($relationship_name, 'Make sure that no relationship option is available');
}
/**
* Tests the relationship method on the base class.
*/
public function testSetRelationship() {
$view = $this->createViewFromConfig('test_handler_relationships');
// Setup a broken relationship.
$view->addItem('default', 'relationship', $this->randomName(), $this->randomName(), array(), 'broken_relationship');
// Setup a valid relationship.
$view->addItem('default', 'relationship', 'comment', 'nid', array('relationship' => 'cid'), 'valid_relationship');
$view->initHandlers();
$field = $view->field['title'];
$field->options['relationship'] = NULL;
$field->setRelationship();
$this->assertFalse($field->relationship, 'Make sure that an empty relationship does not create a relationship on the field.');
$field->options['relationship'] = $this->randomName();
$field->setRelationship();
$this->assertFalse($field->relationship, 'Make sure that a random relationship does not create a relationship on the field.');
$field->options['relationship'] = 'broken_relationship';
$field->setRelationship();
$this->assertFalse($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.');
// Remove the invalid relationship.
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.');
}
/**
* Tests the placeholder function.
*
* @see Drupal\views\Plugin\views\HandlerBase::placeholder()
*/
public function testPlaceholder() {
$view = $this->getView();
$view->initHandlers();
$view->initQuery();
$handler = $view->field['name'];
$table = $handler->table;
$field = $handler->field;
$string = ':' . $table . '_' . $field;
// Make sure the placeholder variables are like expected.
$this->assertEqual($handler->getPlaceholder(), $string);
$this->assertEqual($handler->getPlaceholder(), $string . 1);
$this->assertEqual($handler->getPlaceholder(), $string . 2);
// Set another table/field combination and make sure there are new
// placeholders.
$table = $handler->table = $this->randomName();
$field = $handler->field = $this->randomName();
$string = ':' . $table . '_' . $field;
// Make sure the placeholder variables are like expected.
$this->assertEqual($handler->getPlaceholder(), $string);
$this->assertEqual($handler->getPlaceholder(), $string . 1);
$this->assertEqual($handler->getPlaceholder(), $string . 2);
}
/**
* Tests access to a handler.
*
* @see views_test_data_handler_test_access_callback
*/
public function testAccess() {
$view = views_get_view('test_handler_test_access');
$views_data = $this->viewsData();
$views_data = $views_data['views_test_data'];
// Enable access to callback only field and deny for callback + arguments.
config('views_test_data.tests')->set('handler_access_callback', TRUE)->save();
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, array('title', 'help'))) {
$this->assertTrue($view->field['access_callback'] instanceof HandlerBase, 'Make sure the user got access to the access_callback field ');
$this->assertFalse(isset($view->field['access_callback_arguments']), 'Make sure the user got no access to the access_callback_arguments field ');
}
}
// Enable access to the callback + argument handlers and deny for callback.
config('views_test_data.tests')->set('handler_access_callback', FALSE)->save();
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, array('title', 'help'))) {
$this->assertFalse(isset($view->field['access_callback']), 'Make sure the user got no access to the access_callback field ');
$this->assertTrue($view->field['access_callback_arguments'] instanceof HandlerBase, 'Make sure the user got access to the access_callback_arguments field ');
}
}
}
} }
api_version: '3.0'
base_table: node
core: '8'
description: ''
disabled: '0'
display:
default:
display_options:
fields:
title:
id: title
table: node
field: title
relationships:
cid:
id: cid
table: node
field: cid
nid:
id: nid
table: comment
field: nid
relationship: cid
display_plugin: default
display_title: Master
id: default
position: '0'
human_name: ''
name: test_handler_relationships
tag: ''
api_version: '3.0'
base_table: views_test_data
core: 8
display:
default:
display_options:
fields:
access_callback:
id: access_callback
table: views_test_data
field: access_callback
access_callback_arguments:
id: access_callback_arguments
table: views_test_data
field: access_callback_arguments
filters:
access_callback:
id: access_callback
table: views_test_data
field: access_callback
access_callback_arguments:
id: access_callback_arguments
table: views_test_data
field: access_callback_arguments
arguments:
access_callback:
id: access_callback
table: views_test_data
field: access_callback
access_callback_arguments:
id: access_callback_arguments
table: views_test_data
field: access_callback_arguments
sorts:
access_callback:
id: access_callback
table: views_test_data
field: access_callback
access_callback_arguments:
id: access_callback_arguments
table: views_test_data
field: access_callback_arguments
display_plugin: default
display_title: Master
id: default
position: '0'
name: test_handler_test_access
...@@ -61,4 +61,14 @@ function render($values) { ...@@ -61,4 +61,14 @@ function render($values) {
return $this->sanitizeValue($this->getTestValue()); return $this->sanitizeValue($this->getTestValue());
} }
/**
* A mock function which allows to call placeholder from public.
*
* @return string
* The result of the placeholder method.
*/
public function getPlaceholder() {
return $this->placeholder();
}
} }
...@@ -44,6 +44,43 @@ function views_test_data_test_dynamic_access_callback($access, $argument1, $argu ...@@ -44,6 +44,43 @@ function views_test_data_test_dynamic_access_callback($access, $argument1, $argu
return $access && $argument1 == variable_get('test_dynamic_access_argument1', NULL) && $argument2 == variable_get('test_dynamic_access_argument2', NULL); return $access && $argument1 == variable_get('test_dynamic_access_argument1', NULL) && $argument2 == variable_get('test_dynamic_access_argument2', NULL);
} }
/**+
* Access callback for the generic handler test.
*
* @return bool
* Returns views_test_data.tests->handler_access_callback config. so the user
* has access to the handler.
*
* @see Drupal\views\Tests\Handler\HandlerTest
*/
function views_test_data_handler_test_access_callback() {
return config('views_test_data.tests')->get('handler_access_callback');
}
/**
* Access callback with an argument for the generic handler test.
*
* @param bool $argument
* A parameter to test that an argument got passed.
*
* @return bool
* Returns views_test_data.tests->handler_access_callback_argument, so the
* use has access to the handler.
*
* @see Drupal\views\Tests\Handler\HandlerTest
*/
function views_test_data_handler_test_access_callback_argument($argument = FALSE) {
// Check the argument to be sure that access arguments are passed into the
// callback.
if ($argument) {
return config('views_test_data.tests')->get('handler_access_callback_argument');
}
else {
return FALSE;
}
}
/** /**
* Implements hook_views_pre_render(). * Implements hook_views_pre_render().
*/ */
......
...@@ -1740,7 +1740,7 @@ function views_ui_config_item_form($form, &$form_state) { ...@@ -1740,7 +1740,7 @@ function views_ui_config_item_form($form, &$form_state) {
if (!empty($relationship_options)) { if (!empty($relationship_options)) {
// Make sure the existing relationship is even valid. If not, force // Make sure the existing relationship is even valid. If not, force
// it to none. // it to none.
$base_fields = views_fetch_fields($view->base_table, $form_state['type'], $view->display_handler->useGroupBy()); $base_fields = views_fetch_fields($view->storage->base_table, $form_state['type'], $view->display_handler->useGroupBy());
if (isset($base_fields[$item['table'] . '.' . $item['field']])) { if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
$relationship_options = array_merge(array('none' => t('Do not use a relationship')), $relationship_options); $relationship_options = array_merge(array('none' => t('Do not use a relationship')), $relationship_options);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment