DeleteMultipleFormTest.php 6.5 KB

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