You are here

protected function FieldableEntityDefinitionUpdateTest::insertData in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\FieldableEntityDefinitionUpdateTest::insertData()

Generates test entities for the 'entity_test_update' entity type.

Parameters

bool $revisionable: Whether the entity type is revisionable or not.

bool $translatable: Whether the entity type is translatable or not.

2 calls to FieldableEntityDefinitionUpdateTest::insertData()
FieldableEntityDefinitionUpdateTest::testFieldableEntityTypeUpdates in core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php
@covers ::updateFieldableEntityType @dataProvider providerTestFieldableEntityTypeUpdates
FieldableEntityDefinitionUpdateTest::testFieldableEntityTypeUpdatesErrorHandling in core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php
Tests that a failed entity schema update preserves the existing data.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php, line 293

Class

FieldableEntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager's fieldable entity update functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function insertData($revisionable, $translatable) {

  // Add three test entities in order to make the "data copy" step run at
  // least three times.

  /** @var \Drupal\Core\Entity\TranslatableRevisionableStorageInterface|\Drupal\Core\Entity\EntityStorageInterface $storage */
  $storage = $this->entityTypeManager
    ->getStorage($this->entityTypeId);
  $next_id = $storage
    ->getQuery()
    ->accessCheck(FALSE)
    ->count()
    ->execute() + 1;

  // Create test entities with two translations and two revisions.

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  for ($i = $next_id; $i <= $next_id + 2; $i++) {
    $entity = $storage
      ->create([
      'id' => $i,
      'type' => 'test_bundle',
      'name' => 'test entity - ' . $i . ' - en',
      'new_bundle_field' => 'bundle field - ' . $i . ' - en',
      'test_multiple_properties' => [
        'value1' => 'shared table - ' . $i . ' - value 1 - en',
        'value2' => 'shared table - ' . $i . ' - value 2 - en',
      ],
      'test_multiple_properties_multiple_values' => [
        [
          'value1' => 'dedicated table - ' . $i . ' - delta 0 - value 1 - en',
          'value2' => 'dedicated table - ' . $i . ' - delta 0 - value 2 - en',
        ],
        [
          'value1' => 'dedicated table - ' . $i . ' - delta 1 - value 1 - en',
          'value2' => 'dedicated table - ' . $i . ' - delta 1 - value 2 - en',
        ],
      ],
    ]);
    $entity
      ->save();
    if ($translatable) {
      $translation = $entity
        ->addTranslation('ro', [
        'name' => 'test entity - ' . $i . ' - ro',
        'new_bundle_field' => 'bundle field - ' . $i . ' - ro',
        'test_multiple_properties' => [
          'value1' => 'shared table - ' . $i . ' - value 1 - ro',
          'value2' => 'shared table - ' . $i . ' - value 2 - ro',
        ],
        'test_multiple_properties_multiple_values' => [
          [
            'value1' => 'dedicated table - ' . $i . ' - delta 0 - value 1 - ro',
            'value2' => 'dedicated table - ' . $i . ' - delta 0 - value 2 - ro',
          ],
          [
            'value1' => 'dedicated table - ' . $i . ' - delta 1 - value 1 - ro',
            'value2' => 'dedicated table - ' . $i . ' - delta 1 - value 2 - ro',
          ],
        ],
      ]);
      $translation
        ->save();
    }
    $this->testEntities[$entity
      ->id()] = $entity;
    if ($revisionable) {

      // Create a new pending revision.
      $revision_2 = $storage
        ->createRevision($entity, FALSE);
      $revision_2->name = 'test entity - ' . $i . ' - en - rev2';
      $revision_2->new_bundle_field = 'bundle field - ' . $i . ' - en - rev2';
      $revision_2->test_multiple_properties->value1 = 'shared table - ' . $i . ' - value 1 - en - rev2';
      $revision_2->test_multiple_properties->value2 = 'shared table - ' . $i . ' - value 2 - en - rev2';
      $revision_2->test_multiple_properties_multiple_values[0]->value1 = 'dedicated table - ' . $i . ' - delta 0 - value 1 - en - rev2';
      $revision_2->test_multiple_properties_multiple_values[0]->value2 = 'dedicated table - ' . $i . ' - delta 0 - value 2 - en - rev2';
      $revision_2->test_multiple_properties_multiple_values[1]->value1 = 'dedicated table - ' . $i . ' - delta 1 - value 1 - en - rev2';
      $revision_2->test_multiple_properties_multiple_values[1]->value2 = 'dedicated table - ' . $i . ' - delta 1 - value 2 - en - rev2';
      $revision_2
        ->save();
      if ($translatable) {
        $revision_2_translation = $storage
          ->createRevision($entity
          ->getTranslation('ro'), FALSE);
        $revision_2_translation->name = 'test entity - ' . $i . ' - ro - rev2';
        $revision_2->new_bundle_field = 'bundle field - ' . $i . ' - ro - rev2';
        $revision_2->test_multiple_properties->value1 = 'shared table - ' . $i . ' - value 1 - ro - rev2';
        $revision_2->test_multiple_properties->value2 = 'shared table - ' . $i . ' - value 2 - ro - rev2';
        $revision_2_translation->test_multiple_properties_multiple_values[0]->value1 = 'dedicated table - ' . $i . ' - delta 0 - value 1 - ro - rev2';
        $revision_2_translation->test_multiple_properties_multiple_values[0]->value2 = 'dedicated table - ' . $i . ' - delta 0 - value 2 - ro - rev2';
        $revision_2_translation->test_multiple_properties_multiple_values[1]->value1 = 'dedicated table - ' . $i . ' - delta 1 - value 1 - ro - rev2';
        $revision_2_translation->test_multiple_properties_multiple_values[1]->value2 = 'dedicated table - ' . $i . ' - delta 1 - value 2 - ro - rev2';
        $revision_2_translation
          ->save();
      }
    }
  }
}