View source
<?php
namespace Drupal\Tests\thunder\FunctionalJavascript;
use Drupal\node\Entity\Node;
class ModeratedContentSchedulingTest extends ThunderJavascriptTestBase {
use ThunderArticleTestTrait;
public function testPublishStateSchedule() {
$publish_timestamp = strtotime('yesterday');
$node_storage = \Drupal::entityTypeManager()
->getStorage('node');
$this
->articleFillNew([
'field_channel' => 1,
'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();
$node = $this
->getNodeByTitle('Test workflow article 1 - Published');
$revision_id = $node
->getRevisionId();
$this
->assertEquals(FALSE, Node::load($node
->id())
->isPublished());
$this->container
->get('cron')
->run();
$node = $node_storage
->loadRevision($node_storage
->getLatestRevisionId($node
->id()));
$this
->assertEquals(TRUE, $node
->isPublished());
$this
->assertEquals('published', $node->moderation_state->value);
$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(),
]);
$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();
$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());
}
public function testUnpublishStateSchedule() {
$this
->articleFillNew([
'field_channel' => 1,
'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');
$node->unpublish_on->value = strtotime('yesterday');
$node
->save();
$revision_id = $node
->getRevisionId();
$this
->assertEquals(TRUE, Node::load($node
->id())
->isPublished());
$this->container
->get('cron')
->run();
$this
->assertEquals(FALSE, Node::load($node
->id())
->isPublished());
$this
->assertEquals($revision_id + 1, Node::load($node
->id())
->getRevisionId());
}
public function testPublishOfDraft() {
$this
->articleFillNew([
'field_channel' => 1,
'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();
$this
->assertEquals('Test workflow article 3 - Published', Node::load($nid)
->getTitle());
$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();
$this
->assertEquals('Test workflow article 3 - Published', Node::load($nid)
->getTitle());
$this->container
->get('cron')
->run();
$this
->assertEquals('Test workflow article 3 - Draft', Node::load($nid)
->getTitle());
$this
->assertEquals($revision_id + 1, Node::load($node
->id())
->getRevisionId());
}
}