DeleteMultipleFormTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 function setUp() {
  32. parent::setUp();
  33. EntityTestBundle::create([
  34. 'id' => 'default',
  35. 'label' => 'Default',
  36. ])->save();
  37. $this->account = $this->drupalCreateUser(['administer entity_test content']);
  38. $this->drupalLogin($this->account);
  39. }
  40. /**
  41. * Tests the delete form for translatable entities.
  42. */
  43. public function testTranslatableEntities() {
  44. ConfigurableLanguage::create(['id' => 'es'])->save();
  45. ConfigurableLanguage::create(['id' => 'fr'])->save();
  46. $selection = [];
  47. $entity1 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity1']);
  48. $entity1->addTranslation('es', ['name' => 'entity1 spanish']);
  49. $entity1->addTranslation('fr', ['name' => 'entity1 french']);
  50. $entity1->save();
  51. $selection[$entity1->id()]['en'] = 'en';
  52. $entity2 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity2']);
  53. $entity2->addTranslation('es', ['name' => 'entity2 spanish']);
  54. $entity2->addTranslation('fr', ['name' => 'entity2 french']);
  55. $entity2->save();
  56. $selection[$entity2->id()]['es'] = 'es';
  57. $selection[$entity2->id()]['fr'] = 'fr';
  58. $entity3 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'entity3']);
  59. $entity3->addTranslation('es', ['name' => 'entity3 spanish']);
  60. $entity3->addTranslation('fr', ['name' => 'entity3 french']);
  61. $entity3->save();
  62. $selection[$entity3->id()]['fr'] = 'fr';
  63. // This entity will be inaccessible because of
  64. // Drupal\entity_test\EntityTestAccessControlHandler.
  65. $entity4 = EntityTestMulRevPub::create(['type' => 'default', 'name' => 'forbid_access']);
  66. $entity4->save();
  67. $selection[$entity4->id()]['en'] = 'en';
  68. // Add the selection to the tempstore just like DeleteAction would.
  69. $tempstore = \Drupal::service('tempstore.private')->get('entity_delete_multiple_confirm');
  70. $tempstore->set($this->account->id() . ':entity_test_mulrevpub', $selection);
  71. $this->drupalGet('/entity_test/delete');
  72. $assert = $this->assertSession();
  73. $assert->statusCodeEquals(200);
  74. $assert->elementTextContains('css', '.page-title', 'Are you sure you want to delete these test entity - revisions, data table, and published interface entities?');
  75. $list_selector = '#entity-test-mulrevpub-delete-multiple-confirm-form > div.item-list > ul';
  76. $assert->elementTextContains('css', $list_selector, 'entity1 (Original translation) - The following test entity - revisions, data table, and published interface translations will be deleted:');
  77. $assert->elementTextContains('css', $list_selector, 'entity2 spanish');
  78. $assert->elementTextContains('css', $list_selector, 'entity2 french');
  79. $assert->elementTextNotContains('css', $list_selector, 'entity3 spanish');
  80. $assert->elementTextContains('css', $list_selector, 'entity3 french');
  81. $delete_button = $this->getSession()->getPage()->findButton('Delete');
  82. $delete_button->click();
  83. $assert = $this->assertSession();
  84. $assert->addressEquals('/user/' . $this->account->id());
  85. $assert->responseContains('Deleted 6 items.');
  86. $assert->responseContains('1 item has not been deleted because you do not have the necessary permissions.');
  87. \Drupal::entityTypeManager()->getStorage('entity_test_mulrevpub')->resetCache();
  88. $remaining_entities = EntityTestMulRevPub::loadMultiple([$entity1->id(), $entity2->id(), $entity3->id(), $entity4->id()]);
  89. $this->assertCount(3, $remaining_entities);
  90. }
  91. /**
  92. * Tests the delete form for untranslatable entities.
  93. */
  94. public function testUntranslatableEntities() {
  95. $selection = [];
  96. $entity1 = EntityTestRev::create(['type' => 'default', 'name' => 'entity1']);
  97. $entity1->save();
  98. $selection[$entity1->id()]['en'] = 'en';
  99. $entity2 = EntityTestRev::create(['type' => 'default', 'name' => 'entity2']);
  100. $entity2->save();
  101. $selection[$entity2->id()]['en'] = 'en';
  102. // This entity will be inaccessible because of
  103. // Drupal\entity_test\EntityTestAccessControlHandler.
  104. $entity3 = EntityTestRev::create(['type' => 'default', 'name' => 'forbid_access']);
  105. $entity3->save();
  106. $selection[$entity3->id()]['en'] = 'en';
  107. // This entity will be inaccessible because of
  108. // Drupal\entity_test\EntityTestAccessControlHandler.
  109. $entity4 = EntityTestRev::create(['type' => 'default', 'name' => 'forbid_access']);
  110. $entity4->save();
  111. $selection[$entity4->id()]['en'] = 'en';
  112. // Add the selection to the tempstore just like DeleteAction would.
  113. $tempstore = \Drupal::service('tempstore.private')->get('entity_delete_multiple_confirm');
  114. $tempstore->set($this->account->id() . ':entity_test_rev', $selection);
  115. $this->drupalGet('/entity_test_rev/delete_multiple');
  116. $assert = $this->assertSession();
  117. $assert->statusCodeEquals(200);
  118. $assert->elementTextContains('css', '.page-title', 'Are you sure you want to delete these test entity - revisions entities?');
  119. $list_selector = '#entity-test-rev-delete-multiple-confirm-form > div.item-list > ul';
  120. $assert->elementTextContains('css', $list_selector, 'entity1');
  121. $assert->elementTextContains('css', $list_selector, 'entity2');
  122. $delete_button = $this->getSession()->getPage()->findButton('Delete');
  123. $delete_button->click();
  124. $assert = $this->assertSession();
  125. $assert->addressEquals('/user/' . $this->account->id());
  126. $assert->responseContains('Deleted 2 items.');
  127. $assert->responseContains('2 items have not been deleted because you do not have the necessary permissions.');
  128. \Drupal::entityTypeManager()->getStorage('entity_test_mulrevpub')->resetCache();
  129. $remaining_entities = EntityTestRev::loadMultiple([$entity1->id(), $entity2->id(), $entity3->id(), $entity4->id()]);
  130. $this->assertCount(2, $remaining_entities);
  131. }
  132. }