View source
<?php
namespace Drupal\Tests\system\Functional\Ajax;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Ajax\AddCssCommand;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\SettingsCommand;
use Drupal\Core\Asset\AttachedAssets;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Tests\BrowserTestBase;
class FrameworkTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'node',
'ajax_test',
'ajax_forms_test',
];
public function testAJAXRender() {
$commands = $this
->drupalGetAjax('ajax-test/render');
$expected = new SettingsCommand([
'ajax' => 'test',
], TRUE);
$this
->assertCommand($commands, $expected
->render(), 'JavaScript settings command is present.');
}
public function testOrder() {
$expected_commands = [];
$asset_resolver = \Drupal::service('asset.resolver');
$css_collection_renderer = \Drupal::service('asset.css.collection_renderer');
$js_collection_renderer = \Drupal::service('asset.js.collection_renderer');
$renderer = \Drupal::service('renderer');
$build['#attached']['library'][] = 'ajax_test/order-css-command';
$assets = AttachedAssets::createFromRenderArray($build);
$css_render_array = $css_collection_renderer
->render($asset_resolver
->getCssAssets($assets, FALSE));
$expected_commands[1] = new AddCssCommand($renderer
->renderRoot($css_render_array));
$build['#attached']['library'][] = 'ajax_test/order-header-js-command';
$build['#attached']['library'][] = 'ajax_test/order-footer-js-command';
$assets = AttachedAssets::createFromRenderArray($build);
list($js_assets_header, $js_assets_footer) = $asset_resolver
->getJsAssets($assets, FALSE);
$js_header_render_array = $js_collection_renderer
->render($js_assets_header);
$js_footer_render_array = $js_collection_renderer
->render($js_assets_footer);
$expected_commands[2] = new PrependCommand('head', $js_header_render_array);
$expected_commands[3] = new AppendCommand('body', $js_footer_render_array);
$expected_commands[4] = new HtmlCommand('body', 'Hello, world!');
$commands = $this
->drupalGetAjax('ajax-test/order');
$this
->assertCommand(array_slice($commands, 0, 1), $expected_commands[1]
->render());
$this
->assertCommand(array_slice($commands, 1, 1), $expected_commands[2]
->render());
$this
->assertCommand(array_slice($commands, 2, 1), $expected_commands[3]
->render());
$this
->assertCommand(array_slice($commands, 3, 1), $expected_commands[4]
->render());
}
public function testAJAXRenderError() {
$edit = [
'message' => 'Custom error message.',
];
$commands = $this
->drupalGetAjax('ajax-test/render-error', [
'query' => $edit,
]);
$expected = new AlertCommand($edit['message']);
$this
->assertCommand($commands, $expected
->render(), 'Custom error message is output.');
}
protected function assertCommand($haystack, $needle) {
$found = FALSE;
foreach ($haystack as $command) {
if (isset($command['settings']) && is_array($command['settings']) && isset($needle['settings']) && is_array($needle['settings'])) {
$command['settings'] = array_intersect_key($command['settings'], $needle['settings']);
}
if (array_intersect_key($command, $needle) == $needle) {
$found = TRUE;
break;
}
}
$this
->assertTrue($found);
}
protected function drupalGetAjax($path, array $options = [], array $headers = []) {
$headers[] = 'X-Requested-With: XMLHttpRequest';
if (!isset($options['query'][MainContentViewSubscriber::WRAPPER_FORMAT])) {
$options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] = 'drupal_ajax';
}
return Json::decode($this
->drupalGet($path, $options, $headers));
}
}