You are here

public function NodeRevisionsAutocleanTest::testDeleteRevisions in Node Revisions Autoclean 8

Tests that revisions are deleted as they should be.

File

tests/src/Functional/NodeRevisionsAutocleanTest.php, line 278

Class

NodeRevisionsAutocleanTest
Class NodeRevisionsAutocleanTest.

Namespace

Drupal\Tests\node_revisions_autoclean\Functional

Code

public function testDeleteRevisions() {
  $node = $this
    ->createNodeWithRevisions('1', 'page');

  /* @var $revisionsManager \Drupal\node_revisions_autoclean\Services\RevisionsManager */
  $revisionsManager = \Drupal::service('node_revisions_autoclean.revisions_manager');
  $revisions = $revisionsManager
    ->loadRevisions($node);

  // CreateNodeWithRevisions creates 12 revisions, check :
  $this
    ->assertEquals(12, count($revisions), 'Revisions initial : ' . count($revisions));
  $this
    ->initializeSettings(TRUE);
  $user = $this
    ->drupalCreateUser([
    'configure revisions autoclean settings',
  ]);
  $this
    ->addRevision($node, $user, TRUE);
  $revisionsAfterSave = $revisionsManager
    ->loadRevisions($node);

  // There should be 3 revisions.
  $this
    ->assertEquals(3, count($revisionsAfterSave), 'Revisions after save : ' . count($revisionsAfterSave));
  $this
    ->addRevision($node, $user, FALSE);
  $this
    ->addRevision($node, $user, FALSE);
  $this
    ->addRevision($node, $user, FALSE);
  $revisionsAfterSave2 = $revisionsManager
    ->loadRevisions($node);

  // There should be 6 revisions : all last drafts + 3.
  $this
    ->assertEquals(6, count($revisionsAfterSave2), 'Revisions after insert & save  : ' . count($revisionsAfterSave2));
  $this
    ->addRevision($node, $user, TRUE);
  $this
    ->addRevision($node, $user, TRUE);
  $revisionsAfterSave3 = $revisionsManager
    ->loadRevisions($node);

  // There should be 3 (current + 2 in settings).
  $this
    ->assertEquals(3, count($revisionsAfterSave3), 'Revisions after insert & save  : ' . count($revisionsAfterSave3));

  // Disables autoclean.
  $this
    ->initializeSettings(FALSE);
  $this
    ->addRevision($node, $user, TRUE);
  $revisionsAfterSave4 = $revisionsManager
    ->loadRevisions($node);

  // There should be 4.
  $this
    ->assertEquals(4, count($revisionsAfterSave4), 'Revisions after insert & save  : ' . count($revisionsAfterSave4));
}