View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\node\NodeInterface;
class SchedulerRevisioningTest extends SchedulerBrowserTestBase {
protected function schedule(NodeInterface $node, $action = 'publish') {
$node->{$action . '_on'} = strtotime('-5 hour', $this->requestTime);
$node
->save();
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
return $this->nodeStorage
->load($node
->id());
}
protected function assertRevisionCount($nid, $value, $message = '') {
$count = $this->nodeStorage
->getLatestRevisionId($nid);
$this
->assertEquals($value, (int) $count, $message);
}
protected function assertRevisionLogMessage($nid, $value, $message = '') {
$log_message = $this->database
->select('node_revision', 'r')
->fields('r', [
'revision_log',
])
->condition('nid', $nid)
->orderBy('vid', 'DESC')
->range(0, 1)
->execute()
->fetchField();
return $this
->assertEquals($value, $log_message, $message);
}
public function testRevisioning() {
$created = strtotime('-2 day', $this->requestTime);
$settings = [
'type' => $this->type,
'revision' => 0,
'created' => $created,
];
$node = $this
->drupalCreateNode($settings);
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
->save();
$node = $this
->schedule($node);
$this
->assertRevisionCount($node
->id(), 1, 'No new revision is created by default when a node is published.');
$node = $this
->schedule($node, 'unpublish');
$this
->assertRevisionCount($node
->id(), 1, 'No new revision is created by default when a node is unpublished.');
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_revision', TRUE)
->setThirdPartySetting('scheduler', 'unpublish_revision', TRUE)
->save();
$node = $this
->schedule($node);
$this
->assertRevisionCount($node
->id(), 2, 'A new revision was created when revisioning is enabled.');
$expected_message = sprintf('Published by Scheduler. The scheduled publishing date was %s.', $this->dateFormatter
->format(strtotime('-5 hour', $this->requestTime), 'short'));
$this
->assertRevisionLogMessage($node
->id(), $expected_message, 'The correct message was found in the node revision log after scheduled publishing.');
$node = $this
->schedule($node, 'unpublish');
$this
->assertRevisionCount($node
->id(), 3, 'A new revision was created when a node was unpublished with revisioning enabled.');
$expected_message = sprintf('Unpublished by Scheduler. The scheduled unpublishing date was %s.', $this->dateFormatter
->format(strtotime('-5 hour', $this->requestTime), 'short'));
$this
->assertRevisionLogMessage($node
->id(), $expected_message, 'The correct message was found in the node revision log after scheduled unpublishing.');
}
public function testAlterCreationDate() {
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
->save();
$created = strtotime('-2 day', $this->requestTime);
$settings = [
'type' => $this->type,
'created' => $created,
'status' => FALSE,
];
$node = $this
->drupalCreateNode($settings);
$this
->assertFalse($node
->isPublished(), 'The node is not published.');
$node = $this
->schedule($node, 'publish');
$created_after_cron = $node->created->value;
$this
->assertTrue($node
->isPublished(), 'The node has been published.');
$this
->assertEquals($created, $created_after_cron, 'The node creation date is not changed by default.');
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_touch', TRUE)
->save();
$node = $this
->schedule($node, 'publish');
$created_after_cron = $node->created->value;
$this
->assertEquals(strtotime('-5 hour', $this->requestTime), $created_after_cron, "With 'touch' option set, the node creation date is changed to match the publishing date.");
}
}