You are here

trait EntityBrowserTrait in Lightning Media 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Traits/EntityBrowserTrait.php \Drupal\Tests\lightning_media\Traits\EntityBrowserTrait

Contains methods for interacting with entity browsers in frames.

Hierarchy

7 files declare their use of EntityBrowserTrait
CKEditorMediaBrowserTest.php in tests/src/FunctionalJavascript/CKEditorMediaBrowserTest.php
EmbeddedMediaTest.php in tests/src/FunctionalJavascript/EmbeddedMediaTest.php
ImageBrowserCardinalityTest.php in modules/lightning_media_image/tests/src/FunctionalJavascript/ImageBrowserCardinalityTest.php
ImageBrowserTest.php in tests/src/FunctionalJavascript/ImageBrowserTest.php
ImageMediaTest.php in tests/src/Functional/ImageMediaTest.php

... See full list

File

tests/src/Traits/EntityBrowserTrait.php, line 12

Namespace

Drupal\Tests\lightning_media\Traits
View source
trait EntityBrowserTrait {

  /**
   * The machine name of the entity browser whose frame we are in.
   *
   * @var string
   */
  private $currentEntityBrowser;

  /**
   * Adds the "Library" widget to the entity browsers we ship.
   */
  private function addMediaLibraryToEntityBrowsers() {
    $GLOBALS['install_state'] = [];

    /** @var \Drupal\views\ViewEntityInterface $view */
    $view = View::load('media');
    lightning_media_view_insert($view);
    unset($GLOBALS['install_state']);
  }

  /**
   * Waits for an entity browser frame to load.
   *
   * @param string $id
   *   The machine name of the entity browser.
   * @param bool $switch
   *   (optional) Whether to switch into the entity browser's frame once it has
   *   loaded. Defaults to TRUE.
   */
  private function waitForEntityBrowser($id, $switch = TRUE) {
    $frame = 'entity_browser_iframe_' . $id;
    $this
      ->assertJsCondition("window.{$frame} !== undefined");
    $this
      ->assertJsCondition("window.{$frame}.document.readyState === 'complete'");
    if ($switch) {
      $this
        ->getSession()
        ->switchToIFrame($frame);
      $this->currentEntityBrowser = $id;
    }
  }

  /**
   * Waits for the current entity browser frame to close.
   *
   * @param string $id
   *   (optional) The machine name of the entity browser whose frame we are in.
   *   Defaults to the value of $this->currentEntityBrowser.
   */
  private function waitForEntityBrowserToClose($id = NULL) {
    $id = $id ?: $this->currentEntityBrowser;
    $this
      ->assertNotEmpty($id);
    $this
      ->getSession()
      ->switchToIFrame(NULL);
    $this
      ->assertJsCondition("typeof window.entity_browser_iframe_{$id} === 'undefined'");
  }

  /**
   * Waits for the current entity browser to have at least one selectable item.
   *
   * @return \Behat\Mink\Element\NodeElement[]
   *   The selectable items.
   */
  private function waitForItems() {
    $items = $this
      ->getSession()
      ->getPage()
      ->waitFor(10, function (DocumentElement $page) {
      return $page
        ->findAll('css', '[data-selectable]');
    });
    $this
      ->assertNotEmpty($items);
    return $items;
  }

  /**
   * Selects an item in the current entity browser.
   *
   * @param \Behat\Mink\Element\NodeElement $item
   *   The item element.
   */
  private function selectItem(NodeElement $item) {
    $result = $item
      ->waitFor(10, function (NodeElement $item) {
      $item
        ->click();
      return $item
        ->hasClass('selected') && $item
        ->hasCheckedField('Select this item');
    });
    $this
      ->assertTrue($result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityBrowserTrait::$currentEntityBrowser private property The machine name of the entity browser whose frame we are in.
EntityBrowserTrait::addMediaLibraryToEntityBrowsers private function Adds the "Library" widget to the entity browsers we ship.
EntityBrowserTrait::selectItem private function Selects an item in the current entity browser.
EntityBrowserTrait::waitForEntityBrowser private function Waits for an entity browser frame to load.
EntityBrowserTrait::waitForEntityBrowserToClose private function Waits for the current entity browser frame to close.
EntityBrowserTrait::waitForItems private function Waits for the current entity browser to have at least one selectable item.