BulkFormTest.php 8.23 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\views\Functional;
4

5
use Drupal\Component\Render\FormattableMarkup;
6
use Drupal\Tests\BrowserTestBase;
7
use Drupal\views\Views;
8
9
10
11

/**
 * Tests the views bulk form test.
 *
12
13
 * @group views
 * @see \Drupal\views\Plugin\views\field\BulkForm
14
 */
15
class BulkFormTest extends BrowserTestBase {
16
17

  /**
18
   * Modules to install.
19
20
21
   *
   * @var array
   */
22
  public static $modules = ['node', 'action_bulk_test'];
23

24
25
26
27
28
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

29
30
31
32
  /**
   * Tests the bulk form.
   */
  public function testBulkForm() {
33
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
34

35
36
37
38
39
    // First, test an empty bulk form with the default style plugin to make sure
    // the empty region is rendered correctly.
    $this->drupalGet('test_bulk_form_empty');
    $this->assertText(t('This view is empty.'), 'Empty text found on empty bulk form.');

40
    $nodes = [];
41
    for ($i = 0; $i < 10; $i++) {
42
43
44
      // Ensure nodes are sorted in the same order they are inserted in the
      // array.
      $timestamp = REQUEST_TIME - $i;
45
      $nodes[] = $this->drupalCreateNode([
46
        'title' => 'Node ' . $i,
47
48
49
        'sticky' => FALSE,
        'created' => $timestamp,
        'changed' => $timestamp,
50
      ]);
51
52
53
54
    }

    $this->drupalGet('test_bulk_form');

55
    // Test that the views edit header appears first.
56
    $first_form_element = $this->xpath('//form/div[1][@id = :id]', [':id' => 'edit-header']);
57
    $this->assertNotEmpty($first_form_element, 'The views form edit header appears first.');
58

59
60
61
    $this->assertFieldById('edit-action', NULL, 'The action select field appears.');

    // Make sure a checkbox appears on all rows.
62
    $edit = [];
63
    for ($i = 0; $i < 10; $i++) {
64
      $this->assertFieldById('edit-node-bulk-form-' . $i, NULL, new FormattableMarkup('The checkbox on row @row appears.', ['@row' => $i]));
65
      $edit["node_bulk_form[$i]"] = TRUE;
66
67
    }

68
69
70
    // Log in as a user with 'administer nodes' permission to have access to the
    // bulk operation.
    $this->drupalCreateContentType(['type' => 'page']);
71
    $admin_user = $this->drupalCreateUser(['administer nodes', 'edit any page content', 'delete any page content']);
72
73
74
75
    $this->drupalLogin($admin_user);

    $this->drupalGet('test_bulk_form');

76
    // Set all nodes to sticky and check that.
77
    $edit += ['action' => 'node_make_sticky_action'];
78
    $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
79
80

    foreach ($nodes as $node) {
81
      $changed_node = $node_storage->load($node->id());
82
      $this->assertTrue($changed_node->isSticky(), new FormattableMarkup('Node @nid got marked as sticky.', ['@nid' => $node->id()]));
83
84
    }

85
    $this->assertText('Make content sticky was applied to 10 items.');
86
87

    // Unpublish just one node.
88
    $node = $node_storage->load($nodes[0]->id());
89
    $this->assertTrue($node->isPublished(), 'The node is published.');
90

91
    $edit = ['node_bulk_form[0]' => TRUE, 'action' => 'node_unpublish_action'];
92
    $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
93

94
    $this->assertText('Unpublish content was applied to 1 item.');
95
96

    // Load the node again.
97
    $node_storage->resetCache([$node->id()]);
98
    $node = $node_storage->load($node->id());
99
    $this->assertFalse($node->isPublished(), 'A single node has been unpublished.');
100
101

    // The second node should still be published.
102
    $node_storage->resetCache([$nodes[1]->id()]);
103
    $node = $node_storage->load($nodes[1]->id());
104
    $this->assertTrue($node->isPublished(), 'An unchecked node is still published.');
105
106

    // Set up to include just the sticky actions.
107
    $view = Views::getView('test_bulk_form');
108
    $display = &$view->storage->getDisplay('default');
109
110
111
    $display['display_options']['fields']['node_bulk_form']['include_exclude'] = 'include';
    $display['display_options']['fields']['node_bulk_form']['selected_actions']['node_make_sticky_action'] = 'node_make_sticky_action';
    $display['display_options']['fields']['node_bulk_form']['selected_actions']['node_make_unsticky_action'] = 'node_make_unsticky_action';
112
113
114
    $view->save();

    $this->drupalGet('test_bulk_form');
115
    $options = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-action']);
116
117
118
119
120
    $this->assertEqual(count($options), 2);
    $this->assertOption('edit-action', 'node_make_sticky_action');
    $this->assertOption('edit-action', 'node_make_unsticky_action');

    // Set up to exclude the sticky actions.
121
    $view = Views::getView('test_bulk_form');
122
    $display = &$view->storage->getDisplay('default');
123
    $display['display_options']['fields']['node_bulk_form']['include_exclude'] = 'exclude';
124
125
126
127
128
    $view->save();

    $this->drupalGet('test_bulk_form');
    $this->assertNoOption('edit-action', 'node_make_sticky_action');
    $this->assertNoOption('edit-action', 'node_make_unsticky_action');
129
130
131
132

    // Check the default title.
    $this->drupalGet('test_bulk_form');
    $result = $this->xpath('//label[@for="edit-action"]');
133
    $this->assertEqual('Action', $result[0]->getText());
134
135

    // Setup up a different bulk form title.
136
    $view = Views::getView('test_bulk_form');
137
    $display = &$view->storage->getDisplay('default');
138
    $display['display_options']['fields']['node_bulk_form']['action_title'] = 'Test title';
139
140
141
142
    $view->save();

    $this->drupalGet('test_bulk_form');
    $result = $this->xpath('//label[@for="edit-action"]');
143
    $this->assertEqual('Test title', $result[0]->getText());
144
145
146

    $this->drupalGet('test_bulk_form');
    // Call the node delete action.
147
    $edit = [];
148
149
150
    for ($i = 0; $i < 5; $i++) {
      $edit["node_bulk_form[$i]"] = TRUE;
    }
151
    $edit += ['action' => 'node_delete_action'];
152
    $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
153
154
155
    // Make sure we don't show an action message while we are still on the
    // confirmation page.
    $errors = $this->xpath('//div[contains(@class, "messages--status")]');
156
    $this->assertEmpty($errors, 'No action message shown.');
157
    $this->drupalPostForm(NULL, [], t('Delete'));
158
    $this->assertText(t('Deleted 5 content items.'));
159
160
    // Check if we got redirected to the original page.
    $this->assertUrl('test_bulk_form');
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

    // Test that the bulk form works when a node gets deleted by another user
    // before the loaded bulk form can be used.
    $this->drupalGet('test_bulk_form');
    // Now delete the node we want to delete with the bulk form.
    $link = $this->getSession()->getPage()->findLink($nodes[6]->label());
    $checkbox = $link->getParent()->getParent()->find('css', 'input');
    $nodes[6]->delete();
    $edit = [
      $checkbox->getAttribute('name') => TRUE,
      'action' => 'node_delete_action',
    ];
    $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
    // Make sure we just return to the bulk view with no warnings.
    $this->assertUrl('test_bulk_form');
    $errors = $this->xpath('//div[contains(@class, "messages--status")]');
    $this->assertEmpty($errors, 'No action message shown.');

    // Test that the bulk form works when multiple nodes are selected
    // but one of the selected nodes are already deleted by another user before
    // the loaded bulk form was submitted.
    $this->drupalGet('test_bulk_form');
    // Call the node delete action.
    $nodes[7]->delete();
    $edit = [
      'node_bulk_form[0]' => TRUE,
      'node_bulk_form[1]' => TRUE,
      'action' => 'node_delete_action',
    ];
    $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
    // Make sure we don't show an action message while we are still on the
    // confirmation page.
    $errors = $this->xpath('//div[contains(@class, "messages--status")]');
    $this->assertEmpty($errors, 'No action message shown.');
    $this->drupalPostForm(NULL, [], t('Delete'));
    $this->assertText(t('Deleted 1 content item.'));

    // Test that the bulk form works when multiple nodes are selected
    // but all of the selected nodes are already deleted
    //  by another user before the loaded bulk form was submitted.
    $this->drupalGet('test_bulk_form');
    // Call the node delete action.
    foreach ($nodes as $key => $node) {
      $node->delete();
    }
    $edit = [
      'node_bulk_form[0]' => TRUE,
      'action' => 'node_delete_action',
    ];
    $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
    $this->assertText('No content selected.');
212
213
214
  }

}