You are here

public function ModeratedContentSchedulingTest::testPublishStateSchedule in Thunder 8.2

Tests moderated nodes publish scheduling.

File

tests/src/Kernel/ModeratedContentSchedulingTest.php, line 18

Class

ModeratedContentSchedulingTest
Tests publishing/unpublishing scheduling for moderated nodes.

Namespace

Drupal\Tests\thunder\Kernel

Code

public function testPublishStateSchedule() {
  $node = Node::create([
    'type' => 'example',
    'title' => 'Published title',
    'moderation_state' => 'draft',
    'publish_on' => strtotime('yesterday'),
    'publish_state' => 'published',
  ]);
  $node
    ->save();
  $revision_id = $node
    ->getRevisionId();

  // Make sure node is unpublished.
  $this
    ->assertEquals(FALSE, Node::load($node
    ->id())
    ->isPublished());
  $this->container
    ->get('cron')
    ->run();
  $node = $this->moderationInfo
    ->getLatestRevision('node', $node
    ->id());

  // Assert node is now published.
  $this
    ->assertEquals(TRUE, $node
    ->isPublished());
  $this
    ->assertEquals('published', $node->moderation_state->value);

  // Assert only one revision is created during the operation.
  $this
    ->assertEquals($revision_id + 1, $node
    ->getRevisionId());
  $node
    ->setTitle('Draft title');
  $node->moderation_state->value = 'draft';
  $node->publish_on->value = strtotime('yesterday');
  $node->publish_state->value = 'published';
  $node
    ->save();
  $node = $this->moderationInfo
    ->getLatestRevision('node', $node
    ->id());
  $this
    ->assertEquals('Draft title', $node
    ->getTitle());
  $this
    ->assertEquals('draft', $node->moderation_state->value);
  $this->container
    ->get('cron')
    ->run();
  $node = $this->moderationInfo
    ->getLatestRevision('node', $node
    ->id());
  $this
    ->assertEquals(TRUE, $node
    ->isPublished());
  $this
    ->assertEquals('published', $node->moderation_state->value);
  $this
    ->assertEquals('Draft title', $node
    ->getTitle());
}