2020-05-20 06:45:56 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
|
|
|
* For detailed copyright and license information, please view the
|
|
|
|
* LICENSE file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Flarum\Tests\integration\extenders;
|
|
|
|
|
|
|
|
use Flarum\Extend;
|
2021-03-08 05:32:41 +08:00
|
|
|
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
|
|
|
|
use Flarum\Testing\integration\TestCase;
|
2020-05-20 06:45:56 +08:00
|
|
|
use Flarum\User\DisplayName\DriverInterface;
|
|
|
|
use Flarum\User\User;
|
2020-12-05 04:45:08 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2020-05-20 06:45:56 +08:00
|
|
|
|
|
|
|
class UserTest extends TestCase
|
|
|
|
{
|
|
|
|
use RetrievesAuthorizedUsers;
|
|
|
|
|
2021-01-07 11:34:32 +08:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function setUp(): void
|
2020-05-20 06:45:56 +08:00
|
|
|
{
|
2021-01-07 11:34:32 +08:00
|
|
|
parent::setUp();
|
|
|
|
|
2020-05-20 06:45:56 +08:00
|
|
|
$this->prepareDatabase([
|
|
|
|
'users' => [
|
2020-07-17 18:18:35 +08:00
|
|
|
$this->normalUser(),
|
2020-12-08 09:02:46 +08:00
|
|
|
]
|
2020-05-20 06:45:56 +08:00
|
|
|
]);
|
|
|
|
|
2021-05-03 13:35:46 +08:00
|
|
|
$this->setting('display_name_driver', 'custom');
|
2021-01-08 03:10:53 +08:00
|
|
|
}
|
|
|
|
|
2020-12-05 04:45:08 +08:00
|
|
|
protected function registerTestPreference()
|
|
|
|
{
|
|
|
|
$this->extend(
|
|
|
|
(new Extend\User())
|
|
|
|
->registerPreference('test', 'boolval', true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-20 06:45:56 +08:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function username_display_name_driver_used_by_default()
|
|
|
|
{
|
2021-01-07 11:34:32 +08:00
|
|
|
$this->app();
|
2020-05-20 06:45:56 +08:00
|
|
|
|
|
|
|
$user = User::find(1);
|
|
|
|
|
|
|
|
$this->assertEquals('admin', $user->displayName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function can_use_custom_display_name_driver()
|
|
|
|
{
|
|
|
|
$this->extend(
|
|
|
|
(new Extend\User)
|
|
|
|
->displayNameDriver('custom', CustomDisplayNameDriver::class)
|
|
|
|
);
|
|
|
|
|
2021-01-07 11:34:32 +08:00
|
|
|
$this->app();
|
2020-05-20 06:45:56 +08:00
|
|
|
|
|
|
|
$user = User::find(1);
|
|
|
|
|
|
|
|
$this->assertEquals('admin@machine.local$$$suffix', $user->displayName);
|
|
|
|
}
|
2020-07-17 18:18:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function user_has_permissions_for_expected_groups_if_no_processors_added()
|
|
|
|
{
|
2021-01-07 11:34:32 +08:00
|
|
|
$this->app();
|
|
|
|
|
2020-07-17 18:18:35 +08:00
|
|
|
$user = User::find(2);
|
|
|
|
|
2021-05-12 03:15:27 +08:00
|
|
|
$this->assertContains('searchUsers', $user->getPermissions());
|
2020-07-17 18:18:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function processor_can_restrict_user_groups()
|
|
|
|
{
|
|
|
|
$this->extend((new Extend\User)->permissionGroups(function (User $user, array $groupIds) {
|
|
|
|
return array_filter($groupIds, function ($id) {
|
|
|
|
return $id != 3;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2021-01-07 11:34:32 +08:00
|
|
|
$this->app();
|
|
|
|
|
2020-07-17 18:18:35 +08:00
|
|
|
$user = User::find(2);
|
|
|
|
|
2021-05-12 03:15:27 +08:00
|
|
|
$this->assertNotContains('searchUsers', $user->getPermissions());
|
2020-07-17 18:18:35 +08:00
|
|
|
}
|
2020-11-09 10:36:38 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function processor_can_be_invokable_class()
|
|
|
|
{
|
|
|
|
$this->extend((new Extend\User)->permissionGroups(CustomGroupProcessorClass::class));
|
|
|
|
|
2021-01-07 11:34:32 +08:00
|
|
|
$this->app();
|
|
|
|
|
2020-11-09 10:36:38 +08:00
|
|
|
$user = User::find(2);
|
|
|
|
|
2021-05-12 03:15:27 +08:00
|
|
|
$this->assertNotContains('searchUsers', $user->getPermissions());
|
2020-11-09 10:36:38 +08:00
|
|
|
}
|
2020-12-05 04:45:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function can_add_user_preference()
|
|
|
|
{
|
|
|
|
$this->registerTestPreference();
|
2021-01-07 11:34:32 +08:00
|
|
|
|
|
|
|
$this->app();
|
2020-12-05 04:45:08 +08:00
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::find(2);
|
|
|
|
$this->assertEquals(true, Arr::get($user->preferences, 'test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function can_store_user_preference()
|
|
|
|
{
|
|
|
|
$this->registerTestPreference();
|
2021-01-07 11:34:32 +08:00
|
|
|
|
|
|
|
$this->app();
|
2020-12-05 04:45:08 +08:00
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::find(2);
|
|
|
|
|
|
|
|
$user->setPreference('test', false);
|
|
|
|
|
|
|
|
$this->assertEquals(false, $user->getPreference('test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function storing_user_preference_modified_by_transformer()
|
|
|
|
{
|
|
|
|
$this->registerTestPreference();
|
2021-01-07 11:34:32 +08:00
|
|
|
|
|
|
|
$this->app();
|
2020-12-05 04:45:08 +08:00
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::find(2);
|
|
|
|
|
|
|
|
$user->setPreference('test', []);
|
|
|
|
|
|
|
|
$this->assertEquals(false, $user->getPreference('test'));
|
|
|
|
}
|
2020-05-20 06:45:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class CustomDisplayNameDriver implements DriverInterface
|
|
|
|
{
|
|
|
|
public function displayName(User $user): string
|
|
|
|
{
|
|
|
|
return $user->email.'$$$suffix';
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 10:36:38 +08:00
|
|
|
|
|
|
|
class CustomGroupProcessorClass
|
|
|
|
{
|
|
|
|
public function __invoke(User $user, array $groupIds)
|
|
|
|
{
|
|
|
|
return array_filter($groupIds, function ($id) {
|
|
|
|
return $id != 3;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|