d381de115daa9c7e4fad6d127bbdbec275d82182
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Action / PublishActionTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Action;
4
5 use Drupal\Core\Action\Plugin\Action\Derivative\EntityPublishedActionDeriver;
6 use Drupal\entity_test\Entity\EntityTestMulRevPub;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\system\Entity\Action;
9
10 /**
11  * @group Action
12  */
13 class PublishActionTest extends KernelTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['system', 'entity_test', 'user'];
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp() {
24     parent::setUp();
25     $this->installEntitySchema('entity_test_mulrevpub');
26   }
27
28   /**
29    * @covers \Drupal\Core\Action\Plugin\Action\Derivative\EntityPublishedActionDeriver::getDerivativeDefinitions
30    */
31   public function testGetDerivativeDefinitions() {
32     $deriver = new EntityPublishedActionDeriver(\Drupal::entityTypeManager(), \Drupal::translation());
33     $this->assertArraySubset([
34       'entity_test_mulrevpub' => [
35         'type' => 'entity_test_mulrevpub',
36         'label' => 'Save test entity - revisions, data table, and published interface',
37         'action_label' => 'Save',
38       ],
39     ], $deriver->getDerivativeDefinitions([
40       'action_label' => 'Save',
41     ]));
42   }
43
44   /**
45    * @covers \Drupal\Core\Action\Plugin\Action\PublishAction::execute
46    */
47   public function testPublishAction() {
48     $entity = EntityTestMulRevPub::create(['name' => 'test']);
49     $entity->setUnpublished()->save();
50
51     $action = Action::create([
52       'id' => 'entity_publish_action',
53       'plugin' => 'entity:publish_action:entity_test_mulrevpub',
54     ]);
55     $action->save();
56     $this->assertFalse($entity->isPublished());
57     $action->execute([$entity]);
58     $this->assertTrue($entity->isPublished());
59     $this->assertArraySubset(['module' => ['entity_test']], $action->getDependencies());
60   }
61
62   /**
63    * @covers \Drupal\Core\Action\Plugin\Action\UnpublishAction::execute
64    */
65   public function testUnpublishAction() {
66     $entity = EntityTestMulRevPub::create(['name' => 'test']);
67     $entity->setPublished()->save();
68
69     $action = Action::create([
70       'id' => 'entity_unpublish_action',
71       'plugin' => 'entity:unpublish_action:entity_test_mulrevpub',
72     ]);
73     $action->save();
74     $this->assertTrue($entity->isPublished());
75     $action->execute([$entity]);
76     $this->assertFalse($entity->isPublished());
77     $this->assertArraySubset(['module' => ['entity_test']], $action->getDependencies());
78   }
79
80 }