Skip to content
Snippets Groups Projects
Verified Commit 30cda520 authored by Drew Webber's avatar Drew Webber
Browse files

Issue #3362238 by poker10, DeanThomas, gbirch, sakthi_dev, joelpittet:...

Issue #3362238 by poker10, DeanThomas, gbirch, sakthi_dev, joelpittet: _form_validate sends null to drupal_strlen triggering deprecation notice
parent 842b56b4
Branches
Tags
No related merge requests found
Pipeline #189860 passed
Pipeline: drupal

#189870

    Pipeline: drupal

    #189869

      Pipeline: drupal

      #189868

        +7
        ......@@ -478,6 +478,9 @@ function decode_entities($text) {
        */
        function drupal_strlen($text) {
        global $multibyte;
        if (is_null($text)) {
        return 0;
        }
        if ($multibyte == UNICODE_MULTIBYTE) {
        return mb_strlen($text);
        }
        ......
        ......@@ -768,6 +768,109 @@ class FormValidationTestCase extends DrupalWebTestCase {
        }
        }
        /**
        * Tests validation of additional Form API properties.
        *
        * Limited to maxlength validation at present.
        */
        class FormsElementsValidationTestCase extends DrupalWebTestCase {
        public static function getInfo() {
        return array(
        'name' => 'Form element validation - misc',
        'description' => 'Tests miscellaneous form element validation mechanisms.',
        'group' => 'Form API',
        );
        }
        function setUp() {
        parent::setUp('form_test');
        }
        /**
        * Tests #maxlength validation.
        */
        public function testMaxlengthValidation() {
        $max_length = 5;
        // The field types that support #maxlength.
        $form = array(
        'textfield' => array(
        '#type' => 'textfield',
        '#title' => 'Textfield',
        '#required' => FALSE,
        '#maxlength' => $max_length,
        ),
        'password' => array(
        '#type' => 'password',
        '#title' => 'Password',
        '#maxlength' => $max_length,
        ),
        );
        $edit = array(
        'textfield' => $this->randomString($max_length + 1),
        'password' => $this->randomString($max_length + 1),
        );
        list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, $edit);
        $this->assertFalse(empty($errors), 'Form with overly long inputs returned errors.');
        $this->assertTrue(isset($errors['textfield']) && strpos($errors['textfield'], 'cannot be longer than') !== FALSE, 'Long input error in textfield.');
        $this->assertTrue(isset($errors['password']) && strpos($errors['password'], 'cannot be longer than') !== FALSE, 'Long input error in password.');
        // This test for NULL inputs cannot be performed using the drupalPost() method.
        $edit['textfield'] = NULL;
        $edit['password'] = NULL;
        list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, $edit);
        $this->assertTrue(empty($errors), 'Form with NULL inputs did not return errors.');
        $edit['textfield'] = $this->randomString($max_length);
        $edit['password'] = $this->randomString($max_length);
        list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, $edit);
        $this->assertTrue(empty($errors), 'Form with maxlength inputs did not return errors.');
        }
        /**
        * Helper function for the option check test to submit a form while collecting errors.
        *
        * Copied from FormsElementsTableSelectFunctionalTest.
        *
        * @param $form_element
        * A form element to test.
        * @param $edit
        * An array containing post data.
        *
        * @return
        * An array containing the processed form, the form_state and any errors.
        */
        private function formSubmitHelper($form, $edit) {
        $form_id = $this->randomName();
        $form_state = form_state_defaults();
        $form['op'] = array('#type' => 'submit', '#value' => t('Submit'));
        $form_state['input'] = $edit;
        $form_state['input']['form_id'] = $form_id;
        // The form token CSRF protection should not interfere with this test,
        // so we bypass it by marking this test form as programmed.
        $form_state['programmed'] = TRUE;
        drupal_prepare_form($form_id, $form, $form_state);
        drupal_process_form($form_id, $form, $form_state);
        $errors = form_get_errors();
        // Clear errors and messages.
        drupal_get_messages();
        form_clear_error();
        // Return the processed form together with form_state and errors
        // to allow the caller lowlevel access to the form.
        return array($form, $form_state, $errors);
        }
        }
        /**
        * Test form element labels, required markers and associated output.
        */
        ......
        ......@@ -118,10 +118,12 @@ class UnicodeUnitTest extends DrupalUnitTestCase {
        $testcase = array(
        'tHe QUIcK bRoWn' => 15,
        'ÜBER-åwesome' => 12,
        'NULL' => 0,
        );
        foreach ($testcase as $input => $output) {
        $this->assertEqual(drupal_strlen($input), $output, format_string('%input length is %output', array('%input' => $input, '%output' => $output)));
        $tested_value = ($input === 'NULL' ? NULL : $input);
        $this->assertEqual(drupal_strlen($tested_value), $output, format_string('%input length is %output', array('%input' => $input, '%output' => $output)));
        }
        }
        ......
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment