You are here

public function RoleExpireApiTest::testRoleExpireGetAllUserRecordsAndDeletes in Role Expire 2.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/RoleExpireApiTest.php \Drupal\Tests\role_expire\Functional\RoleExpireApiTest::testRoleExpireGetAllUserRecordsAndDeletes()

Tests getAllUserRecords and three delete methods.

File

tests/src/Functional/RoleExpireApiTest.php, line 102

Class

RoleExpireApiTest
Tests that the Role expire API works.

Namespace

Drupal\Tests\role_expire\Functional

Code

public function testRoleExpireGetAllUserRecordsAndDeletes() {
  $account = $this
    ->createUser([
    'administer role expire',
  ]);
  $this
    ->drupalLogin($account);
  $account_id = $account
    ->id();
  $rid_1 = 'role_test_1';
  $rid_2 = 'role_test_2';
  $rid_3 = 'role_test_3';
  $this
    ->createRole([], $rid_1, 'Role test 1');
  $this
    ->createRole([], $rid_2, 'Role test 2');
  $this
    ->createRole([], $rid_3, 'Role test 3');
  $account
    ->addRole($rid_1);
  $account
    ->addRole($rid_2);
  $account
    ->addRole($rid_3);
  $expiration_1 = strtotime('+1 day');
  $expiration_2 = strtotime('+3 months');
  $expiration_3 = strtotime('+1 year');
  $this->apiService
    ->writeRecord($account_id, $rid_1, $expiration_1);
  $this->apiService
    ->writeRecord($account_id, $rid_2, $expiration_2);
  $this->apiService
    ->writeRecord($account_id, $rid_3, $expiration_3);

  // Test getAllUserRecords method.
  $results = $this->apiService
    ->getAllUserRecords($account_id);
  $actual = count($results) == 3;
  $this
    ->assertTrue($actual);

  // Test delete method 1.
  $this->apiService
    ->deleteRecord($account_id, $rid_1);
  $results = $this->apiService
    ->getAllUserRecords($account_id);
  $actual = count($results) == 2;
  $this
    ->assertTrue($actual);

  // Test delete method 2.
  $this->apiService
    ->deleteRoleRecords($rid_2);
  $results = $this->apiService
    ->getAllUserRecords($account_id);
  $actual = count($results) == 1;
  $this
    ->assertTrue($actual);

  // Test delete method 3.
  $this->apiService
    ->deleteUserRecords($account_id);
  $results = $this->apiService
    ->getAllUserRecords($account_id);
  $actual = count($results) == 0;
  $this
    ->assertTrue($actual);
}