45f88d680e26a1d85553e5046f5a7d6ec378e840
[yaffs-website] / web / core / tests / Drupal / FunctionalTests / Entity / DeleteMultipleFormTest.php
1 <?php
2
3 namespace Drupal\FunctionalTests\Entity;
4
5 use Drupal\entity_test\Entity\EntityTestBundle;
6 use Drupal\entity_test\Entity\EntityTestMulRevPub;
7 use Drupal\entity_test\Entity\EntityTestRev;
8 use Drupal\language\Entity\ConfigurableLanguage;
9 use Drupal\Tests\BrowserTestBase;
10
11 /**
12  * Tests the delete multiple confirmation form.
13  *
14  * @group Entity
15  * @runTestsInSeparateProcesses
16  * @preserveGlobalState disabled
17  */
18 class DeleteMultipleFormTest extends BrowserTestBase {
19
20   /**
21    * The current user.
22    *
23    * @var \Drupal\Core\Session\AccountInterface
24    */
25   protected $account;
26
27   /**
28    * Modules to enable.
29    *
30    * @var array
31    */
32   public static $modules = ['entity_test', 'user', 'language'];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
40     EntityTestBundle::create([
41       'id' => 'default',
42       'label' => 'Default',
43     ])->save();
44     $this->account = $this->drupalCreateUser(['administer entity_test content']);
45     $this->drupalLogin($this->account);
46   }
47
48   /**
49    * Tests the delete form for translatable entities.
50    */
51   public function testTranslatableEntities() {
52     ConfigurableLanguage::create(['id' => 'es'])->save();
53     ConfigurableLanguage::create(['id' => 'fr'])->save();
54
55     $selection = [];
56
57     $entity1 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity1']);
58     $entity1->addTranslation('es', ['name' => 'entity1 spanish']);
59     $entity1->addTranslation('fr', ['name' => 'entity1 french']);
60     $entity1->save();
61     $selection[$entity1->id()]['en'] = 'en';
62
63     $entity2 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity2']);
64     $entity2->addTranslation('es', ['name' => 'entity2 spanish']);
65     $entity2->addTranslation('fr', ['name' => 'entity2 french']);
66     $entity2->save();
67     $selection[$entity2->id()]['es'] = 'es';
68     $selection[$entity2->id()]['fr'] = 'fr';
69
70     $entity3 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity3']);
71     $entity3->addTranslation('es', ['name' => 'entity3 spanish']);
72     $entity3->addTranslation('fr', ['name' => 'entity3 french']);
73     $entity3->save();
74     $selection[$entity3->id()]['fr'] = 'fr';
75
76     // This entity will be inaccessible because of
77     // Drupal\entity_test\EntityTestAccessControlHandler.
78     $entity4 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'forbid_access']);
79     $entity4->save();
80     $selection[$entity4->id()]['en'] = 'en';
81
82     // Add the selection to the tempstore just like DeleteAction would.
83     $tempstore = \Drupal::service('tempstore.private')->get('entity_delete_multiple_confirm');
84     $tempstore->set($this->account->id() . ':entity_test_mulrevpub', $selection);
85
86     $this->drupalGet('/entity_test/delete');
87     $assert = $this->assertSession();
88     $assert->statusCodeEquals(200);
89     $assert->elementTextContains('css', '.page-title', 'Are you sure you want to delete these test entity - revisions, data table, and published interface entities?');
90     $list_selector = '#entity-test-mulrevpub-delete-multiple-confirm-form > div.item-list > ul';
91     $assert->elementTextContains('css', $list_selector, 'entity1 (Original translation) - The following test entity - revisions, data table, and published interface translations will be deleted:');
92     $assert->elementTextContains('css', $list_selector, 'entity2 spanish');
93     $assert->elementTextContains('css', $list_selector, 'entity2 french');
94     $assert->elementTextNotContains('css', $list_selector, 'entity3 spanish');
95     $assert->elementTextContains('css', $list_selector, 'entity3 french');
96     $delete_button = $this->getSession()->getPage()->findButton('Delete');
97     $delete_button->click();
98     $assert = $this->assertSession();
99     $assert->addressEquals('/user/' . $this->account->id());
100     $assert->responseContains('Deleted 6 items.');
101     $assert->responseContains('1 item has not been deleted because you do not have the necessary permissions.');
102
103     \Drupal::entityTypeManager()->getStorage('entity_test_mulrevpub')->resetCache();
104     $remaining_entities = EntityTestMulRevPub::loadMultiple([$entity1->id(), $entity2->id(), $entity3->id(), $entity4->id()]);
105     $this->assertCount(3, $remaining_entities);
106   }
107
108   /**
109    * Tests the delete form for untranslatable entities.
110    */
111   public function testUntranslatableEntities() {
112     $selection = [];
113
114     $entity1 = EntityTestRev::create(['type' => 'default', 'name' => 'entity1']);
115     $entity1->save();
116     $selection[$entity1->id()]['en'] = 'en';
117
118     $entity2 = EntityTestRev::create(['type' => 'default', 'name' => 'entity2']);
119     $entity2->save();
120     $selection[$entity2->id()]['en'] = 'en';
121
122     // This entity will be inaccessible because of
123     // Drupal\entity_test\EntityTestAccessControlHandler.
124     $entity3 = EntityTestRev::create(['type' => 'default', 'name' => 'forbid_access']);
125     $entity3->save();
126     $selection[$entity3->id()]['en'] = 'en';
127
128     // This entity will be inaccessible because of
129     // Drupal\entity_test\EntityTestAccessControlHandler.
130     $entity4 = EntityTestRev::create(['type' => 'default', 'name' => 'forbid_access']);
131     $entity4->save();
132     $selection[$entity4->id()]['en'] = 'en';
133
134     // Add the selection to the tempstore just like DeleteAction would.
135     $tempstore = \Drupal::service('tempstore.private')->get('entity_delete_multiple_confirm');
136     $tempstore->set($this->account->id() . ':entity_test_rev', $selection);
137
138     $this->drupalGet('/entity_test_rev/delete_multiple');
139     $assert = $this->assertSession();
140     $assert->statusCodeEquals(200);
141     $assert->elementTextContains('css', '.page-title', 'Are you sure you want to delete these test entity - revisions entities?');
142     $list_selector = '#entity-test-rev-delete-multiple-confirm-form > div.item-list > ul';
143     $assert->elementTextContains('css', $list_selector, 'entity1');
144     $assert->elementTextContains('css', $list_selector, 'entity2');
145     $delete_button = $this->getSession()->getPage()->findButton('Delete');
146     $delete_button->click();
147     $assert = $this->assertSession();
148     $assert->addressEquals('/user/' . $this->account->id());
149     $assert->responseContains('Deleted 2 items.');
150     $assert->responseContains('2 items have not been deleted because you do not have the necessary permissions.');
151
152     \Drupal::entityTypeManager()->getStorage('entity_test_mulrevpub')->resetCache();
153     $remaining_entities = EntityTestRev::loadMultiple([$entity1->id(), $entity2->id(), $entity3->id(), $entity4->id()]);
154     $this->assertCount(2, $remaining_entities);
155   }
156
157 }