You are here

ModeratedContentSchedulingTest.php in Thunder 6.2.x

File

tests/src/FunctionalJavascript/ModeratedContentSchedulingTest.php
View source
<?php

namespace Drupal\Tests\thunder\FunctionalJavascript;

use Drupal\node\Entity\Node;

/**
 * Tests publishing/unpublishing scheduling for moderated nodes.
 *
 * @group Thunder
 */
class ModeratedContentSchedulingTest extends ThunderJavascriptTestBase {
  use ThunderArticleTestTrait;

  /**
   * Tests moderated nodes publish scheduling.
   */
  public function testPublishStateSchedule() {
    $publish_timestamp = strtotime('yesterday');

    /** @var \Drupal\node\NodeStorageInterface $node_storage */
    $node_storage = \Drupal::entityTypeManager()
      ->getStorage('node');
    $term = $this
      ->loadTermByUuid('bfc251bc-de35-467d-af44-1f7a7012b845');
    $this
      ->articleFillNew([
      'field_channel' => $term
        ->id(),
      'title[0][value]' => 'Test workflow article 1 - Published',
      'field_seo_title[0][value]' => 'Massive gaining seo traffic text 1',
      'moderation_state[0]' => 'draft',
      'publish_on[0][value][date]' => date('Y-m-d', $publish_timestamp),
      'publish_on[0][value][time]' => date('H:i:s', $publish_timestamp),
      'publish_state[0]' => 'published',
    ]);
    $this
      ->clickSave();

    /** @var \Drupal\node\Entity\Node $node */
    $node = $this
      ->getNodeByTitle('Test workflow article 1 - Published');
    $revision_id = $node
      ->getRevisionId();

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

    /** @var \Drupal\node\Entity\Node $node */
    $node = $node_storage
      ->loadRevision($node_storage
      ->getLatestRevisionId($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());
    $edit_url = $node
      ->toUrl('edit-form');
    $this
      ->drupalGet($edit_url);
    $this
      ->expandAllTabs();
    $this
      ->setFieldValues($this
      ->getSession()
      ->getPage(), [
      'title[0][value]' => 'Test workflow article 1 - Draft',
      'moderation_state[0]' => 'draft',
      'publish_on[0][value][date]' => date('Y-m-d', $publish_timestamp),
      'publish_on[0][value][time]' => date('H:i:s', $publish_timestamp),
      'publish_state[0]' => 'published',
    ]);
    $this
      ->clickSave();
    $node_storage
      ->resetCache([
      $node
        ->id(),
    ]);

    /** @var \Drupal\node\Entity\Node $node */
    $node = $node_storage
      ->loadRevision($node_storage
      ->getLatestRevisionId($node
      ->id()));
    $this
      ->assertEquals('Test workflow article 1 - Draft', $node
      ->getTitle());
    $this
      ->assertEquals('draft', $node->moderation_state->value);
    $this->container
      ->get('cron')
      ->run();

    /** @var \Drupal\node\Entity\Node $node */
    $node = $node_storage
      ->loadRevision($node_storage
      ->getLatestRevisionId($node
      ->id()));
    $this
      ->assertEquals(TRUE, $node
      ->isPublished());
    $this
      ->assertEquals('published', $node->moderation_state->value);
    $this
      ->assertEquals('Test workflow article 1 - Draft', $node
      ->getTitle());
  }

  /**
   * Tests moderated nodes unpublish scheduling.
   */
  public function testUnpublishStateSchedule() {
    $term = $this
      ->loadTermByUuid('bfc251bc-de35-467d-af44-1f7a7012b845');
    $this
      ->articleFillNew([
      'field_channel' => $term
        ->id(),
      'title[0][value]' => 'Test workflow article 2 - Published',
      'field_seo_title[0][value]' => 'Massive gaining seo traffic text 2',
      'moderation_state[0]' => 'published',
      'unpublish_on[0][value][date]' => date('Y-m-d', strtotime('tomorrow')),
      'unpublish_state[0]' => 'unpublished',
    ]);
    $this
      ->clickSave();
    $node = $this
      ->getNodeByTitle('Test workflow article 2 - Published');

    // Set date manually, unpublish cannot be in the past.
    $node->unpublish_on->value = strtotime('yesterday');
    $node
      ->save();
    $revision_id = $node
      ->getRevisionId();

    // Make sure node is published.
    $this
      ->assertEquals(TRUE, Node::load($node
      ->id())
      ->isPublished());
    $this->container
      ->get('cron')
      ->run();

    // Assert node is now unpublished.
    $this
      ->assertEquals(FALSE, Node::load($node
      ->id())
      ->isPublished());

    // Assert only one revision is created during the operation.
    $this
      ->assertEquals($revision_id + 1, Node::load($node
      ->id())
      ->getRevisionId());
  }

  /**
   * Tests publish scheduling for a draft of a published node.
   */
  public function testPublishOfDraft() {
    $term = $this
      ->loadTermByUuid('bfc251bc-de35-467d-af44-1f7a7012b845');
    $this
      ->articleFillNew([
      'field_channel' => $term
        ->id(),
      'title[0][value]' => 'Test workflow article 3 - Published',
      'field_seo_title[0][value]' => 'Massive gaining seo traffic text 3',
      'moderation_state[0]' => 'published',
    ]);
    $this
      ->clickSave();
    $node = $this
      ->getNodeByTitle('Test workflow article 3 - Published');
    $nid = $node
      ->id();

    // Assert node is published.
    $this
      ->assertEquals('Test workflow article 3 - Published', Node::load($nid)
      ->getTitle());

    // Create a new pending revision and validate it's not the default published
    // one.
    $node
      ->setTitle('Test workflow article 3 - Draft');
    $node
      ->set('publish_on', strtotime('yesterday'));
    $node
      ->set('moderation_state', 'draft');
    $node
      ->set('publish_state', 'published');
    $node
      ->save();
    $revision_id = $node
      ->getRevisionId();

    // Test latest revision is not the published one.
    $this
      ->assertEquals('Test workflow article 3 - Published', Node::load($nid)
      ->getTitle());
    $this->container
      ->get('cron')
      ->run();

    // Test latest revision is now the published one.
    $this
      ->assertEquals('Test workflow article 3 - Draft', Node::load($nid)
      ->getTitle());

    // Assert only one revision is created during the operation.
    $this
      ->assertEquals($revision_id + 1, Node::load($node
      ->id())
      ->getRevisionId());
  }

}

Classes

Namesort descending Description
ModeratedContentSchedulingTest Tests publishing/unpublishing scheduling for moderated nodes.