From 7c0d98c63d2f7541c26ea9961a8f5d04af25c5bb Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Thu, 7 Jan 2021 14:10:53 -0500 Subject: [PATCH] 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. --- .../core/tests/integration/UsesSettings.php | 25 +++++++++++++++++++ .../integration/extenders/ModelUrlTest.php | 6 +++++ .../integration/extenders/SettingsTest.php | 14 +++++++++++ .../tests/integration/extenders/UserTest.php | 15 +++++++++++ 4 files changed, 60 insertions(+) create mode 100644 framework/core/tests/integration/UsesSettings.php diff --git a/framework/core/tests/integration/UsesSettings.php b/framework/core/tests/integration/UsesSettings.php new file mode 100644 index 000000000..d37aa0f7b --- /dev/null +++ b/framework/core/tests/integration/UsesSettings.php @@ -0,0 +1,25 @@ +app()->getContainer()->forgetInstance(SettingsRepositoryInterface::class); + } +} diff --git a/framework/core/tests/integration/extenders/ModelUrlTest.php b/framework/core/tests/integration/extenders/ModelUrlTest.php index ab3eef49b..a5b095ce8 100644 --- a/framework/core/tests/integration/extenders/ModelUrlTest.php +++ b/framework/core/tests/integration/extenders/ModelUrlTest.php @@ -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); diff --git a/framework/core/tests/integration/extenders/SettingsTest.php b/framework/core/tests/integration/extenders/SettingsTest.php index d279bfd27..f85d175e8 100644 --- a/framework/core/tests/integration/extenders/SettingsTest.php +++ b/framework/core/tests/integration/extenders/SettingsTest.php @@ -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, diff --git a/framework/core/tests/integration/extenders/UserTest.php b/framework/core/tests/integration/extenders/UserTest.php index 497bc7f95..f8b22d5bc 100644 --- a/framework/core/tests/integration/extenders/UserTest.php +++ b/framework/core/tests/integration/extenders/UserTest.php @@ -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);