class ActionTest

Same name in this branch
  1. 9 core/modules/jsonapi/tests/src/Functional/ActionTest.php \Drupal\Tests\jsonapi\Functional\ActionTest
  2. 9 core/modules/action/tests/src/Unit/Plugin/migrate/source/ActionTest.php \Drupal\Tests\action\Unit\Plugin\migrate\source\ActionTest
  3. 9 core/modules/system/tests/src/Kernel/Plugin/migrate/source/ActionTest.php \Drupal\Tests\system\Kernel\Plugin\migrate\source\ActionTest
Same name and namespace in other branches
  1. 11.x core/modules/jsonapi/tests/src/Functional/ActionTest.php \Drupal\Tests\jsonapi\Functional\ActionTest
  2. 11.x core/modules/system/tests/src/Kernel/Action/ActionTest.php \Drupal\Tests\system\Kernel\Action\ActionTest
  3. 11.x core/modules/system/tests/src/Kernel/Plugin/migrate/source/ActionTest.php \Drupal\Tests\system\Kernel\Plugin\migrate\source\ActionTest
  4. 10 core/modules/jsonapi/tests/src/Functional/ActionTest.php \Drupal\Tests\jsonapi\Functional\ActionTest
  5. 10 core/modules/system/tests/src/Kernel/Action/ActionTest.php \Drupal\Tests\system\Kernel\Action\ActionTest
  6. 10 core/modules/system/tests/src/Kernel/Plugin/migrate/source/ActionTest.php \Drupal\Tests\system\Kernel\Plugin\migrate\source\ActionTest
  7. 8.9.x core/modules/jsonapi/tests/src/Functional/ActionTest.php \Drupal\Tests\jsonapi\Functional\ActionTest
  8. 8.9.x core/modules/action/tests/src/Kernel/Plugin/migrate/source/ActionTest.php \Drupal\Tests\action\Kernel\Plugin\migrate\source\ActionTest
  9. 8.9.x core/modules/system/tests/src/Kernel/Action/ActionTest.php \Drupal\Tests\system\Kernel\Action\ActionTest

Tests action plugins.

@group Action

Hierarchy

Expanded class hierarchy of ActionTest

File

core/modules/system/tests/src/Kernel/Action/ActionTest.php, line 15

Namespace

Drupal\Tests\system\Kernel\Action
View source
class ActionTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'field',
    'user',
    'action_test',
  ];
  
  /**
   * The action manager.
   *
   * @var \Drupal\Core\Action\ActionManager
   */
  protected $actionManager;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->actionManager = $this->container
      ->get('plugin.manager.action');
    $this->installEntitySchema('user');
    $this->installSchema('system', [
      'sequences',
    ]);
  }
  
  /**
   * Tests the functionality of test actions.
   */
  public function testOperations() {
    // Test that actions can be discovered.
    $definitions = $this->actionManager
      ->getDefinitions();
    // Verify that the action definitions are found.
    $this->assertGreaterThan(1, count($definitions));
    $this->assertNotEmpty($definitions['action_test_no_type'], 'The test action is among the definitions found.');
    $definition = $this->actionManager
      ->getDefinition('action_test_no_type');
    $this->assertNotEmpty($definition, 'The test action definition is found.');
    $definitions = $this->actionManager
      ->getDefinitionsByType('user');
    $this->assertArrayNotHasKey('action_test_no_type', $definitions, 'An action with no type is not found.');
    // Create an instance of the 'save entity' action.
    $action = $this->actionManager
      ->createInstance('action_test_save_entity');
    $this->assertInstanceOf(ActionInterface::class, $action);
    // Create a new unsaved user.
    $name = $this->randomMachineName();
    $user_storage = $this->container
      ->get('entity_type.manager')
      ->getStorage('user');
    $account = $user_storage->create([
      'name' => $name,
      'bundle' => 'user',
    ]);
    $loaded_accounts = $user_storage->loadMultiple();
    $this->assertCount(0, $loaded_accounts);
    // Execute the 'save entity' action.
    $action->execute($account);
    $loaded_accounts = $user_storage->loadMultiple();
    $this->assertCount(1, $loaded_accounts);
    $account = reset($loaded_accounts);
    $this->assertEquals($name, $account->label());
  }
  
  /**
   * Tests the dependency calculation of actions.
   */
  public function testDependencies() {
    // Create a new action that depends on a user role.
    $action = Action::create([
      'id' => 'user_add_role_action.' . RoleInterface::ANONYMOUS_ID,
      'type' => 'user',
      'label' => 'Add the anonymous role to the selected users',
      'configuration' => [
        'rid' => RoleInterface::ANONYMOUS_ID,
      ],
      'plugin' => 'user_add_role_action',
    ]);
    $action->save();
    $expected = [
      'config' => [
        'user.role.' . RoleInterface::ANONYMOUS_ID,
      ],
      'module' => [
        'user',
      ],
    ];
    $this->assertSame($expected, $action->calculateDependencies()
      ->getDependencies());
  }

}

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