Example #1
0
 /**
  * Submits forms with select and checkbox elements via Ajax.
  */
 function testSimpleAjaxFormValue()
 {
     // Verify form values of a select element.
     foreach (array('red', 'green', 'blue') as $item) {
         $edit = array('select' => $item);
         $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, 'select');
         $expected = new DataCommand('#ajax_selected_color', 'form_state_value_select', $item);
         $this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a selectbox issued with a correct value.');
     }
     // Verify form values of a checkbox element.
     foreach (array(FALSE, TRUE) as $item) {
         $edit = array('checkbox' => $item);
         $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, 'checkbox');
         $expected = new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $item);
         $this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a checkbox issued with a correct value.');
     }
     // Verify that AJAX elements with invalid callbacks return error code 500.
     // Ensure the test error log is empty before these tests.
     $this->assertNoErrorsLogged();
     // We don't need to check for the X-Drupal-Ajax-Token header with these
     // invalid requests.
     $this->assertAjaxHeader = FALSE;
     foreach (array('null', 'empty', 'nonexistent') as $key) {
         $element_name = 'select_' . $key . '_callback';
         $edit = array($element_name => 'red');
         $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, $element_name);
         $this->assertResponse(500);
     }
     // Switch this back to the default.
     $this->assertAjaxHeader = TRUE;
     // The exceptions are expected. Do not interpret them as a test failure.
     // Not using File API; a potential error must trigger a PHP warning.
     unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
 }
Example #2
0
 /**
  * Tests the various Ajax Commands.
  */
 function testAjaxCommands()
 {
     $form_path = 'ajax_forms_test_ajax_commands_form';
     $web_user = $this->drupalCreateUser(array('access content'));
     $this->drupalLogin($web_user);
     $edit = array();
     // Tests the 'add_css' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'add_css' command")));
     $expected = new AddCssCommand('my/file.css');
     $this->assertCommand($commands, $expected->render(), "'add_css' AJAX command issued with correct data.");
     // Tests the 'after' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'After': Click to put something after the div")));
     $expected = new AfterCommand('#after_div', 'This will be placed after');
     $this->assertCommand($commands, $expected->render(), "'after' AJAX command issued with correct data.");
     // Tests the 'alert' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'Alert': Click to alert")));
     $expected = new AlertCommand(t('Alert'));
     $this->assertCommand($commands, $expected->render(), "'alert' AJAX Command issued with correct text.");
     // Tests the 'append' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'Append': Click to append something")));
     $expected = new AppendCommand('#append_div', 'Appended text');
     $this->assertCommand($commands, $expected->render(), "'append' AJAX command issued with correct data.");
     // Tests the 'before' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'before': Click to put something before the div")));
     $expected = new BeforeCommand('#before_div', 'Before text');
     $this->assertCommand($commands, $expected->render(), "'before' AJAX command issued with correct data.");
     // Tests the 'changed' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX changed: Click to mark div changed.")));
     $expected = new ChangedCommand('#changed_div');
     $this->assertCommand($commands, $expected->render(), "'changed' AJAX command issued with correct selector.");
     // Tests the 'changed' command using the second argument.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX changed: Click to mark div changed with asterisk.")));
     $expected = new ChangedCommand('#changed_div', '#changed_div_mark_this');
     $this->assertCommand($commands, $expected->render(), "'changed' AJAX command (with asterisk) issued with correct selector.");
     // Tests the 'css' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("Set the '#box' div to be blue.")));
     $expected = new CssCommand('#css_div', array('background-color' => 'blue'));
     $this->assertCommand($commands, $expected->render(), "'css' AJAX command issued with correct selector.");
     // Tests the 'data' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX data command: Issue command.")));
     $expected = new DataCommand('#data_div', 'testkey', 'testvalue');
     $this->assertCommand($commands, $expected->render(), "'data' AJAX command issued with correct key and value.");
     // Tests the 'html' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX html: Replace the HTML in a selector.")));
     $expected = new HtmlCommand('#html_div', 'replacement text');
     $this->assertCommand($commands, $expected->render(), "'html' AJAX command issued with correct data.");
     // Tests the 'insert' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX insert: Let client insert based on #ajax['method'].")));
     $expected = new InsertCommand('#insert_div', 'insert replacement text');
     $this->assertCommand($commands, $expected->render(), "'insert' AJAX command issued with correct data.");
     // Tests the 'invoke' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX invoke command: Invoke addClass() method.")));
     $expected = new InvokeCommand('#invoke_div', 'addClass', array('error'));
     $this->assertCommand($commands, $expected->render(), "'invoke' AJAX command issued with correct method and argument.");
     // Tests the 'prepend' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'prepend': Click to prepend something")));
     $expected = new PrependCommand('#prepend_div', 'prepended text');
     $this->assertCommand($commands, $expected->render(), "'prepend' AJAX command issued with correct data.");
     // Tests the 'remove' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'remove': Click to remove text")));
     $expected = new RemoveCommand('#remove_text');
     $this->assertCommand($commands, $expected->render(), "'remove' AJAX command issued with correct command and selector.");
     // Tests the 'restripe' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'restripe' command")));
     $expected = new RestripeCommand('#restripe_table');
     $this->assertCommand($commands, $expected->render(), "'restripe' AJAX command issued with correct selector.");
     // Tests the 'settings' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'settings' command")));
     $expected = new SettingsCommand(array('ajax_forms_test' => array('foo' => 42)));
     $this->assertCommand($commands, $expected->render(), "'settings' AJAX command issued with correct data.");
 }
Example #3
0
 /**
  * @covers \Drupal\Core\Ajax\DataCommand
  */
 public function testDataCommand()
 {
     $command = new DataCommand('#page-title', 'my-data', array('key' => 'value'));
     $expected = array('command' => 'data', 'selector' => '#page-title', 'name' => 'my-data', 'value' => array('key' => 'value'));
     $this->assertEquals($expected, $command->render());
 }