3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\file\Entity\File;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
13 * Base class for Entity browser Javascript functional tests.
15 * @package Drupal\Tests\entity_browser\FunctionalJavascript
17 abstract class EntityBrowserJavascriptTestBase extends JavascriptTestBase {
24 public static $modules = [
25 'entity_browser_test',
38 * Permissions for user that will be logged-in for test.
42 protected static $userPermissions = [
43 'access test_entity_browser_file entity browser pages',
44 'create article content',
51 protected function setUp() {
54 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
56 FieldStorageConfig::create([
57 'field_name' => 'field_reference',
58 'type' => 'entity_reference',
59 'entity_type' => 'node',
60 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
62 'target_type' => 'file',
67 'field_name' => 'field_reference',
68 'entity_type' => 'node',
69 'bundle' => 'article',
70 'label' => 'Reference',
74 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
75 $form_display = $this->container->get('entity_type.manager')
76 ->getStorage('entity_form_display')
77 ->load('node.article.default');
79 $form_display->setComponent('field_reference', [
80 'type' => 'entity_browser_entity_reference',
82 'entity_browser' => 'test_entity_browser_file',
83 'field_widget_display' => 'label',
88 $account = $this->drupalCreateUser(static::$userPermissions);
89 $this->drupalLogin($account);
93 * Return an entity browser if it exists or creates a new one.
95 * @param string $browser_name
96 * The entity browser name.
97 * @param string $display_id
98 * The display plugin id.
99 * @param string $widget_selector_id
100 * The widget selector id.
101 * @param string $selection_display_id
102 * The selection display id.
103 * @param array $display_configuration
104 * The display plugin configuration.
105 * @param array $widget_selector_configuration
106 * The widget selector configuration.
107 * @param array $selection_display_configuration
108 * The selection display configuration.
109 * @param array $widget_configurations
110 * Widget configurations. Have be provided with widget UUIDs.
112 * @return \Drupal\entity_browser\EntityBrowserInterface
113 * Returns an Entity Browser.
115 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 = []) {
116 /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
117 $storage = $this->container->get('entity_type.manager')
118 ->getStorage('entity_browser');
120 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
121 $browser = $storage->load($browser_name) ?: $storage->create(['name' => $browser_name]);
123 $browser->setDisplay($display_id);
124 if ($display_configuration) {
125 $browser->getDisplay()->setConfiguration($display_configuration);
128 $browser->setWidgetSelector($widget_selector_id);
129 if ($widget_selector_configuration) {
130 $browser->getSelectionDisplay()
131 ->setConfiguration($widget_selector_configuration);
134 $browser->setSelectionDisplay($selection_display_id);
135 if ($selection_display_configuration) {
136 $browser->getSelectionDisplay()
137 ->setConfiguration($selection_display_configuration);
140 // Apply custom widget configurations.
141 if ($widget_configurations) {
142 foreach ($widget_configurations as $widget_uuid => $widget_config) {
143 $view_widget = $browser->getWidget($widget_uuid);
144 $view_widget->setConfiguration(NestedArray::mergeDeep($view_widget->getConfiguration(), $widget_config));
150 // Clear caches after new browser is saved to remove old cached states.
151 drupal_flush_all_caches();
159 * @param string $name
160 * The name of the image.
161 * @param string $extension
164 * @return \Drupal\file\FileInterface
167 protected function createFile($name, $extension = 'jpg') {
168 file_put_contents('public://' . $name . '.' . $extension, $this->randomMachineName());
170 $image = File::create([
171 'filename' => $name . '.' . $extension,
172 'uri' => 'public://' . $name . '.' . $extension,
174 $image->setPermanent();
181 * Waits for jQuery to become ready and animations to complete.
183 protected function waitForAjaxToFinish() {
184 $this->assertSession()->assertWaitOnAjaxRequest();
188 * Waits and asserts that a given element is visible.
190 * @param string $selector
192 * @param int $timeout
193 * (Optional) Timeout in milliseconds, defaults to 1000.
194 * @param string $message
195 * (Optional) Message to pass to assertJsCondition().
197 protected function waitUntilVisible($selector, $timeout = 1000, $message = '') {
198 $condition = "jQuery('" . $selector . ":visible').length > 0";
199 $this->assertJsCondition($condition, $timeout, $message);
203 * Click on element found by xpath selector.
205 * @param string $xpathSelector
206 * Xpath selector for element that will be used to trigger click on it.
207 * @param bool $waitAfterAction
208 * Flag to wait after click is executed.
210 protected function clickXpathSelector($xpathSelector, $waitAfterAction = TRUE) {
211 $this->getSession()->getPage()->find('xpath', $xpathSelector)->click();
213 if ($waitAfterAction) {
214 $this->waitForAjaxToFinish();
219 * Debugger method to save additional HTML output.
221 * The base class will only save browser output when accessing page using
222 * ::drupalGet and providing a printer class to PHPUnit. This method
223 * is intended for developers to help debug browser test failures and capture
224 * more verbose output.
226 protected function saveHtmlOutput() {
227 $out = $this->getSession()->getPage()->getContent();
228 // Ensure that any changes to variables in the other thread are picked up.
229 $this->refreshVariables();
230 if ($this->htmlOutputEnabled) {
231 $html_output = '<hr />Ending URL: ' . $this->getSession()->getCurrentUrl();
232 $html_output .= '<hr />' . $out;
233 $html_output .= $this->getHtmlOutputHeaders();
234 $this->htmlOutput($html_output);