You are here

public function SchedulerPermissionsTest::testUserPermissionsAdd in Scheduler 2.x

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

Tests that users without permission do not see the scheduler date fields.

@dataProvider dataPermissionsTest()

File

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

Class

SchedulerPermissionsTest
Tests some permissions of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testUserPermissionsAdd($entityTypeId, $bundle, $user) {
  $titleField = $entityTypeId == 'media' ? 'name' : 'title';

  // Log in with the required user, as specified by the parameter.
  $this
    ->drupalLogin($this->{$user});

  // Initially run tests when publishing and unpublishing are not required.
  $this
    ->entityTypeObject($entityTypeId)
    ->setThirdPartySetting('scheduler', 'publish_required', FALSE)
    ->setThirdPartySetting('scheduler', 'unpublish_required', FALSE)
    ->save();

  // Check that the fields are displayed as expected when creating an entity.
  // If the user variable matches the entity type id then that user has
  // scheduling permission on this type, so the fields should be shown.
  // Otherwise the fields should not be shown.
  $add_url = $this
    ->entityAddUrl($entityTypeId, $bundle);
  $this
    ->drupalGet($add_url);
  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]');
  }

  // Check that the new entity can be saved and published.
  $title = 'Published - ' . $this
    ->randomString(15);
  $edit = [
    "{$titleField}[0][value]" => $title,
    'status[value]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (created|successfully saved)/');
  $this
    ->assertNotEmpty($entity = $this
    ->getEntityByTitle($entityTypeId, $title), sprintf('The new %s with title "%s" was created sucessfully.', $entityTypeId, $title));
  $this
    ->assertTrue($entity
    ->isPublished(), 'The new entity is published');

  // Check that a new entity can be saved as unpublished.
  $title = 'Unpublished - ' . $this
    ->randomString(15);
  $edit = [
    "{$titleField}[0][value]" => $title,
    'status[value]' => FALSE,
  ];
  $this
    ->drupalGet($add_url);
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (created|successfully saved)/');
  $this
    ->assertNotEmpty($entity = $this
    ->getEntityByTitle($entityTypeId, $title), sprintf('The new %s with title "%s" was created sucessfully.', $entityTypeId, $title));
  $this
    ->assertFalse($entity
    ->isPublished(), 'The new entity is unpublished');

  // Set publishing and unpublishing to required, to make it a stronger test.
  // @todo Add tests when scheduled publishing and unpublishing are required.
  // Cannot be done until we make a decision on what 'required'  means.
  // @see https://www.drupal.org/node/2707411
  // "Conflict between 'required publishing' and not having scheduler
  // permission"
}