diff --git a/src/Tests/SchedulerTestBase.php b/src/Tests/SchedulerTestBase.php index 14e1ac2..1913bc3 100644 --- a/src/Tests/SchedulerTestBase.php +++ b/src/Tests/SchedulerTestBase.php @@ -53,6 +53,7 @@ abstract class SchedulerTestBase extends WebTestBase { 'administer nodes', 'access content', 'access content overview', + 'access site reports', // required for admin/reports/dblog 'administer site configuration', // required for admin/reports/status 'create page content', 'edit own page content', diff --git a/tests/modules/scheduler_access_test/scheduler_access_test.info.yml b/tests/modules/scheduler_access_test/scheduler_access_test.info.yml new file mode 100644 index 0000000..6cdf664 --- /dev/null +++ b/tests/modules/scheduler_access_test/scheduler_access_test.info.yml @@ -0,0 +1,5 @@ +name: 'Scheduler Access Test' +type: module +description: 'Support module for Scheduler restricted node access testing.' +package: Testing +core: 8.x diff --git a/tests/modules/scheduler_access_test/scheduler_access_test.install b/tests/modules/scheduler_access_test/scheduler_access_test.install new file mode 100644 index 0000000..130c165 --- /dev/null +++ b/tests/modules/scheduler_access_test/scheduler_access_test.install @@ -0,0 +1,21 @@ + 'scheduler', + 'gid' => 1, + 'grant_view' => 0, + 'grant_update' => 0, + 'grant_delete' => 0, + ]); + return $grants; +} + +/** + * Implements hook_node_grants(). + */ +function scheduler_access_test_node_grants(AccountInterface $account, $op) { + // This hook needs to exist, but we do not return any grants. + return array(); +} diff --git a/src/Tests/SchedulerNodeAccessTest.php b/src/Tests/SchedulerNodeAccessTest.php new file mode 100644 index 0000000..aa15dbe --- /dev/null +++ b/src/Tests/SchedulerNodeAccessTest.php @@ -0,0 +1,80 @@ +nodeStorage = $this->container->get('entity.manager')->getStorage('node'); + // scheduler_access_test_install() sets node_access_needs_rebuild(TRUE) and + // this works when testing the module interactively, but during simpletest + // the node access table is not rebuilt. Hence do that here explicitly here. + node_access_rebuild(); + } + + /** + * Tests Scheduler cron functionality when access to the nodes is denied. + * + * The test module scheduler_access_test denies access to all nodes. + */ + public function testNodeAccess() { + + // Get the internal name of the content type. + $type = $this->nodetype->get('type'); + + // Create data test publishing then unpublishing via loop. + $test_data = array( + 'publish_on' => ['status' => FALSE, 'before' => 'unpublished', 'after' => 'published'], + 'unpublish_on' => ['status' => TRUE, 'before' => 'published', 'after' => 'unpublished'], + ); + + foreach ($test_data as $field => $data) { + // Create a node with the required scheduler date. + $settings = [ + 'type' => $type, + 'status' => $data['status'], + 'title' => 'Test node to be ' . $data['after'], + $field => REQUEST_TIME + 1 + ]; + $node = $this->drupalCreateNode($settings); + $this->drupalGet('node/' . $node->id()); + $this->assertResponse(403, 'Before cron, viewing the ' . $data['before'] . ' node returns "403 Not Authorized"'); + + // Delay so that the date entered is now in the past, then run cron. + sleep(2); + $this->cronRun(); + + // Reload the node. + $this->nodeStorage->resetCache([$node->id()]); + $node = $this->nodeStorage->load($node->id()); + // Check that the node has been published or unpublished as required. + $this->assertTrue($node->isPublished() === !$data['status'], 'Scheduler has ' . $data['after'] . ' the node via cron.'); + + // Check the node is still not viewable. + $this->drupalGet('node/' . $node->id()); + $this->assertResponse(403, 'After cron, viewing the ' . $data['after'] . ' node returns "403 Not Authorized"'); + } + + // Log in and show the dblog for info only. + $this->drupalLogin($this->adminUser); + $this->drupalGet('admin/reports/dblog'); + $this->assertText('scheduled publishing', '"Scheduled publishing" message is shown in the dblog'); + $this->assertText('scheduled unpublishing', '"Scheduled unpublishing" message is shown in the dblog'); + + } +}