Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Added fields to profile edit form. (Link to MUNComputerScienceSociety#72
Browse files Browse the repository at this point in the history
.)

Added for both parents and therapists:
- Postal code
Added for therapists:
- ABA training course status
- Certificate of Conduct
- Certificate of Conduct date of reciept (MUNComputerScienceSociety#93)

Updated tests as well.
  • Loading branch information
whymarrh committed Oct 13, 2013
1 parent 16ad27e commit 807d7a7
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 31 deletions.
8 changes: 7 additions & 1 deletion module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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()
Expand Down
81 changes: 67 additions & 14 deletions module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace AbaLookup\Form;

use
AbaLookup\Entity\User
AbaLookup\Entity\User,
AbaLookup\Entity\UserType
;

/**
Expand Down Expand Up @@ -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',
],
]);
}
Expand All @@ -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.
Expand All @@ -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;
}
}
85 changes: 75 additions & 10 deletions module/AbaLookup/test/AbaLookupTest/Form/ProfileEditFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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()
Expand All @@ -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';
Expand All @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}
}
Loading

0 comments on commit 807d7a7

Please sign in to comment.