You are here

public function EntityAutocompleteElementFormTest::testValidEntityAutocompleteElement 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::testValidEntityAutocompleteElement()

Tests valid entries in the EntityAutocomplete Form API element.

File

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

Class

EntityAutocompleteElementFormTest
Tests the EntityAutocomplete Form API element.

Namespace

Drupal\KernelTests\Core\Entity\Element

Code

public function testValidEntityAutocompleteElement() {
  $form_state = (new FormState())
    ->setValues([
    'single' => $this
      ->getAutocompleteInput($this->referencedEntities[0]),
    'single_autocreate' => 'single - autocreated entity label',
    'single_autocreate_specific_uid' => 'single - autocreated entity label with specific uid',
    'tags' => $this
      ->getAutocompleteInput($this->referencedEntities[0]) . ', ' . $this
      ->getAutocompleteInput($this->referencedEntities[1]),
    'tags_autocreate' => $this
      ->getAutocompleteInput($this->referencedEntities[0]) . ', tags - autocreated entity label, ' . $this
      ->getAutocompleteInput($this->referencedEntities[1]),
    'tags_autocreate_specific_uid' => $this
      ->getAutocompleteInput($this->referencedEntities[0]) . ', tags - autocreated entity label with specific uid, ' . $this
      ->getAutocompleteInput($this->referencedEntities[1]),
    'single_string_id' => $this
      ->getAutocompleteInput($this->referencedEntities[2]),
    'tags_string_id' => $this
      ->getAutocompleteInput($this->referencedEntities[2]) . ', ' . $this
      ->getAutocompleteInput($this->referencedEntities[3]),
  ]);
  $form_builder = $this->container
    ->get('form_builder');
  $form_builder
    ->submitForm($this, $form_state);

  // Valid form state.
  $this
    ->assertCount(0, $form_state
    ->getErrors());

  // Test the 'single' element.
  $this
    ->assertEqual($form_state
    ->getValue('single'), $this->referencedEntities[0]
    ->id());

  // Test the 'single_autocreate' element.
  $value = $form_state
    ->getValue('single_autocreate');
  $this
    ->assertEqual($value['entity']
    ->label(), 'single - autocreated entity label');
  $this
    ->assertEqual($value['entity']
    ->bundle(), 'entity_test');
  $this
    ->assertEqual($value['entity']
    ->getOwnerId(), $this->testUser
    ->id());

  // Test the 'single_autocreate_specific_uid' element.
  $value = $form_state
    ->getValue('single_autocreate_specific_uid');
  $this
    ->assertEqual($value['entity']
    ->label(), 'single - autocreated entity label with specific uid');
  $this
    ->assertEqual($value['entity']
    ->bundle(), 'entity_test');
  $this
    ->assertEqual($value['entity']
    ->getOwnerId(), $this->testAutocreateUser
    ->id());

  // Test the 'tags' element.
  $expected = [
    [
      'target_id' => $this->referencedEntities[0]
        ->id(),
    ],
    [
      'target_id' => $this->referencedEntities[1]
        ->id(),
    ],
  ];
  $this
    ->assertEqual($form_state
    ->getValue('tags'), $expected);

  // Test the 'single_autocreate' element.
  $value = $form_state
    ->getValue('tags_autocreate');

  // First value is an existing entity.
  $this
    ->assertEqual($value[0]['target_id'], $this->referencedEntities[0]
    ->id());

  // Second value is an autocreated entity.
  $this
    ->assertTrue(!isset($value[1]['target_id']));
  $this
    ->assertEqual($value[1]['entity']
    ->label(), 'tags - autocreated entity label');
  $this
    ->assertEqual($value[1]['entity']
    ->getOwnerId(), $this->testUser
    ->id());

  // Third value is an existing entity.
  $this
    ->assertEqual($value[2]['target_id'], $this->referencedEntities[1]
    ->id());

  // Test the 'tags_autocreate_specific_uid' element.
  $value = $form_state
    ->getValue('tags_autocreate_specific_uid');

  // First value is an existing entity.
  $this
    ->assertEqual($value[0]['target_id'], $this->referencedEntities[0]
    ->id());

  // Second value is an autocreated entity.
  $this
    ->assertTrue(!isset($value[1]['target_id']));
  $this
    ->assertEqual($value[1]['entity']
    ->label(), 'tags - autocreated entity label with specific uid');
  $this
    ->assertEqual($value[1]['entity']
    ->getOwnerId(), $this->testAutocreateUser
    ->id());

  // Third value is an existing entity.
  $this
    ->assertEqual($value[2]['target_id'], $this->referencedEntities[1]
    ->id());

  // Test the 'single_string_id' element.
  $this
    ->assertEquals($this->referencedEntities[2]
    ->id(), $form_state
    ->getValue('single_string_id'));

  // Test the 'tags_string_id' element.
  $expected = [
    [
      'target_id' => $this->referencedEntities[2]
        ->id(),
    ],
    [
      'target_id' => $this->referencedEntities[3]
        ->id(),
    ],
  ];
  $this
    ->assertEquals($expected, $form_state
    ->getValue('tags_string_id'));
}