From 148f810d960f8b4b3ddd638a20890c05b2a99ec9 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Fri, 23 Apr 2021 18:26:41 -0400 Subject: [PATCH] Don't resolve routes early. In normal Flarum, extensions are only enabled during requests. Here, however, we enable some during boot. This resolves the FilesystemManager early, which resolves the Url Generator early. To fix this, we directly provide a filesystem disk for assets instead of getting it from the filesystem manager. --- .../ExtensionManagerIncludeCurrent.php | 14 +++++ php-packages/testing/tests/extend.php | 3 +- .../tests/tests/integration/TestCaseTest.php | 57 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/php-packages/testing/src/integration/Extension/ExtensionManagerIncludeCurrent.php b/php-packages/testing/src/integration/Extension/ExtensionManagerIncludeCurrent.php index c7a4169f3..75fa63a27 100644 --- a/php-packages/testing/src/integration/Extension/ExtensionManagerIncludeCurrent.php +++ b/php-packages/testing/src/integration/Extension/ExtensionManagerIncludeCurrent.php @@ -11,7 +11,11 @@ namespace Flarum\Testing\integration\Extension; use Flarum\Extension\Extension; use Flarum\Extension\ExtensionManager; +use Illuminate\Contracts\Filesystem\Cloud; +use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Support\Arr; +use League\Flysystem\Adapter\Local; +use League\Flysystem\Filesystem; class ExtensionManagerIncludeCurrent extends ExtensionManager { @@ -39,4 +43,14 @@ class ExtensionManagerIncludeCurrent extends ExtensionManager return $this->extensions; } + + /** + * Get an instance of the assets filesystem. + * This is resolved dynamically because Flarum's filesystem configuration + * might not be booted yet when the ExtensionManager singleton initializes. + */ + protected function getAssetsFilesystem(): Cloud + { + return new FilesystemAdapter(new Filesystem(new Local($this->paths->public.'/assets'), ['url' => resolve('flarum.config')->url().'/assets'])); + } } diff --git a/php-packages/testing/tests/extend.php b/php-packages/testing/tests/extend.php index 075d35db5..9c49c556f 100644 --- a/php-packages/testing/tests/extend.php +++ b/php-packages/testing/tests/extend.php @@ -14,5 +14,6 @@ namespace Flarum\Testing; use Flarum\Extend; return [ - (new Extend\Settings)->serializeToForum('notARealSetting', 'not.a.real.setting') + (new Extend\Settings)->serializeToForum('notARealSetting', 'not.a.real.setting'), + (new Extend\Frontend('forum'))->route('/added-by-extension', 'added-by-extension') ]; diff --git a/php-packages/testing/tests/tests/integration/TestCaseTest.php b/php-packages/testing/tests/tests/integration/TestCaseTest.php index f301b77c3..9efd32302 100644 --- a/php-packages/testing/tests/tests/integration/TestCaseTest.php +++ b/php-packages/testing/tests/tests/integration/TestCaseTest.php @@ -127,4 +127,61 @@ class TestCaseTest extends TestCase $this->assertStringContainsString('notARealSetting', $response->getBody()->getContents()); } + + /** + * @test + */ + public function can_apply_route_extenders() + { + $this->extend( + (new Extend\Frontend('forum'))->route('/arbitrary', 'arbitrary') + ); + + $response = $this->send( + $this->request('GET', '/arbitrary') + ); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @test + */ + public function routes_added_by_current_extension_not_accessible_by_default() + { + $response = $this->send( + $this->request('GET', '/added-by-extension') + ); + + $this->assertEquals(404, $response->getStatusCode()); + } + + /** + * @test + */ + public function routes_added_by_current_extension_accessible() + { + $this->extension('flarum-testing-tests'); + + $response = $this->send( + $this->request('GET', '/added-by-extension') + ); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @test + */ + public function extension_url_correct() + { + $this->extension('flarum-testing-tests'); + $expected = $this->app()->getContainer()->make('filesystem')->disk('flarum-assets')->url('/flarum-testing-tests/'); + // We need to test this since we override it. + $extensions = $this->app()->getContainer()->make('flarum.extensions'); + $currExtension = $extensions->getExtension('flarum-testing-tests'); + $baseAssetsUrl = $extensions->getAsset($currExtension, ''); + + $this->assertEquals($expected, $baseAssetsUrl); + } } \ No newline at end of file