2009-06-27 06:00:10 +02:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\ConfirmedPasswordField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2018-10-20 17:18:31 +02:00
|
|
|
use SilverStripe\Forms\ReadonlyField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2018-11-14 10:54:17 +01:00
|
|
|
use SilverStripe\Security\PasswordValidator;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class ConfirmedPasswordFieldTest extends SapphireTest
|
|
|
|
{
|
2018-11-14 10:54:17 +01:00
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-12-03 00:04:34 +01:00
|
|
|
PasswordValidator::singleton()
|
|
|
|
->setMinLength(0)
|
|
|
|
->setTestNames([]);
|
2018-11-14 10:54:17 +01:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function testSetValue()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA');
|
|
|
|
$this->assertEquals('valueA', $field->Value());
|
|
|
|
$this->assertEquals('valueA', $field->children->fieldByName($field->getName() . '[_Password]')->Value());
|
|
|
|
$this->assertEquals('valueA', $field->children->fieldByName($field->getName() . '[_ConfirmPassword]')->Value());
|
|
|
|
$field->setValue('valueB');
|
|
|
|
$this->assertEquals('valueB', $field->Value());
|
|
|
|
$this->assertEquals('valueB', $field->children->fieldByName($field->getName() . '[_Password]')->Value());
|
|
|
|
$this->assertEquals('valueB', $field->children->fieldByName($field->getName() . '[_ConfirmPassword]')->Value());
|
|
|
|
}
|
2011-02-25 05:01:46 +01:00
|
|
|
|
2018-10-20 17:18:31 +02:00
|
|
|
/**
|
|
|
|
* @useDatabase true
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testHashHidden()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Password', 'Password', 'valueA');
|
|
|
|
$field->setCanBeEmpty(true);
|
2013-06-20 04:08:46 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('valueA', $field->Value());
|
|
|
|
$this->assertEquals('valueA', $field->children->fieldByName($field->getName() . '[_Password]')->Value());
|
|
|
|
$this->assertEquals('valueA', $field->children->fieldByName($field->getName() . '[_ConfirmPassword]')->Value());
|
2013-06-20 04:08:46 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = new Member();
|
|
|
|
$member->Password = "valueB";
|
|
|
|
$member->write();
|
2013-06-20 04:08:46 +02:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @skipUpgrade */
|
|
|
|
$form = new Form(Controller::curr(), 'Form', new FieldList($field), new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->loadDataFrom($member);
|
2013-06-20 04:08:46 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('', $field->Value());
|
|
|
|
$this->assertEquals('', $field->children->fieldByName($field->getName() . '[_Password]')->Value());
|
|
|
|
$this->assertEquals('', $field->children->fieldByName($field->getName() . '[_ConfirmPassword]')->Value());
|
|
|
|
}
|
2013-06-20 04:08:46 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSetShowOnClick()
|
|
|
|
{
|
|
|
|
//hide by default and display show/hide toggle button
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA', null, true);
|
|
|
|
$fieldHTML = $field->Field();
|
|
|
|
$this->assertContains(
|
|
|
|
"showOnClickContainer",
|
|
|
|
$fieldHTML,
|
|
|
|
"Test class for hiding/showing the form contents is set"
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
"showOnClick",
|
|
|
|
$fieldHTML,
|
|
|
|
"Test class for hiding/showing the form contents is set"
|
|
|
|
);
|
2011-02-25 05:01:46 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
//show all by default
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA', null, false);
|
|
|
|
$fieldHTML = $field->Field();
|
|
|
|
$this->assertNotContains(
|
|
|
|
"showOnClickContainer",
|
|
|
|
$fieldHTML,
|
|
|
|
"Test class for hiding/showing the form contents is set"
|
|
|
|
);
|
|
|
|
$this->assertNotContains(
|
|
|
|
"showOnClick",
|
|
|
|
$fieldHTML,
|
|
|
|
"Test class for hiding/showing the form contents is set"
|
|
|
|
);
|
|
|
|
}
|
2014-01-07 05:16:58 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testValidation()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField(
|
|
|
|
'Test',
|
|
|
|
'Testing',
|
2018-10-20 16:34:36 +02:00
|
|
|
[
|
|
|
|
'_Password' => 'abc123',
|
|
|
|
'_ConfirmPassword' => 'abc123',
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
2018-10-20 16:34:36 +02:00
|
|
|
'Validates when both passwords are the same'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2018-10-20 16:34:36 +02:00
|
|
|
$field->setName('TestNew'); //try changing name of field
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
2018-10-20 16:34:36 +02:00
|
|
|
'Validates when field name is changed'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
//non-matching password should make the field invalid
|
2018-10-20 16:34:36 +02:00
|
|
|
$field->setValue([
|
|
|
|
'_Password' => 'abc123',
|
|
|
|
'_ConfirmPassword' => '123abc',
|
|
|
|
]);
|
|
|
|
$this->assertFalse(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Does not validate when passwords differ'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2018-10-20 16:34:36 +02:00
|
|
|
|
|
|
|
// Empty passwords should make the field invalid
|
|
|
|
$field->setCanBeEmpty(false);
|
|
|
|
$field->setValue([
|
|
|
|
'_Password' => '',
|
|
|
|
'_ConfirmPassword' => '',
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$field->validate($validator),
|
2018-10-20 16:34:36 +02:00
|
|
|
'Empty passwords should not be allowed when canBeEmpty is false'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFormValidation()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @skipUpgrade */
|
2015-11-18 14:11:31 +01:00
|
|
|
$form = new Form(
|
2017-06-22 12:50:45 +02:00
|
|
|
Controller::curr(),
|
2015-11-18 14:11:31 +01:00
|
|
|
'Form',
|
|
|
|
new FieldList($field = new ConfirmedPasswordField('Password')),
|
|
|
|
new FieldList()
|
|
|
|
);
|
|
|
|
|
2018-10-20 16:34:36 +02:00
|
|
|
$form->loadDataFrom([
|
|
|
|
'Password' => [
|
2015-11-18 14:11:31 +01:00
|
|
|
'_Password' => '123',
|
|
|
|
'_ConfirmPassword' => '999',
|
2018-10-20 16:34:36 +02:00
|
|
|
],
|
|
|
|
]);
|
2015-11-18 14:11:31 +01:00
|
|
|
|
|
|
|
$this->assertEquals('123', $field->children->first()->Value());
|
|
|
|
$this->assertEquals('999', $field->children->last()->Value());
|
|
|
|
$this->assertNotEquals($field->children->first()->Value(), $field->children->last()->Value());
|
|
|
|
}
|
2018-10-20 16:34:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $minLength
|
|
|
|
* @param int|null $maxLength
|
|
|
|
* @param bool $expectValid
|
|
|
|
* @param string $expectedMessage
|
|
|
|
* @dataProvider lengthValidationProvider
|
|
|
|
*/
|
|
|
|
public function testLengthValidation($minLength, $maxLength, $expectValid, $expectedMessage = '')
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Testing', [
|
|
|
|
'_Password' => 'abc123',
|
|
|
|
'_ConfirmPassword' => 'abc123',
|
|
|
|
]);
|
|
|
|
$field->setMinLength($minLength)->setMaxLength($maxLength);
|
|
|
|
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$result = $field->validate($validator);
|
|
|
|
|
|
|
|
$this->assertSame($expectValid, $result, 'Validate method should return its result');
|
|
|
|
$this->assertSame($expectValid, $validator->getResult()->isValid());
|
|
|
|
if ($expectedMessage) {
|
|
|
|
$this->assertContains($expectedMessage, $validator->getResult()->serialize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function lengthValidationProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'valid: within min and max' => [3, 8, true],
|
|
|
|
'invalid: lower than min with max' => [8, 12, false, 'Passwords must be 8 to 12 characters long'],
|
|
|
|
'valid: greater than min' => [3, null, true],
|
|
|
|
'invalid: lower than min' => [8, null, false, 'Passwords must be at least 8 characters long'],
|
|
|
|
'valid: less than max' => [null, 8, true],
|
|
|
|
'invalid: greater than max' => [null, 4, false, 'Passwords must be at most 4 characters long'],
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStrengthValidation()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Testing', [
|
|
|
|
'_Password' => 'abc',
|
|
|
|
'_ConfirmPassword' => 'abc',
|
|
|
|
]);
|
|
|
|
$field->setRequireStrongPassword(true);
|
|
|
|
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$result = $field->validate($validator);
|
|
|
|
|
|
|
|
$this->assertFalse($result, 'Validate method should return its result');
|
|
|
|
$this->assertFalse($validator->getResult()->isValid());
|
|
|
|
$this->assertContains(
|
|
|
|
'Passwords must have at least one digit and one alphanumeric character',
|
|
|
|
$validator->getResult()->serialize()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:18:31 +02:00
|
|
|
public function testCurrentPasswordValidation()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Testing', [
|
|
|
|
'_Password' => 'abc',
|
|
|
|
'_ConfirmPassword' => 'abc',
|
|
|
|
]);
|
|
|
|
$field->setRequireExistingPassword(true);
|
|
|
|
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$result = $field->validate($validator);
|
|
|
|
|
|
|
|
$this->assertFalse($result, 'Validate method should return its result');
|
|
|
|
$this->assertFalse($validator->getResult()->isValid());
|
|
|
|
$this->assertContains(
|
|
|
|
'You must enter your current password',
|
|
|
|
$validator->getResult()->serialize()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMustBeLoggedInToChangePassword()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Testing');
|
|
|
|
$field->setRequireExistingPassword(true);
|
|
|
|
$field->setValue([
|
|
|
|
'_CurrentPassword' => 'foo',
|
|
|
|
'_Password' => 'abc',
|
|
|
|
'_ConfirmPassword' => 'abc',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$this->logOut();
|
|
|
|
$result = $field->validate($validator);
|
|
|
|
|
|
|
|
$this->assertFalse($result, 'Validate method should return its result');
|
|
|
|
$this->assertFalse($validator->getResult()->isValid());
|
|
|
|
$this->assertContains(
|
|
|
|
'You must be logged in to change your password',
|
|
|
|
$validator->getResult()->serialize()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @useDatabase true
|
|
|
|
*/
|
|
|
|
public function testValidateCorrectPassword()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Testing');
|
|
|
|
$field->setRequireExistingPassword(true);
|
|
|
|
$field->setValue([
|
|
|
|
'_CurrentPassword' => 'foo-not-going-to-be-the-correct-password',
|
|
|
|
'_Password' => 'abc',
|
|
|
|
'_ConfirmPassword' => 'abc',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$result = $field->validate($validator);
|
|
|
|
|
|
|
|
$this->assertFalse($result, 'Validate method should return its result');
|
|
|
|
$this->assertFalse($validator->getResult()->isValid());
|
|
|
|
$this->assertContains(
|
|
|
|
'The current password you have entered is not correct',
|
|
|
|
$validator->getResult()->serialize()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-20 16:34:36 +02:00
|
|
|
public function testTitle()
|
|
|
|
{
|
|
|
|
$this->assertNull(ConfirmedPasswordField::create('Test')->Title(), 'Should not have it\'s own title');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetTitlePropagatesToPasswordField()
|
|
|
|
{
|
|
|
|
/** @var ConfirmedPasswordField $field */
|
|
|
|
$field = ConfirmedPasswordField::create('Test')
|
|
|
|
->setTitle('My password');
|
|
|
|
|
|
|
|
$this->assertSame('My password', $field->getPasswordField()->Title());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetRightTitlePropagatesToChildren()
|
|
|
|
{
|
2018-10-20 17:18:31 +02:00
|
|
|
$field = new ConfirmedPasswordField('Test');
|
2018-10-20 16:34:36 +02:00
|
|
|
|
|
|
|
$this->assertCount(2, $field->getChildren());
|
|
|
|
foreach ($field->getChildren() as $child) {
|
|
|
|
$this->assertEmpty($child->RightTitle());
|
|
|
|
}
|
|
|
|
|
|
|
|
$field->setRightTitle('Please confirm');
|
|
|
|
foreach ($field->getChildren() as $child) {
|
|
|
|
$this->assertSame('Please confirm', $child->RightTitle());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetChildrenTitles()
|
|
|
|
{
|
2018-10-20 17:18:31 +02:00
|
|
|
$field = new ConfirmedPasswordField('Test');
|
2018-10-20 16:34:36 +02:00
|
|
|
$field->setRequireExistingPassword(true);
|
|
|
|
$field->setChildrenTitles([
|
|
|
|
'Current Password',
|
|
|
|
'Password',
|
|
|
|
'Confirm Password',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame('Current Password', $field->getChildren()->shift()->Title());
|
|
|
|
$this->assertSame('Password', $field->getChildren()->shift()->Title());
|
|
|
|
$this->assertSame('Confirm Password', $field->getChildren()->shift()->Title());
|
|
|
|
}
|
2018-10-20 17:18:31 +02:00
|
|
|
|
|
|
|
public function testPerformReadonlyTransformation()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Change it');
|
|
|
|
$result = $field->performReadonlyTransformation();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(ReadonlyField::class, $result);
|
|
|
|
$this->assertSame('Change it', $result->Title());
|
|
|
|
$this->assertContains('***', $result->Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPerformDisabledTransformation()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Change it');
|
|
|
|
$result = $field->performDisabledTransformation();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(ReadonlyField::class, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetRequireExistingPasswordOnlyRunsOnce()
|
|
|
|
{
|
|
|
|
$field = new ConfirmedPasswordField('Test', 'Change it');
|
|
|
|
|
|
|
|
$this->assertCount(2, $field->getChildren());
|
|
|
|
|
|
|
|
$field->setRequireExistingPassword(true);
|
|
|
|
$this->assertCount(3, $field->getChildren(), 'Current password field was not pushed');
|
|
|
|
|
|
|
|
$field->setRequireExistingPassword(true);
|
|
|
|
$this->assertCount(3, $field->getChildren(), 'Current password field should not be pushed again');
|
|
|
|
|
|
|
|
$field->setRequireExistingPassword(false);
|
|
|
|
$this->assertCount(2, $field->getChildren(), 'Current password field should not be removed');
|
|
|
|
}
|
2009-06-27 06:00:10 +02:00
|
|
|
}
|