public function ActionTest::testOperations in Drupal 8
Same name and namespace in other branches
- 9 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\ActionCode
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());
}