Tests: purge settings cache

Some tests need to change settings, but since MemoryCacheSettingsRepository caches settings in-memory, those changes aren't reflected. The new `purgeSettingsCache` removes it from the container, eliminating that cache.

For UserTest, we also need to regenerate the display name driver, since that's set statically on boot, before we'll get a change to clear the settings cache.
This commit is contained in:
Alexander Skvortsov 2021-01-07 14:10:53 -05:00
parent eeac5ffce9
commit 7c0d98c63d
4 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,25 @@
<?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;
use Flarum\Settings\SettingsRepositoryInterface;
trait UsesSettings
{
/**
* Removes the settings respository instance from the IoC container.
*
* This allows test cases that add/modify settings to refresh the in-memory settings cache.
*/
protected function purgeSettingsCache()
{
$this->app()->getContainer()->forgetInstance(SettingsRepositoryInterface::class);
}
}

View File

@ -15,11 +15,13 @@ use Flarum\Http\SlugDriverInterface;
use Flarum\Http\SlugManager;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\Tests\integration\UsesSettings;
use Flarum\User\User;
class ModelUrlTest extends TestCase
{
use RetrievesAuthorizedUsers;
use UsesSettings;
/**
* @inheritDoc
@ -44,6 +46,8 @@ class ModelUrlTest extends TestCase
*/
public function uses_default_driver_by_default()
{
$this->purgeSettingsCache();
$slugManager = $this->app()->getContainer()->make(SlugManager::class);
$testUser = User::find(1);
@ -59,6 +63,8 @@ class ModelUrlTest extends TestCase
{
$this->extend((new Extend\ModelUrl(User::class))->addSlugDriver('testDriver', TestSlugDriver::class));
$this->purgeSettingsCache();
$slugManager = $this->app()->getContainer()->make(SlugManager::class);
$testUser = User::find(1);

View File

@ -10,12 +10,14 @@
namespace Flarum\Tests\integration\extenders;
use Flarum\Extend;
use Flarum\Tests\integration\UsesSettings;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
class SettingsTest extends TestCase
{
use RetrievesAuthorizedUsers;
use UsesSettings;
/**
* @inheritDoc
@ -40,6 +42,8 @@ class SettingsTest extends TestCase
*/
public function custom_setting_isnt_serialized_by_default()
{
$this->purgeSettingsCache();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
@ -61,6 +65,8 @@ class SettingsTest extends TestCase
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting')
);
$this->purgeSettingsCache();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
@ -85,6 +91,8 @@ class SettingsTest extends TestCase
})
);
$this->purgeSettingsCache();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
@ -107,6 +115,8 @@ class SettingsTest extends TestCase
->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2', CustomInvokableClass::class)
);
$this->purgeSettingsCache();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
@ -129,6 +139,8 @@ class SettingsTest extends TestCase
->serializeToForum('customPrefix.noCustomSetting', 'custom-prefix.no_custom_setting', null, 'customDefault')
);
$this->purgeSettingsCache();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
@ -153,6 +165,8 @@ class SettingsTest extends TestCase
}, 'customDefault')
);
$this->purgeSettingsCache();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,

View File

@ -12,6 +12,7 @@ namespace Flarum\Tests\integration\extenders;
use Flarum\Extend;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\Tests\integration\UsesSettings;
use Flarum\User\DisplayName\DriverInterface;
use Flarum\User\User;
use Illuminate\Support\Arr;
@ -19,6 +20,7 @@ use Illuminate\Support\Arr;
class UserTest extends TestCase
{
use RetrievesAuthorizedUsers;
use UsesSettings;
/**
* @inheritDoc
@ -37,6 +39,17 @@ class UserTest extends TestCase
]);
}
/**
* Purge the settings cache and reset the new display name driver.
*/
protected function recalculateDisplayNameDriver()
{
$this->purgeSettingsCache();
$container = $this->app()->getContainer();
$container->forgetInstance('flarum.user.display_name.driver');
User::setDisplayNameDriver($container->make('flarum.user.display_name.driver'));
}
protected function registerTestPreference()
{
$this->extend(
@ -51,6 +64,7 @@ class UserTest extends TestCase
public function username_display_name_driver_used_by_default()
{
$this->app();
$this->recalculateDisplayNameDriver();
$user = User::find(1);
@ -68,6 +82,7 @@ class UserTest extends TestCase
);
$this->app();
$this->recalculateDisplayNameDriver();
$user = User::find(1);