FormValuesTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. /**
  5. * Tests that form values are properly delivered to AJAX callbacks.
  6. *
  7. * @group Ajax
  8. */
  9. class FormValuesTest extends WebDriverTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected $defaultTheme = 'stark';
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp() {
  22. parent::setUp();
  23. $this->drupalLogin($this->drupalCreateUser(['access content']));
  24. }
  25. /**
  26. * Submits forms with select and checkbox elements via Ajax.
  27. */
  28. public function testSimpleAjaxFormValue() {
  29. $this->drupalGet('ajax_forms_test_get_form');
  30. $session = $this->getSession();
  31. $assertSession = $this->assertSession();
  32. // Verify form values of a select element.
  33. foreach (['green', 'blue', 'red'] as $item) {
  34. // Updating the field will trigger a AJAX request/response.
  35. $session->getPage()->selectFieldOption('select', $item);
  36. // The AJAX command in the response will update the DOM
  37. $select = $assertSession->waitForElement('css', "div#ajax_selected_color:contains('$item')");
  38. $this->assertNotNull($select, "DataCommand has updated the page with a value of $item.");
  39. }
  40. // Verify form values of a checkbox element.
  41. $session->getPage()->checkField('checkbox');
  42. $div0 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('checked')");
  43. $this->assertNotNull($div0, 'DataCommand updates the DOM as expected when a checkbox is selected');
  44. $session->getPage()->uncheckField('checkbox');
  45. $div1 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('unchecked')");
  46. $this->assertNotNull($div1, 'DataCommand updates the DOM as expected when a checkbox is de-selected');
  47. // Verify that AJAX elements with invalid callbacks return error code 500.
  48. // Ensure the test error log is empty before these tests.
  49. $this->assertFileNotExists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
  50. // We don't need to check for the X-Drupal-Ajax-Token header with these
  51. // invalid requests.
  52. $this->assertAjaxHeader = FALSE;
  53. foreach (['null', 'empty', 'nonexistent'] as $key) {
  54. $element_name = 'select_' . $key . '_callback';
  55. // Updating the field will trigger a AJAX request/response.
  56. $session->getPage()->selectFieldOption($element_name, 'green');
  57. // The select element is disabled as the AJAX request is issued.
  58. $this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:disabled");
  59. // The select element is enabled as the response is receieved.
  60. $this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:enabled");
  61. $this->assertFileExists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
  62. $this->assertStringContainsString('"The specified #ajax callback is empty or not callable."', file_get_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'));
  63. // The exceptions are expected. Do not interpret them as a test failure.
  64. // Not using File API; a potential error must trigger a PHP warning.
  65. unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
  66. }
  67. // We need to reload the page to kill any unfinished AJAX calls before
  68. // tearDown() is called.
  69. $this->drupalGet('ajax_forms_test_get_form');
  70. }
  71. }