szurubooru/tests/Tests/JobTests/EditUserSettingsJobTest.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

108 lines
3.4 KiB
PHP

<?php
class EditUserSettingsJobTest extends AbstractTest
{
public function testEditing()
{
$this->grantAccess('editUserSettings');
$user = $this->userMocker->mockSingle();
$expectedSafety = (new PostSafety(PostSafety::Sketchy))->toFlag();
$user = $this->assert->doesNotThrow(function() use ($user, $expectedSafety)
{
return Api::run(
new EditUserSettingsJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_SETTINGS =>
[
UserSettings::SETTING_SAFETY => $expectedSafety,
UserSettings::SETTING_ENDLESS_SCROLLING => true,
UserSettings::SETTING_POST_TAG_TITLES => true,
UserSettings::SETTING_HIDE_DISLIKED_POSTS => true,
]
]);
});
$settings = $user->getSettings();
$this->assert->areEqual($expectedSafety, $settings->get(UserSettings::SETTING_SAFETY));
$this->assert->isTrue($settings->get(UserSettings::SETTING_ENDLESS_SCROLLING));
$this->assert->isTrue($settings->get(UserSettings::SETTING_POST_TAG_TITLES));
$this->assert->isTrue($settings->get(UserSettings::SETTING_HIDE_DISLIKED_POSTS));
}
public function testSettingAdditional()
{
$this->grantAccess('editUserSettings');
$user = $this->userMocker->mockSingle();
$user = $this->assert->doesNotThrow(function() use ($user)
{
return Api::run(
new EditUserSettingsJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_SETTINGS =>
[
'additional' => 'rubbish',
]
]);
});
$settings = $user->getSettings();
$expectedSafety = (new PostSafety(PostSafety::Safe))->toFlag();
$this->assert->areEqual($expectedSafety, $settings->get(UserSettings::SETTING_SAFETY));
$this->assert->isTrue($settings->get(UserSettings::SETTING_ENDLESS_SCROLLING));
$this->assert->isFalse($settings->get(UserSettings::SETTING_POST_TAG_TITLES));
$this->assert->isFalse($settings->get(UserSettings::SETTING_HIDE_DISLIKED_POSTS));
$this->assert->areEqual('rubbish', $settings->get('additional'));
}
public function testSettingBadValues()
{
$this->grantAccess('editUserSettings');
$user = $this->userMocker->mockSingle();
$user = $this->assert->doesNotThrow(function() use ($user)
{
return Api::run(
new EditUserSettingsJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_SETTINGS =>
[
UserSettings::SETTING_SAFETY => 'rubbish',
UserSettings::SETTING_ENDLESS_SCROLLING => 'rubbish',
UserSettings::SETTING_POST_TAG_TITLES => 'rubbish',
UserSettings::SETTING_HIDE_DISLIKED_POSTS => 'rubbish',
]
]);
});
$settings = $user->getSettings();
$expectedSafety = (new PostSafety(PostSafety::Safe))->toFlag();
$this->assert->areEqual($expectedSafety, $settings->get(UserSettings::SETTING_SAFETY));
$this->assert->isTrue($settings->get(UserSettings::SETTING_ENDLESS_SCROLLING));
$this->assert->isFalse($settings->get(UserSettings::SETTING_POST_TAG_TITLES));
$this->assert->isFalse($settings->get(UserSettings::SETTING_HIDE_DISLIKED_POSTS));
}
public function testSettingTooLongData()
{
$this->grantAccess('editUserSettings');
$user = $this->userMocker->mockSingle();
$this->assert->throws(function() use ($user)
{
return Api::run(
new EditUserSettingsJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_SETTINGS =>
[
'additional' => str_repeat('rubbish', 50),
]]);
}, 'Too much data');
}
}