mirror of
https://github.com/flarum/core.git
synced 2025-07-24 02:01:19 +02:00
Preloaded API document Improvements (#2754)
* Invalidate preloadedApiDocument if URL has changed * Revert to using `getRouteData()[0]`
This commit is contained in:
committed by
GitHub
parent
945f6478b5
commit
40dc6d0feb
@@ -159,6 +159,8 @@ export default class Application {
|
|||||||
title = '';
|
title = '';
|
||||||
titleCount = 0;
|
titleCount = 0;
|
||||||
|
|
||||||
|
initialRoute;
|
||||||
|
|
||||||
load(payload) {
|
load(payload) {
|
||||||
this.data = payload;
|
this.data = payload;
|
||||||
this.translator.locale = payload.locale;
|
this.translator.locale = payload.locale;
|
||||||
@@ -174,6 +176,8 @@ export default class Application {
|
|||||||
this.session = new Session(this.store.getById('users', this.data.session.userId), this.data.session.csrfToken);
|
this.session = new Session(this.store.getById('users', this.data.session.userId), this.data.session.csrfToken);
|
||||||
|
|
||||||
this.mount();
|
this.mount();
|
||||||
|
|
||||||
|
this.initialRoute = window.location.href;
|
||||||
}
|
}
|
||||||
|
|
||||||
bootExtensions(extensions) {
|
bootExtensions(extensions) {
|
||||||
@@ -226,7 +230,8 @@ export default class Application {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
preloadedApiDocument() {
|
preloadedApiDocument() {
|
||||||
if (this.data.apiDocument) {
|
// If the URL has changed, the preloaded Api document is invalid.
|
||||||
|
if (this.data.apiDocument && window.location.href === this.initialRoute) {
|
||||||
const results = this.store.pushPayload(this.data.apiDocument);
|
const results = this.store.pushPayload(this.data.apiDocument);
|
||||||
|
|
||||||
this.data.apiDocument = null;
|
this.data.apiDocument = null;
|
||||||
|
@@ -204,7 +204,7 @@ class ForumServiceProvider extends AbstractServiceProvider
|
|||||||
$factory = $this->container->make(RouteHandlerFactory::class);
|
$factory = $this->container->make(RouteHandlerFactory::class);
|
||||||
$defaultRoute = $this->container->make('flarum.settings')->get('default_route');
|
$defaultRoute = $this->container->make('flarum.settings')->get('default_route');
|
||||||
|
|
||||||
if (isset($routes->getRoutes()['GET'][$defaultRoute]['handler'])) {
|
if (isset($routes->getRouteData()[0]['GET'][$defaultRoute]['handler'])) {
|
||||||
$toDefaultController = $routes->getRoutes()['GET'][$defaultRoute]['handler'];
|
$toDefaultController = $routes->getRoutes()['GET'][$defaultRoute]['handler'];
|
||||||
} else {
|
} else {
|
||||||
$toDefaultController = $factory->toForum(Content\Index::class);
|
$toDefaultController = $factory->toForum(Content\Index::class);
|
||||||
|
99
tests/integration/forum/DefaultRouteTest.php
Normal file
99
tests/integration/forum/DefaultRouteTest.php
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
<?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\forum;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Flarum\Extend;
|
||||||
|
use Flarum\Foundation\AbstractServiceProvider;
|
||||||
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
|
use Flarum\Testing\integration\TestCase;
|
||||||
|
|
||||||
|
class DefaultRouteTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->prepareDatabase([
|
||||||
|
'discussions' => [
|
||||||
|
['id' => 1, 'title' => 'foo bar', 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'last_posted_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1],
|
||||||
|
],
|
||||||
|
'posts' => [
|
||||||
|
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>']
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is necessary as we need to add the setting to the DB before the app boots.
|
||||||
|
*/
|
||||||
|
protected function setDefaultRoute($defaultRoute)
|
||||||
|
{
|
||||||
|
OverrideDefaultRouteServiceProvider::$defaultRoute = $defaultRoute;
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\ServiceProvider())->register(OverrideDefaultRouteServiceProvider::class)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function default_route_payload_includes_discussions()
|
||||||
|
{
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertStringContainsString('apiDocument', $response->getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function nonexistent_custom_homepage_uses_default_payload()
|
||||||
|
{
|
||||||
|
$this->setDefaultRoute('/nonexistent');
|
||||||
|
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertStringContainsString('apiDocument', $response->getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function existent_custom_homepage_doesnt_use_default_payload()
|
||||||
|
{
|
||||||
|
$this->setDefaultRoute('/settings');
|
||||||
|
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertStringNotContainsString('apiDocument', $response->getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class OverrideDefaultRouteServiceProvider extends AbstractServiceProvider
|
||||||
|
{
|
||||||
|
public static $defaultRoute;
|
||||||
|
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
$settings = $this->container->make(SettingsRepositoryInterface::class);
|
||||||
|
|
||||||
|
$settings->set('default_route', static::$defaultRoute);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user