From a8805629ae4e70a912f18811f0243f91b977bd9e Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Mon, 10 Jul 2023 11:30:39 -0700 Subject: [PATCH] 8.3.4 --- src/Form/AddUserForm.php | 3 ++- tests/src/Kernel/Form/AddUserFormTest.php | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Form/AddUserForm.php b/src/Form/AddUserForm.php index 1683075..1cdc568 100644 --- a/src/Form/AddUserForm.php +++ b/src/Form/AddUserForm.php @@ -133,8 +133,9 @@ public function buildForm(array $form, FormStateInterface $form_state) { */ public static function validateSunetId(array &$element, FormStateInterface $form_state, array &$complete_form){ $value = $element['#value']; - if(!preg_match('/^[a-z0-9]*$/', $value)) { + if (!preg_match('/^[a-z0-9]*$/', $value)) { $form_state->setError($element, t('Invalid SunetID')); + return; } /** @var \Drupal\stanford_ssp\Service\StanfordSSPWorkgroupApiInterface $workgroup_api */ diff --git a/tests/src/Kernel/Form/AddUserFormTest.php b/tests/src/Kernel/Form/AddUserFormTest.php index c8fa22c..0112a27 100644 --- a/tests/src/Kernel/Form/AddUserFormTest.php +++ b/tests/src/Kernel/Form/AddUserFormTest.php @@ -89,34 +89,42 @@ public function testFormSubmit() { // Name is already assigned to a user. $form_state = new FormState(); - $form_state->setValue('sunetid', $this->randomMachineName()); + $form_state->setValue('sunetid', strtolower($this->randomMachineName())); $form_state->setValue('name', $this->existingUser->getAccountName()); \Drupal::formBuilder() ->submitForm('\Drupal\stanford_ssp\Form\AddUserForm', $form_state); $this->assertNotEmpty($form_state->getError(['#parents' => ['name']])); - $this->assertCount(1, $form_state->getErrors()); + $this->assertGreaterThanOrEqual(1, count($form_state->getErrors())); // Incorrect formatted email. $form_state = new FormState(); - $form_state->setValue('sunetid', $this->randomMachineName()); + $form_state->setValue('sunetid', strtolower($this->randomMachineName())); $form_state->setValue('email', $this->randomMachineName() . ' ' . $this->randomMachineName()); \Drupal::formBuilder() ->submitForm('\Drupal\stanford_ssp\Form\AddUserForm', $form_state); $this->assertNotEmpty($form_state->getError(['#parents' => ['email']])); - $this->assertCount(1, $form_state->getErrors()); + $this->assertGreaterThanOrEqual(1, count($form_state->getErrors())); // Email is already assigned to a user. $form_state = new FormState(); - $form_state->setValue('sunetid', $this->randomMachineName()); + $form_state->setValue('sunetid', strtolower($this->randomMachineName())); $form_state->setValue('email', $this->existingUser->getEmail()); \Drupal::formBuilder() ->submitForm('\Drupal\stanford_ssp\Form\AddUserForm', $form_state); $this->assertNotEmpty($form_state->getError(['#parents' => ['email']])); - $this->assertCount(1, $form_state->getErrors()); + $this->assertGreaterThanOrEqual(1, count($form_state->getErrors())); + + // Poorly formed sunetid + $form_state = new FormState(); + $form_state->setValue('sunetid', strtolower($this->randomMachineName()). ' foo'); + \Drupal::formBuilder() + ->submitForm('\Drupal\stanford_ssp\Form\AddUserForm', $form_state); + $this->assertNotEmpty($form_state->getError(['#parents' => ['email']])); + $this->assertGreaterThanOrEqual(1, count($form_state->getErrors())); // No errors submit. $form_state = new FormState(); - $form_state->setValue('sunetid', $this->randomMachineName()); + $form_state->setValue('sunetid', strtolower($this->randomMachineName())); $form_state->setValue('roles', []); $form_state->setValue('notify', TRUE); \Drupal::formBuilder()