ArgumentDefaultTest.php 6.71 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
<?php

3
namespace Drupal\Tests\views\Functional\Plugin;
4

5
use Drupal\Core\Url;
6
7
use Drupal\node\Entity\Node;
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


merlinofchaos's avatar
merlinofchaos committed
14
/**
15
16
17
 * Tests pluggable argument_default for views.
 *
 * @group views
merlinofchaos's avatar
merlinofchaos committed
18
 */
19
class ArgumentDefaultTest extends ViewTestBase {
20

21
  /**
22
   * Views used by this test.
23
24
25
   *
   * @var array
   */
26
  public static $testViews = [
27
28
29
30
    'test_view',
    'test_argument_default_fixed',
    'test_argument_default_current_user',
    '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
42
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
merlinofchaos's avatar
merlinofchaos committed
43

44
    $this->enableViewsTestModule();
merlinofchaos's avatar
merlinofchaos committed
45
46
  }

47
48
49
  /**
   * Tests the argument default test plugin.
   *
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
67
68
    $this->assertTrue($plugin instanceof ArgumentDefaultTestPlugin, 'The correct argument default plugin is used.');

    // 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
71
72
    // Don't pass in a value for the default argument and make sure the query
    // just returns John.
    $this->executeView($view);
73
    $this->assertEqual($view->argument[$id]->getValue(), 'John', 'The correct argument value is used.');
74
75
    $expected_result = [['name' => 'John']];
    $this->assertIdenticalResultset($view, $expected_result, ['views_test_data_name' => 'name']);
76
77
78

    // Pass in value as argument to be sure that not the default value is used.
    $view->destroy();
79
    $this->executeView($view, ['George']);
80
    $this->assertEqual($view->argument[$id]->getValue(), 'George', 'The correct argument value is used.');
81
82
    $expected_result = [['name' => 'George']];
    $this->assertIdenticalResultset($view, $expected_result, ['views_test_data_name' => 'name']);
83
84
85
  }


merlinofchaos's avatar
merlinofchaos committed
86
87
88
  /**
   * Tests the use of a default argument plugin that provides no options.
   */
89
  public function testArgumentDefaultNoOptions() {
90
    $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
merlinofchaos's avatar
merlinofchaos committed
91
92
93
94
    $this->drupalLogin($admin_user);

    // The current_user plugin has no options form, and should pass validation.
    $argument_type = 'current_user';
95
    $edit = [
merlinofchaos's avatar
merlinofchaos committed
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'));
merlinofchaos's avatar
merlinofchaos committed
99
100

    // Note, the undefined index error has two spaces after it.
101
    $error = [
merlinofchaos's avatar
merlinofchaos committed
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]));
merlinofchaos's avatar
merlinofchaos committed
108
109
110
111
112
  }

  /**
   * Tests fixed default argument.
   */
113
  public function testArgumentDefaultFixed() {
114
    $random = $this->randomMachineName();
115
    $view = Views::getView('test_argument_default_fixed');
116
117
118
119
    $view->setDisplay();
    $options = $view->display_handler->getOption('arguments');
    $options['null']['default_argument_options']['argument'] = $random;
    $view->display_handler->overrideOption('arguments', $options);
120
    $view->initHandlers();
merlinofchaos's avatar
merlinofchaos committed
121

122
    $this->assertEqual($view->argument['null']->getDefaultArgument(), $random, 'Fixed argument should be used by default.');
merlinofchaos's avatar
merlinofchaos committed
123
124

    // Make sure that a normal argument provided is used
125
    $random_string = $this->randomMachineName();
126
    $view->executeDisplay('default', [$random_string]);
merlinofchaos's avatar
merlinofchaos committed
127
128
129
130
131
132
133

    $this->assertEqual($view->args[0], $random_string, 'Provided argument should be used.');
  }

  /**
   * @todo Test php default argument.
   */
134
  //function testArgumentDefaultPhp() {}
merlinofchaos's avatar
merlinofchaos committed
135
136

  /**
137
   * Test node default argument.
merlinofchaos's avatar
merlinofchaos committed
138
   */
139
140
  public function testArgumentDefaultNode() {
    // Create a user that has permission to place a view block.
141
    $permissions = [
142
143
144
145
146
      'administer views',
      'administer blocks',
      'bypass node access',
      'access user profiles',
      'view all revisions',
147
      ];
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    $views_admin = $this->drupalCreateUser($permissions);
    $this->drupalLogin($views_admin);

    // Create nodes where should show themselves again as view block.
    $node_type = NodeType::create(['type' => 'page', 'label' => 'Page']);
    $node_type->save();
    $node1 = Node::create(['title' => 'Test node 1', 'type' => 'page']);
    $node1->save();
    $node2 = Node::create(['title' => 'Test node 2', 'type' => 'page']);
    $node2->save();

    // Place the block, visit the pages that display the block, and check that
    // the nodes we expect appear in the respective pages.
    $id = 'view-block-id';
    $this->drupalPlaceBlock("views_block:test_argument_default_node-block_1", ['id' => $id]);
    $xpath = '//*[@id="block-' . $id . '"]';
    $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
  }
merlinofchaos's avatar
merlinofchaos committed
169

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  /**
   * Tests the query parameter default argument.
   */
  public function testArgumentDefaultQueryParameter() {
    $view = Views::getView('test_argument_default_query_param');

    $request = Request::create(Url::fromUri('internal:/whatever', ['absolute' => TRUE])->toString());

    // Check the query parameter default argument fallback value.
    $view->setRequest($request);
    $view->initHandlers();
    $this->assertEqual($view->argument['type']->getDefaultArgument(), 'all');

    // Check the query parameter default argument with a value.
    $request->query->add(['the_node_type' => 'page']);
    $view->setRequest($request);
    $view->initHandlers();
    $this->assertEqual($view->argument['type']->getDefaultArgument(), 'page');
  }

merlinofchaos's avatar
merlinofchaos committed
190
}