3 namespace Drupal\Tests\system\Functional\Ajax;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Ajax\AddCssCommand;
7 use Drupal\Core\Ajax\AlertCommand;
8 use Drupal\Core\Ajax\AppendCommand;
9 use Drupal\Core\Ajax\HtmlCommand;
10 use Drupal\Core\Ajax\PrependCommand;
11 use Drupal\Core\Ajax\SettingsCommand;
12 use Drupal\Core\Asset\AttachedAssets;
13 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
14 use Drupal\Tests\BrowserTestBase;
17 * Performs tests on AJAX framework functions.
21 class FrameworkTest extends BrowserTestBase {
26 protected static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
29 * Verifies the Ajax rendering of a command in the settings.
31 public function testAJAXRender() {
32 // Verify that settings command is generated if JavaScript settings exist.
33 $commands = $this->drupalGetAjax('ajax-test/render');
34 $expected = new SettingsCommand(['ajax' => 'test'], TRUE);
35 $this->assertCommand($commands, $expected->render(), 'JavaScript settings command is present.');
39 * Tests AjaxResponse::prepare() AJAX commands ordering.
41 public function testOrder() {
42 $expected_commands = [];
44 // Expected commands, in a very specific order.
45 $asset_resolver = \Drupal::service('asset.resolver');
46 $css_collection_renderer = \Drupal::service('asset.css.collection_renderer');
47 $js_collection_renderer = \Drupal::service('asset.js.collection_renderer');
48 $renderer = \Drupal::service('renderer');
49 $build['#attached']['library'][] = 'ajax_test/order-css-command';
50 $assets = AttachedAssets::createFromRenderArray($build);
51 $css_render_array = $css_collection_renderer->render($asset_resolver->getCssAssets($assets, FALSE));
52 $expected_commands[1] = new AddCssCommand($renderer->renderRoot($css_render_array));
53 $build['#attached']['library'][] = 'ajax_test/order-header-js-command';
54 $build['#attached']['library'][] = 'ajax_test/order-footer-js-command';
55 $assets = AttachedAssets::createFromRenderArray($build);
56 list($js_assets_header, $js_assets_footer) = $asset_resolver->getJsAssets($assets, FALSE);
57 $js_header_render_array = $js_collection_renderer->render($js_assets_header);
58 $js_footer_render_array = $js_collection_renderer->render($js_assets_footer);
59 $expected_commands[2] = new PrependCommand('head', $js_header_render_array);
60 $expected_commands[3] = new AppendCommand('body', $js_footer_render_array);
61 $expected_commands[4] = new HtmlCommand('body', 'Hello, world!');
63 // Load any page with at least one CSS file, at least one JavaScript file
64 // and at least one #ajax-powered element. The latter is an assumption of
65 // drupalPostAjaxForm(), the two former are assumptions of the Ajax
67 // @todo refactor AJAX Framework + tests to make less assumptions.
68 $this->drupalGet('ajax_forms_test_lazy_load_form');
70 // Verify AJAX command order — this should always be the order:
72 // 2. JavaScript files in the header
73 // 3. JavaScript files in the footer
74 // 4. Any other AJAX commands, in whatever order they were added.
75 $commands = $this->drupalGetAjax('ajax-test/order');
76 $this->assertCommand(array_slice($commands, 0, 1), $expected_commands[1]->render());
77 $this->assertCommand(array_slice($commands, 1, 1), $expected_commands[2]->render());
78 $this->assertCommand(array_slice($commands, 2, 1), $expected_commands[3]->render());
79 $this->assertCommand(array_slice($commands, 3, 1), $expected_commands[4]->render());
83 * Tests the behavior of an error alert command.
85 public function testAJAXRenderError() {
86 // Verify custom error message.
88 'message' => 'Custom error message.',
90 $commands = $this->drupalGetAjax('ajax-test/render-error', ['query' => $edit]);
91 $expected = new AlertCommand($edit['message']);
92 $this->assertCommand($commands, $expected->render(), 'Custom error message is output.');
96 * Asserts the array of Ajax commands contains the searched command.
98 * An AjaxResponse object stores an array of Ajax commands. This array
99 * sometimes includes commands automatically provided by the framework in
100 * addition to commands returned by a particular controller. During testing,
101 * we're usually interested that a particular command is present, and don't
102 * care whether other commands precede or follow the one we're interested in.
103 * Additionally, the command we're interested in may include additional data
104 * that we're not interested in. Therefore, this function simply asserts that
105 * one of the commands in $haystack contains all of the keys and values in
106 * $needle. Furthermore, if $needle contains a 'settings' key with an array
107 * value, we simply assert that all keys and values within that array are
108 * present in the command we're checking, and do not consider it a failure if
109 * the actual command contains additional settings that aren't part of
113 * An array of rendered Ajax commands returned by the server.
115 * Array of info we're expecting in one of those commands.
117 protected function assertCommand($haystack, $needle) {
119 foreach ($haystack as $command) {
120 // If the command has additional settings that we're not testing for, do
121 // not consider that a failure.
122 if (isset($command['settings']) && is_array($command['settings']) && isset($needle['settings']) && is_array($needle['settings'])) {
123 $command['settings'] = array_intersect_key($command['settings'], $needle['settings']);
125 // If the command has additional data that we're not testing for, do not
126 // consider that a failure. Also, == instead of ===, because we don't
127 // require the key/value pairs to be in any particular order
128 // (http://php.net/manual/language.operators.array.php).
129 if (array_intersect_key($command, $needle) == $needle) {
134 $this->assertTrue($found);
138 * Requests a path or URL in drupal_ajax format and JSON-decodes the response.
140 * @param \Drupal\Core\Url|string $path
141 * Drupal path or URL to request from.
142 * @param array $options
143 * Array of URL options.
144 * @param array $headers
150 protected function drupalGetAjax($path, array $options = [], array $headers = []) {
151 $headers[] = 'X-Requested-With: XMLHttpRequest';
152 if (!isset($options['query'][MainContentViewSubscriber::WRAPPER_FORMAT])) {
153 $options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] = 'drupal_ajax';
155 return Json::decode($this->drupalGet($path, $options, $headers));