You are here

public function SchedulerPermissionsTest::testUserPermissionsEdit in Scheduler 2.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/SchedulerPermissionsTest.php \Drupal\Tests\scheduler\Functional\SchedulerPermissionsTest::testUserPermissionsEdit()

Tests that users without permission can edit existing scheduled content.

@dataProvider dataPermissionsTest()

File

tests/src/Functional/SchedulerPermissionsTest.php, line 119

Class

SchedulerPermissionsTest
Tests some permissions of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testUserPermissionsEdit($entityTypeId, $bundle, $user) {
  $storage = $this
    ->entityStorageObject($entityTypeId);
  $titleField = $entityTypeId == 'media' ? 'name' : 'title';

  // Log in with the required user, as specified by the parameter.
  $this
    ->drupalLogin($this->{$user});
  $publish_time = strtotime('+ 6 hours', $this->requestTime);
  $unpublish_time = strtotime('+ 10 hours', $this->requestTime);

  // Create an unpublished entity with a publish_on date.
  $unpublished_entity = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'publish_on' => $publish_time,
  ]);

  // Verify that the publish_on date is stored as expected before editing.
  $this
    ->assertEquals($publish_time, $unpublished_entity->publish_on->value, 'The publish_on value is stored correctly before edit.');

  // Edit the unpublished entity and check that the fields are displayed as
  // expected, depending on the user.
  $this
    ->drupalGet($unpublished_entity
    ->toUrl('edit-form'));
  if (strpos($user, $entityTypeId) !== FALSE) {
    $this
      ->assertSession()
      ->fieldExists('publish_on[0][value][date]');
    $this
      ->assertSession()
      ->fieldExists('unpublish_on[0][value][date]');
  }
  else {
    $this
      ->assertSession()
      ->fieldNotExists('publish_on[0][value][date]');
    $this
      ->assertSession()
      ->fieldNotExists('unpublish_on[0][value][date]');
  }

  // Save the entity and check the title is updated as expected.
  $title = 'For Publishing ' . $this
    ->randomString(10);
  $this
    ->submitForm([
    "{$titleField}[0][value]" => $title,
  ], 'Save');
  $unpublished_entity = $storage
    ->load($unpublished_entity
    ->id());
  $this
    ->assertEquals($title, $unpublished_entity
    ->label(), 'The unpublished entity title has been updated correctly after edit.');

  // Test that the publish_on date is still stored and is unchanged.
  $this
    ->assertEquals($publish_time, $unpublished_entity->publish_on->value, 'The publish_on value is still stored correctly after edit.');

  // Repeat for unpublishing. Create an entity scheduled for unpublishing.
  $published_entity = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'unpublish_on' => $unpublish_time,
  ]);

  // Verify that the unpublish_on date is stored as expected before editing.
  $this
    ->assertEquals($unpublish_time, $published_entity->unpublish_on->value, 'The unpublish_on value is stored correctly before edit.');

  // Edit the published entity and save.
  $title = 'For Unpublishing ' . $this
    ->randomString(10);
  $this
    ->drupalGet($published_entity
    ->toUrl('edit-form'));
  $this
    ->submitForm([
    "{$titleField}[0][value]" => $title,
  ], 'Save');

  // Check the updated title, to verify that edit and save was sucessful.
  $published_entity = $storage
    ->load($published_entity
    ->id());
  $this
    ->assertEquals($title, $published_entity
    ->label(), 'The published entity title has been updated correctly after edit.');

  // Test that the unpublish_on date is still stored and is unchanged.
  $this
    ->assertEquals($unpublish_time, $published_entity->unpublish_on->value, 'The unpublish_on value is still stored correctly after edit.');
}