class RevisionViewTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/FunctionalTests/Entity/RevisionViewTest.php \Drupal\FunctionalTests\Entity\RevisionViewTest
  2. 10 core/tests/Drupal/FunctionalTests/Entity/RevisionViewTest.php \Drupal\FunctionalTests\Entity\RevisionViewTest

Tests revision view page.

Attributes

#[CoversClass(EntityRevisionViewController::class)] #[Group('Entity')] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of RevisionViewTest

File

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

Namespace

Drupal\FunctionalTests\Entity
View source
class RevisionViewTest extends BrowserTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'block',
    'entity_test',
    'entity_test_revlog',
    'field',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->drupalPlaceBlock('page_title_block');
  }
  
  /**
   * Tests revision page.
   *
   * @param string $entityTypeId
   *   Entity type to test.
   * @param string $expectedPageTitle
   *   Expected page title.
   *
   * @legacy-covers ::__invoke
   */
  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);
  }
  
  /**
   * Data provider for testRevisionPage.
   */
  public static function providerRevisionPage() : array {
    return [
      [
        'entity_test_rev',
        'Revision of revision 1, view revision',
      ],
      [
        'entity_test_revlog',
        'Revision of revision 1, view revision from Sun, 11 Jan 2009 - 16:00',
      ],
    ];
  }

}

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