Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.3.4 #142

Merged
merged 1 commit into from
Jul 10, 2023
Merged

8.3.4 #142

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Form/AddUserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
22 changes: 15 additions & 7 deletions tests/src/Kernel/Form/AddUserFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading