You are here

public function NodeRevisionsAutocleanTest::testDeleteDateRevisions in Node Revisions Autoclean 8

Tests that revisions are deleted as they should be with date constraint.

File

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

Class

NodeRevisionsAutocleanTest
Class NodeRevisionsAutocleanTest.

Namespace

Drupal\Tests\node_revisions_autoclean\Functional

Code

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

  /* @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));
  $oneMonthAgo = new \DateTime();
  $oneMonthAgo
    ->sub(new \DateInterval('P1M'));
  $now = new \DateTime();
  $this
    ->initializeSettings(TRUE);
  $user = $this
    ->drupalCreateUser([
    'configure revisions autoclean settings',
  ]);
  $this
    ->addRevision($node, $user, TRUE);
  $revisionsAfterSave = $revisionsManager
    ->loadRevisions($node);

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

  // There should be 18 revisions : all except these onemonthago.
  $this
    ->assertEquals(18, count($revisionsAfterSave2), 'Revisions after insert & save  : ' . count($revisionsAfterSave2));
}