You are here

public function NodeRevisionGenerateTest::testGetAvailableNodesForRevisions in Node Revision Delete 8

Tests the getAvailableNodesForRevisions() method.

@covers ::getAvailableNodesForRevisions @dataProvider providerGetAvailableNodesForRevisions

Parameters

array $expected: The expected result from calling the function.

array $bundles: An array with the selected content types to generate node revisions.

int $revisions_age: Interval in Unix timestamp format to add to the last revision date of the node.

File

modules/node_revision_generate/tests/src/Unit/NodeRevisionGenerateTest.php, line 170

Class

NodeRevisionGenerateTest
Tests the NodeRevisionGenerate class methods.

Namespace

Drupal\Tests\node_revision_generate\Unit

Code

public function testGetAvailableNodesForRevisions(array $expected, array $bundles, $revisions_age) {

  // Mocking getRequestTime method.
  $this->time
    ->expects($this
    ->any())
    ->method('getRequestTime')
    ->willReturn($this
    ->getRequestTime());

  // Variable with the placeholders arguments needed for the expression.
  $interval_time = [
    ':interval' => $revisions_age,
    ':current_time' => $this
      ->getRequestTime(),
  ];

  // StatementInterface mock.
  $statement = $this
    ->createMock('Drupal\\Core\\Database\\StatementInterface');

  // StatementInterface::fetchAll mock.
  $statement
    ->expects($this
    ->any())
    ->method('fetchAll')
    ->willReturn($expected);

  // SelectInterface mock.
  $select = $this
    ->createMock('Drupal\\Core\\Database\\Query\\SelectInterface');

  // SelectInterface::execute mock.
  $select
    ->expects($this
    ->any())
    ->method('execute')
    ->willReturn($statement);

  // SelectInterface::where mock.
  $select
    ->expects($this
    ->any())
    ->method('where')
    ->with('revision.revision_timestamp + :interval <= :current_time', $interval_time)
    ->willReturn($this
    ->returnSelf());

  // SelectInterface::condition mock.
  $select
    ->method('condition')
    ->withConsecutive([
    'node.type',
    $bundles,
    'IN',
  ], [
    'node.status',
    1,
  ])
    ->willReturnOnConsecutiveCalls($this
    ->returnSelf(), $this
    ->returnSelf());

  // SelectInterface::isNotNull mock.
  $select
    ->expects($this
    ->any())
    ->method('isNotNull')
    ->with('node.title')
    ->willReturn($this
    ->returnSelf());

  // SelectInterface::addField mock.
  $select
    ->method('addField')
    ->withConsecutive([
    'node',
    'nid',
  ], [
    'revision',
    'revision_timestamp',
  ])
    ->willReturnOnConsecutiveCalls($this
    ->returnSelf(), $this
    ->returnSelf());

  // SelectInterface::leftJoin mock.
  $select
    ->expects($this
    ->any())
    ->method('leftJoin')
    ->with('node_revision', 'revision', 'node.vid = revision.vid')
    ->willReturn($this
    ->returnSelf());

  // Mocking select method.
  $this->connection
    ->expects($this
    ->any())
    ->method('select')
    ->with('node_field_data', 'node')
    ->willReturn($select);

  // Testing the method.
  $this
    ->assertEquals($expected, $this->nodeRevisionGenerate
    ->getAvailableNodesForRevisions($bundles, $revisions_age));
}