You are here

public function MultiStepSelectionDisplayTest::testAjaxCommands in Entity Browser 8

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

Check that selection state in entity browser Inline Entity Form.

File

tests/src/FunctionalJavascript/MultiStepSelectionDisplayTest.php, line 84

Class

MultiStepSelectionDisplayTest
Test for multi_step_display selection display.

Namespace

Drupal\Tests\entity_browser\FunctionalJavascript

Code

public function testAjaxCommands() {
  $this
    ->createFile('test_file1');
  $this
    ->createFile('test_file2');
  $this
    ->createFile('test_file3');

  // Testing Action buttons (adding and removing) with usage of HTML View.
  $widget_configurations = [
    // View widget configuration.
    '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
      'settings' => [
        'view' => 'files_entity_browser_html',
        'auto_select' => TRUE,
      ],
    ],
  ];
  $this
    ->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
  $this
    ->drupalGet('node/add/article');
  $this
    ->openEntityBrowser();

  // Check that action buttons are not there.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-use-selected"]');
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-show-selection"]');
  $this
    ->clickViewEntity('file:1');
  $this
    ->waitSelectionDisplayAjaxCommands();

  // Check that action buttons are there.
  $this
    ->assertSession()
    ->elementExists('xpath', '//*[@data-drupal-selector="edit-use-selected"]');
  $this
    ->assertSession()
    ->elementExists('xpath', '//*[@data-drupal-selector="edit-show-selection"]');

  // Click on first entity Remove button.
  $this
    ->assertSession()
    ->elementExists('xpath', '//input[@data-row-id="0"]')
    ->press();
  $this
    ->waitSelectionDisplayAjaxCommands();

  // Check that action buttons are not there.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-use-selected"]');
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-show-selection"]');
  $this
    ->clickViewEntity('file:1');
  $this
    ->waitSelectionDisplayAjaxCommands();
  $this
    ->closeEntityBrowser();

  // Testing quick adding and removing of entities with usage of Table
  // (default) view.
  $widget_configurations = [
    // View widget configuration.
    '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
      'settings' => [
        'view' => 'files_entity_browser',
        'auto_select' => TRUE,
      ],
    ],
  ];
  $this
    ->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
  $this
    ->drupalGet('node/add/article');
  $this
    ->openEntityBrowser();

  // Quickly add 5 entities.
  $entitiesToAdd = [
    'file:1',
    'file:2',
    'file:3',
    'file:1',
    'file:2',
  ];
  foreach ($entitiesToAdd as $entityId) {
    $this
      ->clickViewEntity($entityId);
  }
  $this
    ->waitSelectionDisplayAjaxCommands();

  // Check that there are 5 entities in selection display list.
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 5);

  // Quickly remove all 5 entities.
  foreach (array_keys($entitiesToAdd) as $entityIndex) {
    $this
      ->assertSession()
      ->elementExists('xpath', '//input[@data-row-id="' . $entityIndex . '"]')
      ->press();
  }
  $this
    ->waitSelectionDisplayAjaxCommands();

  // Check that action buttons are not there.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-use-selected"]');
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-show-selection"]');
  $this
    ->clickViewEntity('file:1');
  $this
    ->waitSelectionDisplayAjaxCommands();
  $this
    ->closeEntityBrowser();

  // Testing adding with preselection with usage of Grid view.
  $widget_configurations = [
    // View widget configuration.
    '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
      'settings' => [
        'view' => 'files_entity_browser_grid',
        'auto_select' => TRUE,
      ],
    ],
  ];
  $this
    ->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);

  // Change selection mode to 'Edit', to test adding/removing inside EB.
  $this
    ->changeFieldFormDisplayConfig([
    'settings' => [
      'selection_mode' => 'selection_edit',
    ],
  ]);
  $this
    ->drupalGet('node/add/article');
  $this
    ->openEntityBrowser();
  $this
    ->clickViewEntity('file:1');
  $this
    ->waitSelectionDisplayAjaxCommands();
  $this
    ->closeEntityBrowser();
  $this
    ->openEntityBrowser();
  $this
    ->clickViewEntity('file:2');
  $this
    ->waitSelectionDisplayAjaxCommands();
  $this
    ->closeEntityBrowser();
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 2);

  // Testing removing with preselection with usage of Unformatted view.
  $widget_configurations = [
    // View widget configuration.
    '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
      'settings' => [
        'view' => 'files_entity_browser_unformatted',
        'auto_select' => TRUE,
      ],
    ],
  ];
  $this
    ->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
  $this
    ->drupalGet('node/add/article');
  $this
    ->openEntityBrowser();

  // Select 3 entities.
  $entitiesToAdd = [
    'file:1',
    'file:2',
    'file:3',
  ];
  foreach ($entitiesToAdd as $entityId) {
    $this
      ->clickViewEntity($entityId);

    // For some reason PhantomJS crashes here on quick clicking. That's why
    // waiting is added. Selenium works fine.
    $this
      ->waitSelectionDisplayAjaxCommands();
  }
  $this
    ->closeEntityBrowser();

  // Check that there are 3 entities in selection list after closing of EB.
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 3);
  $this
    ->openEntityBrowser();

  // Click on first entity Remove button.
  $this
    ->assertSession()
    ->elementExists('xpath', '//input[@data-row-id="0"]')
    ->press();
  $this
    ->waitSelectionDisplayAjaxCommands();
  $this
    ->closeEntityBrowser();

  // Check that there are 2 entities in selection list after closing of EB.
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 2);
}