Finished user validation; increased readability
This commit is contained in:
parent
ad7cdcb7fe
commit
9882e84aa6
7 changed files with 105 additions and 31 deletions
|
@ -15,7 +15,7 @@ class EditUserEmailJob extends AbstractUserJob
|
||||||
throw new SimpleException('E-mail address is required - you will be sent confirmation e-mail.');
|
throw new SimpleException('E-mail address is required - you will be sent confirmation e-mail.');
|
||||||
|
|
||||||
$user = $this->user;
|
$user = $this->user;
|
||||||
$newEmail = UserModel::validateEmail($this->getArgument(self::NEW_EMAIL));
|
$newEmail = $this->getArgument(self::NEW_EMAIL);
|
||||||
|
|
||||||
$oldEmail = $user->getConfirmedEmail();
|
$oldEmail = $user->getConfirmedEmail();
|
||||||
if ($oldEmail == $newEmail)
|
if ($oldEmail == $newEmail)
|
||||||
|
|
|
@ -22,13 +22,12 @@ final class CommentEntity extends AbstractEntity implements IValidatable
|
||||||
|
|
||||||
public function validate()
|
public function validate()
|
||||||
{
|
{
|
||||||
$text = trim($this->getText());
|
|
||||||
$config = getConfig();
|
$config = getConfig();
|
||||||
|
|
||||||
if (strlen($text) < $config->comments->minLength)
|
if (strlen($this->getText()) < $config->comments->minLength)
|
||||||
throw new SimpleException('Comment must have at least %d characters', $config->comments->minLength);
|
throw new SimpleException('Comment must have at least %d characters', $config->comments->minLength);
|
||||||
|
|
||||||
if (strlen($text) > $config->comments->maxLength)
|
if (strlen($this->getText()) > $config->comments->maxLength)
|
||||||
throw new SimpleException('Comment must have at most %d characters', $config->comments->maxLength);
|
throw new SimpleException('Comment must have at most %d characters', $config->comments->maxLength);
|
||||||
|
|
||||||
if (!$this->getPostId())
|
if (!$this->getPostId())
|
||||||
|
@ -36,8 +35,6 @@ final class CommentEntity extends AbstractEntity implements IValidatable
|
||||||
|
|
||||||
if (!$this->getCreationTime())
|
if (!$this->getCreationTime())
|
||||||
throw new SimpleException('Trying to save comment that has no creation date specified');
|
throw new SimpleException('Trying to save comment that has no creation date specified');
|
||||||
|
|
||||||
$this->setText($text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getText()
|
public function getText()
|
||||||
|
@ -52,7 +49,7 @@ final class CommentEntity extends AbstractEntity implements IValidatable
|
||||||
|
|
||||||
public function setText($text)
|
public function setText($text)
|
||||||
{
|
{
|
||||||
$this->text = $text;
|
$this->text = $text === null ? null : trim($text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPost()
|
public function getPost()
|
||||||
|
|
|
@ -336,7 +336,7 @@ final class PostEntity extends AbstractEntity implements IValidatable
|
||||||
|
|
||||||
public function setSource($source)
|
public function setSource($source)
|
||||||
{
|
{
|
||||||
$this->source = trim($source);
|
$this->source = $source === null ? null : trim($source);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getThumbCustomPath($width = null, $height = null)
|
public function getThumbCustomPath($width = null, $height = null)
|
||||||
|
|
|
@ -41,7 +41,7 @@ final class TagEntity extends AbstractEntity implements IValidatable
|
||||||
|
|
||||||
public function setName($name)
|
public function setName($name)
|
||||||
{
|
{
|
||||||
$this->name = trim($name);
|
$this->name = $name === null ? null : trim($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getName()
|
public function getName()
|
||||||
|
|
|
@ -45,8 +45,8 @@ final class UserEntity extends AbstractEntity implements IValidatable
|
||||||
{
|
{
|
||||||
$this->validateUserName();
|
$this->validateUserName();
|
||||||
$this->validatePassword();
|
$this->validatePassword();
|
||||||
|
$this->validateAccessRank();
|
||||||
//todo: validate e-mails
|
$this->validateEmails();
|
||||||
|
|
||||||
if (empty($this->getAccessRank()))
|
if (empty($this->getAccessRank()))
|
||||||
throw new Exception('No access rank detected');
|
throw new Exception('No access rank detected');
|
||||||
|
@ -114,16 +114,25 @@ final class UserEntity extends AbstractEntity implements IValidatable
|
||||||
throw new SimpleException('Password contains invalid characters');
|
throw new SimpleException('Password contains invalid characters');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function validateAccessRank(AccessRank $accessRank)
|
public function validateAccessRank()
|
||||||
{
|
{
|
||||||
$accessRank->validate();
|
$this->accessRank->validate();
|
||||||
|
|
||||||
if ($accessRank->toInteger() == AccessRank::Nobody)
|
if ($this->accessRank->toInteger() == AccessRank::Nobody)
|
||||||
throw new Exception('Cannot set special access rank "%s"', $accessRank->toString());
|
throw new Exception('Cannot set special access rank "%s"', $this->accessRank->toString());
|
||||||
|
|
||||||
return $accessRank;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function validateEmails()
|
||||||
|
{
|
||||||
|
$this->validateEmail($this->getUnconfirmedEmail());
|
||||||
|
$this->validateEmail($this->getConfirmedEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validateEmail($email)
|
||||||
|
{
|
||||||
|
if (!empty($email) and !TextHelper::isValidEmail($email))
|
||||||
|
throw new SimpleException('E-mail address appears to be invalid');
|
||||||
|
}
|
||||||
|
|
||||||
public function isBanned()
|
public function isBanned()
|
||||||
{
|
{
|
||||||
|
@ -147,7 +156,7 @@ final class UserEntity extends AbstractEntity implements IValidatable
|
||||||
|
|
||||||
public function setName($name)
|
public function setName($name)
|
||||||
{
|
{
|
||||||
$this->name = trim($name);
|
$this->name = $name === null ? null : trim($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getJoinTime()
|
public function getJoinTime()
|
||||||
|
@ -177,7 +186,7 @@ final class UserEntity extends AbstractEntity implements IValidatable
|
||||||
|
|
||||||
public function setUnconfirmedEmail($email)
|
public function setUnconfirmedEmail($email)
|
||||||
{
|
{
|
||||||
$this->emailUnconfirmed = $email;
|
$this->emailUnconfirmed = $email === null ? null : trim($email);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConfirmedEmail()
|
public function getConfirmedEmail()
|
||||||
|
@ -187,7 +196,7 @@ final class UserEntity extends AbstractEntity implements IValidatable
|
||||||
|
|
||||||
public function setConfirmedEmail($email)
|
public function setConfirmedEmail($email)
|
||||||
{
|
{
|
||||||
$this->emailConfirmed = $email;
|
$this->emailConfirmed = $email === null ? null : trim($email);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isStaffConfirmed()
|
public function isStaffConfirmed()
|
||||||
|
|
|
@ -187,17 +187,6 @@ final class UserModel extends AbstractCrudModel
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function validateEmail($email)
|
|
||||||
{
|
|
||||||
$email = trim($email);
|
|
||||||
|
|
||||||
if (!empty($email) and !TextHelper::isValidEmail($email))
|
|
||||||
throw new SimpleException('E-mail address appears to be invalid');
|
|
||||||
|
|
||||||
return $email;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static function getAnonymousName()
|
public static function getAnonymousName()
|
||||||
{
|
{
|
||||||
return '[Anonymous user]';
|
return '[Anonymous user]';
|
||||||
|
|
79
tests/JobTests/EditUserEmailJobTest.php
Normal file
79
tests/JobTests/EditUserEmailJobTest.php
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
<?php
|
||||||
|
class EditUserEmailJobTest extends AbstractTest
|
||||||
|
{
|
||||||
|
public function testNoConfirmation()
|
||||||
|
{
|
||||||
|
getConfig()->registration->needEmailForRegistering = false;
|
||||||
|
Mailer::mockSending();
|
||||||
|
$this->assert->areEqual(0, Mailer::getMailCounter());
|
||||||
|
|
||||||
|
getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous';
|
||||||
|
$this->grantAccess('changeUserEmail');
|
||||||
|
|
||||||
|
$user = $this->mockUser();
|
||||||
|
|
||||||
|
$user = $this->assert->doesNotThrow(function() use ($user)
|
||||||
|
{
|
||||||
|
return Api::run(
|
||||||
|
new EditUserEmailJob(),
|
||||||
|
[
|
||||||
|
EditUserEmailJob::USER_NAME => $user->getName(),
|
||||||
|
EditUserEmailJob::NEW_EMAIL => 'xena@other-side.gr',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assert->areEqual(null, $user->getUnconfirmedEmail());
|
||||||
|
$this->assert->areEqual('xena@other-side.gr', $user->getConfirmedEmail());
|
||||||
|
|
||||||
|
$this->assert->areEqual(0, Mailer::getMailCounter());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConfirmation()
|
||||||
|
{
|
||||||
|
getConfig()->registration->needEmailForRegistering = false;
|
||||||
|
Mailer::mockSending();
|
||||||
|
$this->assert->areEqual(0, Mailer::getMailCounter());
|
||||||
|
|
||||||
|
getConfig()->privileges->changeUserEmailNoConfirm = 'admin';
|
||||||
|
$this->grantAccess('changeUserEmail');
|
||||||
|
|
||||||
|
$user = $this->mockUser();
|
||||||
|
|
||||||
|
$user = $this->assert->doesNotThrow(function() use ($user)
|
||||||
|
{
|
||||||
|
return Api::run(
|
||||||
|
new EditUserEmailJob(),
|
||||||
|
[
|
||||||
|
EditUserEmailJob::USER_NAME => $user->getName(),
|
||||||
|
EditUserEmailJob::NEW_EMAIL => 'xena@other-side.gr',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assert->areEqual('xena@other-side.gr', $user->getUnconfirmedEmail());
|
||||||
|
$this->assert->areEqual(null, $user->getConfirmedEmail());
|
||||||
|
|
||||||
|
$this->assert->areEqual(1, Mailer::getMailCounter());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvalidEmail()
|
||||||
|
{
|
||||||
|
getConfig()->registration->needEmailForRegistering = false;
|
||||||
|
Mailer::mockSending();
|
||||||
|
|
||||||
|
getConfig()->privileges->changeUserEmailNoConfirm = 'nobody';
|
||||||
|
$this->grantAccess('changeUserEmail');
|
||||||
|
|
||||||
|
$user = $this->mockUser();
|
||||||
|
|
||||||
|
$this->assert->throws(function() use ($user)
|
||||||
|
{
|
||||||
|
Api::run(
|
||||||
|
new EditUserEmailJob(),
|
||||||
|
[
|
||||||
|
EditUserEmailJob::USER_NAME => $user->getName(),
|
||||||
|
EditUserEmailJob::NEW_EMAIL => 'hrmfbpdvpds@brtedf',
|
||||||
|
]);
|
||||||
|
}, 'E-mail address appears to be invalid');
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue