PublishActionTest.php

Same filename in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Action/PublishActionTest.php
  2. 10 core/tests/Drupal/KernelTests/Core/Action/PublishActionTest.php
  3. 11.x core/tests/Drupal/KernelTests/Core/Action/PublishActionTest.php

Namespace

Drupal\KernelTests\Core\Action

File

core/tests/Drupal/KernelTests/Core/Action/PublishActionTest.php

View source
<?php

namespace Drupal\KernelTests\Core\Action;

use Drupal\Core\Action\Plugin\Action\Derivative\EntityPublishedActionDeriver;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\KernelTests\KernelTestBase;
use Drupal\system\Entity\Action;

/**
 * @group Action
 */
class PublishActionTest extends KernelTestBase {
    
    /**
     * {@inheritdoc}
     */
    protected static $modules = [
        'system',
        'entity_test',
        'user',
    ];
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->installEntitySchema('entity_test_mulrevpub');
    }
    
    /**
     * @covers \Drupal\Core\Action\Plugin\Action\Derivative\EntityPublishedActionDeriver::getDerivativeDefinitions
     */
    public function testGetDerivativeDefinitions() {
        $deriver = new EntityPublishedActionDeriver(\Drupal::entityTypeManager(), \Drupal::translation());
        $definitions = $deriver->getDerivativeDefinitions([
            'action_label' => 'Save',
        ]);
        $this->assertEquals([
            'type' => 'entity_test_mulrevpub',
            'label' => 'Save test entity - revisions, data table, and published interface',
            'action_label' => 'Save',
        ], $definitions['entity_test_mulrevpub']);
    }
    
    /**
     * @covers \Drupal\Core\Action\Plugin\Action\PublishAction::execute
     */
    public function testPublishAction() {
        $entity = EntityTestMulRevPub::create([
            'name' => 'test',
        ]);
        $entity->setUnpublished()
            ->save();
        $action = Action::create([
            'id' => 'entity_publish_action',
            'plugin' => 'entity:publish_action:entity_test_mulrevpub',
        ]);
        $action->save();
        $this->assertFalse($entity->isPublished());
        $action->execute([
            $entity,
        ]);
        $this->assertTrue($entity->isPublished());
        $this->assertSame([
            'module' => [
                'entity_test',
            ],
        ], $action->getDependencies());
    }
    
    /**
     * @covers \Drupal\Core\Action\Plugin\Action\UnpublishAction::execute
     */
    public function testUnpublishAction() {
        $entity = EntityTestMulRevPub::create([
            'name' => 'test',
        ]);
        $entity->setPublished()
            ->save();
        $action = Action::create([
            'id' => 'entity_unpublish_action',
            'plugin' => 'entity:unpublish_action:entity_test_mulrevpub',
        ]);
        $action->save();
        $this->assertTrue($entity->isPublished());
        $action->execute([
            $entity,
        ]);
        $this->assertFalse($entity->isPublished());
        $this->assertSame([
            'module' => [
                'entity_test',
            ],
        ], $action->getDependencies());
    }

}

Classes

Title Deprecated Summary
PublishActionTest @group Action

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.