3 namespace Drupal\Tests\views\Functional\Plugin;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\Tests\views\Functional\ViewTestBase;
9 use Drupal\views\Views;
10 use Drupal\views_test_data\Plugin\views\argument_default\ArgumentDefaultTest as ArgumentDefaultTestPlugin;
11 use Symfony\Component\HttpFoundation\Request;
15 * Tests pluggable argument_default for views.
19 class ArgumentDefaultTest extends ViewTestBase {
22 * Views used by this test.
26 public static $testViews = [
28 'test_argument_default_fixed',
29 'test_argument_default_current_user',
30 'test_argument_default_node',
31 'test_argument_default_query_param',
39 public static $modules = ['node', 'views_ui', 'block'];
41 protected function setUp($import_test_views = TRUE) {
42 parent::setUp($import_test_views);
44 $this->enableViewsTestModule();
48 * Tests the argument default test plugin.
50 * @see \Drupal\views_test_data\Plugin\views\argument_default\ArgumentDefaultTest
52 public function testArgumentDefaultPlugin() {
53 $view = Views::getView('test_view');
55 // Add a new argument and set the test plugin for the argument_default.
57 'default_argument_type' => 'argument_default_test',
58 'default_argument_options' => [
61 'default_action' => 'default'
63 $id = $view->addHandler('default', 'argument', 'views_test_data', 'name', $options);
64 $view->initHandlers();
65 $plugin = $view->argument[$id]->getPlugin('argument_default');
66 $this->assertTrue($plugin instanceof ArgumentDefaultTestPlugin, 'The correct argument default plugin is used.');
68 // Check that the value of the default argument is as expected.
69 $this->assertEqual($view->argument[$id]->getDefaultArgument(), 'John', 'The correct argument default value is returned.');
70 // Don't pass in a value for the default argument and make sure the query
72 $this->executeView($view);
73 $this->assertEqual($view->argument[$id]->getValue(), 'John', 'The correct argument value is used.');
74 $expected_result = [['name' => 'John']];
75 $this->assertIdenticalResultset($view, $expected_result, ['views_test_data_name' => 'name']);
77 // Pass in value as argument to be sure that not the default value is used.
79 $this->executeView($view, ['George']);
80 $this->assertEqual($view->argument[$id]->getValue(), 'George', 'The correct argument value is used.');
81 $expected_result = [['name' => 'George']];
82 $this->assertIdenticalResultset($view, $expected_result, ['views_test_data_name' => 'name']);
87 * Tests the use of a default argument plugin that provides no options.
89 public function testArgumentDefaultNoOptions() {
90 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
91 $this->drupalLogin($admin_user);
93 // The current_user plugin has no options form, and should pass validation.
94 $argument_type = 'current_user';
96 'options[default_argument_type]' => $argument_type,
98 $this->drupalPostForm('admin/structure/views/nojs/handler/test_argument_default_current_user/default/argument/uid', $edit, t('Apply'));
100 // Note, the undefined index error has two spaces after it.
103 '@message' => 'Undefined index: ' . $argument_type,
104 '%function' => 'views_handler_argument->validateOptionsForm()',
106 $message = t('%type: @message in %function', $error);
107 $this->assertNoRaw($message, format_string('Did not find error message: @message.', ['@message' => $message]));
111 * Tests fixed default argument.
113 public function testArgumentDefaultFixed() {
114 $random = $this->randomMachineName();
115 $view = Views::getView('test_argument_default_fixed');
117 $options = $view->display_handler->getOption('arguments');
118 $options['null']['default_argument_options']['argument'] = $random;
119 $view->display_handler->overrideOption('arguments', $options);
120 $view->initHandlers();
122 $this->assertEqual($view->argument['null']->getDefaultArgument(), $random, 'Fixed argument should be used by default.');
124 // Make sure that a normal argument provided is used
125 $random_string = $this->randomMachineName();
126 $view->executeDisplay('default', [$random_string]);
128 $this->assertEqual($view->args[0], $random_string, 'Provided argument should be used.');
132 * @todo Test php default argument.
134 //function testArgumentDefaultPhp() {}
137 * Test node default argument.
139 public function testArgumentDefaultNode() {
140 // Create a user that has permission to place a view block.
144 'bypass node access',
145 'access user profiles',
146 'view all revisions',
148 $views_admin = $this->drupalCreateUser($permissions);
149 $this->drupalLogin($views_admin);
151 // Create nodes where should show themselves again as view block.
152 $node_type = NodeType::create(['type' => 'page', 'label' => 'Page']);
154 $node1 = Node::create(['title' => 'Test node 1', 'type' => 'page']);
156 $node2 = Node::create(['title' => 'Test node 2', 'type' => 'page']);
159 // Place the block, visit the pages that display the block, and check that
160 // the nodes we expect appear in the respective pages.
161 $id = 'view-block-id';
162 $this->drupalPlaceBlock("views_block:test_argument_default_node-block_1", ['id' => $id]);
163 $xpath = '//*[@id="block-' . $id . '"]';
164 $this->drupalGet('node/' . $node1->id());
165 $this->assertTrue(strpos($this->xpath($xpath)[0]->getText(), $node1->getTitle()));
166 $this->drupalGet('node/' . $node2->id());
167 $this->assertTrue(strpos($this->xpath($xpath)[0]->getText(), $node2->getTitle()));
171 * Tests the query parameter default argument.
173 public function testArgumentDefaultQueryParameter() {
174 $view = Views::getView('test_argument_default_query_param');
176 $request = Request::create(Url::fromUri('internal:/whatever', ['absolute' => TRUE])->toString());
178 // Check the query parameter default argument fallback value.
179 $view->setRequest($request);
180 $view->initHandlers();
181 $this->assertEqual($view->argument['type']->getDefaultArgument(), 'all');
183 // Check the query parameter default argument with a value.
184 $request->query->add(['the_node_type' => 'page']);
185 $view->setRequest($request);
186 $view->initHandlers();
187 $this->assertEqual($view->argument['type']->getDefaultArgument(), 'page');