View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\node\Entity\NodeType;
class SchedulerApiTest extends SchedulerBrowserTestBase {
protected static $modules = [
'scheduler_api_test',
'menu_ui',
'path',
];
protected function setUp() : void {
parent::setUp();
$this->customName = 'scheduler_api_test';
$this->customNodetype = NodeType::load($this->customName);
$this
->assertNotNull($this->customNodetype, 'Custom node type "' . $this->customName . '" was created during install');
$this->webUser = $this
->drupalCreateUser([
'create ' . $this->customName . ' content',
'edit any ' . $this->customName . ' content',
'schedule publishing of nodes',
]);
}
public function testAllowedPublishing() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/add/' . $this->customName);
$this
->assertSession()
->fieldExists('edit-field-approved-publishing-value');
$edit = [
'title[0][value]' => 'Set publish-on date without approval',
'publish_on[0][value][date]' => date('Y-m-d', time() + 3),
'publish_on[0][value][time]' => date('H:i:s', time() + 3),
];
$this
->drupalPostForm('node/add/' . $this->customName, $edit, 'Save');
$this
->assertSession()
->pageTextContains('is scheduled for publishing, but will not be published until approved.');
$node = $this
->createUnapprovedNode('publish_on');
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertFalse($node
->isPublished(), 'An unapproved node is not published during cron processing.');
$node = $this
->createUnapprovedNode('publish_on');
$this
->approveNode($node
->id(), 'field_approved_publishing');
$this
->assertFalse($node
->isPublished(), 'A new approved node is initially not published.');
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isPublished(), 'An approved node is published during cron processing.');
$this->customNodetype
->setThirdPartySetting('scheduler', 'publish_past_date', 'publish')
->save();
$node = $this
->createUnapprovedNode('publish_on');
$this
->assertFalse($node
->isPublished(), 'An unapproved node with a date in the past is not published immediately after saving.');
$this
->approveNode($node
->id(), 'field_approved_publishing');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isPublished(), 'An approved node with a date in the past is published immediately via $node->set()->save().');
$node = $this
->createUnapprovedNode('publish_on');
$this
->drupalPostForm('node/' . $node
->id() . '/edit', [
'field_approved_publishing[value]' => '1',
], 'Save');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isPublished(), 'An approved node with a date in the past is published immediately after saving via edit form.');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/reports/dblog');
}
public function testAllowedUnpublishing() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/add/' . $this->customName);
$this
->assertSession()
->fieldExists('edit-field-approved-unpublishing-value');
$edit = [
'title[0][value]' => 'Set unpublish-on date without approval',
'unpublish_on[0][value][date]' => date('Y-m-d', time() + 3),
'unpublish_on[0][value][time]' => date('H:i:s', time() + 3),
];
$this
->drupalPostForm('node/add/' . $this->customName, $edit, 'Save');
$this
->assertSession()
->pageTextContains('is scheduled for unpublishing, but will not be unpublished until approved.');
$node = $this
->createUnapprovedNode('unpublish_on');
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isPublished(), 'An unapproved node is not unpublished during cron processing.');
$node = $this
->createUnapprovedNode('unpublish_on');
$this
->approveNode($node
->id(), 'field_approved_unpublishing');
$this
->assertTrue($node
->isPublished(), 'A new approved node is initially published.');
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertFalse($node
->isPublished(), 'An approved node is unpublished during cron processing.');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/reports/dblog');
}
protected function createUnapprovedNode($date_field) {
$settings = [
'status' => $date_field == 'unpublish_on',
$date_field => strtotime('-1 day'),
'field_approved_publishing' => 0,
'field_approved_unpublishing' => 0,
'type' => $this->customName,
];
return $this
->drupalCreateNode($settings);
}
protected function approveNode($nid, $field_name) {
$this->nodeStorage
->resetCache([
$nid,
]);
$node = $this->nodeStorage
->load($nid);
$node
->set($field_name, TRUE)
->save();
}
public function testApiNodeAction() {
$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 action "PRE_PUBLISH" has changed the node to sticky.');
$this
->assertTrue($node
->isPromoted(), 'API action "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 action "PRE_UNPUBLISH" has changed the node to not sticky.');
$this
->assertFalse($node
->isPromoted(), 'API action "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
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, 'Save');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isSticky(), 'API action "PRE_PUBLISH_IMMEDIATELY" has changed the node to sticky.');
$this
->assertTrue($node
->isPromoted(), 'API action "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 testNidList() {
$this
->drupalLogin($this->schedulerUser);
$node1 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => FALSE,
'title' => 'API TEST nid_list publish me',
]);
$node2 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => TRUE,
'title' => 'API TEST nid_list unpublish me',
]);
$this
->assertFalse($node1
->isPublished(), 'Before cron, node 1 "' . $node1->title->value . '" is unpublished.');
$this
->assertTrue($node2
->isPublished(), 'Before cron, node 2 "' . $node2->title->value . '" is published.');
scheduler_cron();
$this->nodeStorage
->resetCache();
$node1 = $this->nodeStorage
->load($node1
->id());
$node2 = $this->nodeStorage
->load($node2
->id());
$this
->assertTrue($node1
->isPublished(), 'After cron, node 1 "' . $node1->title->value . '" is published.');
$this
->assertFalse($node2
->isPublished(), 'After cron, node 2 "' . $node2->title->value . '" is unpublished.');
}
public function testNidListAlter() {
$this
->drupalLogin($this->schedulerUser);
$node1 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => FALSE,
'title' => 'API TEST nid_list_alter do not publish me',
'publish_on' => strtotime('-1 day'),
]);
$node2 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => FALSE,
'title' => 'API TEST nid_list_alter publish me',
]);
$node3 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => TRUE,
'title' => 'API TEST nid_list_alter do not unpublish me',
'unpublish_on' => strtotime('-1 day'),
]);
$node4 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => TRUE,
'title' => 'API TEST nid_list_alter unpublish me',
]);
$this
->assertFalse($node1
->isPublished(), 'Before cron, node 1 "' . $node1->title->value . '" is unpublished.');
$this
->assertFalse($node2
->isPublished(), 'Before cron, node 2 "' . $node2->title->value . '" is unpublished.');
$this
->assertTrue($node3
->isPublished(), 'Before cron, node 3 "' . $node3->title->value . '" is published.');
$this
->assertTrue($node4
->isPublished(), 'Before cron, node 4 "' . $node4->title->value . '" is published.');
scheduler_cron();
$this->nodeStorage
->resetCache();
$node1 = $this->nodeStorage
->load($node1
->id());
$node2 = $this->nodeStorage
->load($node2
->id());
$node3 = $this->nodeStorage
->load($node3
->id());
$node4 = $this->nodeStorage
->load($node4
->id());
$this
->assertFalse($node1
->isPublished(), 'After cron, node 1 "' . $node1->title->value . '" is still unpublished.');
$this
->assertTrue($node2
->isPublished(), 'After cron, node 2 "' . $node2->title->value . '" is published.');
$this
->assertTrue($node3
->isPublished(), 'After cron, node 3 "' . $node3->title->value . '" is still published.');
$this
->assertFalse($node4
->isPublished(), 'After cron, node 4 "' . $node4->title->value . '" is unpublished.');
}
public function testHideField() {
$this
->drupalLogin($this->schedulerUser);
$node1 = $this
->drupalCreateNode([
'type' => $this->type,
'title' => 'Red will not have either field hidden',
]);
$node2 = $this
->drupalCreateNode([
'type' => $this->type,
'title' => 'Orange will have the publish-on field hidden',
]);
$node3 = $this
->drupalCreateNode([
'type' => $this->type,
'title' => 'Yellow will have the unpublish-on field hidden',
]);
$node4 = $this
->drupalCreateNode([
'type' => $this->type,
'title' => 'Green will have both Scheduler fields hidden',
]);
$assert = $this
->assertSession();
$this
->drupalGet('node/' . $node1
->id() . '/edit');
$assert
->ElementExists('xpath', '//input[@id = "edit-publish-on-0-value-date"]');
$assert
->ElementExists('xpath', '//input[@id = "edit-unpublish-on-0-value-date"]');
$this
->drupalGet('node/' . $node2
->id() . '/edit');
$assert
->ElementNotExists('xpath', '//input[@id = "edit-publish-on-0-value-date"]');
$assert
->ElementExists('xpath', '//input[@id = "edit-unpublish-on-0-value-date"]');
$this
->drupalGet('node/' . $node3
->id() . '/edit');
$assert
->ElementExists('xpath', '//input[@id = "edit-publish-on-0-value-date"]');
$assert
->ElementNotExists('xpath', '//input[@id = "edit-unpublish-on-0-value-date"]');
$this
->drupalGet('node/' . $node4
->id() . '/edit');
$assert
->ElementNotExists('xpath', '//input[@id = "edit-publish-on-0-value-date"]');
$assert
->ElementNotExists('xpath', '//input[@id = "edit-unpublish-on-0-value-date"]');
}
public function testHookPublishUnpublishAction() {
$this
->drupalLogin($this->schedulerUser);
$node1 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => FALSE,
'title' => 'Red will cause a failure on publishing',
'publish_on' => strtotime('-1 day'),
]);
$node2 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => TRUE,
'title' => 'Orange will be unpublished by the API test module not Scheduler',
'unpublish_on' => strtotime('-1 day'),
]);
$node3 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => FALSE,
'title' => 'Yellow will be published by the API test module not Scheduler',
'publish_on' => strtotime('-1 day'),
]);
$node4 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => TRUE,
'title' => 'Blue will cause a failure on unpublishing',
'unpublish_on' => strtotime('-1 day'),
]);
scheduler_cron();
$this->nodeStorage
->resetCache([
$node1
->id(),
]);
$node1 = $this->nodeStorage
->load($node1
->id());
$this
->assertFalse($node1
->isPublished(), 'The red node is still unpublished.');
$this
->assertNotEmpty($node1->publish_on->value, 'The red node still has a publish-on date.');
$this->nodeStorage
->resetCache([
$node2
->id(),
]);
$node2 = $this->nodeStorage
->load($node2
->id());
$this
->assertFalse($node2
->isPublished(), 'The orange node was unpublished by the API test module.');
$this
->assertNotEmpty(stristr($node2->title->value, 'unpublishing processed by API test module'), 'The orange node was processed by the API test module.');
$this
->assertEmpty($node2->unpublish_on->value, 'The orange node no longer has an unpublish-on date.');
$this->nodeStorage
->resetCache([
$node3
->id(),
]);
$node3 = $this->nodeStorage
->load($node3
->id());
$this
->assertTrue($node3
->isPublished(), 'The yellow node was published by the API test module.');
$this
->assertNotEmpty(stristr($node3->title->value, 'publishing processed by API test module'), 'The yellow node was processed by the API test module.');
$this
->assertEmpty($node3->publish_on->value, 'The yellow node no longer has a publish-on date.');
$this->nodeStorage
->resetCache([
$node4
->id(),
]);
$node4 = $this->nodeStorage
->load($node4
->id());
$this
->assertTrue($node4
->isPublished(), 'The green node is still published.');
$this
->assertNotEmpty($node4->unpublish_on->value, 'The green node still has an unpublish-on date.');
}
}