function ConfigActionTest::testEntityCreate

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Config/Action/ConfigActionTest.php \Drupal\KernelTests\Core\Config\Action\ConfigActionTest::testEntityCreate()

See also

\Drupal\Core\Config\Action\Plugin\ConfigAction\EntityCreate

File

core/tests/Drupal/KernelTests/Core/Config/Action/ConfigActionTest.php, line 32

Class

ConfigActionTest
Tests the config action system.

Namespace

Drupal\KernelTests\Core\Config\Action

Code

public function testEntityCreate() : void {
  $this->assertCount(0, \Drupal::entityTypeManager()->getStorage('config_test')
    ->loadMultiple(), 'There are no config_test entities');
  /** @var \Drupal\Core\Config\Action\ConfigActionManager $manager */
  $manager = $this->container
    ->get('plugin.manager.config_action');
  $manager->applyAction('entity_create:createIfNotExists', 'config_test.dynamic.action_test', [
    'label' => 'Action test',
  ]);
  /** @var \Drupal\config_test\Entity\ConfigTest[] $config_test_entities */
  $config_test_entities = \Drupal::entityTypeManager()->getStorage('config_test')
    ->loadMultiple();
  $this->assertCount(1, \Drupal::entityTypeManager()->getStorage('config_test')
    ->loadMultiple(), 'There is 1 config_test entity');
  $this->assertSame('Action test', $config_test_entities['action_test']->label());
  $this->assertTrue(Uuid::isValid((string) $config_test_entities['action_test']->uuid()), 'Config entity assigned a valid UUID');
  // Calling createIfNotExists action again will not error.
  $manager->applyAction('entity_create:createIfNotExists', 'config_test.dynamic.action_test', [
    'label' => 'Action test',
  ]);
  try {
    $manager->applyAction('entity_create:create', 'config_test.dynamic.action_test', [
      'label' => 'Action test',
    ]);
    $this->fail('Expected exception not thrown');
  } catch (ConfigActionException $e) {
    $this->assertSame('Entity config_test.dynamic.action_test exists', $e->getMessage());
  }
}

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