You are here

public function TrainingAccessTest::testTrainingVisibility in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x tests/src/Functional/TrainingAccessTest.php \Drupal\Tests\opigno_learning_path\Functional\TrainingAccessTest::testTrainingVisibility()

Tests training visibility for users.

File

tests/src/Functional/TrainingAccessTest.php, line 18

Class

TrainingAccessTest
Tests an access to Training.

Namespace

Drupal\Tests\opigno_learning_path\Functional

Code

public function testTrainingVisibility() {

  // Test access if training is public.
  $group = $this
    ->createGroup([
    'field_learning_path_visibility' => 'public',
    'field_learning_path_enable_forum' => 1,
  ]);
  $this
    ->drupalLogout();

  // Create authenticated user to check training access.
  $authenticated = $this
    ->createUser();
  $this
    ->drupalLogin($authenticated);
  $url = Url::fromRoute('entity.group.canonical', [
    'group' => $group
      ->id(),
  ]);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Authenticated user can not see an main page for unpublished training.');

  // Make training published.
  $group
    ->set('field_learning_path_published', TRUE);
  $group
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(200, 'Authenticated user can see an main page for published training.');

  // Authenticated user need to subscribe first.
  $this
    ->assertSession()
    ->linkExists('Subscribe to training');
  $this
    ->drupalLogout();

  // Check access for anonymous user.
  // $anonymous_user = $this->createUser()->getAnonymousUser();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->linkExists('Start');

  // Other links should not be visible for anonymous.
  $this
    ->assertSession()
    ->linkNotExists('Forum');
  $this
    ->assertSession()
    ->linkNotExists('Training Content');
  $this
    ->assertSession()
    ->linkNotExists('Documents Library');

  // Test access if training is semi-private.
  $group
    ->set('field_learning_path_visibility', 'semiprivate');
  $group
    ->save();

  // Check access for authenticated user.
  $this
    ->drupalLogin($authenticated);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->linkExists('Subscribe to training');
  $this
    ->drupalLogout();

  // Check access for anonymous user.
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains('About this training');
  $this
    ->assertSession()
    ->linkNotExists('Subscribe to training');

  // Test access if training is hidden for anonymous user.
  $group
    ->set('field_anonymous_visibility', 1);
  $group
    ->save();
  $this
    ->drupalGet($url);

  // Anonymous user should be redirected to login page.
  $this
    ->assertSession()
    ->pageTextContains('You are not authorized to access this page');

  // Test access if training is semi-private.
  $group
    ->set('field_learning_path_visibility', 'private');
  $group
    ->save();

  // Check access for authenticated user.
  $this
    ->drupalLogin($authenticated);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Authenticated user can not see a private training.');
  $this
    ->drupalLogout();

  // Check access for anonymous user.
  $this
    ->drupalGet($url);

  // Anonymous user should be redirected to login page.
  $this
    ->assertSession()
    ->pageTextContains('You are not authorized to access this page');
}