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']);
85 * Tests the use of a default argument plugin that provides no options.
87 public function testArgumentDefaultNoOptions() {
88 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
89 $this->drupalLogin($admin_user);
91 // The current_user plugin has no options form, and should pass validation.
92 $argument_type = 'current_user';
94 'options[default_argument_type]' => $argument_type,
96 $this->drupalPostForm('admin/structure/views/nojs/handler/test_argument_default_current_user/default/argument/uid', $edit, t('Apply'));
98 // Note, the undefined index error has two spaces after it.
101 '@message' => 'Undefined index: ' . $argument_type,
102 '%function' => 'views_handler_argument->validateOptionsForm()',
104 $message = t('%type: @message in %function', $error);
105 $this->assertNoRaw($message, format_string('Did not find error message: @message.', ['@message' => $message]));
109 * Tests fixed default argument.
111 public function testArgumentDefaultFixed() {
112 $random = $this->randomMachineName();
113 $view = Views::getView('test_argument_default_fixed');
115 $options = $view->display_handler->getOption('arguments');
116 $options['null']['default_argument_options']['argument'] = $random;
117 $view->display_handler->overrideOption('arguments', $options);
118 $view->initHandlers();
120 $this->assertEqual($view->argument['null']->getDefaultArgument(), $random, 'Fixed argument should be used by default.');
122 // Make sure that a normal argument provided is used
123 $random_string = $this->randomMachineName();
124 $view->executeDisplay('default', [$random_string]);
126 $this->assertEqual($view->args[0], $random_string, 'Provided argument should be used.');
130 * @todo Test php default argument.
132 // function testArgumentDefaultPhp() {}
135 * Test node default argument.
137 public function testArgumentDefaultNode() {
138 // Create a user that has permission to place a view block.
142 'bypass node access',
143 'access user profiles',
144 'view all revisions',
146 $views_admin = $this->drupalCreateUser($permissions);
147 $this->drupalLogin($views_admin);
149 // Create nodes where should show themselves again as view block.
150 $node_type = NodeType::create(['type' => 'page', 'label' => 'Page']);
152 $node1 = Node::create(['title' => 'Test node 1', 'type' => 'page']);
154 $node2 = Node::create(['title' => 'Test node 2', 'type' => 'page']);
157 // Place the block, visit the pages that display the block, and check that
158 // the nodes we expect appear in the respective pages.
159 $id = 'view-block-id';
160 $this->drupalPlaceBlock("views_block:test_argument_default_node-block_1", ['id' => $id]);
161 $xpath = '//*[@id="block-' . $id . '"]';
162 $this->drupalGet('node/' . $node1->id());
163 $this->assertTrue(strpos($this->xpath($xpath)[0]->getText(), $node1->getTitle()));
164 $this->drupalGet('node/' . $node2->id());
165 $this->assertTrue(strpos($this->xpath($xpath)[0]->getText(), $node2->getTitle()));
169 * Tests the query parameter default argument.
171 public function testArgumentDefaultQueryParameter() {
172 $view = Views::getView('test_argument_default_query_param');
174 $request = Request::create(Url::fromUri('internal:/whatever', ['absolute' => TRUE])->toString());
176 // Check the query parameter default argument fallback value.
177 $view->setRequest($request);
178 $view->initHandlers();
179 $this->assertEqual($view->argument['type']->getDefaultArgument(), 'all');
181 // Check the query parameter default argument with a value.
182 $request->query->add(['the_node_type' => 'page']);
183 $view->setRequest($request);
184 $view->initHandlers();
185 $this->assertEqual($view->argument['type']->getDefaultArgument(), 'page');