From b59aef78382866aeab5fc5ba23932193fa618bab Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 30 Jul 2019 00:09:10 +0200 Subject: [PATCH 1/2] PHPUnit: Get rid of deprecated annotation Refs #1795. --- .../api/Auth/AuthenticateWithApiKeyTest.php | 4 +++- .../api/Controller/CreateDiscussionControllerTest.php | 11 +++++++---- .../api/Controller/CreateGroupControllerTest.php | 10 +++++++--- .../api/Controller/CreateUserControllerTest.php | 10 +++++++--- .../Controller/ListNotificationsControllerTest.php | 4 +++- .../api/Controller/ListUsersControllerTest.php | 4 +++- .../api/Controller/ShowDiscussionControllerTest.php | 7 +++++-- 7 files changed, 35 insertions(+), 15 deletions(-) diff --git a/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php b/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php index c0bafc589..d4a5fadde 100644 --- a/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php +++ b/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php @@ -17,6 +17,7 @@ use Flarum\Api\Client; use Flarum\Api\Controller\CreateGroupController; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; +use Flarum\User\Exception\PermissionDeniedException; use Flarum\User\Guest; use Flarum\User\User; use Illuminate\Support\Str; @@ -57,7 +58,6 @@ class AuthenticateWithApiKeyTest extends TestCase /** * @test - * @expectedException \Flarum\User\Exception\PermissionDeniedException */ public function cannot_authorize_without_key() { @@ -65,6 +65,8 @@ class AuthenticateWithApiKeyTest extends TestCase $api = $this->app()->getContainer()->make(Client::class); $api->setErrorHandler(null); + $this->expectException(PermissionDeniedException::class); + $api->send(CreateGroupController::class, new Guest); } diff --git a/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php b/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php index eb7af2660..eec919d94 100644 --- a/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php @@ -15,6 +15,7 @@ use Flarum\Api\Controller\CreateDiscussionController; use Flarum\Discussion\Discussion; use Flarum\User\User; use Illuminate\Support\Arr; +use Illuminate\Validation\ValidationException; class CreateDiscussionControllerTest extends ApiControllerTestCase { @@ -65,8 +66,6 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Illuminate\Validation\ValidationException - * @expectedExceptionMessage The given data was invalid. */ public function cannot_create_discussion_without_content() { @@ -74,13 +73,14 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase $data = Arr::except($this->data, 'content'); + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('The given data was invalid.'); + $this->callWith($data); } /** * @test - * @expectedException \Illuminate\Validation\ValidationException - * @expectedExceptionMessage The given data was invalid. */ public function cannot_create_discussion_without_title() { @@ -88,6 +88,9 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase $data = Arr::except($this->data, 'title'); + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('The given data was invalid.'); + $this->callWith($data); } } diff --git a/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php b/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php index c9b9db78c..7ab771fa6 100644 --- a/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php @@ -13,9 +13,11 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreateGroupController; use Flarum\Group\Group; +use Flarum\User\Exception\PermissionDeniedException; use Flarum\User\User; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Illuminate\Validation\ValidationException; class CreateGroupControllerTest extends ApiControllerTestCase { @@ -48,13 +50,14 @@ class CreateGroupControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Illuminate\Validation\ValidationException - * @expectedExceptionMessage The given data was invalid. */ public function admin_cannot_create_group_without_data() { $this->actor = User::find(1); + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('The given data was invalid.'); + $this->callWith(); } @@ -81,12 +84,13 @@ class CreateGroupControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Flarum\User\Exception\PermissionDeniedException */ public function unauthorized_user_cannot_create_group() { $this->actor = User::find(2); + $this->expectException(PermissionDeniedException::class); + $this->callWith($this->data); } } diff --git a/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php b/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php index c5f8e80b0..f437ec699 100644 --- a/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php @@ -13,8 +13,10 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreateUserController; use Flarum\Settings\SettingsRepositoryInterface; +use Flarum\User\Exception\PermissionDeniedException; use Flarum\User\User; use Illuminate\Support\Arr; +use Illuminate\Validation\ValidationException; class CreateUserControllerTest extends ApiControllerTestCase { @@ -48,11 +50,12 @@ class CreateUserControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Illuminate\Validation\ValidationException - * @expectedExceptionMessage The given data was invalid. */ public function cannot_create_user_without_data() { + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('The given data was invalid.'); + $this->callWith(); } @@ -96,7 +99,6 @@ class CreateUserControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Flarum\User\Exception\PermissionDeniedException */ public function disabling_sign_up_prevents_user_creation() { @@ -104,6 +106,8 @@ class CreateUserControllerTest extends ApiControllerTestCase $settings = app(SettingsRepositoryInterface::class); $settings->set('allow_sign_up', false); + $this->expectException(PermissionDeniedException::class); + try { $this->callWith($this->data); } finally { diff --git a/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php b/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php index f72fd9e5c..a7a64924a 100644 --- a/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php @@ -12,6 +12,7 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ListNotificationsController; +use Flarum\User\Exception\PermissionDeniedException; use Flarum\User\User; class ListNotificationsControllerTest extends ApiControllerTestCase @@ -31,10 +32,11 @@ class ListNotificationsControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Flarum\User\Exception\PermissionDeniedException */ public function disallows_index_for_guest() { + $this->expectException(PermissionDeniedException::class); + $this->callWith(); } diff --git a/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php b/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php index 30a961849..919f01ddc 100644 --- a/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php @@ -12,6 +12,7 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ListUsersController; +use Flarum\User\Exception\PermissionDeniedException; use Flarum\User\User; class ListUsersControllerTest extends ApiControllerTestCase @@ -37,10 +38,11 @@ class ListUsersControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Flarum\User\Exception\PermissionDeniedException */ public function disallows_index_for_guest() { + $this->expectException(PermissionDeniedException::class); + $this->callWith(); } diff --git a/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php b/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php index ab1fe702a..50619c789 100644 --- a/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php @@ -15,6 +15,7 @@ use Carbon\Carbon; use Flarum\Api\Controller\ShowDiscussionController; use Flarum\Discussion\Discussion; use Flarum\User\User; +use Illuminate\Database\Eloquent\ModelNotFoundException; class ShowDiscussionControllerTest extends ApiControllerTestCase { @@ -69,10 +70,11 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException */ public function guest_cannot_see_empty_discussion() { + $this->expectException(ModelNotFoundException::class); + $response = $this->callWith([], ['id' => 1]); $this->assertEquals(200, $response->getStatusCode()); @@ -90,10 +92,11 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase /** * @test - * @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException */ public function guests_cannot_see_private_discussion() { + $this->expectException(ModelNotFoundException::class); + $this->callWith([], ['id' => 3]); } } From 1169e4aca64f7ae0d932642a2d38ede7a5771d3b Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 30 Jul 2019 00:56:29 +0200 Subject: [PATCH 2/2] Determine default route after extensions Fixes #1819. --- framework/core/src/Forum/ForumServiceProvider.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/framework/core/src/Forum/ForumServiceProvider.php b/framework/core/src/Forum/ForumServiceProvider.php index 56edb7132..969edb06d 100644 --- a/framework/core/src/Forum/ForumServiceProvider.php +++ b/framework/core/src/Forum/ForumServiceProvider.php @@ -53,6 +53,10 @@ class ForumServiceProvider extends AbstractServiceProvider return $routes; }); + $this->app->afterResolving('flarum.forum.routes', function (RouteCollection $routes) { + $this->setDefaultRoute($routes); + }); + $this->app->singleton('flarum.forum.middleware', function (Application $app) { $pipe = new MiddlewarePipe; @@ -181,7 +185,16 @@ class ForumServiceProvider extends AbstractServiceProvider $this->app->make('events')->fire( new ConfigureForumRoutes($routes, $factory) ); + } + /** + * Determine the default route. + * + * @param RouteCollection $routes + */ + protected function setDefaultRoute(RouteCollection $routes) + { + $factory = $this->app->make(RouteHandlerFactory::class); $defaultRoute = $this->app->make('flarum.settings')->get('default_route'); if (isset($routes->getRouteData()[0]['GET'][$defaultRoute])) {