diff --git a/module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php b/module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php index 3f0c404..6359743 100644 --- a/module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php +++ b/module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php @@ -159,7 +159,7 @@ protected function isPostalCodeValid() $isValid = (new Regex(['pattern' => '/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/i'])) ->isValid($postalCode); if (!$isValid) { - $this->error = 'The entered postal code is not valid.'; + $this->message = 'The entered postal code is not valid.'; } return $isValid; } @@ -168,12 +168,18 @@ protected function isPostalCodeValid() * Returns whether the Certificate of Conduct is properly set * * Checks three possible cases: + * * 1. The checkbox to indicate that the user has recieved their Certificate * of Conduct is checked, and the date entered is valid. * 2. The checkbox is checked, but the entered date is not valid. This will * set the error message appropriately. * 3. The checkbox was not selected, and in this case, the value should be NULL. * + * Postcondition: {@code $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT]} will contain + * the UNIX timestamp for the date the user entered IF the checkbox was checked, ELSE it will + * contain NULL. You should never need to access the date field directly other than in this function. + * "Date field" refers to: {@code $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE]} + * * @return bool Whether the Certificate of Conduct is properly set. */ protected function isCertificateOfConductValid() diff --git a/module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php b/module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php index 4c3eb84..f52e55f 100644 --- a/module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php +++ b/module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php @@ -3,7 +3,8 @@ namespace AbaLookup\Form; use - AbaLookup\Entity\User + AbaLookup\Entity\User, + AbaLookup\Entity\UserType ; /** @@ -61,18 +62,65 @@ public function __construct(User $user) 'name' => self::ELEMENT_NAME_POSTAL_CODE, 'type' => 'text', 'attributes' => [ - 'id' => self::ELEMENT_NAME_POSTAL_CODE, + 'id' => self::ELEMENT_NAME_POSTAL_CODE, + 'value' => $user->getPostalCode(), ], 'options' => [ 'label' => 'Postal code (optional)', ], ]); + // Show therapist-only fields? + if ($user->getUserType() === UserType::TYPE_ABA_THERAPIST) { + // ABA training course + $this->add([ + 'name' => self::ELEMENT_NAME_ABA_COURSE, + 'type' => 'checkbox', + 'attributes' => [ + 'id' => self::ELEMENT_NAME_ABA_COURSE, + 'checked' => $user->getAbaCourse(), + ], + 'options' => [ + 'label' => 'Completed ABA training course', + 'checked_value' => TRUE, + ], + ]); + // Certificate of Conduct and its date + $date = $user->getCertificateOfConduct(); + $this->add([ + 'name' => self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT, + 'type' => 'checkbox', + 'attributes' => [ + 'id' => self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT, + 'checked' => (bool) $date, + ], + 'options' => [ + 'label' => 'Certificate of Conduct', + 'checked_value' => TRUE, + ], + ]); + $dateFormElement = [ + 'name' => self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE, + 'type' => 'text', + 'attributes' => [ + 'id' => self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE, + 'type' => 'date', + 'max' => date('Y-m-d'), // Today + ], + 'options' => [ + 'label' => 'Date on Certificate of Conduct', + ], + ]; + if ($date) { + $dateFormElement['attributes']['value'] = date('Y-m-d', $date); + } + $this->add($dateFormElement); + } // Submit btn $this->add([ 'type' => 'submit', 'name' => 'update', 'attributes' => [ - 'value' => 'Update your information' + 'value' => 'Update your information', ], ]); } @@ -84,13 +132,15 @@ public function setIsValid() { $this->isValid = $this->isDisplayNameValid() && $this->isEmailAddressValid() - && $this->isPhoneNumberValid(); + && $this->isPhoneNumberValid() + && $this->isPostalCodeValid() + && $this->isCertificateOfConductValid(); } /** * Updates the user with their new information * - * Takes the user to update and populates the fields with the updated data. + * Populates the fields with the updated data. * * @param User $user The user to update. * @return bool Whether the update was successful. @@ -101,16 +151,19 @@ public function updateUser(User $user) return FALSE; } // Aliases - $displayName = $this->data[self::ELEMENT_NAME_DISPLAY_NAME]; - $email = $this->data[self::ELEMENT_NAME_EMAIL_ADDRESS]; - $phone = $this->data[self::ELEMENT_NAME_PHONE_NUMBER]; + $abaCourse = $this->data[self::ELEMENT_NAME_ABA_COURSE]; + $certificateOfConduct = $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT]; + $displayName = $this->data[self::ELEMENT_NAME_DISPLAY_NAME]; + $email = $this->data[self::ELEMENT_NAME_EMAIL_ADDRESS]; + $phone = $this->data[self::ELEMENT_NAME_PHONE_NUMBER]; + $postalCode = $this->data[self::ELEMENT_NAME_POSTAL_CODE]; // Update the information - $user->setDisplayName($displayName); - $user->setEmail($email); - if ($phone) { - // The user entered a phone number - $user->setPhone((int) $phone); - } + $user->setAbaCourse($abaCourse !== NULL ? (bool) $abaCourse : $abaCourse) + ->setCertificateOfConduct($certificateOfConduct) + ->setDisplayName($displayName) + ->setEmail($email) + ->setPhone($phone ? (int) $phone : NULL) + ->setPostalCode($postalCode ? $postalCode : NULL); return TRUE; } } diff --git a/module/AbaLookup/test/AbaLookupTest/Form/ProfileEditFormTest.php b/module/AbaLookup/test/AbaLookupTest/Form/ProfileEditFormTest.php index 9c4d780..2a286db 100644 --- a/module/AbaLookup/test/AbaLookupTest/Form/ProfileEditFormTest.php +++ b/module/AbaLookup/test/AbaLookupTest/Form/ProfileEditFormTest.php @@ -35,9 +35,13 @@ class ProfileEditFormTest extends PHPUnit_Framework_TestCase */ protected function setFormData(array $data) { $defaultValues = [ - ProfileEditForm::ELEMENT_NAME_DISPLAY_NAME => 'John Doe', - ProfileEditForm::ELEMENT_NAME_EMAIL_ADDRESS => 'foo@bar.com', - ProfileEditForm::ELEMENT_NAME_PHONE_NUMBER => '', + ProfileEditForm::ELEMENT_NAME_ABA_COURSE => '', + ProfileEditForm::ELEMENT_NAME_DISPLAY_NAME => 'John Doe', + ProfileEditForm::ELEMENT_NAME_EMAIL_ADDRESS => 'foo@bar.com', + ProfileEditForm::ELEMENT_NAME_PHONE_NUMBER => '', + ProfileEditForm::ELEMENT_NAME_POSTAL_CODE => '', + ProfileEditForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT => 0, + ProfileEditForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE => '', ]; $this->form->setData($data + $defaultValues); } @@ -47,7 +51,7 @@ protected function setFormData(array $data) { */ public function setUp() { - $this->user = new User('Ramus', 'foo@bar.com', 'password', UserType::TYPE_PARENT); + $this->user = new User('Ramus', 'foo@bar.com', 'password', UserType::TYPE_ABA_THERAPIST); $this->form = new ProfileEditForm($this->user); } @@ -75,42 +79,49 @@ public function testInvalidDisplayNameDoesNotValidate() { $this->setFormData([ProfileEditForm::ELEMENT_NAME_DISPLAY_NAME => '']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testDisplayNameOnlySpacesDoesNotValidate() { $this->setFormData([ProfileEditForm::ELEMENT_NAME_DISPLAY_NAME => ' ']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testNullDisplayNameDoesNotValidate() { $this->setFormData([ProfileEditForm::ELEMENT_NAME_DISPLAY_NAME => NULL]); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testEmptyEmailAddressDoesNotValidate() { $this->setFormData([ProfileEditForm::ELEMENT_NAME_EMAIL_ADDRESS => '']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testEmailAddressContainingNonEnglishCharactersDoesNotValidate() { $this->setFormData([ProfileEditForm::ELEMENT_NAME_EMAIL_ADDRESS => 'foö@bar.com']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testNullEmailAddressDoesNotValidate() { $this->setFormData([ProfileEditForm::ELEMENT_NAME_EMAIL_ADDRESS => NULL]); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testInvalidPhoneNumberDoesNotValidate() { $this->setFormData([ProfileEditForm::ELEMENT_NAME_PHONE_NUMBER => '1']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testNullPhoneNumberDoesValidate() @@ -131,6 +142,13 @@ public function testPhoneNumberWithHyphensDoesValidate() $this->assertTrue($this->form->isValid()); } + public function testInvalidPostalCodeDoesNotValidate() + { + $this->setFormData([ProfileEditForm::ELEMENT_NAME_POSTAL_CODE => 'this is not real']); + $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); + } + public function testCanUpdateDisplayName() { $displayName = 'John Doe'; @@ -142,7 +160,7 @@ public function testCanUpdateDisplayName() $displayName = ' John Doe '; $this->setFormData([ProfileEditForm::ELEMENT_NAME_DISPLAY_NAME => $displayName]); $this->assertTrue($this->form->isValid()); - $this->form->updateUser($this->user); + $this->assertTrue($this->form->updateUser($this->user)); $this->assertEquals('John Doe', $this->user->getDisplayName()); } @@ -151,7 +169,7 @@ public function testCanUpdateDisplayNameWithNonEnglishCharacters() $displayName = 'ËèŒŁma Kæępø'; $this->setFormData([ProfileEditForm::ELEMENT_NAME_DISPLAY_NAME => $displayName]); $this->assertTrue($this->form->isValid()); - $this->form->updateUser($this->user); + $this->assertTrue($this->form->updateUser($this->user)); $this->assertEquals($displayName, $this->user->getDisplayName()); } @@ -161,7 +179,7 @@ public function testCanUpdatePhoneNumber() $phoneNumber = '7095551234'; $this->setFormData([ProfileEditForm::ELEMENT_NAME_PHONE_NUMBER => $phoneNumber]); $this->assertTrue($this->form->isValid()); - $this->form->updateUser($this->user); + $this->assertTrue($this->form->updateUser($this->user)); $this->assertTrue(7095551234 === $this->user->getPhone()); } @@ -171,7 +189,7 @@ public function testCanUpdatePhoneNumberWithSpaces() $phoneNumber = '709 555 1234'; $this->setFormData([ProfileEditForm::ELEMENT_NAME_PHONE_NUMBER => $phoneNumber]); $this->assertTrue($this->form->isValid()); - $this->form->updateUser($this->user); + $this->assertTrue($this->form->updateUser($this->user)); $this->assertTrue(7095551234 === $this->user->getPhone()); } @@ -181,7 +199,7 @@ public function testCanUpdatePhoneNumberWithPeriodsBetweensSets() $phoneNumber = '709.555.1234'; $this->setFormData([ProfileEditForm::ELEMENT_NAME_PHONE_NUMBER => $phoneNumber]); $this->assertTrue($this->form->isValid()); - $this->form->updateUser($this->user); + $this->assertTrue($this->form->updateUser($this->user)); $this->assertTrue(7095551234 === $this->user->getPhone()); } @@ -191,7 +209,54 @@ public function testCanUpdatePhoneNumberWithHyphens() $phoneNumber = '709-555-1234'; $this->setFormData([ProfileEditForm::ELEMENT_NAME_PHONE_NUMBER => $phoneNumber]); $this->assertTrue($this->form->isValid()); - $this->form->updateUser($this->user); + $this->assertTrue($this->form->updateUser($this->user)); $this->assertTrue(7095551234 === $this->user->getPhone()); } + + public function testCanUpdatePostalCode() + { + $this->assertNull($this->user->getPostalCode()); + $this->setFormData([ProfileEditForm::ELEMENT_NAME_POSTAL_CODE => 'A1B 3X9']); + $this->assertTrue($this->form->isValid()); + $this->assertTrue($this->form->updateUser($this->user)); + $this->assertEquals('A1B3X9', $this->user->getPostalCode()); + $this->setFormData([ProfileEditForm::ELEMENT_NAME_POSTAL_CODE => '']); + $this->assertTrue($this->form->isValid()); + $this->assertTrue($this->form->updateUser($this->user)); + $this->assertNull($this->user->getPostalCode()); + } + + public function testCanUpdateAbaCourseStatus() + { + $this->assertNull($this->user->getAbaCourse()); + $this->setFormData([ProfileEditForm::ELEMENT_NAME_ABA_COURSE => '1']); + $this->assertTrue($this->form->isValid()); + $this->assertTrue($this->form->updateUser($this->user)); + $this->assertTrue($this->user->getAbaCourse()); + $this->setFormData([ProfileEditForm::ELEMENT_NAME_ABA_COURSE => '0']); + $this->assertTrue($this->form->isValid()); + $this->assertTrue($this->form->updateUser($this->user)); + $this->assertFalse($this->user->getAbaCourse()); + } + + public function testCanUpdateCertificateOfConduct() + { + $this->assertNull($this->user->getCertificateOfConduct()); + $date = date('Y-m-d', strtotime('3 days ago')); + $time = strtotime($date); + $this->setFormData([ + ProfileEditForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT => '1', + ProfileEditForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE => $date, + ]); + $this->assertTrue($this->form->isValid()); + $this->assertTrue($this->form->updateUser($this->user)); + $this->assertEquals($time, $this->user->getCertificateOfConduct()); + $this->setFormData([ + ProfileEditForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT => '0', + ProfileEditForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE => $date, + ]); + $this->assertTrue($this->form->isValid()); + $this->assertTrue($this->form->updateUser($this->user)); + $this->assertNull($this->user->getCertificateOfConduct()); + } } diff --git a/module/AbaLookup/test/AbaLookupTest/Form/RegisterFormTest.php b/module/AbaLookup/test/AbaLookupTest/Form/RegisterFormTest.php index d117e79..05a763c 100644 --- a/module/AbaLookup/test/AbaLookupTest/Form/RegisterFormTest.php +++ b/module/AbaLookup/test/AbaLookupTest/Form/RegisterFormTest.php @@ -1,6 +1,6 @@ FALSE, + RegisterForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT => 0, + RegisterForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE => date('Y-m-d'), + RegisterForm::ELEMENT_NAME_CONFIRM_PASSWORD => 'password', RegisterForm::ELEMENT_NAME_DISPLAY_NAME => 'John Doe', RegisterForm::ELEMENT_NAME_EMAIL_ADDRESS => 'jdoe@email.com', + RegisterForm::ELEMENT_NAME_GENDER => NULL, RegisterForm::ELEMENT_NAME_PASSWORD => 'password', - RegisterForm::ELEMENT_NAME_CONFIRM_PASSWORD => 'password', RegisterForm::ELEMENT_NAME_PHONE_NUMBER => '7095551234', - RegisterForm::ELEMENT_NAME_GENDER => NULL, - RegisterForm::ELEMENT_NAME_ABA_COURSE => FALSE, - RegisterForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT => 0, - RegisterForm::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE => date('Y-m-d'), RegisterForm::ELEMENT_NAME_POSTAL_CODE => '', ]; $this->form->setData($data + $defaultValues); @@ -99,36 +99,42 @@ public function testNullDisplayNameDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_DISPLAY_NAME => NULL]); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testEmptyDisplayNameDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_DISPLAY_NAME => '']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testDisplayNameFilledWithWhitespaceDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_DISPLAY_NAME => ' ']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testEmailAddressContainingNonEnglishCharactersDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_EMAIL_ADDRESS => 'foö@bar.com']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testEmptyEmailAddressDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_EMAIL_ADDRESS => '']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testNullEmailAddressDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_EMAIL_ADDRESS => NULL]); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testPasswordTooShortDoesNotValidate() @@ -138,12 +144,14 @@ public function testPasswordTooShortDoesNotValidate() RegisterForm::ELEMENT_NAME_CONFIRM_PASSWORD => 'pass', ]); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testNullPasswordDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_PASSWORD => NULL]); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testNotConfirmingPasswordDoesNotValidate() @@ -153,12 +161,14 @@ public function testNotConfirmingPasswordDoesNotValidate() RegisterForm::ELEMENT_NAME_CONFIRM_PASSWORD => 'not the same password', ]); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testInvalidPhoneNumberDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_PHONE_NUMBER => '000']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testPhoneNumberWithHyphensDoesValidate() @@ -269,8 +279,10 @@ public function testInvalidPostalCodeDoesNotValidate() { $this->setFormData([RegisterForm::ELEMENT_NAME_POSTAL_CODE => 3]); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); $this->setFormData([RegisterForm::ELEMENT_NAME_POSTAL_CODE => 'not a postal code']); $this->assertFalse($this->form->isValid()); + $this->assertInternalType('string', $this->form->getMessage()); } public function testNullPostalCodeDoesValidate()