You are here

public function TrainingAccessTest::testTrainingStart 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::testTrainingStart()

Tests which users can subscribe and start a training.

File

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

Class

TrainingAccessTest
Tests an access to Training.

Namespace

Drupal\Tests\opigno_learning_path\Functional

Code

public function testTrainingStart() {

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

  // Log out privileged user.
  $this
    ->drupalLogout();
  $subscribe_path = Url::fromRoute('entity.group.join', [
    'group' => $group
      ->id(),
  ]);
  $start_path = Url::fromRoute('opigno_learning_path.steps.start', [
    'group' => $group
      ->id(),
  ]);
  $canonical_path = Url::fromRoute('entity.group.canonical', [
    'group' => $group
      ->id(),
  ]);

  // Create authenticated user to check a training access.
  $user_one = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user_one);

  // Authenticated user need to subscribe to a training first.
  $this
    ->drupalGet($subscribe_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $join_button = $this
    ->assertSession()
    ->buttonExists('Join training');
  $join_button
    ->click();

  // Successful subscribed.
  $this
    ->assertSession()
    ->statusCodeEquals(200, 'Authenticated user can subscribe to a public training.');

  // Authenticated user can start training.
  $this
    ->drupalGet($start_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200, 'Authenticated user can start a public training.');
  $this
    ->assertSession()
    ->pageTextNotContains('You are not authorized to access this page');
  $this
    ->drupalLogout();

  // Anonymous user can't join to a public training and can immediately start.
  $this
    ->drupalGet($subscribe_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Anonymous user can not subscribe to a public training. Redirected to login page.');
  $this
    ->assertSession()
    ->pageTextContains('You are not authorized to access this page');
  $this
    ->drupalGet($start_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200, 'Anonymous user can start a public training.');

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

  // Create authenticated user to check a training access.
  $user_two = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user_two);

  // Authenticated user need to subscribe to a training first.
  $this
    ->drupalGet($subscribe_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $join_button = $this
    ->assertSession()
    ->buttonExists('Join training');
  $join_button
    ->click();

  // Successful subscribed.
  $this
    ->assertSession()
    ->statusCodeEquals(200, 'Authenticated user can subscribe to a semi-private training.');

  // Authenticated user can start a training.
  $this
    ->drupalGet($start_path);
  $this
    ->assertSession()
    ->pageTextContains('No first step assigned');
  $this
    ->assertSession()
    ->statusCodeEquals(200, 'Authenticated user can start a semi-private training.');
  $this
    ->drupalLogout();

  // Anonymous user can't subscribe to a semi-private training.
  $this
    ->drupalGet($subscribe_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Anonymous user can not subscribe to a semi-private training.');

  // Anonymous user can't start a semi-private training.
  $this
    ->resetAll();
  $this
    ->drupalGet($start_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Anonymous user can not start a semi-private training.');

  // Test access for a training
  // where user need to be accepted by training admin.
  $this
    ->drupalLogin($user_two);
  $group
    ->removeMember($user_two);
  $group
    ->set('field_requires_validation', 1);
  $group
    ->save();
  $this
    ->drupalGet($subscribe_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $join_button = $this
    ->assertSession()
    ->buttonExists('Join training');
  $join_button
    ->click();
  $this
    ->drupalGet($start_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Authenticated user can not start a semi-private training if user validation is required.');
  $this
    ->drupalLogout();

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

  // Create authenticated user to check a training access.
  $user_three = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user_three);

  // Authenticated user cant to subscribe to a training.
  $this
    ->drupalGet($subscribe_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Authenticated user can not subscribe to a private training.');

  // Authenticated user can not start a training.
  $this
    ->drupalGet($start_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Authenticated user can not start a private training.');
  $this
    ->drupalLogout();

  // Anonymous user can't subscribe to a private training.
  $this
    ->drupalGet($subscribe_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Anonymous user can not subscribe to a private training.');

  // Anonymous user can't start a private training.
  $this
    ->drupalGet($start_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403, 'Anonymous user can not start a private training.');
}