You are here

protected function ReorderChildrenWithRevisionsFunctionalTest::createTestEntity in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ReorderChildrenWithRevisionsFunctionalTest.php \Drupal\Tests\entity_hierarchy\Functional\ReorderChildrenWithRevisionsFunctionalTest::createTestEntity()

Creates a new test entity.

Parameters

int|null $parentId: Parent ID.

string $label: Entity label.

int $weight: Entity weight amongst sibling, if parent is set.

Return value

\Drupal\Core\Entity\EntityInterface New entity.

Overrides EntityHierarchyTestTrait::createTestEntity

1 call to ReorderChildrenWithRevisionsFunctionalTest::createTestEntity()
ReorderChildrenWithRevisionsFunctionalTest::testReordering in tests/src/Functional/ReorderChildrenWithRevisionsFunctionalTest.php
Tests children reorder form.

File

tests/src/Functional/ReorderChildrenWithRevisionsFunctionalTest.php, line 44

Class

ReorderChildrenWithRevisionsFunctionalTest
Tests reordering with revisions.

Namespace

Drupal\Tests\entity_hierarchy\Functional

Code

protected function createTestEntity($parentId, $label = 'Child 1', $weight = 0) {
  $values = [
    'type' => static::ENTITY_TYPE,
    $this->container
      ->get('entity_type.manager')
      ->getDefinition(static::ENTITY_TYPE)
      ->getKey('label') => $label,
  ];
  if ($parentId) {
    $values[static::FIELD_NAME] = [
      'target_id' => $parentId,
      'weight' => $weight,
    ];
  }
  $entity = $this
    ->doCreateTestEntity($values);

  // Create a revision with the wrong weight.
  $entity
    ->setNewRevision(TRUE);
  if ($parentId) {
    $entity->{static::FIELD_NAME}->weight = -1 * $weight;
  }
  $entity
    ->save();

  // And a default revision with the correct weight.
  $entity
    ->setNewRevision(TRUE);
  if ($parentId) {
    $entity->{static::FIELD_NAME}->weight = $weight;
  }
  $entity
    ->save();
  return $entity;
}