Version 1
[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_bulk_form[0]' => TRUE, // Node 1, English, original.
174       'node_bulk_form[1]' => TRUE, // Node 1, British English.
175       'node_bulk_form[2]' => TRUE, // Node 1, Italian.
176       // Original and only one translation.
177       'node_bulk_form[3]' => TRUE, // Node 2, English.
178       'node_bulk_form[4]' => TRUE, // Node 2, British English, original.
179       'node_bulk_form[5]' => FALSE, // Node 2, Italian.
180       // Only a single translation.
181       'node_bulk_form[6]' => TRUE, // Node 3, English.
182       'node_bulk_form[7]' => FALSE, // Node 3, Italian, original.
183       // Only a single untranslated node.
184       'node_bulk_form[8]' => TRUE, // Node 4, English, untranslated.
185       'node_bulk_form[9]' => FALSE, // Node 5, British English, untranslated.
186       'action' => 'node_unpublish_action',
187     ];
188     $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
189     $node = $this->loadNode(1);
190     $this->assertFalse($node->getTranslation('en')->isPublished(), '1: English translation has been unpublished');
191     $this->assertFalse($node->getTranslation('en-gb')->isPublished(), '1: British English translation has been unpublished');
192     $this->assertFalse($node->getTranslation('it')->isPublished(), '1: Italian translation has been unpublished');
193     $node = $this->loadNode(2);
194     $this->assertFalse($node->getTranslation('en')->isPublished(), '2: English translation has been unpublished');
195     $this->assertFalse($node->getTranslation('en-gb')->isPublished(), '2: British English translation has been unpublished');
196     $this->assertTrue($node->getTranslation('it')->isPublished(), '2: Italian translation has not been unpublished');
197     $node = $this->loadNode(3);
198     $this->assertFalse($node->getTranslation('en')->isPublished(), '3: English translation has been unpublished');
199     $this->assertTrue($node->getTranslation('it')->isPublished(), '3: Italian translation has not been unpublished');
200     $node = $this->loadNode(4);
201     $this->assertFalse($node->isPublished(), '4: Node has been unpublished');
202     $node = $this->loadNode(5);
203     $this->assertTrue($node->isPublished(), '5: Node has not been unpublished');
204   }
205
206   /**
207    * Test multiple deletion.
208    */
209   public function testBulkDeletion() {
210     // Select a bunch of translated and untranslated nodes and check that
211     // nodes and individual translations are properly deleted.
212     $edit = [
213       // Original and all translations.
214       'node_bulk_form[0]' => TRUE, // Node 1, English, original.
215       'node_bulk_form[1]' => TRUE, // Node 1, British English.
216       'node_bulk_form[2]' => TRUE, // Node 1, Italian.
217       // Original and only one translation.
218       'node_bulk_form[3]' => TRUE, // Node 2, English.
219       'node_bulk_form[4]' => TRUE, // Node 2, British English, original.
220       'node_bulk_form[5]' => FALSE, // Node 2, Italian.
221       // Only a single translation.
222       'node_bulk_form[6]' => TRUE, // Node 3, English.
223       'node_bulk_form[7]' => FALSE, // Node 3, Italian, original.
224       // Only a single untranslated node.
225       'node_bulk_form[8]' => TRUE, // Node 4, English, untranslated.
226       'node_bulk_form[9]' => FALSE, // Node 5, British English, untranslated.
227       'action' => 'node_delete_action',
228     ];
229     $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
230
231     $label = $this->loadNode(1)->label();
232     $this->assertText("$label (Original translation) - The following content translations will be deleted:");
233     $label = $this->loadNode(2)->label();
234     $this->assertText("$label (Original translation) - The following content translations will be deleted:");
235     $label = $this->loadNode(3)->getTranslation('en')->label();
236     $this->assertText($label);
237     $this->assertNoText("$label (Original translation) - The following content translations will be deleted:");
238     $label = $this->loadNode(4)->label();
239     $this->assertText($label);
240     $this->assertNoText("$label (Original translation) - The following content translations will be deleted:");
241
242     $this->drupalPostForm(NULL, [], t('Delete'));
243
244     $node = $this->loadNode(1);
245     $this->assertNull($node, '1: Node has been deleted');
246     $node = $this->loadNode(2);
247     $this->assertNull($node, '2: Node has been deleted');
248     $node = $this->loadNode(3);
249     $result = count($node->getTranslationLanguages()) && $node->language()->getId() == 'it';
250     $this->assertTrue($result, '3: English translation has been deleted');
251     $node = $this->loadNode(4);
252     $this->assertNull($node, '4: Node has been deleted');
253     $node = $this->loadNode(5);
254     $this->assertTrue($node, '5: Node has not been deleted');
255
256     $this->assertText('Deleted 8 posts.');
257   }
258
259   /**
260    * Load the specified node from the storage.
261    *
262    * @param int $id
263    *   The node identifier.
264    *
265    * @return \Drupal\node\NodeInterface
266    *   The loaded node.
267    */
268   protected function loadNode($id) {
269     /** @var \Drupal\node\NodeStorage $storage */
270     $storage = $this->container->get('entity.manager')->getStorage('node');
271     $storage->resetCache([$id]);
272     return $storage->load($id);
273   }
274
275 }