Chris@0: drupalCreateUser(['access content']); Chris@0: $this->drupalLogin($web_user); Chris@0: Chris@0: $edit = []; Chris@0: Chris@0: // Tests the 'add_css' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'add_css' command")]); Chris@0: $expected = new AddCssCommand('my/file.css'); Chris@0: $this->assertCommand($commands, $expected->render(), "'add_css' AJAX command issued with correct data."); Chris@0: Chris@0: // Tests the 'after' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'After': Click to put something after the div")]); Chris@0: $expected = new AfterCommand('#after_div', 'This will be placed after'); Chris@0: $this->assertCommand($commands, $expected->render(), "'after' AJAX command issued with correct data."); Chris@0: Chris@0: // Tests the 'alert' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'Alert': Click to alert")]); Chris@0: $expected = new AlertCommand(t('Alert')); Chris@0: $this->assertCommand($commands, $expected->render(), "'alert' AJAX Command issued with correct text."); Chris@0: Chris@0: // Tests the 'append' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'Append': Click to append something")]); Chris@0: $expected = new AppendCommand('#append_div', 'Appended text'); Chris@0: $this->assertCommand($commands, $expected->render(), "'append' AJAX command issued with correct data."); Chris@0: Chris@0: // Tests the 'before' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'before': Click to put something before the div")]); Chris@0: $expected = new BeforeCommand('#before_div', 'Before text'); Chris@0: $this->assertCommand($commands, $expected->render(), "'before' AJAX command issued with correct data."); Chris@0: Chris@0: // Tests the 'changed' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX changed: Click to mark div changed.")]); Chris@0: $expected = new ChangedCommand('#changed_div'); Chris@0: $this->assertCommand($commands, $expected->render(), "'changed' AJAX command issued with correct selector."); Chris@0: Chris@0: // Tests the 'changed' command using the second argument. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX changed: Click to mark div changed with asterisk.")]); Chris@0: $expected = new ChangedCommand('#changed_div', '#changed_div_mark_this'); Chris@0: $this->assertCommand($commands, $expected->render(), "'changed' AJAX command (with asterisk) issued with correct selector."); Chris@0: Chris@0: // Tests the 'css' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("Set the '#box' div to be blue.")]); Chris@0: $expected = new CssCommand('#css_div', ['background-color' => 'blue']); Chris@0: $this->assertCommand($commands, $expected->render(), "'css' AJAX command issued with correct selector."); Chris@0: Chris@0: // Tests the 'data' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX data command: Issue command.")]); Chris@0: $expected = new DataCommand('#data_div', 'testkey', 'testvalue'); Chris@0: $this->assertCommand($commands, $expected->render(), "'data' AJAX command issued with correct key and value."); Chris@0: Chris@0: // Tests the 'html' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX html: Replace the HTML in a selector.")]); Chris@0: $expected = new HtmlCommand('#html_div', 'replacement text'); Chris@0: $this->assertCommand($commands, $expected->render(), "'html' AJAX command issued with correct data."); Chris@0: Chris@0: // Tests the 'insert' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX insert: Let client insert based on #ajax['method'].")]); Chris@0: $expected = new InsertCommand('#insert_div', 'insert replacement text'); Chris@0: $this->assertCommand($commands, $expected->render(), "'insert' AJAX command issued with correct data."); Chris@0: Chris@0: // Tests the 'invoke' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX invoke command: Invoke addClass() method.")]); Chris@0: $expected = new InvokeCommand('#invoke_div', 'addClass', ['error']); Chris@0: $this->assertCommand($commands, $expected->render(), "'invoke' AJAX command issued with correct method and argument."); Chris@0: Chris@0: // Tests the 'prepend' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'prepend': Click to prepend something")]); Chris@0: $expected = new PrependCommand('#prepend_div', 'prepended text'); Chris@0: $this->assertCommand($commands, $expected->render(), "'prepend' AJAX command issued with correct data."); Chris@0: Chris@0: // Tests the 'remove' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'remove': Click to remove text")]); Chris@0: $expected = new RemoveCommand('#remove_text'); Chris@0: $this->assertCommand($commands, $expected->render(), "'remove' AJAX command issued with correct command and selector."); Chris@0: Chris@0: // Tests the 'restripe' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'restripe' command")]); Chris@0: $expected = new RestripeCommand('#restripe_table'); Chris@0: $this->assertCommand($commands, $expected->render(), "'restripe' AJAX command issued with correct selector."); Chris@0: Chris@0: // Tests the 'settings' command. Chris@0: $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'settings' command")]); Chris@0: $expected = new SettingsCommand(['ajax_forms_test' => ['foo' => 42]]); Chris@0: $this->assertCommand($commands, $expected->render(), "'settings' AJAX command issued with correct data."); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Regression test: Settings command exists regardless of JS aggregation. Chris@0: */ Chris@0: public function testAttachedSettings() { Chris@0: $assert = function ($message) { Chris@0: $response = new AjaxResponse(); Chris@0: $response->setAttachments([ Chris@0: 'library' => ['core/drupalSettings'], Chris@0: 'drupalSettings' => ['foo' => 'bar'], Chris@0: ]); Chris@0: Chris@0: $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor'); Chris@0: $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor); Chris@0: $event = new FilterResponseEvent( Chris@0: \Drupal::service('http_kernel'), Chris@0: new Request(), Chris@0: HttpKernelInterface::MASTER_REQUEST, Chris@0: $response Chris@0: ); Chris@0: $subscriber->onResponse($event); Chris@0: $expected = [ Chris@0: 'command' => 'settings', Chris@0: ]; Chris@0: $this->assertCommand($response->getCommands(), $expected, $message); Chris@0: }; Chris@0: Chris@0: $config = $this->config('system.performance'); Chris@0: Chris@0: $config->set('js.preprocess', FALSE)->save(); Chris@0: $assert('Settings command exists when JS aggregation is disabled.'); Chris@0: Chris@0: $config->set('js.preprocess', TRUE)->save(); Chris@0: $assert('Settings command exists when JS aggregation is enabled.'); Chris@0: } Chris@0: Chris@0: }