You are here

public function EntityAutocompleteElementFormTest::testValidEntityAutocompleteElement in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\system\Tests\Entity\Element\EntityAutocompleteElementFormTest::testValidEntityAutocompleteElement()

Tests valid entries in the EntityAutocomplete Form API element.

File

core/modules/system/src/Tests/Entity/Element/EntityAutocompleteElementFormTest.php, line 174
Contains \Drupal\system\Tests\Entity\Element\EntityAutocompleteElementFormTest.

Class

EntityAutocompleteElementFormTest
Tests the EntityAutocomplete Form API element.

Namespace

Drupal\system\Tests\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]),
  ]);
  $form_builder = $this->container
    ->get('form_builder');
  $form_builder
    ->submitForm($this, $form_state);

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

  // 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 = array(
    array(
      'target_id' => $this->referencedEntities[0]
        ->id(),
    ),
    array(
      '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());
}