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