function ContentModerationStateTest::createEntity

Same name in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationStateTest::createEntity()
  2. 8.9.x core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationStateTest::createEntity()
  3. 10 core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationStateTest::createEntity()

Creates an entity.

The entity will have required fields populated and the corresponding bundle will be enabled for content moderation.

Parameters

string $entity_type_id: The entity type ID.

string $moderation_state: (optional) The initial moderation state of the newly created entity. Defaults to 'published'.

bool $create_workflow: (optional) Whether to create an editorial workflow and configure it for the given entity type. Defaults to TRUE.

Return value

\Drupal\Core\Entity\ContentEntityInterface The created entity.

8 calls to ContentModerationStateTest::createEntity()
ContentModerationStateTest::doTestBasicModeration in core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
Tests basic monolingual content moderation through the API.
ContentModerationStateTest::doTestContentModerationStateDataRemoval in core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
Tests removal of content moderation state entity.
ContentModerationStateTest::doTestContentModerationStatePendingRevisionDataRemoval in core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
Tests removal of content moderation state pending entity revisions.
ContentModerationStateTest::doTestContentModerationStateRevisionDataRemoval in core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
Tests removal of content moderation state entity revisions.
ContentModerationStateTest::doTestContentModerationStateTranslationDataRemoval in core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
Tests removal of content moderation state translations.

... See full list

1 method overrides ContentModerationStateTest::createEntity()
WorkspacesContentModerationStateTest::createEntity in core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php
Creates an entity.

File

core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php, line 772

Class

ContentModerationStateTest
Tests links between a content entity and a content_moderation_state entity.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

protected function createEntity($entity_type_id, $moderation_state = 'published', $create_workflow = TRUE) {
    $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
    $bundle_id = $entity_type_id;
    // Set up a bundle entity type for the specified entity type, if needed.
    if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
        $bundle_entity_type = $this->entityTypeManager
            ->getDefinition($bundle_entity_type_id);
        $bundle_entity_storage = $this->entityTypeManager
            ->getStorage($bundle_entity_type_id);
        $bundle_id = 'example';
        if (!$bundle_entity_storage->load($bundle_id)) {
            $bundle_entity = $bundle_entity_storage->create([
                $bundle_entity_type->getKey('id') => 'example',
            ]);
            if ($bundle_entity_type->hasKey('label')) {
                $bundle_entity->set($bundle_entity_type->getKey('label'), $this->randomMachineName());
            }
            if ($entity_type_id == 'media') {
                $bundle_entity->set('source', 'test');
                $bundle_entity->save();
                $source_field = $bundle_entity->getSource()
                    ->createSourceField($bundle_entity);
                $source_field->getFieldStorageDefinition()
                    ->save();
                $source_field->save();
                $bundle_entity->set('source_configuration', [
                    'source_field' => $source_field->getName(),
                ]);
            }
            $bundle_entity->save();
        }
    }
    if ($create_workflow) {
        $workflow = $this->createEditorialWorkflow();
        $workflow->getTypePlugin()
            ->addEntityTypeAndBundle($entity_type_id, $bundle_id);
        $workflow->save();
    }
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity_storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $entity = $entity_storage->create([
        $entity_type->getKey('label') => 'Test title',
        $entity_type->getKey('bundle') => $bundle_id,
        'moderation_state' => $moderation_state,
    ]);
    // Make sure we add values for all of the required fields.
    if ($entity_type_id == 'block_content') {
        $entity->info = $this->randomString();
    }
    $entity->save();
    return $entity;
}

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