function RevisionDeleteFormTest::testSubmitForm

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

Tests revision deletion, and expected response after deletion.

@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.

int $totalRevisions: Total number of revisions to create.

string $expectedLog: Expected log.

string $expectedMessage: Expected messenger message.

string|int $expectedDestination: Expected destination after deletion.

File

core/tests/Drupal/FunctionalTests/Entity/RevisionDeleteFormTest.php, line 223

Class

RevisionDeleteFormTest
Tests deleting a revision with revision delete form.

Namespace

Drupal\FunctionalTests\Entity

Code

public function testSubmitForm(array $permissions, string $entityTypeId, string $entityLabel, int $totalRevisions, string $expectedLog, string $expectedMessage, $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();
    $otherRevisionIds = [];
    for ($i = 0; $i < $totalRevisions - 1; $i++) {
        if ($entity instanceof RevisionLogInterface) {
            $entity->setRevisionCreationTime($date->modify('+1 hour')
                ->getTimestamp());
        }
        $entity->setNewRevision();
        $entity->save();
        $otherRevisionIds[] = $entity->getRevisionId();
    }
    $revision = $storage->loadRevision($revisionId);
    $this->drupalGet($revision->toUrl('revision-delete-form'));
    $this->submitForm([], 'Delete');
    // The revision was deleted.
    $this->assertNull($storage->loadRevision($revisionId));
    // Make sure the other revisions were not deleted.
    foreach ($otherRevisionIds as $otherRevisionId) {
        $this->assertNotNull($storage->loadRevision($otherRevisionId));
    }
    // Destination.
    if ($expectedDestination === 404) {
        $this->assertSession()
            ->statusCodeEquals(404);
    }
    else {
        $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.