You are here

public function SchedulerSetupTrait::createEntity in Scheduler 2.x

Creates a test entity.

This is called to generate a node, media or product entity, for tests that process all types of entities, either in loops or via a data provider.

Parameters

string $entityTypeId: The name of the entity type - 'node', 'media' or 'commerce_product'.

string $bundle: The name of the bundle. Optional, will default to $this->type for nodes $this->mediaTypeName for media, or $this->productTypeName for products.

array $values: Values for the new entity.

Return value

\Drupal\Core\Entity\EntityInterface The created entity object.

24 calls to SchedulerSetupTrait::createEntity()
SchedulerDeleteEntityTest::testDeleteEntityWhenSchedulingIsRequired in tests/src/Functional/SchedulerDeleteEntityTest.php
Tests the deletion of an entity when the scheduler dates are required.
SchedulerDeleteEntityTest::testDeleteEntityWithPastDates in tests/src/Functional/SchedulerDeleteEntityTest.php
Tests the deletion of scheduled entities.
SchedulerDrushTest::testDrushCronPublishing in tests/src/Functional/SchedulerDrushTest.php
Tests scheduled publishing and unpublishing of entities via Drush.
SchedulerEntityAccessTest::testEntityAccess in tests/src/Functional/SchedulerEntityAccessTest.php
Tests Scheduler cron functionality when access to the entity is denied.
SchedulerEventsTest::testSchedulerEvents in tests/src/Functional/SchedulerEventsTest.php
Tests six scheduler events for entity types other than node.

... See full list

File

tests/src/Traits/SchedulerSetupTrait.php, line 240

Class

SchedulerSetupTrait
Generic setup for all Scheduler tests.

Namespace

Drupal\Tests\scheduler\Traits

Code

public function createEntity(string $entityTypeId, string $bundle = NULL, array $values = []) {
  switch ($entityTypeId) {
    case 'media':
      $values += [
        'bundle' => $bundle ?? $this->mediaTypeName,
      ];

      // For Media, the title is stored in the 'name' field, so get the title
      // when the 'name' is not defined, to allow the same $value parameters
      // as for Node.
      if (isset($values['title'])) {
        $values['name'] = $values['name'] ?? $values['title'];
        unset($values['title']);
      }
      $entity = $this
        ->createMediaItem($values);
      break;
    case 'commerce_product':
      $values += [
        'type' => $bundle ?? $this->productTypeName,
      ];
      $entity = $this
        ->createProduct($values);
      break;
    case 'node':
    default:

      // For nodes the field for bundle is called 'type'.
      $values += [
        'type' => $bundle ?? $this->type,
      ];
      $entity = $this
        ->drupalCreateNode($values);
      break;
  }
  return $entity;
}