You are here

public function Select2EntityReferenceWidgetTest::testMultipleAutocreation in Select 2 8

Test autocreation for a multi value field.

File

tests/src/FunctionalJavascript/FieldWidget/Select2EntityReferenceWidgetTest.php, line 211

Class

Select2EntityReferenceWidgetTest
Tests select2 entity reference widget.

Namespace

Drupal\Tests\select2\FunctionalJavascript\FieldWidget

Code

public function testMultipleAutocreation() {
  EntityTestBundle::create([
    'id' => 'test1',
    'label' => 'Test1 label',
    'description' => 'My test description',
  ])
    ->save();
  EntityTestBundle::create([
    'id' => 'test2',
    'label' => 'Test2 label',
    'description' => 'My test description',
  ])
    ->save();
  $this
    ->createField('select2', 'node', 'test', 'entity_reference', [
    'target_type' => 'entity_test_with_bundle',
    'cardinality' => -1,
  ], [
    'handler' => 'default:entity_test_with_bundle',
    'handler_settings' => [
      'target_bundles' => [
        'test1' => 'test1',
        'test2' => 'test2',
      ],
      'auto_create' => TRUE,
      'auto_create_bundle' => 'test2',
    ],
  ], 'select2_entity_reference');
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->drupalGet('/node/add/test');
  $page
    ->fillField('title[0][value]', 'Test node');
  $this
    ->click('.form-item-select2 .select2-selection.select2-selection--multiple');
  $page
    ->find('css', '.select2-search__field')
    ->setValue('New value 1');
  $page
    ->find('css', '.select2-results__option--highlighted')
    ->click();
  $this
    ->click('.form-item-select2 .select2-selection.select2-selection--multiple');
  $page
    ->find('css', '.select2-search__field')
    ->setValue('New value 2');
  $page
    ->find('css', '.select2-results__option--highlighted')
    ->click();
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node', TRUE);
  $this
    ->assertEquals([
    [
      'target_id' => 1,
    ],
    [
      'target_id' => 2,
    ],
  ], $node->select2
    ->getValue());
  $entity = EntityTestWithBundle::load(1);
  $this
    ->assertNotEmpty($entity);
  $this
    ->assertSame('test2', $entity
    ->bundle());
  $entity = EntityTestWithBundle::load(2);
  $this
    ->assertNotEmpty($entity);
  $this
    ->assertSame('test2', $entity
    ->bundle());
  $field = FieldConfig::loadByName('node', 'test', 'select2');
  $field
    ->setSetting('handler_settings', [
    'target_bundles' => [
      'test1' => 'test1',
      'test2' => 'test2',
    ],
    'auto_create' => TRUE,
    'auto_create_bundle' => 'test1',
  ]);
  $field
    ->save();
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $this
    ->click('.form-item-select2 .select2-selection.select2-selection--multiple');
  $page
    ->find('css', '.select2-search__field')
    ->setValue('New value 3');
  $page
    ->find('css', '.select2-results__option--highlighted')
    ->click();
  $page
    ->pressButton('Save');
  $entity = EntityTestWithBundle::load(3);
  $this
    ->assertNotEmpty($entity);
  $this
    ->assertSame('test1', $entity
    ->bundle());
}