You are here

public function Select2ValidOptionsTest::testAvailableOptions in Select 2 8

Tests that available options are set according to values.

File

tests/src/Kernel/Select2ValidOptionsTest.php, line 18

Class

Select2ValidOptionsTest
Test the options of the select2 element.

Namespace

Drupal\Tests\select2\Kernel

Code

public function testAvailableOptions() {
  $name = 'test_select2';
  $storage_settings = [
    'target_type' => 'entity_test_mulrevpub',
    'cardinality' => -1,
  ];
  $field_settings = [
    'handler' => 'default:entity_test_mulrevpub',
    'handler_settings' => [
      'target_bundles' => [
        'entity_test_mulrevpub' => 'entity_test_mulrevpub',
      ],
      'auto_create' => TRUE,
    ],
  ];
  $this
    ->createField($name, 'entity_test', 'entity_test', 'entity_reference', $storage_settings, $field_settings, 'select2_entity_reference', [
    'autocomplete' => TRUE,
  ]);
  $entity = EntityTest::create();
  $ref1 = EntityTestMulRevPub::create([
    'name' => 'Drupal Temp',
  ]);
  $ref2 = EntityTestMulRevPub::create([
    'name' => 'Test',
  ]);
  $ref1
    ->save();
  $ref2
    ->save();

  // Create a new revision to trigger problem.
  $ref1
    ->setName('Drupal')
    ->setNewRevision();
  $ref1
    ->save();
  $entity->{$name}
    ->setValue([
    [
      'target_id' => $ref1
        ->id(),
    ],
    [
      'target_id' => $ref2
        ->id(),
    ],
  ]);
  $entity
    ->save();
  $form = \Drupal::service('entity.form_builder')
    ->getForm($entity);
  $this
    ->assertEquals([
    $ref1
      ->id() => $ref1
      ->getName(),
    $ref2
      ->id() => $ref2
      ->getName(),
  ], $form[$name]['widget']['#options'], 'Option values differ from expected values.');
}