You are here

public function EntityBrowserTest::testExistingSelection in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/Extension/EntityBrowserTest.php \Drupal\Tests\entity_browser\Kernel\Extension\EntityBrowserTest::testExistingSelection()

Tests propagation of existing selection.

File

tests/src/Kernel/Extension/EntityBrowserTest.php, line 366

Class

EntityBrowserTest
Tests the entity_browser config entity.

Namespace

Drupal\Tests\entity_browser\Kernel\Extension

Code

public function testExistingSelection() {
  $this
    ->installConfig([
    'entity_browser_test',
  ]);
  $this
    ->installEntitySchema('user');

  /** @var \Drupal\entity_browser\EntityBrowserInterface $entity */
  $entity = $this->controller
    ->load('test');

  /** @var \Drupal\user\UserInterface $user */
  $user = $this->container
    ->get('entity_type.manager')
    ->getStorage('user')
    ->create([
    'name' => $this
      ->randomString(),
    'mail' => 'info@example.com',
  ]);
  $user
    ->save();

  /** @var \Symfony\Component\HttpFoundation\Request $request */
  $uuid = $this->container
    ->get('uuid')
    ->generate();
  $this->container
    ->get('request_stack')
    ->getCurrentRequest()->query
    ->set('uuid', $uuid);
  $this->container
    ->get('entity_browser.selection_storage')
    ->setWithExpire($uuid, [
    'selected_entities' => [
      $user,
    ],
  ], 21600);

  /** @var \Drupal\entity_browser\EntityBrowserFormInterface $form_object */
  $form_object = $entity
    ->getFormObject();
  $form_object
    ->setEntityBrowser($entity);
  $form_state = new FormState();
  $form = [];
  $form_object
    ->buildForm($form, $form_state);
  $propagated_entities = $form_state
    ->get([
    'entity_browser',
    'selected_entities',
  ]);
  $this
    ->assertEquals(1, count($propagated_entities), 'Correct number of entities was propagated.');
  $this
    ->assertEquals($user
    ->id(), $propagated_entities[0]
    ->id(), 'Propagated entity ID is correct.');
  $this
    ->assertEquals($user
    ->getAccountName(), $propagated_entities[0]
    ->getAccountName(), 'Propagated entity name is correct.');
  $this
    ->assertEquals($user
    ->getEmail(), $propagated_entities[0]
    ->getEmail(), 'Propagated entity name is correct.');
}