You are here

public function SchedulerEntityAccessTest::testEntityAccess in Scheduler 2.x

Tests Scheduler cron functionality when access to the entity is denied.

@dataProvider dataEntityAccess()

File

tests/src/Functional/SchedulerEntityAccessTest.php, line 42

Class

SchedulerEntityAccessTest
Tests that Scheduler cron has full access to the scheduled entities.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testEntityAccess($entityTypeId, $bundle, $field, $status) {
  $storage = $this
    ->entityStorageObject($entityTypeId);

  // scheduler_access_test_install() sets node_access_needs_rebuild(TRUE) and
  // this works when testing the module interactively, but in a phpunit run
  // the node access table is not rebuilt. Hence do that explicitly here.
  node_access_rebuild();

  // Login as a user who is only able to view the published entities.
  $this
    ->drupalLogin($this
    ->drupalCreateUser());

  // Create an entity with the necessary scheduler date.
  $process = $status ? 'unpublishing' : 'publishing';
  $settings = [
    'status' => $status,
    'title' => "{$entityTypeId} {$bundle} for {$process}",
    $field => $this->requestTime + 1,
  ];
  $entity = $this
    ->createEntity($entityTypeId, $bundle, $settings);
  $this
    ->drupalGet($entity
    ->toUrl());

  // Before running cron, viewing the entity should give "403 Not Authorized"
  // regardless of whether it is published or unpublished.
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Delay so that the date entered is now in the past, then run cron.
  sleep(2);
  $this
    ->cronRun();

  // Reload the entity.
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());

  // Check that the entity has been published or unpublished as required.
  $this
    ->assertTrue($entity
    ->isPublished() === !$status, "Scheduled {$process} of {$entityTypeId} via cron.");

  // Check that the entity is still not viewable.
  $this
    ->drupalGet($entity
    ->toUrl());

  // After cron, viewing the entity should still give "403 Not Authorized".
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Log in as admin and check that the dblog cron message is shown.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/reports/dblog');
  $this
    ->assertSession()
    ->pageTextContains($this
    ->entityTypeObject($entityTypeId)
    ->label() . ": scheduled {$process}");
}