2014-09-02 09:09:07 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests;
|
|
|
|
|
|
|
|
final class ValidatorTest extends \Szurubooru\Tests\AbstractTestCase
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
private $configMock;
|
2014-09-02 09:09:07 +02:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock = $this->mockConfig();
|
2014-09-02 09:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMinLengthName()
|
|
|
|
{
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$this->setExpectedException(\Exception::class, 'Object must have at least 50 character(s)');
|
|
|
|
$validator->validateMinLength('too short', 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMaxLengthName()
|
|
|
|
{
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$this->setExpectedException(\Exception::class, 'Object must have at most 1 character(s)');
|
|
|
|
$validator->validateMaxLength('too long', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidLengthName()
|
|
|
|
{
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$this->assertNull($validator->validateLength('fitting', 1, 50));
|
|
|
|
$this->assertNull($validator->validateMaxLength('fitting', 50));
|
|
|
|
$this->assertNull($validator->validateMinLength('fitting', 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmptyUserName()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock->set('users/minUserNameLength', 0);
|
|
|
|
$this->configMock->set('users/maxUserNameLength', 1);
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'User name cannot be empty');
|
|
|
|
$userName = '';
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$validator->validateUserName($userName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooShortUserName()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock->set('users/minUserNameLength', 30);
|
|
|
|
$this->configMock->set('users/maxUserNameLength', 50);
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'User name must have at least 30 character(s)');
|
|
|
|
$userName = 'godzilla';
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$validator->validateUserName($userName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooLongUserName()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock->set('users/minUserNameLength', 30);
|
|
|
|
$this->configMock->set('users/maxUserNameLength', 50);
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'User name must have at most 50 character(s)');
|
|
|
|
$userName = 'godzilla' . str_repeat('a', 50);
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$validator->validateUserName($userName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserNameWithInvalidCharacters()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock->set('users/minUserNameLength', 0);
|
|
|
|
$this->configMock->set('users/maxUserNameLength', 100);
|
2014-09-02 09:09:07 +02:00
|
|
|
$userName = '..:xXx:godzilla:xXx:..';
|
|
|
|
$this->setExpectedException(\Exception::class, 'User name may contain only');
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$validator->validateUserName($userName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmailWithoutAt()
|
|
|
|
{
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$this->setExpectedException(\DomainException::class);
|
|
|
|
$validator->validateEmail('ghost');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmailWithoutDotInDomain()
|
|
|
|
{
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$this->setExpectedException(\DomainException::class);
|
|
|
|
$validator->validateEmail('ghost@cemetery');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidEmail()
|
|
|
|
{
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$this->assertNull($validator->validateEmail('ghost@cemetery.consulting'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmptyPassword()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock->set('security/minPasswordLength', 0);
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'Password cannot be empty');
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$validator->validatePassword('');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooShortPassword()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock->set('security/minPasswordLength', 10000);
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'Password must have at least 10000 character(s)');
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$validator->validatePassword('password123');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonAsciiPassword()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock->set('security/minPasswordLength', 0);
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'Password may contain only');
|
|
|
|
$validator = $this->getValidator();
|
|
|
|
$validator->validatePassword('良いパスワード');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidPassword()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock->set('security/minPasswordLength', 0);
|
2014-09-02 09:09:07 +02:00
|
|
|
$validator = $this->getValidator();
|
|
|
|
$this->assertNull($validator->validatePassword('password'));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getValidator()
|
|
|
|
{
|
2014-09-05 20:08:54 +02:00
|
|
|
return new \Szurubooru\Validator($this->configMock);
|
2014-09-02 09:09:07 +02:00
|
|
|
}
|
|
|
|
}
|