b12e43e857c26d89b84994afa530746686dcea86
[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    * Tests the use of a default argument plugin that provides no options.
86    */
87   public function testArgumentDefaultNoOptions() {
88     $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
89     $this->drupalLogin($admin_user);
90
91     // The current_user plugin has no options form, and should pass validation.
92     $argument_type = 'current_user';
93     $edit = [
94       'options[default_argument_type]' => $argument_type,
95     ];
96     $this->drupalPostForm('admin/structure/views/nojs/handler/test_argument_default_current_user/default/argument/uid', $edit, t('Apply'));
97
98     // Note, the undefined index error has two spaces after it.
99     $error = [
100       '%type' => 'Notice',
101       '@message' => 'Undefined index:  ' . $argument_type,
102       '%function' => 'views_handler_argument->validateOptionsForm()',
103     ];
104     $message = t('%type: @message in %function', $error);
105     $this->assertNoRaw($message, format_string('Did not find error message: @message.', ['@message' => $message]));
106   }
107
108   /**
109    * Tests fixed default argument.
110    */
111   public function testArgumentDefaultFixed() {
112     $random = $this->randomMachineName();
113     $view = Views::getView('test_argument_default_fixed');
114     $view->setDisplay();
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();
119
120     $this->assertEqual($view->argument['null']->getDefaultArgument(), $random, 'Fixed argument should be used by default.');
121
122     // Make sure that a normal argument provided is used
123     $random_string = $this->randomMachineName();
124     $view->executeDisplay('default', [$random_string]);
125
126     $this->assertEqual($view->args[0], $random_string, 'Provided argument should be used.');
127   }
128
129   /**
130    * @todo Test php default argument.
131    */
132   // function testArgumentDefaultPhp() {}
133
134   /**
135    * Test node default argument.
136    */
137   public function testArgumentDefaultNode() {
138     // Create a user that has permission to place a view block.
139     $permissions = [
140       'administer views',
141       'administer blocks',
142       'bypass node access',
143       'access user profiles',
144       'view all revisions',
145       ];
146     $views_admin = $this->drupalCreateUser($permissions);
147     $this->drupalLogin($views_admin);
148
149     // Create nodes where should show themselves again as view block.
150     $node_type = NodeType::create(['type' => 'page', 'label' => 'Page']);
151     $node_type->save();
152     $node1 = Node::create(['title' => 'Test node 1', 'type' => 'page']);
153     $node1->save();
154     $node2 = Node::create(['title' => 'Test node 2', 'type' => 'page']);
155     $node2->save();
156
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()));
166   }
167
168   /**
169    * Tests the query parameter default argument.
170    */
171   public function testArgumentDefaultQueryParameter() {
172     $view = Views::getView('test_argument_default_query_param');
173
174     $request = Request::create(Url::fromUri('internal:/whatever', ['absolute' => TRUE])->toString());
175
176     // Check the query parameter default argument fallback value.
177     $view->setRequest($request);
178     $view->initHandlers();
179     $this->assertEqual($view->argument['type']->getDefaultArgument(), 'all');
180
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');
186   }
187
188 }