ExposedFormRenderTest.php 4.44 KB
Newer Older
1
2
3
4
5
<?php

namespace Drupal\Tests\views\Kernel\Plugin;

use Drupal\Component\Utility\Html;
6
use Drupal\node\Entity\NodeType;
7
8
9
10
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;

/**
11
 * Tests the exposed form.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * @group views
 * @see \Drupal\views_test_data\Plugin\views\display_extender\DisplayExtenderTest
 */
class ExposedFormRenderTest extends ViewsKernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $testViews = ['test_exposed_form_buttons'];

  /**
   * {@inheritdoc}
   */
26
  protected static $modules = ['node'];
27
28
29
30

  /**
   * {@inheritdoc}
   */
31
  protected function setUp($import_test_views = TRUE): void {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    parent::setUp();
    $this->installEntitySchema('node');
  }

  /**
   * Tests the exposed form markup.
   */
  public function testExposedFormRender() {
    $view = Views::getView('test_exposed_form_buttons');
    $this->executeView($view);
    $exposed_form = $view->display_handler->getPlugin('exposed_form');
    $output = $exposed_form->renderExposedForm();
    $this->setRawContent(\Drupal::service('renderer')->renderRoot($output));

    $this->assertFieldByXpath('//form/@id', Html::cleanCssIdentifier('views-exposed-form-' . $view->storage->id() . '-' . $view->current_display), 'Expected form ID found.');

    $view->setDisplay('page_1');
    $expected_action = $view->display_handler->getUrlInfo()->toString();
    $this->assertFieldByXPath('//form/@action', $expected_action, 'The expected value for the action attribute was found.');
    // Make sure the description is shown.
52
    $result = $this->xpath('//form//div[contains(@id, :id) and normalize-space(text())=:description]', [':id' => 'edit-type--2--description', ':description' => t('Exposed description')]);
53
    $this->assertCount(1, $result, 'Filter description was found.');
54
55
  }

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  /**
   * Tests the exposed form raw input.
   */
  public function testExposedFormRawInput() {
    $node_type = NodeType::create(['type' => 'article']);
    $node_type->save();

    $view = Views::getView('test_exposed_form_buttons');
    $view->setDisplay();
    $view->displayHandlers->get('default')->overrideOption('filters', [
      'type' => [
        'exposed' => TRUE,
        'field' => 'type',
        'id' => 'type',
        'table' => 'node_field_data',
        'plugin_id' => 'in_operator',
        'entity_type' => 'node',
        'entity_field' => 'type',
        'expose' => [
          'identifier' => 'type',
          'label' => 'Content: Type',
          'operator_id' => 'type_op',
          'reduce' => FALSE,
          'multiple' => FALSE,
        ],
      ],
      'type_with_default_value' => [
        'exposed' => TRUE,
        'field' => 'type',
        'id' => 'type_with_default_value',
        'table' => 'node_field_data',
        'plugin_id' => 'in_operator',
        'entity_type' => 'node',
        'entity_field' => 'type',
        'value' => ['article', 'article'],
        'expose' => [
          'identifier' => 'type_with_default_value',
          'label' => 'Content: Type with value',
          'operator_id' => 'type_op',
          'reduce' => FALSE,
          'multiple' => FALSE,
        ],
      ],
      'multiple_types' => [
        'exposed' => TRUE,
        'field' => 'type',
        'id' => 'multiple_types',
        'table' => 'node_field_data',
        'plugin_id' => 'in_operator',
        'entity_type' => 'node',
        'entity_field' => 'type',
        'expose' => [
          'identifier' => 'multiple_types',
          'label' => 'Content: Type (multiple)',
          'operator_id' => 'type_op',
          'reduce' => FALSE,
          'multiple' => TRUE,
        ],
      ],
      'multiple_types_with_default_value' => [
        'exposed' => TRUE,
        'field' => 'type',
        'id' => 'multiple_types_with_default_value',
        'table' => 'node_field_data',
        'plugin_id' => 'in_operator',
        'entity_type' => 'node',
        'entity_field' => 'type',
        'value' => ['article', 'article'],
        'expose' => [
          'identifier' => 'multiple_types_with_default_value',
          'label' => 'Content: Type with default value (multiple)',
          'operator_id' => 'type_op',
          'reduce' => FALSE,
          'multiple' => TRUE,
        ],
      ],
    ]);
    $view->save();
    $this->executeView($view);

    $expected = [
      'type' => 'All',
      'type_with_default_value' => 'article',
139
      'multiple_types_with_default_value' => ['article' => 'article'],
140
141
142
143
    ];
    $this->assertSame($view->exposed_raw_input, $expected);
  }

144
}