You are here

public function SectionCacheTest::testSectionCache in Workbench Access 8

Test create access integration.

File

tests/src/Kernel/SectionCacheTest.php, line 92

Class

SectionCacheTest
Tests the internal caching of section data.

Namespace

Drupal\Tests\workbench_access\Kernel

Code

public function testSectionCache() {

  // The first user in a kernel test gets UID 1, so we need to make sure we're
  // not testing with that user.
  $this
    ->createUser();

  // Create a section.
  $term = Term::create([
    'vid' => $this->vocabulary
      ->id(),
    'name' => 'Some section',
  ]);
  $term
    ->save();

  // Create one user and assign to the section.
  $permissions = [
    'use workbench access',
  ];
  $editor = $this
    ->createUser($permissions);
  $editor
    ->save();
  $this->userStorage
    ->addUser($this->scheme, $editor, [
    $term
      ->id(),
  ]);

  // Now fetch the sections for this user. Count should be 1.
  $sections = $this->userSectionStorage
    ->getUserSections($this->scheme, $editor);
  $this
    ->assertTrue(count($sections) == 1);

  // Create a new section.
  $term2 = Term::create([
    'vid' => $this->vocabulary
      ->id(),
    'name' => 'Some new section',
  ]);
  $term2
    ->save();

  // Add to the user.
  $this->userSectionStorage
    ->addUser($this->scheme, $editor, [
    $term2
      ->id(),
  ]);

  // Now fetch the sections for this user. Count should be 2.
  $sections = $this->userSectionStorage
    ->getUserSections($this->scheme, $editor);
  $this
    ->assertTrue(count($sections) == 2);

  // Now remove and test again.
  $this->userSectionStorage
    ->removeUser($this->scheme, $editor, [
    $term2
      ->id(),
  ]);
  $sections = $this->userSectionStorage
    ->getUserSections($this->scheme, $editor);
  $this
    ->assertTrue(count($sections) == 1);
  $this
    ->assertEquals([
    $editor
      ->id() => $editor
      ->label(),
  ], $this->userSectionStorage
    ->getEditors($this->scheme, $term
    ->id()));
}