You are here

public function CardinalityTest::testEntityReferenceWidget in Entity Browser 8.2

Same name and namespace in other branches
  1. 8 tests/src/FunctionalJavascript/CardinalityTest.php \Drupal\Tests\entity_browser\FunctionalJavascript\CardinalityTest::testEntityReferenceWidget()

Tests Entity Reference widget.

File

tests/src/FunctionalJavascript/CardinalityTest.php, line 51

Class

CardinalityTest
Tests the Cardinality handling.

Namespace

Drupal\Tests\entity_browser\FunctionalJavascript

Code

public function testEntityReferenceWidget() {

  // Create an entity_reference field to test the widget.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_fellowship',
    'type' => 'entity_reference',
    'entity_type' => 'node',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings' => [
      'target_type' => 'node',
    ],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'field_fellowship',
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => 'Referenced articles',
    'settings' => [
      'handler' => 'default:node',
      'handler_settings' => [
        'target_bundles' => [
          'article' => 'article',
          'shark' => 'shark',
          'jet' => 'jet',
        ],
      ],
    ],
  ]);
  $field
    ->save();

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
  $form_display = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_form_display')
    ->load('node.article.default');
  $form_display
    ->setComponent('field_fellowship', [
    'type' => 'entity_browser_entity_reference',
    'settings' => [
      'entity_browser' => 'cardinality',
      'open' => TRUE,
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'field_widget_replace' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
      'field_widget_display' => 'label',
      'field_widget_display_settings' => [],
    ],
  ])
    ->save();
  $gollum = $this
    ->createNode([
    'type' => 'shark',
    'title' => 'Gollum',
  ]);
  $aragorn = $this
    ->createNode([
    'type' => 'jet',
    'title' => 'Aragorn',
  ]);
  $gandolf = $this
    ->createNode([
    'type' => 'article',
    'title' => 'Gandolf',
  ]);
  $legolas = $this
    ->createNode([
    'type' => 'article',
    'title' => 'Legolas',
  ]);
  $boromir = $this
    ->createNode([
    'type' => 'article',
    'title' => 'Boromir',
  ]);

  // Test the cardinality handling.
  $role = Role::load('authenticated');
  $this
    ->grantPermissions($role, [
    'access cardinality entity browser pages',
    'bypass node access',
    'administer node form display',
  ]);
  FieldStorageConfig::load('node.field_fellowship')
    ->setCardinality(2)
    ->save();

  // Without using field cardinality, the view should contain checkboxes.
  // Set view to use field cardinality.
  $this
    ->config('views.view.bundle_filter_exposed')
    ->set('display.default.display_options.fields.entity_browser_select.use_field_cardinality', FALSE)
    ->save();
  $this
    ->drupalGet('/node/add/article');
  $this
    ->assertSession()
    ->fieldExists('title[0][value]')
    ->setValue('Le Seigneur des anneaux');
  $this
    ->openIframe();
  $gollum_checkbox = $this
    ->assertCheckboxExistsByValue('node:' . $gollum
    ->id());
  $gollum_checkbox
    ->check();
  $aragorn_checkbox = $this
    ->assertCheckboxExistsByValue('node:' . $aragorn
    ->id());
  $aragorn_checkbox
    ->check();
  $this
    ->assertTrue($gollum_checkbox
    ->isChecked());
  $this
    ->assertTrue($aragorn_checkbox
    ->isChecked());

  // If using field cardinality and field cardinality is greater than 1 then
  // there should be still checkboxes.
  $this
    ->config('views.view.bundle_filter_exposed')
    ->set('display.default.display_options.fields.entity_browser_select.use_field_cardinality', TRUE)
    ->save();
  $this
    ->drupalGet('/node/add/article');
  $this
    ->openIframe();
  $gollum_checkbox = $this
    ->assertCheckboxExistsByValue('node:' . $gollum
    ->id());
  $aragorn_checkbox = $this
    ->assertCheckboxExistsByValue('node:' . $aragorn
    ->id());
  $gandolf_checkbox = $this
    ->assertCheckboxExistsByValue('node:' . $gandolf
    ->id());
  $this
    ->assertCheckboxExistsByValue('node:' . $legolas
    ->id());
  $this
    ->assertCheckboxExistsByValue('node:' . $boromir
    ->id());

  // If we attempt to select 3 nodes, Entity Browser should prevent it and
  // return an error message.
  $gollum_checkbox
    ->check();
  $aragorn_checkbox
    ->check();
  $gandolf_checkbox
    ->check();
  $this
    ->assertSession()
    ->buttonExists('Select entities')
    ->press();
  $this
    ->waitForAjaxToFinish();
  $this
    ->assertSession()
    ->pageTextContains('You can only select up to 2 items');

  // If we change the cardinality to 1, we should have radios.
  FieldStorageConfig::load('node.field_fellowship')
    ->setCardinality(1)
    ->save();
  $this
    ->drupalGet('/node/add/article');
  $this
    ->openIframe();
  $gollum_radio = $this
    ->assertRadioExistsByValue('node:' . $gollum
    ->id());
  $gollum_radio
    ->click();
  $this
    ->assertSession()
    ->buttonExists('Select entities')
    ->press();
  $this
    ->getSession()
    ->switchToIFrame();
  $this
    ->waitForAjaxToFinish();

  // Assert the selected entity.
  $this
    ->assertSession()
    ->pageTextContains('Gollum');

  // Attempt to select more than one element.
  $this
    ->assertSession()
    ->buttonExists('Replace')
    ->press();
  $this
    ->waitForAjaxToFinish();
  $this
    ->getSession()
    ->switchToIFrame('entity_browser_iframe_cardinality');
  $this
    ->waitForAjaxToFinish();
  $gollum_radio = $this
    ->assertRadioExistsByValue('node:' . $gollum
    ->id());
  $gollum_radio
    ->click();
  $gandolf_radio = $this
    ->assertRadioExistsByValue('node:' . $gandolf
    ->id());
  $gandolf_radio
    ->click();
  $this
    ->assertFalse($gollum_radio
    ->isSelected());
  $this
    ->assertTrue($gandolf_radio
    ->isSelected());
  $this
    ->assertSession()
    ->buttonExists('Select entities')
    ->press();
  $this
    ->getSession()
    ->switchToIFrame();
  $this
    ->waitForAjaxToFinish();

  // Assert the selected entity.
  $this
    ->assertSession()
    ->pageTextContains('Gandolf');
  $this
    ->assertSession()
    ->pageTextNotContains('Gollum');
  $this
    ->assertSession()
    ->buttonExists('Replace')
    ->press();
  $this
    ->waitForAjaxToFinish();
  $this
    ->getSession()
    ->switchToIFrame('entity_browser_iframe_cardinality');
  $this
    ->waitForAjaxToFinish();

  // Test that cardinality setting persists when using exposed filters form,
  // When applying the exposed filters, the radios should persist.
  $this
    ->assertSession()
    ->selectExists('Type')
    ->selectOption('jet');
  $this
    ->assertSession()
    ->buttonExists('Apply')
    ->press();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRadioExistsByValue('node:' . $aragorn
    ->id());
  $this
    ->assertRadioNotExistsByValue('node:' . $legolas
    ->id());
  $this
    ->assertSession()
    ->selectExists('Type')
    ->selectOption('shark');
  $this
    ->assertSession()
    ->buttonExists('Apply')
    ->press();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRadioExistsByValue('node:' . $gollum
    ->id());
  $this
    ->assertRadioNotExistsByValue('node:' . $aragorn
    ->id());
}