function EntityRevisionsTest::runRevisionsTests
Same name in other branches
- 9 core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::runRevisionsTests()
- 10 core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::runRevisionsTests()
- 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 70
Class
- EntityRevisionsTest
- Create a entity with revisions and test viewing, saving, reverting, and deleting revisions.
Namespace
Drupal\Tests\system\Functional\EntityCode
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.
$this->assertTrue($entity->revision_id->value > $legacy_revision_id, new FormattableMarkup('%entity_type: Revision ID changed.', [
'%entity_type' => $entity_type,
]));
$this->assertNotEqual($entity->name->value, $legacy_name, new FormattableMarkup('%entity_type: Name changed.', [
'%entity_type' => $entity_type,
]));
$this->assertNotEqual($entity->translatable_test_field->value, $legacy_text, new FormattableMarkup('%entity_type: Text changed.', [
'%entity_type' => $entity_type,
]));
}
$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->assertEqual($entity_revision->revision_id->value, $revision_ids[$i], new FormattableMarkup('%entity_type: Revision ID matches.', [
'%entity_type' => $entity_type,
]));
$this->assertEqual($entity_revision->name->value, $values['en'][$i]['name'], new FormattableMarkup('%entity_type: Name matches.', [
'%entity_type' => $entity_type,
]));
$this->assertEqual($entity_revision->translatable_test_field[0]->value, $values['en'][$i]['translatable_test_field'][0], new FormattableMarkup('%entity_type: Text matches.', [
'%entity_type' => $entity_type,
]));
$this->assertEqual($entity_revision->translatable_test_field[1]->value, $values['en'][$i]['translatable_test_field'][1], new FormattableMarkup('%entity_type: Text matches.', [
'%entity_type' => $entity_type,
]));
// Check the translated values.
if ($entity->getEntityType()
->isTranslatable()) {
$revision_translation = $entity_revision->getTranslation('de');
$this->assertEqual($revision_translation->name->value, $values['de'][$i]['name'], new FormattableMarkup('%entity_type: Name matches.', [
'%entity_type' => $entity_type,
]));
$this->assertEqual($revision_translation->translatable_test_field[0]->value, $values['de'][$i]['translatable_test_field'][0], new FormattableMarkup('%entity_type: Text matches.', [
'%entity_type' => $entity_type,
]));
$this->assertEqual($revision_translation->translatable_test_field[1]->value, $values['de'][$i]['translatable_test_field'][1], new FormattableMarkup('%entity_type: Text matches.', [
'%entity_type' => $entity_type,
]));
}
// Check non-revisioned values are loaded.
$this->assertTrue(isset($entity_revision->created->value), new FormattableMarkup('%entity_type: Non-revisioned field is loaded.', [
'%entity_type' => $entity_type,
]));
$this->assertEqual($entity_revision->created->value, $values['en'][2]['created'], new FormattableMarkup('%entity_type: Non-revisioned field value is the same between revisions.', [
'%entity_type' => $entity_type,
]));
}
// 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->assertFieldById('edit-name-0-value', $entity->name->value, new FormattableMarkup('%entity_type: Name matches in UI.', [
'%entity_type' => $entity_type,
]));
$this->assertFieldById('edit-translatable-test-field-0-value', $entity->translatable_test_field->value, new FormattableMarkup('%entity_type: Text matches in UI.', [
'%entity_type' => $entity_type,
]));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.