public function SchedulerApiTest::testAllowedPublishing in Scheduler 8
Covers hook_scheduler_allow_publishing()
This hook can allow or deny the publishing of individual nodes. This test uses the customised content type which has checkboxes 'Approved for publication' and 'Approved for unpublication'.
@todo Create and update the nodes through the interface so we can check if the correct messages are displayed.
File
- tests/
src/ Functional/ SchedulerApiTest.php, line 58
Class
- SchedulerApiTest
- Tests the API of the Scheduler module.
Namespace
Drupal\Tests\scheduler\FunctionalCode
public function testAllowedPublishing() {
$this
->drupalLogin($this->webUser);
// Check the 'approved for publishing' field is shown on the node form.
$this
->drupalGet('node/add/' . $this->customName);
$this
->assertSession()
->fieldExists('edit-field-approved-publishing-value');
// Check that the message is shown when scheduling a node for publishing
// which is not yet allowed to be published.
$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.');
// Create a node that is scheduled but not approved for publication. Then
// simulate a cron run, and check that the node is still not published.
$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.');
// Create a node and approve it for publication, simulate a cron run and
// check that the node is published. This is a stronger test than simply
// approving the previously used node above, as we do not know what publish
// state that may be in after the cron run above.
$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.');
// Turn on immediate publication of nodes with publication dates in the past
// and repeat the tests. It is not needed to simulate cron runs here.
$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.');
// Check that the node can be approved and published programatically.
$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().');
// Check that a node can be approved and published via edit form.
$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.');
// Show the dblog messages.
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/reports/dblog');
}