SchedulerBasicTest.php in Scheduler 8
File
tests/src/Functional/SchedulerBasicTest.php
View source
<?php
namespace Drupal\Tests\scheduler\Functional;
class SchedulerBasicTest extends SchedulerBrowserTestBase {
public function testPublishingAndUnpublishing() {
$this
->drupalLogin($this->schedulerUser);
$edit = [
'title[0][value]' => 'Publish This Node',
'publish_on[0][value][date]' => $this->dateFormatter
->format(time() + 3600, 'custom', 'Y-m-d'),
'publish_on[0][value][time]' => $this->dateFormatter
->format(time() + 3600, 'custom', 'H:i:s'),
];
$this
->helpTestScheduler($edit);
$edit['unpublish_on[0][value][date]'] = $edit['publish_on[0][value][date]'];
$edit['unpublish_on[0][value][time]'] = $edit['publish_on[0][value][time]'];
unset($edit['publish_on[0][value][date]']);
unset($edit['publish_on[0][value][time]']);
$edit['title[0][value]'] = 'Unpublish This Node';
$this
->helpTestScheduler($edit);
}
protected function helpTestScheduler($edit) {
$this
->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertNotEmpty($node, sprintf('"%s" was created sucessfully.', $edit['title[0][value]']));
if (empty($node)) {
$this
->assertTrue(FALSE, 'Test halted because node was not created.');
return;
}
if (isset($edit['publish_on[0][value][date]'])) {
$key = 'publish_on';
$this
->assertFalse($node
->isPublished(), 'Node is unpublished before Cron');
}
else {
$key = 'unpublish_on';
$this
->assertTrue($node
->isPublished(), 'Node is published before Cron');
}
$node->{$key} = $this->requestTime - 1;
$node
->save();
$this
->cronRun();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
if ($key == 'publish_on') {
$this
->assertTrue($node
->isPublished(), 'Node is published after Cron');
}
else {
$this
->assertFalse($node
->isPublished(), 'Node is unpublished after Cron');
}
}
}