View source
<?php
namespace Drupal\views\Tests\Plugin;
use Drupal\views\Views;
use Drupal\views_test_data\Plugin\views\argument_default\ArgumentDefaultTest as ArgumentDefaultTestPlugin;
class ArgumentDefaultTest extends PluginTestBase {
public static $testViews = array(
'test_view',
'test_argument_default_fixed',
'test_argument_default_current_user',
);
public static $modules = array(
'node',
'views_ui',
);
protected function setUp() {
parent::setUp();
$this
->enableViewsTestModule();
}
public function testArgumentDefaultPlugin() {
$view = Views::getView('test_view');
$options = array(
'default_argument_type' => 'argument_default_test',
'default_argument_options' => array(
'value' => 'John',
),
'default_action' => 'default',
);
$id = $view
->addHandler('default', 'argument', 'views_test_data', 'name', $options);
$view
->initHandlers();
$plugin = $view->argument[$id]
->getPlugin('argument_default');
$this
->assertTrue($plugin instanceof ArgumentDefaultTestPlugin, 'The correct argument default plugin is used.');
$this
->assertEqual($view->argument[$id]
->getDefaultArgument(), 'John', 'The correct argument default value is returned.');
$this
->executeView($view);
$this
->assertEqual($view->argument[$id]
->getValue(), 'John', 'The correct argument value is used.');
$expected_result = array(
array(
'name' => 'John',
),
);
$this
->assertIdenticalResultset($view, $expected_result, array(
'views_test_data_name' => 'name',
));
$view
->destroy();
$this
->executeView($view, array(
'George',
));
$this
->assertEqual($view->argument[$id]
->getValue(), 'George', 'The correct argument value is used.');
$expected_result = array(
array(
'name' => 'George',
),
);
$this
->assertIdenticalResultset($view, $expected_result, array(
'views_test_data_name' => 'name',
));
}
function testArgumentDefaultNoOptions() {
$admin_user = $this
->drupalCreateUser(array(
'administer views',
'administer site configuration',
));
$this
->drupalLogin($admin_user);
$argument_type = 'current_user';
$edit = array(
'options[default_argument_type]' => $argument_type,
);
$this
->drupalPostForm('admin/structure/views/nojs/handler/test_argument_default_current_user/default/argument/uid', $edit, t('Apply'));
$error = array(
'%type' => 'Notice',
'@message' => 'Undefined index: ' . $argument_type,
'%function' => 'views_handler_argument->validateOptionsForm()',
);
$message = t('%type: @message in %function', $error);
$this
->assertNoRaw($message, format_string('Did not find error message: @message.', array(
'@message' => $message,
)));
}
function testArgumentDefaultFixed() {
$random = $this
->randomMachineName();
$view = Views::getView('test_argument_default_fixed');
$view
->setDisplay();
$options = $view->display_handler
->getOption('arguments');
$options['null']['default_argument_options']['argument'] = $random;
$view->display_handler
->overrideOption('arguments', $options);
$view
->initHandlers();
$this
->assertEqual($view->argument['null']
->getDefaultArgument(), $random, 'Fixed argument should be used by default.');
$random_string = $this
->randomMachineName();
$view
->executeDisplay('default', array(
$random_string,
));
$this
->assertEqual($view->args[0], $random_string, 'Provided argument should be used.');
}
}