function ActionTest::testOperations

Same name in other branches
  1. 9 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();
    $this->assertTrue(count($definitions) > 1, 'Action definitions are found.');
    $this->assertTrue(!empty($definitions['action_test_no_type']), 'The test action is among the definitions found.');
    $definition = $this->actionManager
        ->getDefinition('action_test_no_type');
    $this->assertTrue(!empty($definition), 'The test action definition is found.');
    $definitions = $this->actionManager
        ->getDefinitionsByType('user');
    $this->assertTrue(empty($definitions['action_test_no_type']), '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->assertEqual($name, $account->label());
}

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