Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preloaded API document Improvements #2754

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion js/src/common/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export default class Application {
title = '';
titleCount = 0;

initialRoute;

load(payload) {
this.data = payload;
this.translator.locale = payload.locale;
Expand All @@ -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.mount();

this.initialRoute = window.location.href;
}

bootExtensions(extensions) {
Expand Down Expand Up @@ -226,7 +230,8 @@ export default class Application {
* @public
*/
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);

this.data.apiDocument = null;
Expand Down
2 changes: 1 addition & 1 deletion src/Forum/ForumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected function setDefaultRoute(RouteCollection $routes)
$factory = $this->container->make(RouteHandlerFactory::class);
$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'];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$toDefaultController = $routes->getRoutes()['GET'][$defaultRoute]['handler'];
$toDefaultController = $routes->getRouteData()[0]['GET'][$defaultRoute]['handler'];
Does it need to be modified ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @xiaowu771086986,

Yea we hadn't noticed it at the time and tests didn't catch it, but we did actually fix it not long after here: 1cca4e8

Is this what you meant ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes ,thank you !

} else {
$toDefaultController = $factory->toForum(Content\Index::class);
Expand Down
99 changes: 99 additions & 0 deletions tests/integration/forum/DefaultRouteTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}