function EntityAutocompleteElementFormTest::testEntityAutocompleteAccess

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testEntityAutocompleteAccess()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testEntityAutocompleteAccess()
  3. 11.x 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 346

Class

EntityAutocompleteElementFormTest
Tests the EntityAutocomplete Form API element.

Namespace

Drupal\KernelTests\Core\Entity\Element

Code

public function testEntityAutocompleteAccess() : void {
    $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->assertEquals($expected, $form['single_access']['#value']);
    $expected .= ', ' . $this->referencedEntities[1]
        ->label() . ' (' . $this->referencedEntities[1]
        ->id() . ')';
    $this->assertEquals($expected, $form['tags_access']['#value']);
    // 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 = '- Restricted access - (' . $this->referencedEntities[0]
        ->id() . ')';
    $this->assertEquals($expected, $form['single_access']['#value']);
    $expected .= ', - Restricted access - (' . $this->referencedEntities[1]
        ->id() . ')';
    $this->assertEquals($expected, $form['tags_access']['#value']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.