View source
<?php
namespace Drupal\Tests\lightning_scheduler\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\workflows\Entity\Workflow;
class ScheduledTransitionTest extends BrowserTestBase {
use CronRunTrait;
protected static $modules = [
'lightning_scheduler',
'node',
];
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
]);
node_access_rebuild();
$workflow = Workflow::load('editorial');
$plugin = $workflow
->getTypePlugin();
$plugin
->addEntityTypeAndBundle('node', 'page');
$workflow
->save();
$account = $this
->drupalCreateUser([
'create page content',
'edit own page content',
'view own unpublished content',
'view latest version',
'use editorial transition create_new_draft',
'use editorial transition publish',
'use editorial transition archive',
'use editorial transition archived_draft',
'schedule editorial transition publish',
'schedule editorial transition archive',
]);
$this
->drupalLogin($account);
$account
->set('timezone', 'America/New_York')
->save();
$this
->config('system.date')
->set('timezone.default', 'America/New_York')
->save();
date_default_timezone_set('UTC');
}
public function providerSingleTransition() {
return [
'draft to published in future' => [
10,
'published',
'Published',
],
'draft to archived in future' => [
10,
'archived',
'Draft',
],
'draft to published in past' => [
-10,
'published',
'Published',
],
'draft to archived in past' => [
-10,
'archived',
'Draft',
],
];
}
public function testSingleTransition($offset, $to_state, $expected_state_label) {
$assert = $this
->assertSession();
$this
->drupalGet('/node/add/page');
$assert
->statusCodeEquals(200);
$transitions = Json::encode([
[
'when' => date('c', time() + $offset),
'state' => $to_state,
],
]);
$assert
->fieldExists('Title')
->setValue('Schedule This');
$field = $assert
->elementExists('css', 'input[name="moderation_state[0][scheduled_transitions][data]"]');
$field
->setValue($transitions);
$assert
->buttonExists('Save')
->press();
$assert
->addressMatches('/^\\/node\\/[0-9]+$/');
$edit_url = $this
->getUrl() . '/edit';
if ($offset > 0) {
sleep($offset + 2);
}
$this
->cronRun();
$this
->drupalGet($edit_url);
$assert
->statusCodeEquals(200);
$assert
->pageTextContains("Current state {$expected_state_label}");
}
public function testFutureSequence() {
$assert = $this
->assertSession();
$this
->drupalGet('/node/add/page');
$assert
->statusCodeEquals(200);
$transitions = Json::encode([
[
'when' => date('c', time() + 10),
'state' => 'published',
],
[
'when' => date('c', time() + 20),
'state' => 'archived',
],
]);
$assert
->fieldExists('Title')
->setValue('Schedule This');
$field = $assert
->elementExists('css', 'input[name="moderation_state[0][scheduled_transitions][data]"]');
$field
->setValue($transitions);
$assert
->buttonExists('Save')
->press();
$assert
->addressMatches('/^\\/node\\/[0-9]+$/');
$edit_url = $this
->getUrl() . '/edit';
sleep(12);
$this
->cronRun();
sleep(12);
$this
->cronRun();
$this
->drupalGet($edit_url);
$assert
->statusCodeEquals(200);
$assert
->pageTextContains("Current state Archived");
}
public function testInvalidPastSequence() {
$assert = $this
->assertSession();
$this
->drupalGet('/node/add/page');
$assert
->statusCodeEquals(200);
$transitions = Json::encode([
[
'when' => date('c', time() - 20),
'state' => 'published',
],
[
'when' => date('c', time() - 10),
'state' => 'archived',
],
]);
$assert
->fieldExists('Title')
->setValue('Schedule This');
$field = $assert
->elementExists('css', 'input[name="moderation_state[0][scheduled_transitions][data]"]');
$field
->setValue($transitions);
$assert
->buttonExists('Save')
->press();
$assert
->addressMatches('/^\\/node\\/[0-9]+$/');
$edit_url = $this
->getUrl() . '/edit';
$this
->cronRun();
$this
->drupalGet($edit_url);
$assert
->statusCodeEquals(200);
$assert
->pageTextContains("Current state Draft");
}
public function testSingleTransitionWithPendingRevision() {
$assert = $this
->assertSession();
$this
->drupalGet('/node/add/page');
$assert
->statusCodeEquals(200);
$assert
->fieldExists('Title')
->setValue('Schedule This');
$assert
->fieldExists('moderation_state[0][state]')
->selectOption('Published');
$assert
->buttonExists('Save')
->press();
$assert
->addressMatches('/^\\/node\\/[0-9]+$/');
$edit_url = $this
->getUrl() . '/edit';
$this
->drupalGet($edit_url);
$assert
->fieldExists('Title')
->setValue('MC Hammer');
$assert
->fieldExists('moderation_state[0][state]')
->selectOption('Draft');
$transitions = Json::encode([
[
'when' => date('c', time() + 10),
'state' => 'published',
],
]);
$field = $assert
->elementExists('css', 'input[name="moderation_state[0][scheduled_transitions][data]"]');
$field
->setValue($transitions);
$assert
->buttonExists('Save')
->press();
sleep(12);
$this
->cronRun();
$this
->drupalGet($edit_url);
$assert
->statusCodeEquals(200);
$assert
->fieldValueEquals('Title', 'MC Hammer');
}
}