Commit 2a4bd21a authored by Adrian Cid Almaguer's avatar Adrian Cid Almaguer
Browse files

Issue #3107848 by adriancid: Create a unit test for the getRevisionCreationBatch method

parent 28aa4e81
Loading
Loading
Loading
Loading
+1 −1
Changes for modules/node_revision_generate/node_revision_generate.services.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
services:
  node_revision_generate:
    class: Drupal\node_revision_generate\NodeRevisionGenerate
    arguments: ["@datetime.time", '@database']
    arguments: ["@datetime.time", '@database', '@string_translation']
+7 −2
Changes for modules/node_revision_generate/src/NodeRevisionGenerate.php: 7 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\node_revision_generate;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Database\Connection;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Batch\BatchBuilder;
@@ -37,13 +38,17 @@ class NodeRevisionGenerate implements NodeRevisionGenerateInterface {
   *   The date time instance.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   */
  public function __construct(
    TimeInterface $time,
    Connection $connection
    Connection $connection,
    TranslationInterface $string_translation
  ) {
    $this->time = $time;
    $this->connection = $connection;
    $this->stringTranslation = $string_translation;
  }

  /**
@@ -82,7 +87,7 @@ class NodeRevisionGenerate implements NodeRevisionGenerateInterface {
    // Defining the batch builder.
    $batch_builder = new BatchBuilder();
    $batch_builder->setTitle($this->t('Generating revisions'))
      ->setInitMessage($this->t('Starting the creation of revisions.'))
      ->setInitMessage($this->t('Starting to create revisions.'))
      ->setProgressMessage($this->t('Processed @current out of @total (@percentage%). Estimated time: @estimate.'))
      ->setErrorMessage($this->t('The revision creation process has encountered an error.'))
      ->setFinishCallback([NodeRevisionGenerateBatch::class, 'finish']);
+176 −0
Changes for modules/node_revision_generate/tests/src/Unit/NodeRevisionGenerateTest.php: 176 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\node_revision_generate\Unit;

use Drupal\node_revision_generate\NodeRevisionGenerateBatch;
use Drupal\Tests\UnitTestCase;
use Drupal\node_revision_generate\NodeRevisionGenerate;

/**
 * Tests the NodeRevisionGenerate class methods.
 *
 * @group node_revision_generate
 * @coversDefaultClass \Drupal\node_revision_generate\NodeRevisionGenerate
 */
class NodeRevisionGenerateTest extends UnitTestCase {

  /**
   * Drupal\Core\Database\Driver\mysql\Connection definition.
   *
   * @var \Drupal\Core\Database\Connection|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $connection;

  /**
   * A date time instance.
   *
   * @var \Drupal\Component\Datetime\TimeInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $time;

  /**
   * The NodeRevisionGenerate Object.
   *
   * @var Drupal\node_revision_generate\NodeRevisionGenerate
   */
  protected $nodeRevisionGenerate;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Time mock.
    $this->time = $this->createMock('Drupal\Component\Datetime\TimeInterface');
    // Connection mock.
    $this->connection = $this->createMock('Drupal\Core\Database\Connection');

    // Creating the object.
    $this->nodeRevisionGenerate = new NodeRevisionGenerate(
      $this->time,
      $this->connection,
      $this->getStringTranslationStub()
    );
  }

  /**
   * Tests the getRevisionCreationBatch() method.
   *
   * @param array $expected
   *   The expected result from calling the function.
   * @param array $nodes_for_revisions
   *   The nodes for revisions array.
   * @param int $revisions_number
   *   Number of revisions to generate.
   * @param int $revisions_age
   *   Interval in Unix timestamp format to add to the last revision date of the
   *   node.
   *
   * @covers ::getRevisionCreationBatch
   * @dataProvider providerGetRevisionCreationBatch
   */
  public function testGetRevisionCreationBatch(array $expected, array $nodes_for_revisions, $revisions_number, $revisions_age) {
    // Mocking getRequestTime method.
    $this->time->expects($this->any())
      ->method('getRequestTime')
      ->willReturn(1579631058);

    // Testing the method.
    $this->assertEquals($expected, $this->nodeRevisionGenerate->getRevisionCreationBatch($nodes_for_revisions, $revisions_number, $revisions_age));
  }

  /**
   * Data provider for testGetRevisionCreationBatch().
   *
   * @return array
   *   An array of arrays, each containing:
   *   - 'expected' - Expected return from ::getRevisionCreationBatch().
   *   - 'nodes_for_revisions' - The nodes for revisions array.
   *   - 'revisions_number' -  Number of revisions to generate.
   *   - 'revisions_age' - Time to add to the last revision date of the node.
   *
   * @see testGetRevisionCreationBatch()
   */
  public function providerGetRevisionCreationBatch() {
    $node1 = new \stdClass();
    $node1->nid = 1;
    $node1->revision_timestamp = 1570222375;

    $node2 = new \stdClass();
    $node2->nid = 2;
    $node2->revision_timestamp = 1569253391;

    $node3 = new \stdClass();
    $node3->nid = 3;
    $node3->revision_timestamp = 1550234556;

    // Sets of revisions.
    $revision_sets = [
      [],
      [$node1],
      [$node1, $node2],
      [$node1, $node2, $node3],
      [$node2, $node3],
    ];

    $number = [1, 5, 2, 7, 3];

    // Set for the revisions age option.
    $revisions_age = [
      86400,
      604800,
      2592000,
      86400,
      604800,
    ];

    // The batch.
    $batch_template = [
      'title' => $this->getStringTranslationStub()->translate('Generating revisions'),
      'init_message' => $this->getStringTranslationStub()->translate('Starting to create revisions.'),
      'progress_message' => $this->getStringTranslationStub()->translate('Processed @current out of @total (@percentage%). Estimated time: @estimate.'),
      'error_message' => $this->getStringTranslationStub()->translate('The revision creation process has encountered an error.'),
      'operations' => [],
      'finished' => [NodeRevisionGenerateBatch::class, 'finish'],
      'file' => NULL,
      'library' => [],
      'url_options' => [],
      'progressive' => TRUE,
    ];

    $expected = [];

    // Creating the expected arrays.
    foreach ($revision_sets as $set => $nodes_for_revisions) {
      $expected[$set] = $batch_template;

      // Building batch operations, one per revision.
      foreach ($nodes_for_revisions as $node) {
        $revision_timestamp = $node->revision_timestamp;
        // Initializing variables.
        $i = 0;
        $revision_timestamp += $revisions_age[$set];
        // Adding operations.
        while ($i < $number[$set] && $revision_timestamp <= 1579631058) {
          // Adding the operation.
          $expected[$set]['operations'][] = [
            [NodeRevisionGenerateBatch::class, 'generateRevisions'],
            [$node->nid, $revision_timestamp],
          ];
          $revision_timestamp += $revisions_age[$set];
          $i++;
        }
      }
    }

    $tests[] = [$expected[0], $revision_sets[0], $number[0], $revisions_age[0]];
    $tests[] = [$expected[1], $revision_sets[1], $number[1], $revisions_age[1]];
    $tests[] = [$expected[2], $revision_sets[2], $number[2], $revisions_age[2]];
    $tests[] = [$expected[3], $revision_sets[3], $number[3], $revisions_age[3]];
    $tests[] = [$expected[4], $revision_sets[4], $number[4], $revisions_age[4]];

    return $tests;
  }

}
+2 −2
Changes for tests/src/Unit/NodeRevisionDeleteTest.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ class NodeRevisionDeleteTest extends UnitTestCase {
  /**
   * A connection instance.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   * @var \Drupal\Core\Database\Connection|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $connection;

@@ -315,7 +315,7 @@ class NodeRevisionDeleteTest extends UnitTestCase {
   *   - 'revisions' - An array of revisions.
   *   - 'dry_run' - An option to know if we should delete or not the revisions.
   *
   * @see getRevisionDeletionBatch()
   * @see testGetRevisionDeletionBatch()
   */
  public function providerGetRevisionDeletionBatch() {
    // Sets of revisions.