Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / tests / src / Kernel / DeleteActionTest.php
1 <?php
2
3 namespace Drupal\Tests\entity\Kernel;
4
5 use Drupal\entity\Plugin\Action\DeleteAction;
6 use Drupal\entity_module_test\Entity\EnhancedEntity;
7 use Drupal\system\Entity\Action;
8 use Drupal\user\Entity\User;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * Tests the delete entity action.
13  *
14  * @group entity
15  */
16 class DeleteActionTest extends KernelTestBase {
17
18   /**
19    * The current user.
20    *
21    * @var \Drupal\user\UserInterface
22    */
23   protected $user;
24
25   /**
26    * {@inheritdoc}
27    */
28   public static $modules = [
29     'action', 'node', 'entity_module_test', 'entity', 'user', 'system',
30   ];
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37
38     $this->installEntitySchema('user');
39     $this->installEntitySchema('entity_test_enhanced');
40     $this->installSchema('system', ['key_value_expire', 'sequences']);
41
42     $this->user = User::create([
43       'name' => 'username',
44       'status' => 1,
45     ]);
46     $this->user->save();
47     \Drupal::service('current_user')->setAccount($this->user);
48   }
49
50   public function testAction() {
51     /** @var \Drupal\system\ActionConfigEntityInterface $action */
52     $action = Action::create([
53       'id' => 'enhanced_entity_delete_action',
54       'label' => 'Delete enhanced entity',
55       'plugin' => 'entity_delete_action:entity_test_enhanced',
56     ]);
57     $status = $action->save();
58     $this->assertEquals(SAVED_NEW, $status);
59     $this->assertInstanceOf(DeleteAction::class, $action->getPlugin());
60
61     $entities = [];
62     for ($i = 0; $i < 2; $i++) {
63       $entity = EnhancedEntity::create([
64         'type' => 'default',
65       ]);
66       $entity->save();
67       $entities[$entity->id()] = $entity;
68     }
69
70     $action->execute($entities);
71     // Confirm that the entity ids and langcodes are now in the tempstore.
72     $tempstore = \Drupal::service('tempstore.private')->get('entity_delete_multiple_confirm');
73     $selection = $tempstore->get($this->user->id() . ':entity_test_enhanced');
74     $this->assertEquals(array_keys($entities), array_keys($selection));
75     $this->assertEquals([['en' => 'en'], ['en' => 'en']], array_values($selection));
76   }
77
78 }