function RevisionRevertFormTest::testSubmitForm

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

Tests revision revert, and expected response after revert.

@covers ::submitForm @dataProvider providerSubmitForm

Parameters

array $permissions: If not empty, a user will be created and logged in with these permissions.

string $entityTypeId: The entity type to test.

string $entityLabel: The entity label, which corresponds to access grants.

string $expectedLog: Expected log.

string $expectedMessage: Expected messenger message.

string $expectedDestination: Expected destination after deletion.

File

core/tests/Drupal/FunctionalTests/Entity/RevisionRevertFormTest.php, line 195

Class

RevisionRevertFormTest
Tests reverting a revision with revision revert form.

Namespace

Drupal\FunctionalTests\Entity

Code

public function testSubmitForm(array $permissions, string $entityTypeId, string $entityLabel, string $expectedLog, string $expectedMessage, string $expectedDestination) : void {
    if (count($permissions) > 0) {
        $this->drupalLogin($this->createUser($permissions));
    }
    
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage($entityTypeId);
    $entity = $storage->create([
        'type' => $entityTypeId,
        'name' => $entityLabel,
    ]);
    if ($entity instanceof RevisionLogInterface) {
        $date = new \DateTime('11 January 2009 4:00:00pm');
        $entity->setRevisionCreationTime($date->getTimestamp());
    }
    $entity->save();
    $revisionId = $entity->getRevisionId();
    if ($entity instanceof RevisionLogInterface) {
        $entity->setRevisionCreationTime($date->modify('+1 hour')
            ->getTimestamp());
    }
    $entity->setNewRevision();
    $entity->save();
    $revision = $storage->loadRevision($revisionId);
    $this->drupalGet($revision->toUrl('revision-revert-form'));
    $count = $this->countRevisions($entityTypeId);
    $this->submitForm([], 'Revert');
    // A new revision was created.
    $this->assertEquals($count + 1, $this->countRevisions($entityTypeId));
    // Destination.
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->addressEquals($expectedDestination);
    // Logger log.
    $logs = $this->getLogs($entity->getEntityType()
        ->getProvider());
    $this->assertEquals([
        0 => $expectedLog,
    ], $logs);
    // Messenger message.
    $this->assertSession()
        ->pageTextContains($expectedMessage);
}

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