Skip to content

Commit

Permalink
User Preferences Extender and Tests (#2463)
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 authored Dec 4, 2020
1 parent 641619e commit eed4078
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 14 deletions.
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::registerPreference($key, $preference['transformer'], $preference['default']);
}
}
}
2 changes: 1 addition & 1 deletion src/Notification/Driver/AlertNotificationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function send(BlueprintInterface $blueprint, array $users): void
*/
public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
{
User::addPreference(
User::registerPreference(
User::getNotificationPreferenceKey($blueprintClass::getType(), 'alert'),
'boolval',
in_array('alert', $driversEnabledByDefault)
Expand Down
2 changes: 1 addition & 1 deletion src/Notification/Driver/EmailNotificationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function mailNotifications(MailableInterface $blueprint, array $recipi
public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
{
if ((new ReflectionClass($blueprintClass))->implementsInterface(MailableInterface::class)) {
User::addPreference(
User::registerPreference(
User::getNotificationPreferenceKey($blueprintClass::getType(), 'email'),
'boolval',
in_array('email', $driversEnabledByDefault)
Expand Down
17 changes: 17 additions & 0 deletions src/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public static function boot()
Notification::whereSubject($user)->delete();
});

/**
* @deprecated beta 15, remove beta 16
*/
static::$dispatcher->dispatch(
new ConfigureUserPreferences
);
Expand Down Expand Up @@ -801,13 +804,27 @@ public static function setHasher(Hasher $hasher)
}

/**
* @deprecated beta 15, remove beta 16. Use `registerPreference` instead.
*
* Register a preference with a transformer and a default value.
*
* @param string $key
* @param callable $transformer
* @param mixed $default
*/
public static function addPreference($key, callable $transformer = null, $default = null)
{
return static::registerPreference($key, $transformer, $default);
}

/**
* Register a preference with a transformer and a default value.
*
* @param string $key
* @param callable $transformer
* @param mixed $default
*/
public static function registerPreference($key, callable $transformer = null, $default = null)
{
static::$preferences[$key] = compact('transformer', 'default');
}
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::registerPreference('discloseOnline', 'boolval', true);
User::registerPreference('indexProfile', 'boolval', true);
User::registerPreference('locale');
}
}
54 changes: 54 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 @@ -35,6 +36,14 @@ protected function prepDb()
]);
}

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

/**
* @test
*/
Expand Down Expand Up @@ -104,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

0 comments on commit eed4078

Please sign in to comment.