You are here

public function CardinalityTest::testEntityEmbed in Entity Browser 8

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

Tests cardinality functionality using Entity Embed button.

File

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

Class

CardinalityTest
Tests the Cardinality handling.

Namespace

Drupal\Tests\entity_browser\FunctionalJavascript

Code

public function testEntityEmbed() {
  $this
    ->config('entity_browser.browser.bundle_filter')
    ->set('widgets.b882a89d-9ce4-4dfe-9802-62df93af232a.settings.view', 'bundle_filter_exposed')
    ->save();
  $role = Role::load('authenticated');
  $this
    ->grantPermissions($role, [
    'access content',
    'use text format full_html',
    'create test_entity_embed content',
    'access bundle_filter entity browser pages',
  ]);
  FieldStorageConfig::load('node.field_nodes')
    ->setCardinality(1)
    ->save();
  $westley = $this
    ->createNode([
    'type' => 'shark',
    'title' => 'Westley',
  ]);
  $buttercup = $this
    ->createNode([
    'type' => 'jet',
    'title' => 'Buttercup',
  ]);
  $humperdinck = $this
    ->createNode([
    'type' => 'article',
    'title' => 'Humperdinck',
  ]);
  $this
    ->drupalGet('/node/add/test_entity_embed');
  $this
    ->assertSession()
    ->waitForElement('css', 'a.cke_button__bundle_filter_test')
    ->click();
  $this
    ->assertSession()
    ->waitForElementVisible('xpath', "//iframe[contains(@name, 'entity_browser_iframe_bundle_filter')]", 3000);
  $this
    ->getSession()
    ->switchToIFrame('entity_browser_iframe_bundle_filter');
  $this
    ->assertSession()
    ->waitForElementVisible('xpath', "//div[contains(@class, 'views-exposed-form')]");

  // Without use_field_cardinality set, there should be checkboxes, the default.
  $this
    ->assertCheckBoxExistsByValue('node:' . $westley
    ->id());
  $this
    ->assertCheckBoxExistsByValue('node:' . $buttercup
    ->id());
  $this
    ->assertCheckBoxNotExistsByValue('node:' . $humperdinck
    ->id());

  // 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', TRUE)
    ->save();
  $this
    ->drupalGet('/node/add/test_entity_embed');
  $this
    ->assertSession()
    ->waitForElement('css', 'a.cke_button__bundle_filter_test')
    ->click();
  $this
    ->assertSession()
    ->waitForElementVisible('xpath', "//iframe[contains(@name, 'entity_browser_iframe_bundle_filter')]", 3000);
  $this
    ->getSession()
    ->switchToIFrame('entity_browser_iframe_bundle_filter');
  $this
    ->assertSession()
    ->waitForElementVisible('xpath', "//div[contains(@class, 'views-exposed-form')]");

  // With use_field_cardinality set to true, there should be radios, since
  // cardinality on entity embed is always 1.
  $this
    ->assertRadioExistsByValue('node:' . $westley
    ->id());
  $this
    ->assertRadioExistsByValue('node:' . $buttercup
    ->id());
  $this
    ->assertRadioNotExistsByValue('node:' . $humperdinck
    ->id());

  // 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
    ->assertRadioNotExistsByValue('node:' . $westley
    ->id());
  $this
    ->assertRadioExistsByValue('node:' . $buttercup
    ->id());
  $this
    ->assertRadioNotExistsByValue('node:' . $humperdinck
    ->id());
  $this
    ->assertSession()
    ->selectExists('Type')
    ->selectOption('shark');
  $this
    ->assertSession()
    ->buttonExists('Apply')
    ->press();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRadioExistsByValue('node:' . $westley
    ->id());
  $this
    ->assertRadioNotExistsByValue('node:' . $buttercup
    ->id());
  $this
    ->assertRadioNotExistsByValue('node:' . $humperdinck
    ->id());
}