You are here

public function SchedulerApiTest::testNidList in Scheduler 8

Covers hook_scheduler_nid_list($action)

Hook_scheduler_nid_list() allows other modules to add more node ids into the list to be processed. In real scenarios, the third-party module would likely have more complex data structures and/or tables from which to identify nodes to add. In this test, to keep it simple, we identify nodes by the text of the title.

File

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

Class

SchedulerApiTest
Tests the API of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testNidList() {
  $this
    ->drupalLogin($this->schedulerUser);

  // Create test nodes. Use the ordinary page type for this test, as having
  // the 'approved' fields here would unnecessarily complicate the processing.
  // Node 1 is not published and has no publishing date set. The test API
  // module will add node 1 into the list to be published.
  $node1 = $this
    ->drupalCreateNode([
    'type' => $this->type,
    'status' => FALSE,
    'title' => 'API TEST nid_list publish me',
  ]);

  // Node 2 is published and has no unpublishing date set. The test API module
  // will add node 2 into the list to be unpublished.
  $node2 = $this
    ->drupalCreateNode([
    'type' => $this->type,
    'status' => TRUE,
    'title' => 'API TEST nid_list unpublish me',
  ]);

  // Before cron, check node 1 is unpublished and node 2 is published.
  $this
    ->assertFalse($node1
    ->isPublished(), 'Before cron, node 1 "' . $node1->title->value . '" is unpublished.');
  $this
    ->assertTrue($node2
    ->isPublished(), 'Before cron, node 2 "' . $node2->title->value . '" is published.');

  // Run cron and refresh the nodes.
  scheduler_cron();
  $this->nodeStorage
    ->resetCache();
  $node1 = $this->nodeStorage
    ->load($node1
    ->id());
  $node2 = $this->nodeStorage
    ->load($node2
    ->id());

  // Check node 1 is published and node 2 is unpublished.
  $this
    ->assertTrue($node1
    ->isPublished(), 'After cron, node 1 "' . $node1->title->value . '" is published.');
  $this
    ->assertFalse($node2
    ->isPublished(), 'After cron, node 2 "' . $node2->title->value . '" is unpublished.');
}