b3956de8ea5ea2175df431cc2c83a324f84c9787
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / ArgumentDefaultTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Core\Url;
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;
12
13
14 /**
15  * Tests pluggable argument_default for views.
16  *
17  * @group views
18  */
19 class ArgumentDefaultTest extends ViewTestBase {
20
21   /**
22    * Views used by this test.
23    *
24    * @var array
25    */
26   public static $testViews = [
27     'test_view',
28     'test_argument_default_fixed',
29     'test_argument_default_current_user',
30     'test_argument_default_node',
31     'test_argument_default_query_param',
32     ];
33
34   /**
35    * Modules to enable.
36    *
37    * @var array
38    */
39   public static $modules = ['node', 'views_ui', 'block'];
40
41   protected function setUp($import_test_views = TRUE) {
42     parent::setUp($import_test_views);
43
44     $this->enableViewsTestModule();
45   }
46
47   /**
48    * Tests the argument default test plugin.
49    *
50    * @see \Drupal\views_test_data\Plugin\views\argument_default\ArgumentDefaultTest
51    */
52   public function testArgumentDefaultPlugin() {
53     $view = Views::getView('test_view');
54
55     // Add a new argument and set the test plugin for the argument_default.
56     $options = [
57       'default_argument_type' => 'argument_default_test',
58       'default_argument_options' => [
59         'value' => 'John'
60       ],
61       'default_action' => 'default'
62     ];
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.');
67
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
71     // just returns John.
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']);
76
77     // Pass in value as argument to be sure that not the default value is used.
78     $view->destroy();
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']);
83   }
84
85
86   /**
87    * Tests the use of a default argument plugin that provides no options.
88    */
89   public function testArgumentDefaultNoOptions() {
90     $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
91     $this->drupalLogin($admin_user);
92
93     // The current_user plugin has no options form, and should pass validation.
94     $argument_type = 'current_user';
95     $edit = [
96       'options[default_argument_type]' => $argument_type,
97     ];
98     $this->drupalPostForm('admin/structure/views/nojs/handler/test_argument_default_current_user/default/argument/uid', $edit, t('Apply'));
99
100     // Note, the undefined index error has two spaces after it.
101     $error = [
102       '%type' => 'Notice',
103       '@message' => 'Undefined index:  ' . $argument_type,
104       '%function' => 'views_handler_argument->validateOptionsForm()',
105     ];
106     $message = t('%type: @message in %function', $error);
107     $this->assertNoRaw($message, format_string('Did not find error message: @message.', ['@message' => $message]));
108   }
109
110   /**
111    * Tests fixed default argument.
112    */
113   public function testArgumentDefaultFixed() {
114     $random = $this->randomMachineName();
115     $view = Views::getView('test_argument_default_fixed');
116     $view->setDisplay();
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();
121
122     $this->assertEqual($view->argument['null']->getDefaultArgument(), $random, 'Fixed argument should be used by default.');
123
124     // Make sure that a normal argument provided is used
125     $random_string = $this->randomMachineName();
126     $view->executeDisplay('default', [$random_string]);
127
128     $this->assertEqual($view->args[0], $random_string, 'Provided argument should be used.');
129   }
130
131   /**
132    * @todo Test php default argument.
133    */
134   //function testArgumentDefaultPhp() {}
135
136   /**
137    * Test node default argument.
138    */
139   public function testArgumentDefaultNode() {
140     // Create a user that has permission to place a view block.
141     $permissions = [
142       'administer views',
143       'administer blocks',
144       'bypass node access',
145       'access user profiles',
146       'view all revisions',
147       ];
148     $views_admin = $this->drupalCreateUser($permissions);
149     $this->drupalLogin($views_admin);
150
151     // Create nodes where should show themselves again as view block.
152     $node_type = NodeType::create(['type' => 'page', 'label' => 'Page']);
153     $node_type->save();
154     $node1 = Node::create(['title' => 'Test node 1', 'type' => 'page']);
155     $node1->save();
156     $node2 = Node::create(['title' => 'Test node 2', 'type' => 'page']);
157     $node2->save();
158
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()));
168   }
169
170   /**
171    * Tests the query parameter default argument.
172    */
173   public function testArgumentDefaultQueryParameter() {
174     $view = Views::getView('test_argument_default_query_param');
175
176     $request = Request::create(Url::fromUri('internal:/whatever', ['absolute' => TRUE])->toString());
177
178     // Check the query parameter default argument fallback value.
179     $view->setRequest($request);
180     $view->initHandlers();
181     $this->assertEqual($view->argument['type']->getDefaultArgument(), 'all');
182
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');
188   }
189
190 }