function RevisionViewTest::testRevisionPage

Same name in other branches
  1. 11.x core/tests/Drupal/FunctionalTests/Entity/RevisionViewTest.php \Drupal\FunctionalTests\Entity\RevisionViewTest::testRevisionPage()

Tests revision page.

@covers ::__invoke

@dataProvider providerRevisionPage

Parameters

string $entityTypeId: Entity type to test.

string $expectedPageTitle: Expected page title.

File

core/tests/Drupal/FunctionalTests/Entity/RevisionViewTest.php, line 55

Class

RevisionViewTest
Tests revision view page.

Namespace

Drupal\FunctionalTests\Entity

Code

public function testRevisionPage(string $entityTypeId, string $expectedPageTitle) : void {
    
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage($entityTypeId);
    // Add a field to test revision page output.
    $fieldStorage = FieldStorageConfig::create([
        'entity_type' => $entityTypeId,
        'field_name' => 'foo',
        'type' => 'string',
    ]);
    $fieldStorage->save();
    FieldConfig::create([
        'field_storage' => $fieldStorage,
        'bundle' => $entityTypeId,
    ])->save();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $displayRepository */
    $displayRepository = \Drupal::service('entity_display.repository');
    $displayRepository->getViewDisplay($entityTypeId, $entityTypeId)
        ->setComponent('foo', [
        'type' => 'string',
    ])
        ->save();
    $entity = $storage->create([
        'type' => $entityTypeId,
    ]);
    $entity->setName('revision 1, view revision');
    $revision1Body = $this->randomMachineName();
    $entity->foo = $revision1Body;
    $entity->setNewRevision();
    if ($entity instanceof RevisionLogInterface) {
        $date = new \DateTime('11 January 2009 4:00:00pm');
        $entity->setRevisionCreationTime($date->getTimestamp());
    }
    $entity->save();
    $revisionId = $entity->getRevisionId();
    $entity->setName('revision 2, view revision');
    $revision2Body = $this->randomMachineName();
    $entity->foo = $revision2Body;
    if ($entity instanceof RevisionLogInterface) {
        $entity->setRevisionCreationTime($date->modify('+1 hour')
            ->getTimestamp());
    }
    $entity->setNewRevision();
    $entity->save();
    $revision = $storage->loadRevision($revisionId);
    $this->drupalGet($revision->toUrl('revision'));
    $this->assertSession()
        ->pageTextContains($expectedPageTitle);
    $this->assertSession()
        ->pageTextContains($revision1Body);
    $this->assertSession()
        ->pageTextNotContains($revision2Body);
}

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