szurubooru/tests/Tests/JobTests/EditUserNameJobTest.php
Marcin Kurczewski e59b7e8b7b Refactored privilege system a bit
- Jobs specify main privilege and sub privileges separately
  Rationale: increase maintenance, restrict what can be done runtime
- Renamed ChangeUser* to EditUser* (consistency with EditPost*)
- Simplified enum names and configuration reading
- IJob interface members must be explicitly implemented
  Rationale: reduce chances of forgetting something, or typos in
  inherited method names
- Invalid privileges names in configuration yield exceptions
2014-05-18 21:59:56 +02:00

101 lines
2.4 KiB
PHP

<?php
class EditUserNameJobTest extends AbstractTest
{
public function testEditing()
{
$this->grantAccess('editUserName');
$user = $this->userMocker->mockSingle();
$newName = uniqid();
$this->assert->areNotEqual($newName, $user->getName());
$user = $this->assert->doesNotThrow(function() use ($user, $newName)
{
return Api::run(
new EditUserNameJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_USER_NAME => $newName,
]);
});
$this->assert->areEqual($newName, $user->getName());
}
public function testTooShortName()
{
$this->grantAccess('editUserName');
$user = $this->userMocker->mockSingle();
$newName = str_repeat('a', Core::getConfig()->registration->userNameMinLength - 1);
$this->assert->throws(function() use ($user, $newName)
{
Api::run(
new EditUserNameJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_USER_NAME => $newName,
]);
}, 'user name must have at least');
}
public function testTooLongName()
{
$this->grantAccess('editUserName');
$user = $this->userMocker->mockSingle();
$newName = str_repeat('a', Core::getConfig()->registration->userNameMaxLength + 1);
$this->assert->throws(function() use ($user, $newName)
{
Api::run(
new EditUserNameJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_USER_NAME => $newName,
]);
}, 'user name must have at most');
}
public function testInvalidName()
{
$this->grantAccess('editUserName');
$user = $this->userMocker->mockSingle();
$newName = 'ble/ble';
$this->assert->throws(function() use ($user, $newName)
{
Api::run(
new EditUserNameJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_USER_NAME => $newName,
]);
}, 'user name contains invalid characters');
}
public function testChangingToExistingDenial()
{
$this->grantAccess('editUserName');
list ($user, $otherUser)
= $this->userMocker->mockMultiple(2);
$newName = $otherUser->getName();
$this->assert->areNotEqual($newName, $user->getName());
$this->assert->throws(function() use ($user, $newName)
{
Api::run(
new EditUserNameJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_USER_NAME => $newName,
]);
}, 'User with this name is already registered');
$this->assert->areNotEqual($newName, $user->getName());
}
}