You are here

public function EntityBrowserTest::testValidators 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::testValidators()

Tests validators.

File

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

Class

EntityBrowserTest
Tests the entity_browser config entity.

Namespace

Drupal\Tests\entity_browser\Kernel\Extension

Code

public function testValidators() {
  $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);
  $storage = [
    'validators' => [
      'entity_type' => [
        'type' => 'user',
      ],
    ],
  ];
  $this->container
    ->get('entity_browser.selection_storage')
    ->setWithExpire($uuid, $storage, 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_state);
  $validators = $form_state
    ->get([
    'entity_browser',
    'validators',
  ]);
  $this
    ->assertSame($validators, $storage['validators'], 'Correct validators were passed to form');

  // Set a valid triggering element
  // (see \Drupal\entity_browser\WidgetBase::validate())
  $element = [
    '#array_parents' => [
      'submit',
    ],
  ];
  $form_state
    ->setTriggeringElement($element);

  // Use an entity that we know will fail validation.
  $form_state
    ->setValue('dummy_entities', [
    $entity,
  ]);
  $form_object
    ->validateForm($form, $form_state);
  $this
    ->assertNotEmpty($form_state
    ->getErrors(), t('Validation failed where expected'));

  // Use an entity that we know will pass validation.
  $form_state
    ->clearErrors();
  $form_state
    ->setValue('dummy_entities', [
    $user,
  ]);
  $form_object
    ->validateForm($form, $form_state);
  $this
    ->assertEmpty($form_state
    ->getErrors(), t('Validation succeeded where expected'));
}