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