3 namespace Drupal\FunctionalTests\Entity;
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;
12 * Tests the delete multiple confirmation form.
15 * @runTestsInSeparateProcesses
16 * @preserveGlobalState disabled
18 class DeleteMultipleFormTest extends BrowserTestBase {
23 * @var \Drupal\Core\Session\AccountInterface
32 public static $modules = ['entity_test', 'user', 'language'];
37 protected function setUp() {
40 EntityTestBundle::create([
44 $this->account = $this->drupalCreateUser(['administer entity_test content']);
45 $this->drupalLogin($this->account);
49 * Tests the delete form for translatable entities.
51 public function testTranslatableEntities() {
52 ConfigurableLanguage::create(['id' => 'es'])->save();
53 ConfigurableLanguage::create(['id' => 'fr'])->save();
57 $entity1 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity1']);
58 $entity1->addTranslation('es', ['name' => 'entity1 spanish']);
59 $entity1->addTranslation('fr', ['name' => 'entity1 french']);
61 $selection[$entity1->id()]['en'] = 'en';
63 $entity2 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity2']);
64 $entity2->addTranslation('es', ['name' => 'entity2 spanish']);
65 $entity2->addTranslation('fr', ['name' => 'entity2 french']);
67 $selection[$entity2->id()]['es'] = 'es';
68 $selection[$entity2->id()]['fr'] = 'fr';
70 $entity3 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity3']);
71 $entity3->addTranslation('es', ['name' => 'entity3 spanish']);
72 $entity3->addTranslation('fr', ['name' => 'entity3 french']);
74 $selection[$entity3->id()]['fr'] = 'fr';
76 // This entity will be inaccessible because of
77 // Drupal\entity_test\EntityTestAccessControlHandler.
78 $entity4 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'forbid_access']);
80 $selection[$entity4->id()]['en'] = 'en';
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);
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.');
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);
109 * Tests the delete form for untranslatable entities.
111 public function testUntranslatableEntities() {
114 $entity1 = EntityTestRev::create(['type' => 'default', 'name' => 'entity1']);
116 $selection[$entity1->id()]['en'] = 'en';
118 $entity2 = EntityTestRev::create(['type' => 'default', 'name' => 'entity2']);
120 $selection[$entity2->id()]['en'] = 'en';
122 // This entity will be inaccessible because of
123 // Drupal\entity_test\EntityTestAccessControlHandler.
124 $entity3 = EntityTestRev::create(['type' => 'default', 'name' => 'forbid_access']);
126 $selection[$entity3->id()]['en'] = 'en';
128 // This entity will be inaccessible because of
129 // Drupal\entity_test\EntityTestAccessControlHandler.
130 $entity4 = EntityTestRev::create(['type' => 'default', 'name' => 'forbid_access']);
132 $selection[$entity4->id()]['en'] = 'en';
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);
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.');
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);