annotate core/tests/Drupal/FunctionalJavascriptTests/Ajax/FormValuesTest.php @ 19:fa3358dc1485 tip

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