View source
<?php
namespace Drupal\Tests\entity_browser\FunctionalJavascript;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\file\Entity\File;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Behat\Mink\Element\NodeElement;
use Drupal\language\Entity\ConfigurableLanguage;
abstract class EntityBrowserWebDriverTestBase extends WebDriverTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'entity_browser_test',
'contextual',
'views',
'block',
'node',
'file',
'image',
'field_ui',
'views_ui',
'system',
'language',
];
protected static $userPermissions = [
'access test_entity_browser_file entity browser pages',
'create article content',
'access content',
];
protected function setUp() {
parent::setUp();
FieldStorageConfig::create([
'field_name' => 'field_reference',
'type' => 'entity_reference',
'entity_type' => 'node',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'target_type' => 'file',
],
])
->save();
FieldConfig::create([
'field_name' => 'field_reference',
'entity_type' => 'node',
'bundle' => 'article',
'label' => 'Reference',
'settings' => [],
])
->save();
$form_display = $this->container
->get('entity_type.manager')
->getStorage('entity_form_display')
->load('node.article.default');
$form_display
->setComponent('field_reference', [
'type' => 'entity_browser_entity_reference',
'settings' => [
'entity_browser' => 'test_entity_browser_file',
'field_widget_display' => 'label',
'open' => TRUE,
],
])
->save();
ConfigurableLanguage::createFromLangcode('fr')
->save();
$account = $this
->drupalCreateUser(static::$userPermissions);
$this
->drupalLogin($account);
}
protected function getEntityBrowser($browser_name, $display_id, $widget_selector_id, $selection_display_id, array $display_configuration = [], array $widget_selector_configuration = [], array $selection_display_configuration = [], array $widget_configurations = []) {
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_browser');
$browser = $storage
->load($browser_name) ?: $storage
->create([
'name' => $browser_name,
]);
$browser
->setDisplay($display_id);
if ($display_configuration) {
$browser
->getDisplay()
->setConfiguration($display_configuration);
}
$browser
->setWidgetSelector($widget_selector_id);
if ($widget_selector_configuration) {
$browser
->getSelectionDisplay()
->setConfiguration($widget_selector_configuration);
}
$browser
->setSelectionDisplay($selection_display_id);
if ($selection_display_configuration) {
$browser
->getSelectionDisplay()
->setConfiguration($selection_display_configuration);
}
if ($widget_configurations) {
foreach ($widget_configurations as $widget_uuid => $widget_config) {
$view_widget = $browser
->getWidget($widget_uuid);
$view_widget
->setConfiguration(NestedArray::mergeDeep($view_widget
->getConfiguration(), $widget_config));
}
}
$browser
->save();
drupal_flush_all_caches();
return $browser;
}
protected function createFile($name, $extension = 'jpg') {
file_put_contents('public://' . $name . '.' . $extension, $this
->randomMachineName());
$image = File::create([
'filename' => $name . '.' . $extension,
'uri' => 'public://' . $name . '.' . $extension,
]);
$image
->setPermanent();
$image
->save();
return $image;
}
protected function waitForAjaxToFinish() {
$this
->assertSession()
->assertWaitOnAjaxRequest();
}
protected function dragDropElement(NodeElement $element, $offsetX, $offsetY) {
$elemXpath = $element
->getXpath();
$jsCode = "var fireMouseEvent = function (type, element, x, y) {\n var event = document.createEvent('MouseEvents');\n event.initMouseEvent(type, true, (type !== 'mousemove'), window, 0, 0, 0, x, y, false, false, false, false, 0, element);\n element.dispatchEvent(event); };";
$jsCode .= "(function() {\n var dragElement = document.evaluate(\"{$elemXpath}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\n var pos = dragElement.getBoundingClientRect();\n var centerX = Math.floor((pos.left + pos.right) / 2);\n var centerY = Math.floor((pos.top + pos.bottom) / 2);\n fireMouseEvent('mousedown', dragElement, centerX, centerY);\n var xOffset = {$offsetX};\n var yOffset = {$offsetY};\n var moves = 3;\n\t for (i = 0 ; i < moves ; i++ ) {\n\t\t\t centerX += xOffset / moves;\n\t\t\t centerY += yOffset / moves;\n\t\t fireMouseEvent('mousemove', dragElement, Math.round(centerX), Math.round(centerY));\n\t\t }\n fireMouseEvent('mouseup', dragElement, centerX, centerY);\n })();";
$this
->getSession()
->executeScript($jsCode);
}
protected function assertRadioExistsByValue($value) {
$value = (string) $value;
return $this
->assertSession()
->elementExists('xpath', "//input[contains(@type, 'radio') and contains(@value, '" . $value . "')]");
}
protected function assertRadioNotExistsByValue($value) {
$value = (string) $value;
return $this
->assertSession()
->elementNotExists('xpath', "//input[contains(@type, 'radio') and contains(@value, '" . $value . "')]");
}
protected function assertCheckboxExistsByValue($value) {
$value = (string) $value;
return $this
->assertSession()
->elementExists('xpath', "//input[contains(@type, 'checkbox') and contains(@value, '" . $value . "')]");
}
protected function assertCheckboxNotExistsByValue($value) {
$value = (string) $value;
return $this
->assertSession()
->elementNotExists('xpath', "//input[contains(@type, 'checkbox') and contains(@value, '" . $value . "')]");
}
}