439ffe4d6d46ea5455ca159b4c7b797ca7aff6b3
[yaffs-website] / web / core / modules / action / tests / src / Functional / BulkFormTest.php
1 <?php
2
3 namespace Drupal\Tests\action\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the views bulk form test.
10  *
11  * @group action
12  * @see \Drupal\action\Plugin\views\field\BulkForm
13  */
14 class BulkFormTest extends BrowserTestBase {
15
16   /**
17    * Modules to install.
18    *
19    * @var array
20    */
21   public static $modules = ['node', 'action_bulk_test'];
22
23   /**
24    * Tests the bulk form.
25    */
26   public function testBulkForm() {
27     $node_storage = $this->container->get('entity.manager')->getStorage('node');
28
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.');
33
34     $nodes = [];
35     for ($i = 0; $i < 10; $i++) {
36       // Ensure nodes are sorted in the same order they are inserted in the
37       // array.
38       $timestamp = REQUEST_TIME - $i;
39       $nodes[] = $this->drupalCreateNode([
40         'sticky' => FALSE,
41         'created' => $timestamp,
42         'changed' => $timestamp,
43       ]);
44     }
45
46     $this->drupalGet('test_bulk_form');
47
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.');
51
52     $this->assertFieldById('edit-action', NULL, 'The action select field appears.');
53
54     // Make sure a checkbox appears on all rows.
55     $edit = [];
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;
59     }
60
61     // Log in as a user with 'administer nodes' permission to have access to the
62     // bulk operation.
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);
66
67     $this->drupalGet('test_bulk_form');
68
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'));
72
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()]));
76     }
77
78     $this->assertText('Make content sticky was applied to 10 items.');
79
80     // Unpublish just one node.
81     $node = $node_storage->load($nodes[0]->id());
82     $this->assertTrue($node->isPublished(), 'The node is published.');
83
84     $edit = ['node_bulk_form[0]' => TRUE, 'action' => 'node_unpublish_action'];
85     $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
86
87     $this->assertText('Unpublish content was applied to 1 item.');
88
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.');
93
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.');
98
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';
105     $view->save();
106
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');
112
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';
117     $view->save();
118
119     $this->drupalGet('test_bulk_form');
120     $this->assertNoOption('edit-action', 'node_make_sticky_action');
121     $this->assertNoOption('edit-action', 'node_make_unsticky_action');
122
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());
127
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';
132     $view->save();
133
134     $this->drupalGet('test_bulk_form');
135     $result = $this->xpath('//label[@for="edit-action"]');
136     $this->assertEqual('Test title', $result[0]->getText());
137
138     $this->drupalGet('test_bulk_form');
139     // Call the node delete action.
140     $edit = [];
141     for ($i = 0; $i < 5; $i++) {
142       $edit["node_bulk_form[$i]"] = TRUE;
143     }
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');
154   }
155
156 }