function EntityRevisionsTest::runRevisionsTests

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::runRevisionsTests()
  2. 8.9.x core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::runRevisionsTests()
  3. 11.x core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::runRevisionsTests()

Executes the revision tests for the given entity type.

Parameters

string $entity_type: The entity type to run the tests with.

1 call to EntityRevisionsTest::runRevisionsTests()
EntityRevisionsTest::testRevisions in core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php
Check node revision related operations.

File

core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php, line 71

Class

EntityRevisionsTest
Tests modifying an entity with revisions.

Namespace

Drupal\Tests\system\Functional\Entity

Code

protected function runRevisionsTests($entity_type) {
    // Create a translatable test field.
    $field_storage = FieldStorageConfig::create([
        'entity_type' => $entity_type,
        'field_name' => 'translatable_test_field',
        'type' => 'text',
        'cardinality' => 2,
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'label' => $this->randomMachineName(),
        'bundle' => $entity_type,
        'translatable' => TRUE,
    ]);
    $field->save();
    \Drupal::service('entity_display.repository')->getFormDisplay($entity_type, $entity_type, 'default')
        ->setComponent('translatable_test_field')
        ->save();
    
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage($entity_type);
    // Create initial entity.
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $storage->create([
        'name' => 'foo',
        'user_id' => $this->webUser
            ->id(),
    ]);
    $entity->translatable_test_field->value = 'bar';
    $entity->addTranslation('de', [
        'name' => 'foo - de',
    ]);
    $entity->save();
    $values = [];
    $revision_ids = [];
    // Create three revisions.
    $revision_count = 3;
    for ($i = 0; $i < $revision_count; $i++) {
        $legacy_revision_id = $entity->revision_id->value;
        $legacy_name = $entity->name->value;
        $legacy_text = $entity->translatable_test_field->value;
        $entity = $storage->load($entity->id->value);
        $entity->setNewRevision(TRUE);
        $values['en'][$i] = [
            'name' => $this->randomMachineName(32),
            'translatable_test_field' => [
                $this->randomMachineName(32),
                $this->randomMachineName(32),
            ],
            'created' => time() + $i + 1,
        ];
        $entity->set('name', $values['en'][$i]['name']);
        $entity->set('translatable_test_field', $values['en'][$i]['translatable_test_field']);
        $entity->set('created', $values['en'][$i]['created']);
        $entity->save();
        $revision_ids[] = $entity->revision_id->value;
        // Add some values for a translation of this revision.
        if ($entity->getEntityType()
            ->isTranslatable()) {
            $values['de'][$i] = [
                'name' => $this->randomMachineName(32),
                'translatable_test_field' => [
                    $this->randomMachineName(32),
                    $this->randomMachineName(32),
                ],
            ];
            $translation = $entity->getTranslation('de');
            $translation->set('name', $values['de'][$i]['name']);
            $translation->set('translatable_test_field', $values['de'][$i]['translatable_test_field']);
            $translation->save();
        }
        // Check that the fields and properties contain new content.
        // Verify that the revision ID changed.
        $this->assertGreaterThan($legacy_revision_id, $entity->revision_id->value);
        $this->assertNotEquals($legacy_name, $entity->name->value, "{$entity_type}: Name changed.");
        $this->assertNotEquals($legacy_text, $entity->translatable_test_field->value, "{$entity_type}: Text changed.");
    }
    $revisions = $storage->loadMultipleRevisions($revision_ids);
    for ($i = 0; $i < $revision_count; $i++) {
        // Load specific revision.
        $entity_revision = $revisions[$revision_ids[$i]];
        // Check if properties and fields contain the revision specific content.
        $this->assertEquals($revision_ids[$i], $entity_revision->revision_id->value, "{$entity_type}: Revision ID matches.");
        $this->assertEquals($values['en'][$i]['name'], $entity_revision->name->value, "{$entity_type}: Name matches.");
        $this->assertEquals($values['en'][$i]['translatable_test_field'][0], $entity_revision->translatable_test_field[0]->value, "{$entity_type}: Text matches.");
        $this->assertEquals($values['en'][$i]['translatable_test_field'][1], $entity_revision->translatable_test_field[1]->value, "{$entity_type}: Text matches.");
        // Check the translated values.
        if ($entity->getEntityType()
            ->isTranslatable()) {
            $revision_translation = $entity_revision->getTranslation('de');
            $this->assertEquals($values['de'][$i]['name'], $revision_translation->name->value, "{$entity_type}: Name matches.");
            $this->assertEquals($values['de'][$i]['translatable_test_field'][0], $revision_translation->translatable_test_field[0]->value, "{$entity_type}: Text matches.");
            $this->assertEquals($values['de'][$i]['translatable_test_field'][1], $revision_translation->translatable_test_field[1]->value, "{$entity_type}: Text matches.");
        }
        // Check non-revisioned values are loaded.
        $this->assertTrue(isset($entity_revision->created->value), "{$entity_type}: Non-revisioned field is loaded.");
        $this->assertEquals($values['en'][2]['created'], $entity_revision->created->value, "{$entity_type}: Non-revisioned field value is the same between revisions.");
    }
    // Confirm the correct revision text appears in the edit form.
    $entity = $storage->load($entity->id->value);
    $this->drupalGet($entity_type . '/manage/' . $entity->id->value . '/edit');
    $this->assertSession()
        ->fieldValueEquals('edit-name-0-value', $entity->name->value);
    $this->assertSession()
        ->fieldValueEquals('edit-translatable-test-field-0-value', $entity->translatable_test_field->value);
}

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