You are here

public function TokenTest::testTokensDeletedWithEntity in Access unpublished 8

Tests that tokens are deleted if associated entity gets deleted.

File

tests/src/Kernel/TokenTest.php, line 35

Class

TokenTest
Test various token use cases.

Namespace

Drupal\Tests\access_unpublished\Kernel

Code

public function testTokensDeletedWithEntity() {
  $entity1 = EntityTestMulRevPub::create([
    'title' => 'Foo',
    'status' => 0,
  ]);
  $entity1
    ->save();
  $entity2 = EntityTestMulRevPub::create([
    'title' => 'Foo',
    'status' => 0,
  ]);
  $entity2
    ->save();

  // Create tokens for the entity.
  $requestTime = \Drupal::time()
    ->getRequestTime();
  $token1 = AccessToken::create([
    'entity_type' => 'entity_test_mulrevpub',
    'entity_id' => $entity1
      ->id(),
    'value' => 'iAmExpired',
    'expire' => $requestTime - 100,
  ]);
  $token1
    ->save();
  $token2 = AccessToken::create([
    'entity_type' => 'entity_test_mulrevpub',
    'entity_id' => $entity1
      ->id(),
    'value' => 'iAmValid',
    'expire' => $requestTime + 100,
  ]);
  $token2
    ->save();
  $token3 = AccessToken::create([
    'entity_type' => 'entity_test_mulrevpub',
    'entity_id' => $entity2
      ->id(),
    'value' => 'iAmValid',
    'expire' => $requestTime + 100,
  ]);
  $token3
    ->save();
  $entity1
    ->delete();
  $this
    ->assertNull(AccessToken::load($token1
    ->id()));
  $this
    ->assertNull(AccessToken::load($token2
    ->id()));
  $this
    ->assertInstanceOf(AccessToken::class, AccessToken::load($token3
    ->id()));
}