View source
<?php
namespace Drupal\Tests\scheduler\Functional;
class SchedulerPastDatesTest extends SchedulerBrowserTestBase {
public function testSchedulerPastDates($entityTypeId, $bundle) {
$storage = $this
->entityStorageObject($entityTypeId);
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$entityType = $this
->entityTypeObject($entityTypeId, $bundle);
$this
->drupalLogin($this->schedulerUser);
$title = 'Publish in the past ' . $this
->randomString(10);
$edit = [
"{$titleField}[0][value]" => $title,
'publish_on[0][value][date]' => $this->dateFormatter
->format(strtotime('-1 day', $this->requestTime), 'custom', 'Y-m-d'),
'publish_on[0][value][time]' => $this->dateFormatter
->format(strtotime('-1 day', $this->requestTime), 'custom', 'H:i:s'),
];
$entity = $this
->createEntity($entityTypeId, $bundle, [
'status' => FALSE,
]);
$created_time = $entity
->getCreatedTime();
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("The 'publish on' date must be in the future");
$entityType
->setThirdPartySetting('scheduler', 'publish_past_date', 'error')
->save();
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("The 'publish on' date must be in the future");
$entityType
->setThirdPartySetting('scheduler', 'publish_past_date', 'publish')
->save();
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextNotContains("The 'publish on' date must be in the future");
$this
->assertSession()
->pageTextMatches('/' . preg_quote($title, '/') . ' has been (updated|successfully saved)/');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$this
->assertTrue($entity
->isPublished(), 'The entity has been published immediately when the publication date is in the past and the "publish" behavior is chosen.');
$this
->assertNull($entity->publish_on->value, 'The entity publish_on date has been removed after publishing when the "publish" behavior is chosen.');
$this
->assertEquals($entity
->getChangedTime(), strtotime('-1 day', $this->requestTime), 'The changed time of the entity has been updated to the publish_on time when published immediately.');
$this
->assertEquals($entity
->getCreatedTime(), $created_time, 'The created time of the entity has not been changed when the "publish" behavior is chosen.');
$entityType
->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
->save();
$entity = $this
->createEntity($entityTypeId, $bundle, [
'status' => FALSE,
]);
$created_time = $entity
->getCreatedTime();
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextNotContains("The 'publish on' date must be in the future");
$this
->assertSession()
->pageTextContains(sprintf('%s is scheduled to be published', $title));
$this
->assertSession()
->pageTextMatches('/' . preg_quote($title, '/') . ' has been (updated|successfully saved)/');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$this
->assertFalse($entity
->isPublished(), 'The entity has been unpublished when the publication date is in the past and the "schedule" behavior is chosen.');
$this
->assertEquals(strtotime('-1 day', $this->requestTime), (int) $entity->publish_on->value, 'The entity has the correct publish_on date stored.');
scheduler_cron();
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$this
->assertTrue($entity
->isPublished(), 'The entity with publication date in the past and the "schedule" behavior has now been published by cron.');
$this
->assertEquals($entity
->getChangedTime(), strtotime('-1 day', $this->requestTime), 'The changed time of the entity has been updated to the publish_on time when published via cron.');
$this
->assertEquals($entity
->getCreatedTime(), $created_time, 'The created time of the entity has not been changed when the "schedule" behavior is chosen.');
$entityType
->setThirdPartySetting('scheduler', 'publish_past_date_created', TRUE)
->save();
$past_date_options = [
'publish' => 'publish',
'schedule' => 'schedule',
];
foreach ($past_date_options as $key => $option) {
$entityType
->setThirdPartySetting('scheduler', 'publish_past_date', $key)
->save();
$entity = $this
->createEntity($entityTypeId, $bundle, [
'status' => FALSE,
]);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
if ($option == 'schedule') {
scheduler_cron();
}
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$this
->assertEquals($entity
->getCreatedTime(), strtotime('-1 day', $this->requestTime), sprintf('The created time of the entity has not been changed when the %s option is chosen.', $option));
}
$edit = [
"{$titleField}[0][value]" => 'Unpublish in the past ' . $this
->randomString(10),
'unpublish_on[0][value][date]' => $this->dateFormatter
->format($this->requestTime - 3600, 'custom', 'Y-m-d'),
'unpublish_on[0][value][time]' => $this->dateFormatter
->format($this->requestTime - 3600, 'custom', 'H:i:s'),
];
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $bundle));
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("The 'unpublish on' date must be in the future");
}
}