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;
14 * Tests pluggable argument_default for views.
18 class ArgumentDefaultTest extends ViewTestBase {
21 * Views used by this test.
25 public static $testViews = [
27 'test_argument_default_fixed',
28 'test_argument_default_current_user',
29 'test_argument_default_node',
30 'test_argument_default_query_param',
38 public static $modules = ['node', 'views_ui', 'block'];
40 protected function setUp($import_test_views = TRUE) {
41 parent::setUp($import_test_views);
43 $this->enableViewsTestModule();
47 * Tests the argument default test plugin.
49 * @see \Drupal\views_test_data\Plugin\views\argument_default\ArgumentDefaultTest
51 public function testArgumentDefaultPlugin() {
52 $view = Views::getView('test_view');
54 // Add a new argument and set the test plugin for the argument_default.
56 'default_argument_type' => 'argument_default_test',
57 'default_argument_options' => [
60 'default_action' => 'default'
62 $id = $view->addHandler('default', 'argument', 'views_test_data', 'name', $options);
63 $view->initHandlers();
64 $plugin = $view->argument[$id]->getPlugin('argument_default');
65 $this->assertTrue($plugin instanceof ArgumentDefaultTestPlugin, 'The correct argument default plugin is used.');
67 // Check that the value of the default argument is as expected.
68 $this->assertEqual($view->argument[$id]->getDefaultArgument(), 'John', 'The correct argument default value is returned.');
69 // Don't pass in a value for the default argument and make sure the query
71 $this->executeView($view);
72 $this->assertEqual($view->argument[$id]->getValue(), 'John', 'The correct argument value is used.');
73 $expected_result = [['name' => 'John']];
74 $this->assertIdenticalResultset($view, $expected_result, ['views_test_data_name' => 'name']);
76 // Pass in value as argument to be sure that not the default value is used.
78 $this->executeView($view, ['George']);
79 $this->assertEqual($view->argument[$id]->getValue(), 'George', 'The correct argument value is used.');
80 $expected_result = [['name' => 'George']];
81 $this->assertIdenticalResultset($view, $expected_result, ['views_test_data_name' => 'name']);
86 * Tests the use of a default argument plugin that provides no options.
88 public function testArgumentDefaultNoOptions() {
89 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
90 $this->drupalLogin($admin_user);
92 // The current_user plugin has no options form, and should pass validation.
93 $argument_type = 'current_user';
95 'options[default_argument_type]' => $argument_type,
97 $this->drupalPostForm('admin/structure/views/nojs/handler/test_argument_default_current_user/default/argument/uid', $edit, t('Apply'));
99 // Note, the undefined index error has two spaces after it.
102 '@message' => 'Undefined index: ' . $argument_type,
103 '%function' => 'views_handler_argument->validateOptionsForm()',
105 $message = t('%type: @message in %function', $error);
106 $this->assertNoRaw($message, format_string('Did not find error message: @message.', ['@message' => $message]));
110 * Tests fixed default argument.
112 public function testArgumentDefaultFixed() {
113 $random = $this->randomMachineName();
114 $view = Views::getView('test_argument_default_fixed');
116 $options = $view->display_handler->getOption('arguments');
117 $options['null']['default_argument_options']['argument'] = $random;
118 $view->display_handler->overrideOption('arguments', $options);
119 $view->initHandlers();
121 $this->assertEqual($view->argument['null']->getDefaultArgument(), $random, 'Fixed argument should be used by default.');
123 // Make sure that a normal argument provided is used
124 $random_string = $this->randomMachineName();
125 $view->executeDisplay('default', [$random_string]);
127 $this->assertEqual($view->args[0], $random_string, 'Provided argument should be used.');
131 * @todo Test php default argument.
133 // function testArgumentDefaultPhp() {}
136 * Test node default argument.
138 public function testArgumentDefaultNode() {
139 // Create a user that has permission to place a view block.
143 'bypass node access',
144 'access user profiles',
145 'view all revisions',
147 $views_admin = $this->drupalCreateUser($permissions);
148 $this->drupalLogin($views_admin);
150 // Create nodes where should show themselves again as view block.
151 $node_type = NodeType::create(['type' => 'page', 'label' => 'Page']);
153 $node1 = Node::create(['title' => 'Test node 1', 'type' => 'page']);
155 $node2 = Node::create(['title' => 'Test node 2', 'type' => 'page']);
158 // Place the block, visit the pages that display the block, and check that
159 // the nodes we expect appear in the respective pages.
160 $id = 'view-block-id';
161 $this->drupalPlaceBlock("views_block:test_argument_default_node-block_1", ['id' => $id]);
162 $xpath = '//*[@id="block-' . $id . '"]';
163 $this->drupalGet('node/' . $node1->id());
164 $this->assertTrue(strpos($this->xpath($xpath)[0]->getText(), $node1->getTitle()));
165 $this->drupalGet('node/' . $node2->id());
166 $this->assertTrue(strpos($this->xpath($xpath)[0]->getText(), $node2->getTitle()));
170 * Tests the query parameter default argument.
172 public function testArgumentDefaultQueryParameter() {
173 $view = Views::getView('test_argument_default_query_param');
175 $request = Request::create(Url::fromUri('internal:/whatever', ['absolute' => TRUE])->toString());
177 // Check the query parameter default argument fallback value.
178 $view->setRequest($request);
179 $view->initHandlers();
180 $this->assertEqual($view->argument['type']->getDefaultArgument(), 'all');
182 // Check the query parameter default argument with a value.
183 $request->query->add(['the_node_type' => 'page']);
184 $view->setRequest($request);
185 $view->initHandlers();
186 $this->assertEqual($view->argument['type']->getDefaultArgument(), 'page');