You are here

public function EntityAutocompleteElementFormTest::testEntityAutocompleteAccess in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testEntityAutocompleteAccess()

Tests that access is properly checked by the EntityAutocomplete element.

File

core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php, line 324

Class

EntityAutocompleteElementFormTest
Tests the EntityAutocomplete Form API element.

Namespace

Drupal\KernelTests\Core\Entity\Element

Code

public function testEntityAutocompleteAccess() {
  $form_builder = $this->container
    ->get('form_builder');
  $form = $form_builder
    ->getForm($this);

  // Check that the current user has proper access to view entity labels.
  $expected = $this->referencedEntities[0]
    ->label() . ' (' . $this->referencedEntities[0]
    ->id() . ')';
  $this
    ->assertEqual($form['single_access']['#value'], $expected);
  $expected .= ', ' . $this->referencedEntities[1]
    ->label() . ' (' . $this->referencedEntities[1]
    ->id() . ')';
  $this
    ->assertEqual($form['tags_access']['#value'], $expected);

  // Set up a non-admin user that is *not* allowed to view test entities.
  \Drupal::currentUser()
    ->setAccount($this
    ->createUser([], []));

  // Rebuild the form.
  $form = $form_builder
    ->getForm($this);
  $expected = t('- Restricted access -') . ' (' . $this->referencedEntities[0]
    ->id() . ')';
  $this
    ->assertEqual($form['single_access']['#value'], $expected);
  $expected .= ', ' . t('- Restricted access -') . ' (' . $this->referencedEntities[1]
    ->id() . ')';
  $this
    ->assertEqual($form['tags_access']['#value'], $expected);
}