View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\node\Entity\NodeType;
class SchedulerHooksLegacyTest extends SchedulerBrowserTestBase {
protected static $modules = [
'scheduler_api_test',
'scheduler_api_legacy_test',
'menu_ui',
'path',
];
protected function setUp() : void {
parent::setUp();
$this->customName = 'scheduler_api_node_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',
]);
$this->webUser
->set('name', 'Wenlock the Web user')
->save();
}
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 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
->drupalGet("node/add/{$this->customName}");
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextMatches('/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(), "Unapproved '{$node->label()}' should not be published during cron processing.");
$node = $this
->createUnapprovedNode('publish_on');
$this
->approveNode($node
->id(), 'field_approved_publishing');
$this
->assertFalse($node
->isPublished(), "New approved '{$node->label()}' should not be initially published.");
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isPublished(), "Approved '{$node->label()}' should be published during cron processing.");
$this->customNodetype
->setThirdPartySetting('scheduler', 'publish_past_date', 'publish')
->save();
$node = $this
->createUnapprovedNode('publish_on');
$this
->assertFalse($node
->isPublished(), "New unapproved '{$node->label()}' with a date in the past should not be 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(), "New approved '{$node->label()}' with a date in the past should be published immediately when created programatically.");
$node = $this
->createUnapprovedNode('publish_on');
$this
->drupalGet("node/{$node->id()}/edit");
$this
->submitForm([
'field_approved_publishing[value]' => '1',
], 'Save');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertTrue($node
->isPublished(), "Approved '{$node->label()}' with a date in the past should be 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
->drupalGet("node/add/{$this->customName}");
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextMatches('/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(), "Unapproved '{$node->label()}' should not be unpublished during cron processing.");
$node = $this
->createUnapprovedNode('unpublish_on');
$this
->approveNode($node
->id(), 'field_approved_unpublishing');
$this
->assertTrue($node
->isPublished(), "New approved '{$node->label()}' should initially remain published.");
scheduler_cron();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertFalse($node
->isPublished(), "Approved '{$node->label()}' should be unpublished during cron processing.");
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/reports/dblog');
}
protected function createUnapprovedNode($date_field) {
$settings = [
'title' => ($date_field == 'publish_on' ? 'Blue' : 'Red') . " legacy node {$this->randomMachineName(10)}",
'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);
$node
->set('title', $node
->label() . " - approved for publishing: {$node->field_approved_publishing->value}, for unpublishing: {$node->field_approved_unpublishing->value}")
->save();
}
public function testHideField() {
$this
->drupalLogin($this->schedulerUser);
$node1 = $this
->drupalCreateNode([
'type' => $this->type,
'title' => 'Red Legacy will not have either field hidden',
]);
$node2 = $this
->drupalCreateNode([
'type' => $this->type,
'title' => 'Orange Legacy will have the publish-on field hidden',
]);
$node3 = $this
->drupalCreateNode([
'type' => $this->type,
'title' => 'Yellow Legacy will have the unpublish-on field hidden',
]);
$node4 = $this
->drupalCreateNode([
'type' => $this->type,
'title' => 'Green Legacy will have both Scheduler fields hidden',
]);
$this->nodetype
->setThirdPartySetting('scheduler', 'expand_fieldset', 'always')
->save();
$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 testPublishUnpublishAction() {
$this
->drupalLogin($this->schedulerUser);
$node1 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => FALSE,
'title' => 'Red Legacy will cause a failure on publishing',
'publish_on' => strtotime('-1 day'),
]);
$node2 = $this
->drupalCreateNode([
'type' => $this->type,
'status' => TRUE,
'title' => 'Orange Legacy 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 Legacy 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 Legacy 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.');
}
}