3 namespace Drupal\Tests\action\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\views\Views;
9 * Tests the views bulk form test.
12 * @see \Drupal\action\Plugin\views\field\BulkForm
14 class BulkFormTest extends BrowserTestBase {
21 public static $modules = ['node', 'action_bulk_test'];
24 * Tests the bulk form.
26 public function testBulkForm() {
27 $node_storage = $this->container->get('entity.manager')->getStorage('node');
29 // First, test an empty bulk form with the default style plugin to make sure
30 // the empty region is rendered correctly.
31 $this->drupalGet('test_bulk_form_empty');
32 $this->assertText(t('This view is empty.'), 'Empty text found on empty bulk form.');
35 for ($i = 0; $i < 10; $i++) {
36 // Ensure nodes are sorted in the same order they are inserted in the
38 $timestamp = REQUEST_TIME - $i;
39 $nodes[] = $this->drupalCreateNode([
41 'created' => $timestamp,
42 'changed' => $timestamp,
46 $this->drupalGet('test_bulk_form');
48 // Test that the views edit header appears first.
49 $first_form_element = $this->xpath('//form/div[1][@id = :id]', [':id' => 'edit-header']);
50 $this->assertTrue($first_form_element, 'The views form edit header appears first.');
52 $this->assertFieldById('edit-action', NULL, 'The action select field appears.');
54 // Make sure a checkbox appears on all rows.
56 for ($i = 0; $i < 10; $i++) {
57 $this->assertFieldById('edit-node-bulk-form-' . $i, NULL, format_string('The checkbox on row @row appears.', ['@row' => $i]));
58 $edit["node_bulk_form[$i]"] = TRUE;
61 // Log in as a user with 'administer nodes' permission to have access to the
63 $this->drupalCreateContentType(['type' => 'page']);
64 $admin_user = $this->drupalCreateUser(['administer nodes', 'edit any page content', 'delete any page content']);
65 $this->drupalLogin($admin_user);
67 $this->drupalGet('test_bulk_form');
69 // Set all nodes to sticky and check that.
70 $edit += ['action' => 'node_make_sticky_action'];
71 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
73 foreach ($nodes as $node) {
74 $changed_node = $node_storage->load($node->id());
75 $this->assertTrue($changed_node->isSticky(), format_string('Node @nid got marked as sticky.', ['@nid' => $node->id()]));
78 $this->assertText('Make content sticky was applied to 10 items.');
80 // Unpublish just one node.
81 $node = $node_storage->load($nodes[0]->id());
82 $this->assertTrue($node->isPublished(), 'The node is published.');
84 $edit = ['node_bulk_form[0]' => TRUE, 'action' => 'node_unpublish_action'];
85 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
87 $this->assertText('Unpublish content was applied to 1 item.');
89 // Load the node again.
90 $node_storage->resetCache([$node->id()]);
91 $node = $node_storage->load($node->id());
92 $this->assertFalse($node->isPublished(), 'A single node has been unpublished.');
94 // The second node should still be published.
95 $node_storage->resetCache([$nodes[1]->id()]);
96 $node = $node_storage->load($nodes[1]->id());
97 $this->assertTrue($node->isPublished(), 'An unchecked node is still published.');
99 // Set up to include just the sticky actions.
100 $view = Views::getView('test_bulk_form');
101 $display = &$view->storage->getDisplay('default');
102 $display['display_options']['fields']['node_bulk_form']['include_exclude'] = 'include';
103 $display['display_options']['fields']['node_bulk_form']['selected_actions']['node_make_sticky_action'] = 'node_make_sticky_action';
104 $display['display_options']['fields']['node_bulk_form']['selected_actions']['node_make_unsticky_action'] = 'node_make_unsticky_action';
107 $this->drupalGet('test_bulk_form');
108 $options = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-action']);
109 $this->assertEqual(count($options), 2);
110 $this->assertOption('edit-action', 'node_make_sticky_action');
111 $this->assertOption('edit-action', 'node_make_unsticky_action');
113 // Set up to exclude the sticky actions.
114 $view = Views::getView('test_bulk_form');
115 $display = &$view->storage->getDisplay('default');
116 $display['display_options']['fields']['node_bulk_form']['include_exclude'] = 'exclude';
119 $this->drupalGet('test_bulk_form');
120 $this->assertNoOption('edit-action', 'node_make_sticky_action');
121 $this->assertNoOption('edit-action', 'node_make_unsticky_action');
123 // Check the default title.
124 $this->drupalGet('test_bulk_form');
125 $result = $this->xpath('//label[@for="edit-action"]');
126 $this->assertEqual('Action', $result[0]->getText());
128 // Setup up a different bulk form title.
129 $view = Views::getView('test_bulk_form');
130 $display = &$view->storage->getDisplay('default');
131 $display['display_options']['fields']['node_bulk_form']['action_title'] = 'Test title';
134 $this->drupalGet('test_bulk_form');
135 $result = $this->xpath('//label[@for="edit-action"]');
136 $this->assertEqual('Test title', $result[0]->getText());
138 $this->drupalGet('test_bulk_form');
139 // Call the node delete action.
141 for ($i = 0; $i < 5; $i++) {
142 $edit["node_bulk_form[$i]"] = TRUE;
144 $edit += ['action' => 'node_delete_action'];
145 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
146 // Make sure we don't show an action message while we are still on the
147 // confirmation page.
148 $errors = $this->xpath('//div[contains(@class, "messages--status")]');
149 $this->assertFalse($errors, 'No action message shown.');
150 $this->drupalPostForm(NULL, [], t('Delete'));
151 $this->assertText(t('Deleted 5 posts.'));
152 // Check if we got redirected to the original page.
153 $this->assertUrl('test_bulk_form');