Pull merge.
[yaffs-website] / web / core / modules / system / tests / src / Functional / Ajax / FrameworkTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Ajax;
4
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;
15
16 /**
17  * Performs tests on AJAX framework functions.
18  *
19  * @group Ajax
20  */
21 class FrameworkTest extends BrowserTestBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   protected static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
27
28   /**
29    * Verifies the Ajax rendering of a command in the settings.
30    */
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.');
36   }
37
38   /**
39    * Tests AjaxResponse::prepare() AJAX commands ordering.
40    */
41   public function testOrder() {
42     $expected_commands = [];
43
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!');
62
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
66     // renderer.
67     // @todo refactor AJAX Framework + tests to make less assumptions.
68     $this->drupalGet('ajax_forms_test_lazy_load_form');
69
70     // Verify AJAX command order — this should always be the order:
71     // 1. CSS files
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());
80   }
81
82   /**
83    * Tests the behavior of an error alert command.
84    */
85   public function testAJAXRenderError() {
86     // Verify custom error message.
87     $edit = [
88       'message' => 'Custom error message.',
89     ];
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.');
93   }
94
95   /**
96    * Asserts the array of Ajax commands contains the searched command.
97    *
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
110    * $needle.
111    *
112    * @param $haystack
113    *   An array of rendered Ajax commands returned by the server.
114    * @param $needle
115    *   Array of info we're expecting in one of those commands.
116    */
117   protected function assertCommand($haystack, $needle) {
118     $found = FALSE;
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']);
124       }
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) {
130         $found = TRUE;
131         break;
132       }
133     }
134     $this->assertTrue($found);
135   }
136
137   /**
138    * Requests a path or URL in drupal_ajax format and JSON-decodes the response.
139    *
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
145    *   Array of headers.
146    *
147    * @return array
148    *   Decoded JSON.
149    */
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';
154     }
155     return Json::decode($this->drupalGet($path, $options, $headers));
156   }
157
158 }