function ActionTest::testOperations

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Kernel/Action/ActionTest.php \Drupal\Tests\system\Kernel\Action\ActionTest::testOperations()
  2. 10 core/modules/system/tests/src/Kernel/Action/ActionTest.php \Drupal\Tests\system\Kernel\Action\ActionTest::testOperations()
  3. 11.x core/modules/system/tests/src/Kernel/Action/ActionTest.php \Drupal\Tests\system\Kernel\Action\ActionTest::testOperations()

Tests the functionality of test actions.

File

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

Class

ActionTest
Tests action plugins.

Namespace

Drupal\Tests\system\Kernel\Action

Code

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());
}

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