You are here

public function InlineEntityFormTest::testInlineEntityForm in Workbench Access 8

Tests that workbench_access integrates with inline_entity_form.

File

tests/src/Kernel/InlineEntityFormTest.php, line 126

Class

InlineEntityFormTest
Tests workbench access with inline entity form.

Namespace

Drupal\Tests\workbench_access\Kernel

Code

public function testInlineEntityForm() {

  // Get uid 1 out of the way.
  $this
    ->createUser();

  // Set up an editor and log in as them.
  $editor = $this
    ->setUpEditorUser();
  $this->container
    ->get('current_user')
    ->setAccount($editor);

  // Set up some roles and terms for this test.
  // Create terms and roles.
  $staff_term = Term::create([
    'vid' => $this->vocabulary
      ->id(),
    'name' => 'Staff',
  ]);
  $staff_term
    ->save();
  $super_staff_term = Term::create([
    'vid' => $this->vocabulary
      ->id(),
    'name' => 'Super staff',
  ]);
  $super_staff_term
    ->save();
  $base_term = Term::create([
    'vid' => $this->vocabulary
      ->id(),
    'name' => 'Editor',
  ]);
  $base_term
    ->save();
  $editor->{WorkbenchAccessManagerInterface::FIELD_NAME} = 'editorial_section:' . $base_term
    ->id();
  $editor
    ->save();
  $staff_rid = $this
    ->createRole([], 'staff');
  $super_staff_rid = $this
    ->createRole([], 'super_staff');

  // Set the role -> term mapping.
  $this->container
    ->get('workbench_access.role_section_storage')
    ->addRole($this->scheme, $staff_rid, [
    $staff_term
      ->id(),
  ]);
  $this->container
    ->get('workbench_access.role_section_storage')
    ->addRole($this->scheme, $super_staff_rid, [
    $super_staff_term
      ->id(),
  ]);
  $markup = $this
    ->getRenderedFormAsCrawler();

  // Assert we can't see the options yet.
  $this
    ->assertNotContains($staff_term
    ->getName(), $markup
    ->filter('option')
    ->extract([
    '_text',
  ]));
  $this
    ->assertNotContains($super_staff_term
    ->getName(), $markup
    ->filter('option')
    ->extract([
    '_text',
  ]));

  // Add the staff role and check the option exists.
  $editor
    ->addRole($staff_rid);
  $editor
    ->save();

  // We need to forcefully clear the user section storage cache.
  $user_section = $this->container
    ->get('workbench_access.user_section_storage');
  $reflection = new \ReflectionClass($user_section);
  $property = $reflection
    ->getProperty('userSectionCache');
  $property
    ->setAccessible(TRUE);
  $property
    ->setValue($user_section, []);
  $markup = $this
    ->getRenderedFormAsCrawler();
  $this
    ->assertContains($staff_term
    ->getName(), $markup
    ->filter('option')
    ->extract([
    '_text',
  ]));
}