View source
<?php
namespace Drupal\Tests\scheduler\Functional;
class SchedulerDefaultTimeTest extends SchedulerBrowserTestBase {
protected static $modules = [
'scheduler_extras',
];
protected $defaultTime;
protected $publishTime;
protected $unpublishTime;
protected function setUp() : void {
parent::setUp();
$this->defaultTime = '06:30:15';
$config = $this
->config('scheduler.settings');
$config
->set('default_time', $this->defaultTime)
->save();
$this->publishTime = new \DateTime();
$this->publishTime
->add(new \DateInterval('P1D'))
->setTime(6, 30, 15);
$this->unpublishTime = new \DateTime();
$this->unpublishTime
->add(new \DateInterval('P2D'))
->setTime(6, 30, 15);
}
public function testDefaultTime() {
$this
->drupalLogin($this->schedulerUser);
$config = $this
->config('scheduler.settings');
$publish_validation_message = 'The Publish on date is invalid.';
$unpublish_validation_message = 'The Unpublish on date is invalid.';
$config
->set('allow_date_only', FALSE)
->save();
$edit = [
'title[0][value]' => 'No time ' . $this
->randomMachineName(8),
'publish_on[0][value][date]' => $this->publishTime
->format('Y-m-d'),
'unpublish_on[0][value][date]' => $this->unpublishTime
->format('Y-m-d'),
];
$this
->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
$this
->assertSession()
->pageTextNotMatches('/' . $edit['title[0][value]'] . ' is scheduled to be published .* and unpublished .*/');
$this
->assertSession()
->pageTextContains($publish_validation_message);
$this
->assertSession()
->pageTextContains($unpublish_validation_message);
$config
->set('allow_date_only', TRUE)
->save();
$this
->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
$this
->assertSession()
->pageTextNotContains($publish_validation_message);
$this
->assertSession()
->pageTextNotContains($unpublish_validation_message);
$date_format_storage = $this->container
->get('entity_type.manager')
->getStorage('date_format');
$long_pattern = $date_format_storage
->load('long')
->getPattern();
$this
->assertSession()
->pageTextContains(sprintf('%s is scheduled to be published %s and unpublished %s', $edit['title[0][value]'], $this->publishTime
->format($long_pattern), $this->unpublishTime
->format($long_pattern)));
if ($node = $this
->drupalGetNodeByTitle($edit['title[0][value]'])) {
$this
->assertEquals($this->publishTime
->getTimestamp(), (int) $node->publish_on->value, 'The node publish_on value is stored correctly.');
$this
->assertEquals($this->unpublishTime
->getTimestamp(), (int) $node->unpublish_on->value, 'The node unpublish_on value is stored correctly.');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->FieldValueEquals('publish_on[0][value][time]', $this->defaultTime);
$this
->assertSession()
->FieldValueEquals('unpublish_on[0][value][time]', $this->defaultTime);
}
else {
$this
->fail('The expected node was not found.');
}
}
public function testDefaultTimeWithHiddenTime() {
$type = 'hidden_time';
$typeName = 'Content with hidden times';
$nodetype = $this
->drupalCreateContentType([
'type' => $type,
'name' => $typeName,
]);
$nodetype
->setThirdPartySetting('scheduler', 'publish_enable', TRUE)
->setThirdPartySetting('scheduler', 'unpublish_enable', TRUE)
->save();
$this
->drupalLogin($this
->drupalCreateUser([
'create ' . $type . ' content',
'edit own ' . $type . ' content',
'delete own ' . $type . ' content',
'view own unpublished content',
'schedule publishing of nodes',
'view scheduled content',
]));
$this
->config('scheduler.settings')
->set('allow_date_only', TRUE)
->save();
$edit = [
'title[0][value]' => 'Hidden Time Elements ' . $this
->randomMachineName(8),
'publish_on[0][value][date]' => $this->publishTime
->format('Y-m-d'),
'unpublish_on[0][value][date]' => $this->unpublishTime
->format('Y-m-d'),
];
$this
->drupalPostForm('node/add/' . $type, $edit, 'Save');
$date_format_storage = $this->container
->get('entity_type.manager')
->getStorage('date_format');
$long_pattern = $date_format_storage
->load('long')
->getPattern();
$this
->assertSession()
->pageTextContains(sprintf('%s is scheduled to be published %s and unpublished %s', $edit['title[0][value]'], $this->publishTime
->format($long_pattern), $this->unpublishTime
->format($long_pattern)));
if ($node = $this
->drupalGetNodeByTitle($edit['title[0][value]'])) {
$this
->assertEquals($this->publishTime
->getTimestamp(), (int) $node->publish_on->value, 'The node publish_on value is stored correctly.');
$this
->assertEquals($this->unpublishTime
->getTimestamp(), (int) $node->unpublish_on->value, 'The node unpublish_on value is stored correctly.');
}
else {
$this
->fail('The expected node was not found.');
}
}
}