Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Preferences Extender and Tests #2463

Merged
merged 4 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Event/ConfigureUserPreferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use Flarum\User\User;

/**
* @deprecated beta 15, removed beta 16
*/
class ConfigureUserPreferences
{
public function add($key, callable $transformer = null, $default = null)
Expand Down
20 changes: 20 additions & 0 deletions src/Extend/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
namespace Flarum\Extend;

use Flarum\Extension\Extension;
use Flarum\User\User as FlarumUser;
use Illuminate\Contracts\Container\Container;

class User implements ExtenderInterface
{
private $displayNameDrivers = [];
private $groupProcessors = [];
private $preferences = [];

/**
* Add a display name driver.
Expand Down Expand Up @@ -51,6 +53,20 @@ public function permissionGroups($callback)
return $this;
}

/**
* Register a new user preference.
*
* @param string $key
* @param callable $transformer
* @param $default
*/
public function registerPreference(string $key, callable $transformer = null, $default = null)
{
$this->preferences[$key] = compact('transformer', 'default');

return $this;
}

public function extend(Container $container, Extension $extension = null)
{
$container->extend('flarum.user.display_name.supported_drivers', function ($existingDrivers) {
Expand All @@ -60,5 +76,9 @@ public function extend(Container $container, Extension $extension = null)
$container->extend('flarum.user.group_processors', function ($existingRelations) {
return array_merge($existingRelations, $this->groupProcessors);
});

foreach ($this->preferences as $key => $preference) {
FlarumUser::addPreference($key, $preference['transformer'], $preference['default']);
}
}
}
3 changes: 3 additions & 0 deletions src/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public static function boot()
Notification::whereSubject($user)->delete();
});

/**
* @deprecated beta 15, remove beta 16
*/
static::$dispatcher->dispatch(
new ConfigureUserPreferences
);
Expand Down
15 changes: 3 additions & 12 deletions src/User/UserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Flarum\User;

use Flarum\Event\ConfigureUserPreferences;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\ContainerUtil;
use Flarum\Settings\SettingsRepositoryInterface;
Expand Down Expand Up @@ -94,16 +93,8 @@ public function boot()
$events->subscribe(UserMetadataUpdater::class);
$events->subscribe(UserPolicy::class);

$events->listen(ConfigureUserPreferences::class, [$this, 'configureUserPreferences']);
}

/**
* @param ConfigureUserPreferences $event
*/
public function configureUserPreferences(ConfigureUserPreferences $event)
{
$event->add('discloseOnline', 'boolval', true);
$event->add('indexProfile', 'boolval', true);
$event->add('locale');
User::addPreference('discloseOnline', 'boolval', true);
User::addPreference('indexProfile', 'boolval', true);
User::addPreference('locale');
}
}
57 changes: 57 additions & 0 deletions tests/integration/extenders/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Tests\integration\TestCase;
use Flarum\User\DisplayName\DriverInterface;
use Flarum\User\User;
use Illuminate\Support\Arr;

class UserTest extends TestCase
{
Expand All @@ -26,12 +27,23 @@ protected function prepDb()
$this->adminUser(),
$this->normalUser(),
],
'group_permission' => [
['permission' => 'viewUserList', 'group_id' => 3],
],
'settings' => [
['key' => 'display_name_driver', 'value' => 'custom'],
],
]);
}

protected function registerTestPreference()
{
$this->extend(
(new Extend\User())
->registerPreference('test', 'boolval', true)
);
}

/**
* @test
*/
Expand Down Expand Up @@ -101,6 +113,51 @@ public function processor_can_be_invokable_class()

$this->assertNotContains('viewUserList', $user->getPermissions());
}

/**
* @test
*/
public function can_add_user_preference()
{
$this->registerTestPreference();
$this->prepDb();

/** @var User $user */
$user = User::find(2);
$this->assertEquals(true, Arr::get($user->preferences, 'test'));
}

/**
* @test
*/
public function can_store_user_preference()
{
$this->registerTestPreference();
$this->prepDb();

/** @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();
$this->prepDb();

/** @var User $user */
$user = User::find(2);

$user->setPreference('test', []);

$this->assertEquals(false, $user->getPreference('test'));
}
}

class CustomDisplayNameDriver implements DriverInterface
Expand Down