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.
This commit is contained in:
Alexander Skvortsov 2021-04-23 18:26:41 -04:00
parent 04b04f3635
commit 148f810d96
3 changed files with 73 additions and 1 deletions

View File

@ -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']));
}
}

View File

@ -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')
];

View File

@ -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);
}
}