Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / node / tests / src / Functional / Views / BulkFormTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional\Views;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\views\Views;
8
9 /**
10  * Tests a node bulk form.
11  *
12  * @group node
13  * @see \Drupal\node\Plugin\views\field\BulkForm
14  */
15 class BulkFormTest extends NodeTestBase {
16
17   /**
18    * Modules to be enabled.
19    *
20    * @var array
21    */
22   public static $modules = ['node_test_views', 'language'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_node_bulk_form'];
30
31   /**
32    * The test nodes.
33    *
34    * @var \Drupal\node\NodeInterface[]
35    */
36   protected $nodes;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp($import_test_views = TRUE) {
42     parent::setUp($import_test_views);
43
44     ConfigurableLanguage::createFromLangcode('en-gb')->save();
45     ConfigurableLanguage::createFromLangcode('it')->save();
46
47     // Create some test nodes.
48     $this->nodes = [];
49     $langcodes = ['en', 'en-gb', 'it'];
50     for ($i = 1; $i <= 5; $i++) {
51       $langcode = $langcodes[($i - 1) % 3];
52       $values = [
53         'title' => $this->randomMachineName() . ' [' . $i . ':' . $langcode . ']',
54         'langcode' => $langcode,
55         'promote' => FALSE,
56       ];
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;
60     }
61
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()]));
70         }
71       }
72       $node->save();
73     }
74
75     // Create a node with only one translation.
76     $node = $this->nodes[2];
77     $langcode = 'en';
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()]));
81     $node->save();
82
83     // Check that all created translations are selected by the test view.
84     $view = Views::getView('test_node_bulk_form');
85     $view->execute();
86     $this->assertEqual(count($view->result), 10, 'All created translations are selected.');
87
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.');
93   }
94
95   /**
96    * Tests the node bulk form.
97    */
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');
104     $edit = [
105       'node_bulk_form[0]' => TRUE,
106       'action' => 'node_unpublish_action',
107     ];
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');
113
114     // Publish action.
115     $edit = [
116       'node_bulk_form[0]' => TRUE,
117       'action' => 'node_publish_action',
118     ];
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');
122
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');
127     $edit = [
128       'node_bulk_form[0]' => TRUE,
129       'action' => 'node_make_sticky_action',
130     ];
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');
136
137     // Make unsticky action.
138     $edit = [
139       'node_bulk_form[0]' => TRUE,
140       'action' => 'node_make_unsticky_action',
141     ];
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');
145
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');
150     $edit = [
151       'node_bulk_form[0]' => TRUE,
152       'action' => 'node_promote_action',
153     ];
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');
159
160     // Demote from front page.
161     $edit = [
162       'node_bulk_form[0]' => TRUE,
163       'action' => 'node_unpromote_action',
164     ];
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');
168
169     // Select a bunch of translated and untranslated nodes and check that
170     // operations are always applied to individual translations.
171     $edit = [
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,
177       // Node 1, Italian.
178       'node_bulk_form[2]' => TRUE,
179       // Original and only one translation.
180       // Node 2, English.
181       'node_bulk_form[3]' => TRUE,
182       // Node 2, British English, original.
183       'node_bulk_form[4]' => TRUE,
184       // Node 2, Italian.
185       'node_bulk_form[5]' => FALSE,
186       // Only a single translation.
187       // Node 3, English.
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',
197     ];
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');
214   }
215
216   /**
217    * Test multiple deletion.
218    */
219   public function testBulkDeletion() {
220     // Select a bunch of translated and untranslated nodes and check that
221     // nodes and individual translations are properly deleted.
222     $edit = [
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,
228       // Node 1, Italian.
229       'node_bulk_form[2]' => TRUE,
230       // Original and only one translation.
231       // Node 2, English.
232       'node_bulk_form[3]' => TRUE,
233       // Node 2, British English, original.
234       'node_bulk_form[4]' => TRUE,
235       // Node 2, Italian.
236       'node_bulk_form[5]' => FALSE,
237       // Only a single translation.
238       // Node 3, English.
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',
248     ];
249     $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
250
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:");
261
262     $this->drupalPostForm(NULL, [], t('Delete'));
263
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');
275
276     $this->assertText('Deleted 8 posts.');
277   }
278
279   /**
280    * Load the specified node from the storage.
281    *
282    * @param int $id
283    *   The node identifier.
284    *
285    * @return \Drupal\node\NodeInterface
286    *   The loaded node.
287    */
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);
293   }
294
295 }