View source
<?php
namespace Drupal\Tests\scheduler\Functional;
class SchedulerEventsTest extends SchedulerBrowserTestBase {
protected static $modules = [
'scheduler_api_test',
'menu_ui',
'path',
];
public function testNodeEvents() {
$this
->drupalLogin($this->schedulerUser);
$settings = [
'publish_on' => strtotime('-1 day'),
'type' => $this->type,
'promote' => FALSE,
'sticky' => FALSE,
'title' => 'API TEST node action',
];
$node = $this
->drupalCreateNode($settings);
$this
->assertFalse($node
->isSticky(), 'The unpublished node is not sticky.');
$this
->assertFalse($node
->isPromoted(), 'The unpublished node is not promoted.');
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isSticky(), 'API event "PRE_PUBLISH" has changed the node to sticky.');
$this
->assertTrue($node
->isPromoted(), 'API event "PUBLISH" has changed the node to promoted.');
$node
->set('unpublish_on', strtotime('-1 day'))
->set('sticky', TRUE)
->set('promote', TRUE)
->save();
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertFalse($node
->isSticky(), 'API event "PRE_UNPUBLISH" has changed the node to not sticky.');
$this
->assertFalse($node
->isPromoted(), 'API event "UNPUBLISH" has changed the node to not promoted.');
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_past_date', 'publish')
->save();
$node
->set('sticky', FALSE)
->set('promote', FALSE)
->save();
$edit = [
'publish_on[0][value][date]' => date('Y-m-d', strtotime('-2 day', $this->requestTime)),
'publish_on[0][value][time]' => date('H:i:s', strtotime('-2 day', $this->requestTime)),
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isSticky(), 'API event "PRE_PUBLISH_IMMEDIATELY" has changed the node to sticky.');
$this
->assertTrue($node
->isPromoted(), 'API event "PUBLISH_IMMEDIATELY" has changed the node to promoted.');
$this
->assertEquals('Published immediately', $node->title->value, 'API action "PUBLISH_IMMEDIATELY" has changed the node title correctly.');
}
public function testSchedulerEvents($entityTypeId, $bundle) {
$this
->drupalLogin($this->schedulerUser);
$storage = $this
->entityStorageObject($entityTypeId);
$title_prefix = strtoupper("API TEST {$entityTypeId}");
$entity = $this
->createEntity($entityTypeId, $bundle, [
'title' => $title_prefix,
'publish_on' => strtotime('-1 day'),
]);
scheduler_cron();
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$this
->assertEquals($title_prefix . ' - altered a second time by "PUBLISH" event', $entity
->label());
$entity = $this
->createEntity($entityTypeId, $bundle, [
'title' => $title_prefix,
'unpublish_on' => strtotime('-1 day'),
]);
scheduler_cron();
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$this
->assertEquals($title_prefix . ' - altered a second time by "UNPUBLISH" event', $entity
->label());
$this
->entityTypeObject($entityTypeId, $bundle)
->setThirdPartySetting('scheduler', 'publish_past_date', 'publish')
->save();
$entity = $this
->createEntity($entityTypeId, $bundle, [
'title' => $title_prefix,
'status' => FALSE,
]);
$edit = [
'publish_on[0][value][date]' => date('Y-m-d', strtotime('-2 day', $this->requestTime)),
'publish_on[0][value][time]' => date('H:i:s', strtotime('-2 day', $this->requestTime)),
];
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$this
->assertEquals($title_prefix . ' - altered a second time by "PUBLISH_IMMEDIATELY" event', $entity
->label());
}
public function dataSchedulerEvents() {
$data = $this
->dataStandardEntityTypes();
unset($data['#node']);
return $data;
}
}