You are here

public function OpignoClassMembershipTest::testLoad in Opigno class 8

Same name and namespace in other branches
  1. 3.x tests/src/Functional/OpignoClassMembershipTest.php \Drupal\Tests\opigno_class\Functional\OpignoClassMembershipTest::testLoad()

Tests that the home page loads with a 200 response.

File

tests/src/Functional/OpignoClassMembershipTest.php, line 84

Class

OpignoClassMembershipTest
Tests Opigno Class membership.

Namespace

Drupal\Tests\opigno_class\Functional

Code

public function testLoad() {

  // Create test user.
  $user = $this
    ->drupalCreateUser();

  // Create class group.
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $class = $entity_type_manager
    ->getStorage('group')
    ->create([
    'type' => 'opigno_class',
    'label' => $this
      ->randomMachineName(),
    'uid' => $this->user,
  ]);
  $class
    ->enforceIsNew();
  $class
    ->save();

  // Create LP training group.
  $lp = $entity_type_manager
    ->getStorage('group')
    ->create([
    'type' => 'learning_path',
    'label' => $this
      ->randomMachineName(),
    'uid' => $this->user,
  ]);
  $lp
    ->enforceIsNew();
  $lp
    ->save();

  // Add Class to LP group.
  $lp
    ->addContent($class, 'subgroup:opigno_class');

  // Add user to Class.
  $class
    ->addMember($user);

  // Get LP members.
  $members = $lp
    ->getMembers();
  $this
    ->assertTrue($members, 'Error getting members of LP training');
  $users_ids = [];
  if ($members) {
    foreach ($members as $member) {
      $account = $member
        ->getUser();
      $uid = $account
        ->id();
      $users_ids[$uid] = $uid;
    }
  }
  $this
    ->assertArrayHasKey($user
    ->id(), $users_ids, "User from Class wasn't added to Class LP training");

  // Remove user from Class.
  $class
    ->removeMember($user);

  // Get LP members.
  $members = $lp
    ->getMembers();
  $this
    ->assertTrue($members, 'Error getting members of LP training');
  $users_ids = [];
  if ($members) {
    foreach ($members as $member) {
      $account = $member
        ->getUser();
      $uid = $account
        ->id();
      $users_ids[$uid] = $uid;
    }
  }
  $this
    ->assertArrayNotHasKey($user
    ->id(), $users_ids, "User from Class wasn't removed from Class LP training");
}