3 namespace Drupal\Tests\node\Functional\Views;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\views\Views;
10 * Tests a node bulk form.
13 * @see \Drupal\node\Plugin\views\field\BulkForm
15 class BulkFormTest extends NodeTestBase {
18 * Modules to be enabled.
22 public static $modules = ['node_test_views', 'language'];
25 * Views used by this test.
29 public static $testViews = ['test_node_bulk_form'];
34 * @var \Drupal\node\NodeInterface[]
41 protected function setUp($import_test_views = TRUE) {
42 parent::setUp($import_test_views);
44 ConfigurableLanguage::createFromLangcode('en-gb')->save();
45 ConfigurableLanguage::createFromLangcode('it')->save();
47 // Create some test nodes.
49 $langcodes = ['en', 'en-gb', 'it'];
50 for ($i = 1; $i <= 5; $i++) {
51 $langcode = $langcodes[($i - 1) % 3];
53 'title' => $this->randomMachineName() . ' [' . $i . ':' . $langcode . ']',
54 'langcode' => $langcode,
57 $node = $this->drupalCreateNode($values);
58 $this->pass(SafeMarkup::format('Node %title created with language %langcode.', ['%title' => $node->label(), '%langcode' => $node->language()->getId()]));
59 $this->nodes[] = $node;
62 // Create translations for all languages for some nodes.
63 for ($i = 0; $i < 2; $i++) {
64 $node = $this->nodes[$i];
65 foreach ($langcodes as $langcode) {
66 if (!$node->hasTranslation($langcode)) {
67 $title = $this->randomMachineName() . ' [' . $node->id() . ':' . $langcode . ']';
68 $translation = $node->addTranslation($langcode, ['title' => $title, 'promote' => FALSE]);
69 $this->pass(SafeMarkup::format('Translation %title created with language %langcode.', ['%title' => $translation->label(), '%langcode' => $translation->language()->getId()]));
75 // Create a node with only one translation.
76 $node = $this->nodes[2];
78 $title = $this->randomMachineName() . ' [' . $node->id() . ':' . $langcode . ']';
79 $translation = $node->addTranslation($langcode, ['title' => $title]);
80 $this->pass(SafeMarkup::format('Translation %title created with language %langcode.', ['%title' => $translation->label(), '%langcode' => $translation->language()->getId()]));
83 // Check that all created translations are selected by the test view.
84 $view = Views::getView('test_node_bulk_form');
86 $this->assertEqual(count($view->result), 10, 'All created translations are selected.');
88 // Check the operations are accessible to the logged in user.
89 $this->drupalLogin($this->drupalCreateUser(['administer nodes', 'access content overview', 'bypass node access']));
90 $this->drupalGet('test-node-bulk-form');
91 $elements = $this->xpath('//select[@id="edit-action"]//option');
92 $this->assertIdentical(count($elements), 8, 'All node operations are found.');
96 * Tests the node bulk form.
98 public function testBulkForm() {
99 // Unpublish a node using the bulk form.
100 $node = reset($this->nodes);
101 $this->assertTrue($node->isPublished(), 'Node is initially published');
102 $this->assertTrue($node->getTranslation('en-gb')->isPublished(), 'Node translation is published');
103 $this->assertTrue($node->getTranslation('it')->isPublished(), 'Node translation is published');
105 'node_bulk_form[0]' => TRUE,
106 'action' => 'node_unpublish_action',
108 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
109 $node = $this->loadNode($node->id());
110 $this->assertFalse($node->isPublished(), 'Node has been unpublished');
111 $this->assertTrue($node->getTranslation('en-gb')->isPublished(), 'Node translation has not been unpublished');
112 $this->assertTrue($node->getTranslation('it')->isPublished(), 'Node translation has not been unpublished');
116 'node_bulk_form[0]' => TRUE,
117 'action' => 'node_publish_action',
119 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
120 $node = $this->loadNode($node->id());
121 $this->assertTrue($node->isPublished(), 'Node has been published again');
123 // Make sticky action.
124 $this->assertFalse($node->isSticky(), 'Node is not sticky');
125 $this->assertFalse($node->getTranslation('en-gb')->isSticky(), 'Node translation is not sticky');
126 $this->assertFalse($node->getTranslation('it')->isSticky(), 'Node translation is not sticky');
128 'node_bulk_form[0]' => TRUE,
129 'action' => 'node_make_sticky_action',
131 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
132 $node = $this->loadNode($node->id());
133 $this->assertTrue($node->isSticky(), 'Node has been made sticky');
134 $this->assertFalse($node->getTranslation('en-gb')->isSticky(), 'Node translation has not been made sticky');
135 $this->assertFalse($node->getTranslation('it')->isSticky(), 'Node translation has not been made sticky');
137 // Make unsticky action.
139 'node_bulk_form[0]' => TRUE,
140 'action' => 'node_make_unsticky_action',
142 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
143 $node = $this->loadNode($node->id());
144 $this->assertFalse($node->isSticky(), 'Node is not sticky anymore');
146 // Promote to front page.
147 $this->assertFalse($node->isPromoted(), 'Node is not promoted to the front page');
148 $this->assertFalse($node->getTranslation('en-gb')->isPromoted(), 'Node translation is not promoted to the front page');
149 $this->assertFalse($node->getTranslation('it')->isPromoted(), 'Node translation is not promoted to the front page');
151 'node_bulk_form[0]' => TRUE,
152 'action' => 'node_promote_action',
154 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
155 $node = $this->loadNode($node->id());
156 $this->assertTrue($node->isPromoted(), 'Node has been promoted to the front page');
157 $this->assertFalse($node->getTranslation('en-gb')->isPromoted(), 'Node translation has not been promoted to the front page');
158 $this->assertFalse($node->getTranslation('it')->isPromoted(), 'Node translation has not been promoted to the front page');
160 // Demote from front page.
162 'node_bulk_form[0]' => TRUE,
163 'action' => 'node_unpromote_action',
165 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
166 $node = $this->loadNode($node->id());
167 $this->assertFalse($node->isPromoted(), 'Node has been demoted');
169 // Select a bunch of translated and untranslated nodes and check that
170 // operations are always applied to individual translations.
172 // Original and all translations.
173 // Node 1, English, original.
174 'node_bulk_form[0]' => TRUE,
175 // Node 1, British English.
176 'node_bulk_form[1]' => TRUE,
178 'node_bulk_form[2]' => TRUE,
179 // Original and only one translation.
181 'node_bulk_form[3]' => TRUE,
182 // Node 2, British English, original.
183 'node_bulk_form[4]' => TRUE,
185 'node_bulk_form[5]' => FALSE,
186 // Only a single translation.
188 'node_bulk_form[6]' => TRUE,
189 // Node 3, Italian, original.
190 'node_bulk_form[7]' => FALSE,
191 // Only a single untranslated node.
192 // Node 4, English, untranslated.
193 'node_bulk_form[8]' => TRUE,
194 // Node 5, British English, untranslated.
195 'node_bulk_form[9]' => FALSE,
196 'action' => 'node_unpublish_action',
198 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
199 $node = $this->loadNode(1);
200 $this->assertFalse($node->getTranslation('en')->isPublished(), '1: English translation has been unpublished');
201 $this->assertFalse($node->getTranslation('en-gb')->isPublished(), '1: British English translation has been unpublished');
202 $this->assertFalse($node->getTranslation('it')->isPublished(), '1: Italian translation has been unpublished');
203 $node = $this->loadNode(2);
204 $this->assertFalse($node->getTranslation('en')->isPublished(), '2: English translation has been unpublished');
205 $this->assertFalse($node->getTranslation('en-gb')->isPublished(), '2: British English translation has been unpublished');
206 $this->assertTrue($node->getTranslation('it')->isPublished(), '2: Italian translation has not been unpublished');
207 $node = $this->loadNode(3);
208 $this->assertFalse($node->getTranslation('en')->isPublished(), '3: English translation has been unpublished');
209 $this->assertTrue($node->getTranslation('it')->isPublished(), '3: Italian translation has not been unpublished');
210 $node = $this->loadNode(4);
211 $this->assertFalse($node->isPublished(), '4: Node has been unpublished');
212 $node = $this->loadNode(5);
213 $this->assertTrue($node->isPublished(), '5: Node has not been unpublished');
217 * Test multiple deletion.
219 public function testBulkDeletion() {
220 // Select a bunch of translated and untranslated nodes and check that
221 // nodes and individual translations are properly deleted.
223 // Original and all translations.
224 // Node 1, English, original.
225 'node_bulk_form[0]' => TRUE,
226 // Node 1, British English.
227 'node_bulk_form[1]' => TRUE,
229 'node_bulk_form[2]' => TRUE,
230 // Original and only one translation.
232 'node_bulk_form[3]' => TRUE,
233 // Node 2, British English, original.
234 'node_bulk_form[4]' => TRUE,
236 'node_bulk_form[5]' => FALSE,
237 // Only a single translation.
239 'node_bulk_form[6]' => TRUE,
240 // Node 3, Italian, original.
241 'node_bulk_form[7]' => FALSE,
242 // Only a single untranslated node.
243 // Node 4, English, untranslated.
244 'node_bulk_form[8]' => TRUE,
245 // Node 5, British English, untranslated.
246 'node_bulk_form[9]' => FALSE,
247 'action' => 'node_delete_action',
249 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
251 $label = $this->loadNode(1)->label();
252 $this->assertText("$label (Original translation) - The following content translations will be deleted:");
253 $label = $this->loadNode(2)->label();
254 $this->assertText("$label (Original translation) - The following content translations will be deleted:");
255 $label = $this->loadNode(3)->getTranslation('en')->label();
256 $this->assertText($label);
257 $this->assertNoText("$label (Original translation) - The following content translations will be deleted:");
258 $label = $this->loadNode(4)->label();
259 $this->assertText($label);
260 $this->assertNoText("$label (Original translation) - The following content translations will be deleted:");
262 $this->drupalPostForm(NULL, [], t('Delete'));
264 $node = $this->loadNode(1);
265 $this->assertNull($node, '1: Node has been deleted');
266 $node = $this->loadNode(2);
267 $this->assertNull($node, '2: Node has been deleted');
268 $node = $this->loadNode(3);
269 $result = count($node->getTranslationLanguages()) && $node->language()->getId() == 'it';
270 $this->assertTrue($result, '3: English translation has been deleted');
271 $node = $this->loadNode(4);
272 $this->assertNull($node, '4: Node has been deleted');
273 $node = $this->loadNode(5);
274 $this->assertTrue($node, '5: Node has not been deleted');
276 $this->assertText('Deleted 8 posts.');
280 * Load the specified node from the storage.
283 * The node identifier.
285 * @return \Drupal\node\NodeInterface
288 protected function loadNode($id) {
289 /** @var \Drupal\node\NodeStorage $storage */
290 $storage = $this->container->get('entity.manager')->getStorage('node');
291 $storage->resetCache([$id]);
292 return $storage->load($id);