DeleteActionTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Drupal\KernelTests\Core\Action;
  3. use Drupal\Core\Action\Plugin\Action\Derivative\EntityDeleteActionDeriver;
  4. use Drupal\entity_test\Entity\EntityTestMulRevPub;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Drupal\system\Entity\Action;
  7. use Drupal\user\Entity\User;
  8. /**
  9. * @group Action
  10. */
  11. class DeleteActionTest extends KernelTestBase {
  12. protected $testUser;
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public static $modules = ['system', 'entity_test', 'user'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function setUp() {
  21. parent::setUp();
  22. $this->installEntitySchema('entity_test_mulrevpub');
  23. $this->installEntitySchema('user');
  24. $this->installSchema('system', ['sequences', 'key_value_expire']);
  25. $this->testUser = User::create([
  26. 'name' => 'foobar',
  27. 'mail' => 'foobar@example.com',
  28. ]);
  29. $this->testUser->save();
  30. \Drupal::service('current_user')->setAccount($this->testUser);
  31. }
  32. /**
  33. * @covers \Drupal\Core\Action\Plugin\Action\Derivative\EntityDeleteActionDeriver::getDerivativeDefinitions
  34. */
  35. public function testGetDerivativeDefinitions() {
  36. $deriver = new EntityDeleteActionDeriver(\Drupal::entityTypeManager(), \Drupal::translation());
  37. $this->assertEquals([
  38. 'entity_test_mulrevpub' => [
  39. 'type' => 'entity_test_mulrevpub',
  40. 'label' => 'Delete test entity - revisions, data table, and published interface',
  41. 'action_label' => 'Delete',
  42. 'confirm_form_route_name' => 'entity.entity_test_mulrevpub.delete_multiple_form',
  43. ],
  44. 'entity_test_revpub' => [
  45. 'type' => 'entity_test_revpub',
  46. 'label' => 'Delete test entity - revisions and publishing status',
  47. 'action_label' => 'Delete',
  48. 'confirm_form_route_name' => 'entity.entity_test_revpub.delete_multiple_form',
  49. ],
  50. 'entity_test_rev' => [
  51. 'type' => 'entity_test_rev',
  52. 'label' => 'Delete test entity - revisions',
  53. 'action_label' => 'Delete',
  54. 'confirm_form_route_name' => 'entity.entity_test_rev.delete_multiple_form',
  55. ],
  56. ], $deriver->getDerivativeDefinitions([
  57. 'action_label' => 'Delete',
  58. ]));
  59. }
  60. /**
  61. * @covers \Drupal\Core\Action\Plugin\Action\DeleteAction::execute
  62. */
  63. public function testDeleteAction() {
  64. $entity = EntityTestMulRevPub::create(['name' => 'test']);
  65. $entity->save();
  66. $action = Action::create([
  67. 'id' => 'entity_delete_action',
  68. 'plugin' => 'entity:delete_action:entity_test_mulrevpub',
  69. ]);
  70. $action->save();
  71. $action->execute([$entity]);
  72. $this->assertArraySubset(['module' => ['entity_test']], $action->getDependencies());
  73. /** @var \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store */
  74. $temp_store = \Drupal::service('tempstore.private');
  75. $store_entries = $temp_store->get('entity_delete_multiple_confirm')->get($this->testUser->id() . ':entity_test_mulrevpub');
  76. $this->assertArraySubset([$this->testUser->id() => ['en' => 'en']], $store_entries);
  77. }
  78. }