Interim commit.
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / EntityBrowserJavascriptTestBase.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
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;
11
12 /**
13  * Base class for Entity browser Javascript functional tests.
14  *
15  * @package Drupal\Tests\entity_browser\FunctionalJavascript
16  */
17 abstract class EntityBrowserJavascriptTestBase extends JavascriptTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = [
25     'entity_browser_test',
26     'ctools',
27     'views',
28     'block',
29     'node',
30     'file',
31     'image',
32     'field_ui',
33     'views_ui',
34     'system',
35   ];
36
37   /**
38    * Permissions for user that will be logged-in for test.
39    *
40    * @var array
41    */
42   protected static $userPermissions = [
43     'access test_entity_browser_file entity browser pages',
44     'create article content',
45     'access content',
46   ];
47
48   /**
49    * {@inheritdoc}
50    */
51   protected function setUp() {
52     parent::setUp();
53
54     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
55
56     FieldStorageConfig::create([
57       'field_name' => 'field_reference',
58       'type' => 'entity_reference',
59       'entity_type' => 'node',
60       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
61       'settings' => [
62         'target_type' => 'file',
63       ],
64     ])->save();
65
66     FieldConfig::create([
67       'field_name' => 'field_reference',
68       'entity_type' => 'node',
69       'bundle' => 'article',
70       'label' => 'Reference',
71       'settings' => [],
72     ])->save();
73
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');
78
79     $form_display->setComponent('field_reference', [
80       'type' => 'entity_browser_entity_reference',
81       'settings' => [
82         'entity_browser' => 'test_entity_browser_file',
83         'field_widget_display' => 'label',
84         'open' => TRUE,
85       ],
86     ])->save();
87
88     $account = $this->drupalCreateUser(static::$userPermissions);
89     $this->drupalLogin($account);
90   }
91
92   /**
93    * Return an entity browser if it exists or creates a new one.
94    *
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.
111    *
112    * @return \Drupal\entity_browser\EntityBrowserInterface
113    *   Returns an Entity Browser.
114    */
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');
119
120     /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
121     $browser = $storage->load($browser_name) ?: $storage->create(['name' => $browser_name]);
122
123     $browser->setDisplay($display_id);
124     if ($display_configuration) {
125       $browser->getDisplay()->setConfiguration($display_configuration);
126     }
127
128     $browser->setWidgetSelector($widget_selector_id);
129     if ($widget_selector_configuration) {
130       $browser->getSelectionDisplay()
131         ->setConfiguration($widget_selector_configuration);
132     }
133
134     $browser->setSelectionDisplay($selection_display_id);
135     if ($selection_display_configuration) {
136       $browser->getSelectionDisplay()
137         ->setConfiguration($selection_display_configuration);
138     }
139
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));
145       }
146     }
147
148     $browser->save();
149
150     // Clear caches after new browser is saved to remove old cached states.
151     drupal_flush_all_caches();
152
153     return $browser;
154   }
155
156   /**
157    * Creates an image.
158    *
159    * @param string $name
160    *   The name of the image.
161    * @param string $extension
162    *   File extension.
163    *
164    * @return \Drupal\file\FileInterface
165    *   Returns an image.
166    */
167   protected function createFile($name, $extension = 'jpg') {
168     file_put_contents('public://' . $name . '.' . $extension, $this->randomMachineName());
169
170     $image = File::create([
171       'filename' => $name . '.' . $extension,
172       'uri' => 'public://' . $name . '.' . $extension,
173     ]);
174     $image->setPermanent();
175     $image->save();
176
177     return $image;
178   }
179
180   /**
181    * Waits for jQuery to become ready and animations to complete.
182    */
183   protected function waitForAjaxToFinish() {
184     $this->assertSession()->assertWaitOnAjaxRequest();
185   }
186
187   /**
188    * Waits and asserts that a given element is visible.
189    *
190    * @param string $selector
191    *   The CSS selector.
192    * @param int $timeout
193    *   (Optional) Timeout in milliseconds, defaults to 1000.
194    * @param string $message
195    *   (Optional) Message to pass to assertJsCondition().
196    */
197   protected function waitUntilVisible($selector, $timeout = 1000, $message = '') {
198     $condition = "jQuery('" . $selector . ":visible').length > 0";
199     $this->assertJsCondition($condition, $timeout, $message);
200   }
201
202   /**
203    * Click on element found by xpath selector.
204    *
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.
209    */
210   protected function clickXpathSelector($xpathSelector, $waitAfterAction = TRUE) {
211     $this->getSession()->getPage()->find('xpath', $xpathSelector)->click();
212
213     if ($waitAfterAction) {
214       $this->waitForAjaxToFinish();
215     }
216   }
217
218   /**
219    * Debugger method to save additional HTML output.
220    *
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.
225    */
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);
235     }
236   }
237
238 }