diff --git a/framework/core/tests/integration/BuildsHttpRequests.php b/framework/core/tests/integration/BuildsHttpRequests.php index 09704de8b..410d67b56 100644 --- a/framework/core/tests/integration/BuildsHttpRequests.php +++ b/framework/core/tests/integration/BuildsHttpRequests.php @@ -9,8 +9,9 @@ namespace Flarum\Tests\integration; +use Carbon\Carbon; use Dflydev\FigCookies\SetCookie; -use Flarum\Http\AccessToken; +use Illuminate\Support\Str; use Laminas\Diactoros\CallbackStream; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -33,10 +34,21 @@ trait BuildsHttpRequests protected function requestAsUser(Request $req, int $userId): Request { - $token = AccessToken::generate($userId); - $token->save(); + $token = Str::random(40); - return $req->withAddedHeader('Authorization', "Token {$token->token}"); + /** + * We insert this directly instead of via `prepareDatabase` + * so that requests can be created/sent after the app is booted. + */ + $this->database()->table('access_tokens')->insert([ + 'token' => $token, + 'user_id' => $userId, + 'created_at' => Carbon::now()->toDateTimeString(), + 'last_activity_at' => Carbon::now()->toDateTimeString(), + 'lifetime_seconds' => 3600 + ]); + + return $req->withAddedHeader('Authorization', "Token {$token}"); } protected function requestWithCookiesFrom(Request $req, Response $previous): Request diff --git a/framework/core/tests/integration/RetrievesAuthorizedUsers.php b/framework/core/tests/integration/RetrievesAuthorizedUsers.php index df2f80e87..a4bd58a98 100644 --- a/framework/core/tests/integration/RetrievesAuthorizedUsers.php +++ b/framework/core/tests/integration/RetrievesAuthorizedUsers.php @@ -11,50 +11,6 @@ namespace Flarum\Tests\integration; trait RetrievesAuthorizedUsers { - protected function adminGroup(): array - { - return [ - 'id' => 1, - 'name_singular' => 'Admin', - 'name_plural' => 'Admins', - 'color' => '#B72A2A', - 'icon' => 'fas fa-wrench', - ]; - } - - protected function guestGroup(): array - { - return [ - 'id' => 2, - 'name_singular' => 'Guest', - 'name_plural' => 'Guests', - 'color' => null, - 'icon' => null, - ]; - } - - protected function memberGroup(): array - { - return [ - 'id' => 3, - 'name_singular' => 'Member', - 'name_plural' => 'Members', - 'color' => null, - 'icon' => null, - ]; - } - - protected function adminUser(): array - { - return [ - 'id' => 1, - 'username' => 'admin', - 'password' => '$2y$10$HMOAe.XaQjOimA778VmFue1OCt7tj5j0wk5vfoL/CMSJq2BQlfBV2', // BCrypt hash for "password" - 'email' => 'admin@machine.local', - 'is_email_confirmed' => 1, - ]; - } - protected function normalUser(): array { return [ diff --git a/framework/core/tests/integration/TestCase.php b/framework/core/tests/integration/TestCase.php index 16a65f07e..1bf515a41 100644 --- a/framework/core/tests/integration/TestCase.php +++ b/framework/core/tests/integration/TestCase.php @@ -23,6 +23,16 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { use BuildsHttpRequests; + /** + * @inheritDoc + */ + protected function tearDown(): void + { + parent::tearDown(); + + $this->database()->rollBack(); + } + /** * @var \Flarum\Foundation\InstalledApp */ @@ -47,6 +57,10 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase $site->extendWith($this->extenders); $this->app = $site->bootApp(); + + $this->database()->beginTransaction(); + + $this->populateDatabase(); } return $this->app; @@ -89,20 +103,23 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase return $this->database; } + protected $databaseContent = []; + protected function prepareDatabase(array $tableData) + { + $this->databaseContent = array_merge_recursive( + $this->databaseContent, + $tableData + ); + } + + protected function populateDatabase() { // We temporarily disable foreign key checks to simplify this process. $this->database()->getSchemaBuilder()->disableForeignKeyConstraints(); - // First, truncate all referenced tables so that they are empty. - foreach (array_keys($tableData) as $table) { - if ($table !== 'settings') { - $this->database()->table($table)->truncate(); - } - } - // Then, insert all rows required for this test case. - foreach ($tableData as $table => $rows) { + foreach ($this->databaseContent as $table => $rows) { foreach ($rows as $row) { if ($table === 'settings') { $this->database()->table($table)->updateOrInsert( diff --git a/framework/core/tests/integration/UsesSettings.php b/framework/core/tests/integration/UsesSettings.php new file mode 100644 index 000000000..d37aa0f7b --- /dev/null +++ b/framework/core/tests/integration/UsesSettings.php @@ -0,0 +1,25 @@ +app()->getContainer()->forgetInstance(SettingsRepositoryInterface::class); + } +} diff --git a/framework/core/tests/integration/api/authentication/WithApiKeyTest.php b/framework/core/tests/integration/api/authentication/WithApiKeyTest.php index 37e9bf3ca..fdbf14128 100644 --- a/framework/core/tests/integration/api/authentication/WithApiKeyTest.php +++ b/framework/core/tests/integration/api/authentication/WithApiKeyTest.php @@ -13,39 +13,29 @@ use Carbon\Carbon; use Flarum\Api\ApiKey; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; -use Illuminate\Support\Str; class WithApiKeyTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), ], - 'group_permission' => [ - ['permission' => 'viewUserList', 'group_id' => 3] - ], - 'api_keys' => [], + 'api_keys' => [ + ['key' => 'mastertoken', 'user_id' => null, 'created_at' => Carbon::now()->toDateTimeString()], + ['key' => 'personaltoken', 'user_id' => 2, 'created_at' => Carbon::now()->toDateTimeString()], + ] ]); } - protected function key(int $user_id = null): ApiKey - { - return ApiKey::unguarded(function () use ($user_id) { - return ApiKey::query()->firstOrCreate([ - 'key' => Str::random(), - 'user_id' => $user_id, - 'created_at' => Carbon::now() - ]); - }); - } - /** * @test */ @@ -64,18 +54,16 @@ class WithApiKeyTest extends TestCase */ public function master_token_can_authenticate_as_anyone() { - $key = $this->key(); - $response = $this->send( $this->request('GET', '/api') - ->withAddedHeader('Authorization', "Token {$key->key}; userId=1") + ->withAddedHeader('Authorization', 'Token mastertoken; userId=1') ); $data = json_decode($response->getBody(), true); $this->assertTrue($data['data']['attributes']['canViewUserList']); $this->assertArrayHasKey('adminUrl', $data['data']['attributes']); - $key->refresh(); + $key = ApiKey::where('key', 'mastertoken')->first(); $this->assertNotNull($key->last_activity_at); } @@ -85,18 +73,16 @@ class WithApiKeyTest extends TestCase */ public function personal_api_token_cannot_authenticate_as_anyone() { - $key = $this->key(2); - $response = $this->send( $this->request('GET', '/api') - ->withAddedHeader('Authorization', "Token {$key->key}; userId=1") + ->withAddedHeader('Authorization', 'Token personaltoken; userId=1') ); $data = json_decode($response->getBody(), true); $this->assertTrue($data['data']['attributes']['canViewUserList']); $this->assertArrayNotHasKey('adminUrl', $data['data']['attributes']); - $key->refresh(); + $key = ApiKey::where('key', 'personaltoken')->first(); $this->assertNotNull($key->last_activity_at); } @@ -106,18 +92,16 @@ class WithApiKeyTest extends TestCase */ public function personal_api_token_authenticates_user() { - $key = $this->key(2); - $response = $this->send( $this->request('GET', '/api') - ->withAddedHeader('Authorization', "Token {$key->key}") + ->withAddedHeader('Authorization', 'Token personaltoken') ); $data = json_decode($response->getBody(), true); $this->assertTrue($data['data']['attributes']['canViewUserList']); $this->assertArrayNotHasKey('adminUrl', $data['data']['attributes']); - $key->refresh(); + $key = ApiKey::where('key', 'personaltoken')->first(); $this->assertNotNull($key->last_activity_at); } diff --git a/framework/core/tests/integration/api/authentication/WithTokenTest.php b/framework/core/tests/integration/api/authentication/WithTokenTest.php index 4405076ea..79d7b0014 100644 --- a/framework/core/tests/integration/api/authentication/WithTokenTest.php +++ b/framework/core/tests/integration/api/authentication/WithTokenTest.php @@ -17,6 +17,9 @@ class WithTokenTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); diff --git a/framework/core/tests/integration/api/csrf_protection/RequireCsrfTokenTest.php b/framework/core/tests/integration/api/csrf_protection/RequireCsrfTokenTest.php index b6540eb66..152bcf4a6 100644 --- a/framework/core/tests/integration/api/csrf_protection/RequireCsrfTokenTest.php +++ b/framework/core/tests/integration/api/csrf_protection/RequireCsrfTokenTest.php @@ -16,23 +16,14 @@ class RequireCsrfTokenTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ - 'users' => [ - $this->adminUser(), - ], - 'groups' => [ - $this->adminGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], - 'group_permission' => [ - ['permission' => 'viewUserList', 'group_id' => 3], - ], 'api_keys' => [ ['user_id' => 1, 'key' => 'superadmin'], ], diff --git a/framework/core/tests/integration/api/discussions/CreateTest.php b/framework/core/tests/integration/api/discussions/CreateTest.php index 471bf1a2c..c0cf9c210 100644 --- a/framework/core/tests/integration/api/discussions/CreateTest.php +++ b/framework/core/tests/integration/api/discussions/CreateTest.php @@ -18,28 +18,16 @@ class CreateTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ - 'discussions' => [], - 'posts' => [], 'users' => [ - $this->adminUser(), $this->normalUser(), - ], - 'groups' => [ - $this->adminGroup(), - $this->memberGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ['user_id' => 2, 'group_id' => 3], - ], - 'group_permission' => [ - ['permission' => 'viewDiscussions', 'group_id' => 3], - ['permission' => 'startDiscussion', 'group_id' => 3], ] ]); } diff --git a/framework/core/tests/integration/api/discussions/DeletionTest.php b/framework/core/tests/integration/api/discussions/DeletionTest.php index aa948d046..f81922ab6 100644 --- a/framework/core/tests/integration/api/discussions/DeletionTest.php +++ b/framework/core/tests/integration/api/discussions/DeletionTest.php @@ -17,6 +17,9 @@ class DeletionTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); @@ -29,15 +32,8 @@ class DeletionTest extends TestCase ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

foo bar

'], ], 'users' => [ - $this->adminUser(), $this->normalUser(), ], - 'groups' => [ - $this->adminGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], ]); } diff --git a/framework/core/tests/integration/api/discussions/ListTest.php b/framework/core/tests/integration/api/discussions/ListTest.php index 3e56c4150..48e1ee949 100644 --- a/framework/core/tests/integration/api/discussions/ListTest.php +++ b/framework/core/tests/integration/api/discussions/ListTest.php @@ -17,6 +17,9 @@ class ListTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); @@ -30,13 +33,6 @@ class ListTest extends TestCase ], 'users' => [ $this->normalUser(), - ], - 'groups' => [ - $this->memberGroup(), - $this->guestGroup(), - ], - 'group_permission' => [ - ['permission' => 'viewDiscussions', 'group_id' => 2], ] ]); } @@ -71,96 +67,4 @@ class ListTest extends TestCase $this->assertEquals(200, $response->getStatusCode()); } - - /** - * @test - */ - public function can_search_for_word_in_post() - { - $this->database()->table('discussions')->insert([ - ['id' => 2, 'title' => 'lightsail in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], - ['id' => 3, 'title' => 'not in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], - ]); - - $this->database()->table('posts')->insert([ - ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

not in text

'], - ['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

lightsail in text

'], - ]); - - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => 'lightsail'], - 'include' => 'mostRelevantPost', - ]) - ); - - $data = json_decode($response->getBody()->getContents(), true); - $ids = array_map(function ($row) { - return $row['id']; - }, $data['data']); - - // Order-independent comparison - $this->assertEquals(['3'], $ids, 'IDs do not match', 0.0, 10, true); - } - - /** - * @test - */ - public function ignores_non_word_characters_when_searching() - { - $this->database()->table('discussions')->insert([ - ['id' => 2, 'title' => 'lightsail in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], - ['id' => 3, 'title' => 'not in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], - ]); - - $this->database()->table('posts')->insert([ - ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

not in text

'], - ['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

lightsail in text

'], - ]); - - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => 'lightsail+'], - 'include' => 'mostRelevantPost', - ]) - ); - - $data = json_decode($response->getBody()->getContents(), true); - $ids = array_map(function ($row) { - return $row['id']; - }, $data['data']); - - // Order-independent comparison - $this->assertEquals(['3'], $ids, 'IDs do not match', 0.0, 10, true); - } - - /** - * @test - */ - public function search_for_special_characters_gives_empty_result() - { - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => '*'], - 'include' => 'mostRelevantPost', - ]) - ); - - $data = json_decode($response->getBody()->getContents(), true); - $this->assertEquals([], $data['data']); - - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => '@'], - 'include' => 'mostRelevantPost', - ]) - ); - - $data = json_decode($response->getBody()->getContents(), true); - $this->assertEquals([], $data['data']); - } } diff --git a/framework/core/tests/integration/api/discussions/ListTestWithFulltextSearch.php b/framework/core/tests/integration/api/discussions/ListTestWithFulltextSearch.php new file mode 100644 index 000000000..ce9edf8f9 --- /dev/null +++ b/framework/core/tests/integration/api/discussions/ListTestWithFulltextSearch.php @@ -0,0 +1,130 @@ +database()->rollBack(); + + // We need to insert these outside of a transaction, because FULLTEXT indexing, + // which is needed for search, doesn't happen in transactions. + // We clean it up explcitly at the end. + $this->database()->table('discussions')->insert([ + ['id' => 1, 'title' => 'lightsail in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1], + ['id' => 2, 'title' => 'not in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1], + ]); + + $this->database()->table('posts')->insert([ + ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

not in text

'], + ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

lightsail in text

'], + ]); + + // We need to call these again, since we rolled back the transaction started by `::app()`. + $this->database()->beginTransaction(); + + $this->populateDatabase(); + } + + /** + * @inheritDoc + */ + protected function tearDown(): void + { + parent::tearDown(); + + $this->database()->table('discussions')->whereIn('id', [1, 2])->delete(); + $this->database()->table('posts')->whereIn('id', [1, 2])->delete(); + } + + /** + * @test + */ + public function can_search_for_word_in_post() + { + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => 'lightsail'], + 'include' => 'mostRelevantPost', + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true); + $ids = array_map(function ($row) { + return $row['id']; + }, $data['data']); + + // Order-independent comparison + $this->assertEquals(['3'], $ids, 'IDs do not match', 0.0, 10, true); + } + + /** + * @test + */ + public function ignores_non_word_characters_when_searching() + { + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => 'lightsail+'], + 'include' => 'mostRelevantPost', + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true); + $ids = array_map(function ($row) { + return $row['id']; + }, $data['data']); + + // Order-independent comparison + $this->assertEquals(['3'], $ids, 'IDs do not match', 0.0, 10, true); + } + + /** + * @test + */ + public function search_for_special_characters_gives_empty_result() + { + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => '*'], + 'include' => 'mostRelevantPost', + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true); + $this->assertEquals([], $data['data']); + + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => '@'], + 'include' => 'mostRelevantPost', + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true); + $this->assertEquals([], $data['data']); + } +} diff --git a/framework/core/tests/integration/api/discussions/ShowTest.php b/framework/core/tests/integration/api/discussions/ShowTest.php index b4300a34a..cf6f82ca5 100644 --- a/framework/core/tests/integration/api/discussions/ShowTest.php +++ b/framework/core/tests/integration/api/discussions/ShowTest.php @@ -20,6 +20,9 @@ class ShowTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); @@ -37,17 +40,6 @@ class ShowTest extends TestCase ], 'users' => [ $this->normalUser(), - ], - 'groups' => [ - $this->guestGroup(), - $this->memberGroup(), - ], - 'group_user' => [ - ['user_id' => 2, 'group_id' => 3], - ], - 'group_permission' => [ - ['permission' => 'viewDiscussions', 'group_id' => 2], - ['permission' => 'viewDiscussions', 'group_id' => 3], ] ]); } diff --git a/framework/core/tests/integration/api/forum/ShowTest.php b/framework/core/tests/integration/api/forum/ShowTest.php index 965473678..7bb072f10 100644 --- a/framework/core/tests/integration/api/forum/ShowTest.php +++ b/framework/core/tests/integration/api/forum/ShowTest.php @@ -17,23 +17,17 @@ class ShowTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), - ], - 'groups' => [ - $this->adminGroup(), - $this->memberGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ['user_id' => 2, 'group_id' => 3], - ], + ] ]); } diff --git a/framework/core/tests/integration/api/groups/CreateTest.php b/framework/core/tests/integration/api/groups/CreateTest.php index 360e7f3b3..a92e33877 100644 --- a/framework/core/tests/integration/api/groups/CreateTest.php +++ b/framework/core/tests/integration/api/groups/CreateTest.php @@ -18,21 +18,17 @@ class CreateTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), ], - 'groups' => [ - $this->adminGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], ]); } diff --git a/framework/core/tests/integration/api/groups/ListTest.php b/framework/core/tests/integration/api/groups/ListTest.php index 13f476b1f..0b1f7bf74 100644 --- a/framework/core/tests/integration/api/groups/ListTest.php +++ b/framework/core/tests/integration/api/groups/ListTest.php @@ -17,21 +17,16 @@ class ListTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ - 'users' => [ - $this->adminUser(), - $this->normalUser(), - ], 'groups' => [ - $this->adminGroup(), - $this->hiddenGroup() - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], + $this->hiddenGroup(), ], ]); } @@ -48,7 +43,8 @@ class ListTest extends TestCase $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($response->getBody()->getContents(), true); - $this->assertEquals(['1'], Arr::pluck($data['data'], 'id')); + // The four default groups created by the installer + $this->assertEquals(['1', '2', '3', '4'], Arr::pluck($data['data'], 'id')); } /** @@ -65,7 +61,8 @@ class ListTest extends TestCase $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($response->getBody()->getContents(), true); - $this->assertEquals(['1', '10'], Arr::pluck($data['data'], 'id')); + // The four default groups created by the installer and our hidden group + $this->assertEquals(['1', '2', '3', '4', '10'], Arr::pluck($data['data'], 'id')); } protected function hiddenGroup(): array diff --git a/framework/core/tests/integration/api/notifications/ListTest.php b/framework/core/tests/integration/api/notifications/ListTest.php index a33a756de..8a0a0a563 100644 --- a/framework/core/tests/integration/api/notifications/ListTest.php +++ b/framework/core/tests/integration/api/notifications/ListTest.php @@ -16,6 +16,9 @@ class ListTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); diff --git a/framework/core/tests/integration/api/posts/CreateTest.php b/framework/core/tests/integration/api/posts/CreateTest.php index 613810078..d1a57151d 100644 --- a/framework/core/tests/integration/api/posts/CreateTest.php +++ b/framework/core/tests/integration/api/posts/CreateTest.php @@ -17,6 +17,9 @@ class CreateTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); @@ -25,18 +28,8 @@ class CreateTest extends TestCase 'discussions' => [ ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2], ], - 'posts' => [], 'users' => [ $this->normalUser(), - ], - 'groups' => [ - $this->memberGroup(), - ], - 'group_user' => [ - ['user_id' => 2, 'group_id' => 3], - ], - 'group_permission' => [ - ['permission' => 'viewDiscussions', 'group_id' => 3], ] ]); } diff --git a/framework/core/tests/integration/api/users/CreateTest.php b/framework/core/tests/integration/api/users/CreateTest.php index 65e821ab7..c3eb38ace 100644 --- a/framework/core/tests/integration/api/users/CreateTest.php +++ b/framework/core/tests/integration/api/users/CreateTest.php @@ -18,21 +18,14 @@ class CreateTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ - 'users' => [ - $this->adminUser(), - $this->normalUser(), - ], - 'groups' => [ - $this->adminGroup() - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], 'settings' => [ ['key' => 'mail_driver', 'value' => 'log'], ], diff --git a/framework/core/tests/integration/api/users/ListTest.php b/framework/core/tests/integration/api/users/ListTest.php index 3e33786b4..c2b05a941 100644 --- a/framework/core/tests/integration/api/users/ListTest.php +++ b/framework/core/tests/integration/api/users/ListTest.php @@ -9,7 +9,6 @@ namespace Flarum\Tests\integration\api\users; -use Flarum\Group\Permission; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; @@ -17,25 +16,6 @@ class ListTest extends TestCase { use RetrievesAuthorizedUsers; - protected function setUp(): void - { - parent::setUp(); - - $this->prepareDatabase([ - 'users' => [ - $this->adminUser(), - ], - 'groups' => [ - $this->adminGroup(), - $this->guestGroup(), - ], - 'group_permission' => [], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], - ]); - } - /** * @test */ @@ -53,12 +33,11 @@ class ListTest extends TestCase */ public function shows_index_for_guest_when_they_have_permission() { - Permission::unguarded(function () { - Permission::create([ - 'permission' => 'viewUserList', - 'group_id' => 2, - ]); - }); + $this->prepareDatabase([ + 'group_permission' => [ + ['permission' => 'viewUserList', 'group_id' => 2], + ], + ]); $response = $this->send( $this->request('GET', '/api/users') diff --git a/framework/core/tests/integration/api/users/ShowTest.php b/framework/core/tests/integration/api/users/ShowTest.php index 3ee210203..c3bcefaee 100644 --- a/framework/core/tests/integration/api/users/ShowTest.php +++ b/framework/core/tests/integration/api/users/ShowTest.php @@ -16,27 +16,30 @@ class ShowTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), ], - 'groups' => [ - $this->adminGroup() - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], - 'settings' => [ - ['key' => 'mail_driver', 'value' => 'log'], - ], ]); } + private function forbidGuestsFromSeeingForum() + { + $this->database()->table('group_permission')->where('permission', 'viewDiscussions')->where('group_id', 2)->delete(); + } + + private function forbidMembersFromSearchingUsers() + { + $this->database()->table('group_permission')->where('permission', 'viewUserList')->where('group_id', 3)->delete(); + } + /** * @test */ @@ -70,22 +73,52 @@ class ShowTest extends TestCase /** * @test */ - public function guest_cannot_see_user() + public function guest_can_see_user_by_default() { $response = $this->send( $this->request('GET', '/api/users/2') ); + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @test + */ + public function guest_can_see_user_by_slug_by_default() + { + $response = $this->send( + $this->request('GET', '/api/users/normal')->withQueryParams([ + 'bySlug' => true + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @test + */ + public function guest_cant_see_user_if_blocked() + { + $this->forbidGuestsFromSeeingForum(); + + $response = $this->send( + $this->request('GET', '/api/users/2') + ); + $this->assertEquals(404, $response->getStatusCode()); } /** * @test */ - public function guest_cannot_see_user_by_slug() + public function guest_cant_see_user_by_slug_if_blocked() { + $this->forbidGuestsFromSeeingForum(); + $response = $this->send( - $this->request('GET', '/api/users/2')->withQueryParams([ + $this->request('GET', '/api/users/normal')->withQueryParams([ 'bySlug' => true ]) ); @@ -126,7 +159,7 @@ class ShowTest extends TestCase /** * @test */ - public function user_cant_see_others_by_default() + public function user_can_see_others_by_default() { $response = $this->send( $this->request('GET', '/api/users/1', [ @@ -134,55 +167,31 @@ class ShowTest extends TestCase ]) ); - $this->assertEquals(404, $response->getStatusCode()); - } - - /** - * @test - */ - public function user_cant_see_others_by_default_via_slug() - { - $response = $this->send( - $this->request('GET', '/api/users/admin', [ - 'authenticatedAs' => 2, - ])->withQueryParams([ - 'bySlug' => true - ]) - ); - - $this->assertEquals(404, $response->getStatusCode()); - } - - /** - * @test - */ - public function user_can_see_others_if_allowed() - { - $this->prepareDatabase([ - 'group_permission' => [ - ['permission' => 'viewDiscussions', 'group_id' => 3], - ] - ]); - - $response = $this->send( - $this->request('GET', '/api/users/1', [ - 'authenticatedAs' => 2, - ]) - ); - $this->assertEquals(200, $response->getStatusCode()); } /** * @test */ - public function user_can_see_others_if_allowed_via_slug() + public function user_can_see_others_by_default_via_slug() { - $this->prepareDatabase([ - 'group_permission' => [ - ['permission' => 'viewDiscussions', 'group_id' => 3], - ] - ]); + $response = $this->send( + $this->request('GET', '/api/users/admin', [ + 'authenticatedAs' => 2, + ])->withQueryParams([ + 'bySlug' => true + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @test + */ + public function user_can_still_see_others_via_slug_even_if_cant_search() + { + $this->forbidMembersFromSearchingUsers(); $response = $this->send( $this->request('GET', '/api/users/admin', [ diff --git a/framework/core/tests/integration/api/users/UpdateTest.php b/framework/core/tests/integration/api/users/UpdateTest.php index e143e6565..280554b81 100644 --- a/framework/core/tests/integration/api/users/UpdateTest.php +++ b/framework/core/tests/integration/api/users/UpdateTest.php @@ -16,26 +16,16 @@ class UpdateTest extends TestCase { use RetrievesAuthorizedUsers; + /** + * @inheritDoc + */ protected function setUp(): void { parent::setUp(); $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), - ], - 'groups' => [ - $this->adminGroup(), - $this->memberGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ['user_id' => 2, 'group_id' => 3], - ], - 'group_permission' => [ - ['permission' => 'viewUserList', 'group_id' => 3], - ['permission' => 'viewDiscussions', 'group_id' => 3] ] ]); } diff --git a/framework/core/tests/integration/extenders/ApiControllerTest.php b/framework/core/tests/integration/extenders/ApiControllerTest.php index 5d8784cd7..2c6be92ff 100644 --- a/framework/core/tests/integration/extenders/ApiControllerTest.php +++ b/framework/core/tests/integration/extenders/ApiControllerTest.php @@ -31,17 +31,17 @@ class ApiControllerTest extends TestCase { use RetrievesAuthorizedUsers; - protected function prepDb() + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser() ], - 'groups' => [ - $this->adminGroup(), - $this->memberGroup() - ], 'discussions' => [ ['id' => 1, 'title' => 'Custom Discussion Title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 0, 'comment_count' => 1, 'is_private' => 0], ['id' => 2, 'title' => 'Custom Discussion Title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 3, 'first_post_id' => 0, 'comment_count' => 1, 'is_private' => 0], @@ -62,8 +62,6 @@ class ApiControllerTest extends TestCase }) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions/1', [ 'authenticatedAs' => 1, @@ -85,8 +83,6 @@ class ApiControllerTest extends TestCase ->prepareDataForSerialization(CustomPrepareDataSerializationInvokableClass::class) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions/1', [ 'authenticatedAs' => 1, @@ -113,8 +109,6 @@ class ApiControllerTest extends TestCase }) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api', [ 'authenticatedAs' => 1, @@ -139,8 +133,6 @@ class ApiControllerTest extends TestCase ->prepareDataForSerialization(CustomInvokableClassArgsReference::class) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api', [ 'authenticatedAs' => 1, @@ -166,8 +158,6 @@ class ApiControllerTest extends TestCase }) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions/1', [ 'authenticatedAs' => 1, @@ -197,8 +187,6 @@ class ApiControllerTest extends TestCase }) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions/1', [ 'authenticatedAs' => 1, @@ -224,8 +212,6 @@ class ApiControllerTest extends TestCase }) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions/1', [ 'authenticatedAs' => 1, @@ -255,8 +241,6 @@ class ApiControllerTest extends TestCase }) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions/1', [ 'authenticatedAs' => 1, @@ -273,8 +257,6 @@ class ApiControllerTest extends TestCase */ public function custom_serializer_doesnt_work_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions/1', [ 'authenticatedAs' => 1, @@ -296,8 +278,6 @@ class ApiControllerTest extends TestCase ->setSerializer(CustomDiscussionSerializer::class) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions/1', [ 'authenticatedAs' => 1, @@ -318,8 +298,6 @@ class ApiControllerTest extends TestCase (new Extend\ApiController(ShowPostController::class)) ->setSerializer(CustomPostSerializer::class, CustomApiControllerInvokableClass::class) ); - - $this->prepDb(); $this->prepareDatabase([ 'posts' => [ ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

foo bar

'], @@ -349,8 +327,6 @@ class ApiControllerTest extends TestCase }) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, @@ -367,8 +343,6 @@ class ApiControllerTest extends TestCase */ public function custom_relationship_not_included_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, @@ -395,8 +369,6 @@ class ApiControllerTest extends TestCase ->addInclude('customApiControllerRelation') ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, @@ -422,8 +394,6 @@ class ApiControllerTest extends TestCase ->addOptionalInclude('customApiControllerRelation2') ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, @@ -442,8 +412,6 @@ class ApiControllerTest extends TestCase */ public function custom_relationship_included_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, @@ -465,8 +433,6 @@ class ApiControllerTest extends TestCase ->removeInclude('groups') ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, @@ -493,8 +459,6 @@ class ApiControllerTest extends TestCase ->removeOptionalInclude('customApiControllerRelation2') ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, @@ -511,8 +475,6 @@ class ApiControllerTest extends TestCase */ public function custom_limit_doesnt_work_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, @@ -534,8 +496,6 @@ class ApiControllerTest extends TestCase ->setLimit(1) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, @@ -557,8 +517,6 @@ class ApiControllerTest extends TestCase ->setMaxLimit(1) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, @@ -577,8 +535,6 @@ class ApiControllerTest extends TestCase */ public function custom_sort_field_doesnt_exist_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, @@ -602,8 +558,6 @@ class ApiControllerTest extends TestCase }) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, @@ -625,8 +579,6 @@ class ApiControllerTest extends TestCase ->addSortField('userId') ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, @@ -646,8 +598,6 @@ class ApiControllerTest extends TestCase */ public function custom_sort_field_exists_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, @@ -669,8 +619,6 @@ class ApiControllerTest extends TestCase ->removeSortField('createdAt') ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, @@ -693,8 +641,6 @@ class ApiControllerTest extends TestCase ->setSort(['userId' => 'desc']) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/discussions', [ 'authenticatedAs' => 1, diff --git a/framework/core/tests/integration/extenders/ApiSerializerTest.php b/framework/core/tests/integration/extenders/ApiSerializerTest.php index 2bee2bafd..8548d453c 100644 --- a/framework/core/tests/integration/extenders/ApiSerializerTest.php +++ b/framework/core/tests/integration/extenders/ApiSerializerTest.php @@ -27,11 +27,15 @@ class ApiSerializerTest extends TestCase { use RetrievesAuthorizedUsers; - protected function prepDb() + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser() ], 'discussions' => [ @@ -328,8 +332,6 @@ class ApiSerializerTest extends TestCase ->hasMany('customSerializerRelation', DiscussionSerializer::class) ); - $this->prepDb(); - $request = $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, ]); @@ -355,8 +357,6 @@ class ApiSerializerTest extends TestCase ->hasOne('customSerializerRelation', DiscussionSerializer::class) ); - $this->prepDb(); - $request = $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, ]); @@ -384,8 +384,6 @@ class ApiSerializerTest extends TestCase }) ); - $this->prepDb(); - $request = $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, ]); @@ -411,8 +409,6 @@ class ApiSerializerTest extends TestCase ->relationship('customSerializerRelation', CustomRelationshipInvokableClass::class) ); - $this->prepDb(); - $request = $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, ]); @@ -438,8 +434,6 @@ class ApiSerializerTest extends TestCase ->hasMany('anotherCustomRelation', DiscussionSerializer::class) ); - $this->prepDb(); - $request = $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, ]); @@ -471,8 +465,6 @@ class ApiSerializerTest extends TestCase }) ); - $this->prepDb(); - $request = $this->request('GET', '/api/users/2', [ 'authenticatedAs' => 1, ]); diff --git a/framework/core/tests/integration/extenders/CsrfTest.php b/framework/core/tests/integration/extenders/CsrfTest.php index 3219ab48e..0b6f8cec0 100644 --- a/framework/core/tests/integration/extenders/CsrfTest.php +++ b/framework/core/tests/integration/extenders/CsrfTest.php @@ -21,20 +21,11 @@ class CsrfTest extends TestCase 'email' => 'test@machine.local', ]; - protected function prepDb() - { - $this->prepareDatabase([ - 'users' => [], - ]); - } - /** * @test */ public function create_user_post_needs_csrf_token_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('POST', '/api/users', [ 'json' => [ @@ -59,8 +50,6 @@ class CsrfTest extends TestCase ->exemptPath('/api/users') ); - $this->prepDb(); - $response = $this->send( $this->request('POST', '/api/users', [ 'json' => [ @@ -90,8 +79,6 @@ class CsrfTest extends TestCase ->exemptRoute('users.create') ); - $this->prepDb(); - $response = $this->send( $this->request('POST', '/api/users', [ 'json' => [ @@ -122,8 +109,6 @@ class CsrfTest extends TestCase ->exemptPath('/api/fake/*/up') ); - $this->prepDb(); - $response = $this->send( $this->request('POST', '/api/fake/route/i/made/up') ); diff --git a/framework/core/tests/integration/extenders/EventTest.php b/framework/core/tests/integration/extenders/EventTest.php index de32cd1ee..0aab2ce06 100644 --- a/framework/core/tests/integration/extenders/EventTest.php +++ b/framework/core/tests/integration/extenders/EventTest.php @@ -24,15 +24,6 @@ class EventTest extends TestCase protected function buildGroup() { - $this->prepareDatabase([ - 'groups' => [ - $this->adminGroup(), - ], - 'users' => [ - $this->adminUser(), - ], - ]); - $bus = $this->app()->getContainer()->make(Dispatcher::class); return $bus->dispatch( diff --git a/framework/core/tests/integration/extenders/MailTest.php b/framework/core/tests/integration/extenders/MailTest.php index 7a8a1dc52..a62773b3f 100644 --- a/framework/core/tests/integration/extenders/MailTest.php +++ b/framework/core/tests/integration/extenders/MailTest.php @@ -23,28 +23,11 @@ class MailTest extends TestCase { use RetrievesAuthorizedUsers; - protected function prepDb() - { - $this->prepareDatabase([ - 'users' => [ - $this->adminUser(), - ], - 'groups' => [ - $this->adminGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], - ]); - } - /** * @test */ public function drivers_are_unchanged_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/mail/settings', [ 'authenticatedAs' => 1, @@ -76,8 +59,6 @@ class MailTest extends TestCase ->driver('custom', CustomDriver::class) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/mail/settings', [ 'authenticatedAs' => 1, @@ -100,8 +81,6 @@ class MailTest extends TestCase ->driver('smtp', CustomDriver::class) ); - $this->prepDb(); - $response = $this->send( $this->request('GET', '/api/mail/settings', [ 'authenticatedAs' => 1, diff --git a/framework/core/tests/integration/extenders/ModelTest.php b/framework/core/tests/integration/extenders/ModelTest.php index 6f7959e13..f40c71d90 100644 --- a/framework/core/tests/integration/extenders/ModelTest.php +++ b/framework/core/tests/integration/extenders/ModelTest.php @@ -25,23 +25,23 @@ class ModelTest extends TestCase { use RetrievesAuthorizedUsers; - protected function prepDb() + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), ], - 'discussions' => [] ]); } protected function prepPostsHierarchy() { $this->prepareDatabase([ - 'users' => [ - $this->normalUser(), - ], 'discussions' => [ ['id' => 1, 'title' => 'Discussion with post', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 1, 'is_private' => 0], ], @@ -56,7 +56,7 @@ class ModelTest extends TestCase */ public function custom_relationship_does_not_exist_by_default() { - $this->prepDB(); + $this->app(); $user = User::find(1); @@ -74,7 +74,7 @@ class ModelTest extends TestCase ->hasOne('customRelation', Discussion::class, 'user_id') ); - $this->prepDB(); + $this->app(); $user = User::find(1); @@ -91,7 +91,7 @@ class ModelTest extends TestCase ->hasMany('customRelation', Discussion::class, 'user_id') ); - $this->prepDB(); + $this->app(); $user = User::find(1); @@ -108,7 +108,7 @@ class ModelTest extends TestCase ->belongsTo('customRelation', Discussion::class, 'user_id') ); - $this->prepDB(); + $this->app(); $user = User::find(1); @@ -127,7 +127,7 @@ class ModelTest extends TestCase }) ); - $this->prepDB(); + $this->app(); $user = User::find(1); @@ -144,7 +144,7 @@ class ModelTest extends TestCase ->relationship('customRelation', CustomRelationClass::class) ); - $this->prepDB(); + $this->app(); $user = User::find(1); @@ -163,13 +163,14 @@ class ModelTest extends TestCase }) ); - $this->prepDB(); $this->prepareDatabase([ 'discussions' => [ ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1] ] ]); + $this->app(); + $user = User::find(1); $this->assertNotEquals([], $user->customRelation()->get()->toArray()); @@ -188,6 +189,8 @@ class ModelTest extends TestCase $this->prepPostsHierarchy(); + $this->app(); + $post = CommentPost::find(1); $this->assertInstanceOf(Discussion::class, $post->ancestor); @@ -208,6 +211,8 @@ class ModelTest extends TestCase $this->prepPostsHierarchy(); + $this->app(); + $post = DiscussionRenamedPost::find(1); $this->assertInstanceOf(Discussion::class, $post->ancestor); @@ -227,6 +232,9 @@ class ModelTest extends TestCase ); $this->prepPostsHierarchy(); + + $this->app(); + $post = DiscussionRenamedPost::find(1); $this->assertInstanceOf(User::class, $post->ancestor); @@ -245,12 +253,7 @@ class ModelTest extends TestCase }) ); - $this->prepDB(); - $this->prepareDatabase([ - 'groups' => [ - $this->adminGroup() - ] - ]); + $this->app(); $group = Group::find(1); diff --git a/framework/core/tests/integration/extenders/ModelUrlTest.php b/framework/core/tests/integration/extenders/ModelUrlTest.php index e5eea08ad..a5b095ce8 100644 --- a/framework/core/tests/integration/extenders/ModelUrlTest.php +++ b/framework/core/tests/integration/extenders/ModelUrlTest.php @@ -15,18 +15,24 @@ use Flarum\Http\SlugDriverInterface; use Flarum\Http\SlugManager; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; +use Flarum\Tests\integration\UsesSettings; use Flarum\User\User; class ModelUrlTest extends TestCase { use RetrievesAuthorizedUsers; + use UsesSettings; - protected function prepDb() + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $userClass = User::class; $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), ], 'settings' => [ @@ -40,7 +46,7 @@ class ModelUrlTest extends TestCase */ public function uses_default_driver_by_default() { - $this->prepDb(); + $this->purgeSettingsCache(); $slugManager = $this->app()->getContainer()->make(SlugManager::class); @@ -57,7 +63,7 @@ class ModelUrlTest extends TestCase { $this->extend((new Extend\ModelUrl(User::class))->addSlugDriver('testDriver', TestSlugDriver::class)); - $this->prepDb(); + $this->purgeSettingsCache(); $slugManager = $this->app()->getContainer()->make(SlugManager::class); diff --git a/framework/core/tests/integration/extenders/ModelVisibilityTest.php b/framework/core/tests/integration/extenders/ModelVisibilityTest.php index 0121e6e8a..25322167e 100644 --- a/framework/core/tests/integration/extenders/ModelVisibilityTest.php +++ b/framework/core/tests/integration/extenders/ModelVisibilityTest.php @@ -23,8 +23,13 @@ class ModelVisibilityTest extends TestCase { use RetrievesAuthorizedUsers; - protected function prepDb() + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $this->prepareDatabase([ 'discussions' => [ ['id' => 1, 'title' => 'Empty discussion', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => null, 'comment_count' => 0, 'is_private' => 0], @@ -37,17 +42,6 @@ class ModelVisibilityTest extends TestCase ], 'users' => [ $this->normalUser(), - ], - 'groups' => [ - $this->guestGroup(), - $this->memberGroup(), - ], - 'group_user' => [ - ['user_id' => 2, 'group_id' => 3], - ], - 'group_permission' => [ - ['permission' => 'viewDiscussions', 'group_id' => 2], - ['permission' => 'viewDiscussions', 'group_id' => 3], ] ]); } @@ -57,7 +51,7 @@ class ModelVisibilityTest extends TestCase */ public function user_can_see_posts_by_default() { - $this->prepDb(); + $this->app(); $actor = User::find(2); @@ -78,7 +72,7 @@ class ModelVisibilityTest extends TestCase }, 'view') ); - $this->prepDb(); + $this->app(); $actor = User::find(2); @@ -99,7 +93,7 @@ class ModelVisibilityTest extends TestCase }, 'view') ); - $this->prepDb(); + $this->app(); $actor = User::find(2); @@ -124,7 +118,7 @@ class ModelVisibilityTest extends TestCase }, 'view') ); - $this->prepDb(); + $this->app(); $actor = User::find(2); @@ -149,7 +143,7 @@ class ModelVisibilityTest extends TestCase }, 'viewPrivate') ); - $this->prepDb(); + $this->app(); $actor = User::find(2); @@ -178,7 +172,7 @@ class ModelVisibilityTest extends TestCase }) ); - $this->prepDb(); + $this->app(); $actor = User::find(2); diff --git a/framework/core/tests/integration/extenders/PolicyTest.php b/framework/core/tests/integration/extenders/PolicyTest.php index d45c2cdc5..984885277 100644 --- a/framework/core/tests/integration/extenders/PolicyTest.php +++ b/framework/core/tests/integration/extenders/PolicyTest.php @@ -28,11 +28,15 @@ class PolicyTest extends TestCase // Request body to hide discussions sent in tests. protected $hideQuery = ['authenticatedAs' => 2, 'json' => ['data' => ['attributes' => ['isHidden' => true]]]]; - private function prepDb() + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), ], 'discussions' => [ @@ -49,8 +53,6 @@ class PolicyTest extends TestCase */ public function unrelated_user_cant_hide_discussion_by_default() { - $this->prepDb(); - $response = $this->send( $this->request('PATCH', '/api/discussions/1', $this->hideQuery) ); @@ -68,8 +70,6 @@ class PolicyTest extends TestCase ->modelPolicy(Discussion::class, CustomPolicy::class) ); - $this->prepDb(); - $response = $this->send( $this->request('PATCH', '/api/discussions/1', $this->hideQuery) ); @@ -88,8 +88,6 @@ class PolicyTest extends TestCase ->modelPolicy(Discussion::class, CustomPolicy::class) ); - $this->prepDb(); - $response = $this->send( $this->request('PATCH', '/api/discussions/1', $this->hideQuery) ); @@ -109,8 +107,6 @@ class PolicyTest extends TestCase ->modelPolicy(Discussion::class, CustomPolicy::class) ); - $this->prepDb(); - $response = $this->send( $this->request('PATCH', '/api/discussions/1', $this->hideQuery) ); @@ -131,8 +127,6 @@ class PolicyTest extends TestCase ->modelPolicy(Discussion::class, ForceAllowHidePolicy::class) ); - $this->prepDb(); - $response = $this->send( $this->request('PATCH', '/api/discussions/1', $this->hideQuery) ); @@ -143,30 +137,30 @@ class PolicyTest extends TestCase /** * @test */ - public function regular_user_cant_start_discussions_by_default() + public function regular_user_can_start_discussions_by_default() { - $this->prepDb(); + $this->app(); $user = User::find(2); - $this->assertEquals(false, $user->can('startDiscussion')); + $this->assertEquals(true, $user->can('startDiscussion')); } /** * @test */ - public function regular_user_can_start_discussions_if_granted_by_global_policy() + public function regular_user_cant_start_discussions_if_blocked_by_global_policy() { $this->extend( (new Extend\Policy) ->globalPolicy(GlobalStartDiscussionPolicy::class) ); - $this->prepDb(); + $this->app(); $user = User::find(2); - $this->assertEquals(true, $user->can('startDiscussion')); + $this->assertEquals(false, $user->can('startDiscussion')); } /** @@ -179,11 +173,11 @@ class PolicyTest extends TestCase ->globalPolicy(GlobalStartDiscussionPolicy::class) ); - $this->prepDb(); + $this->app(); $user = User::find(2); - $this->assertEquals(false, $user->can('startDiscussion', Discussion::find(1))); + $this->assertEquals(true, $user->can('startDiscussion', Discussion::find(1))); } /** @@ -191,7 +185,7 @@ class PolicyTest extends TestCase */ public function unrelated_user_cant_hide_post_by_default() { - $this->prepDb(); + $this->app(); $user = User::find(2); @@ -206,7 +200,7 @@ class PolicyTest extends TestCase $this->extend( (new Extend\Policy)->modelPolicy(CommentPost::class, CommentPostChildClassPolicy::class) ); - $this->prepDb(); + $this->app(); $user = User::find(2); @@ -222,7 +216,7 @@ class PolicyTest extends TestCase (new Extend\Policy)->modelPolicy(Post::class, PostParentClassPolicy::class), (new Extend\Policy)->modelPolicy(CommentPost::class, CommentPostChildClassPolicy::class) ); - $this->prepDb(); + $this->app(); $user = User::find(2); @@ -266,7 +260,7 @@ class GlobalStartDiscussionPolicy extends AbstractPolicy { protected function startDiscussion(User $user) { - return $this->allow(); + return $this->deny(); } } diff --git a/framework/core/tests/integration/extenders/SettingsTest.php b/framework/core/tests/integration/extenders/SettingsTest.php index 6a8f0b142..84433de25 100644 --- a/framework/core/tests/integration/extenders/SettingsTest.php +++ b/framework/core/tests/integration/extenders/SettingsTest.php @@ -12,16 +12,22 @@ namespace Flarum\Tests\integration\extenders; use Flarum\Extend; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; +use Flarum\Tests\integration\UsesSettings; class SettingsTest extends TestCase { use RetrievesAuthorizedUsers; + use UsesSettings; - protected function prepDb() + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser() ], 'settings' => [ @@ -36,7 +42,7 @@ class SettingsTest extends TestCase */ public function custom_setting_isnt_serialized_by_default() { - $this->prepDb(); + $this->purgeSettingsCache(); $response = $this->send( $this->request('GET', '/api', [ @@ -59,7 +65,7 @@ class SettingsTest extends TestCase ->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting') ); - $this->prepDb(); + $this->purgeSettingsCache(); $response = $this->send( $this->request('GET', '/api', [ @@ -85,7 +91,7 @@ class SettingsTest extends TestCase }) ); - $this->prepDb(); + $this->purgeSettingsCache(); $response = $this->send( $this->request('GET', '/api', [ @@ -109,7 +115,7 @@ class SettingsTest extends TestCase ->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2', CustomInvokableClass::class) ); - $this->prepDb(); + $this->purgeSettingsCache(); $response = $this->send( $this->request('GET', '/api', [ @@ -133,7 +139,7 @@ class SettingsTest extends TestCase ->serializeToForum('customPrefix.noCustomSetting', 'custom-prefix.no_custom_setting', null, 'customDefault') ); - $this->prepDb(); + $this->purgeSettingsCache(); $response = $this->send( $this->request('GET', '/api', [ @@ -159,7 +165,7 @@ class SettingsTest extends TestCase }, 'customDefault') ); - $this->prepDb(); + $this->purgeSettingsCache(); $response = $this->send( $this->request('GET', '/api', [ diff --git a/framework/core/tests/integration/extenders/ThrottleApiTest.php b/framework/core/tests/integration/extenders/ThrottleApiTest.php index 79400cf40..b2e88798b 100644 --- a/framework/core/tests/integration/extenders/ThrottleApiTest.php +++ b/framework/core/tests/integration/extenders/ThrottleApiTest.php @@ -17,20 +17,16 @@ class ThrottleApiTest extends TestCase { use RetrievesAuthorizedUsers; - protected function prepDb(): void + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $this->prepareDatabase([ 'users' => [ $this->normalUser(), - ], - 'groups' => [ - $this->memberGroup(), - ], - 'group_user' => [ - ['user_id' => 2, 'group_id' => 3], - ], - 'group_permission' => [ - ['permission' => 'viewDiscussions', 'group_id' => 3], ] ]); } @@ -40,8 +36,6 @@ class ThrottleApiTest extends TestCase */ public function list_discussions_not_restricted_by_default() { - $this->prepDb(); - $response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2])); $this->assertEquals(200, $response->getStatusCode()); @@ -58,8 +52,6 @@ class ThrottleApiTest extends TestCase } })); - $this->prepDb(); - $response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2])); $this->assertEquals(429, $response->getStatusCode()); @@ -83,10 +75,6 @@ class ThrottleApiTest extends TestCase }) ); - $this->prepDb(); - - $this->prepDb(); - $response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2])); $this->assertEquals(200, $response->getStatusCode()); diff --git a/framework/core/tests/integration/extenders/UserTest.php b/framework/core/tests/integration/extenders/UserTest.php index a4108509a..f8b22d5bc 100644 --- a/framework/core/tests/integration/extenders/UserTest.php +++ b/framework/core/tests/integration/extenders/UserTest.php @@ -12,6 +12,7 @@ namespace Flarum\Tests\integration\extenders; use Flarum\Extend; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; +use Flarum\Tests\integration\UsesSettings; use Flarum\User\DisplayName\DriverInterface; use Flarum\User\User; use Illuminate\Support\Arr; @@ -19,26 +20,36 @@ use Illuminate\Support\Arr; class UserTest extends TestCase { use RetrievesAuthorizedUsers; + use UsesSettings; - protected function prepDb() + /** + * @inheritDoc + */ + protected function setUp(): void { + parent::setUp(); + $this->prepareDatabase([ 'users' => [ - $this->adminUser(), $this->normalUser(), ], - 'group_permission' => [ - ['permission' => 'viewUserList', 'group_id' => 3] - ], 'settings' => [ ['key' => 'display_name_driver', 'value' => 'custom'], - ], - 'group_permission' => [ - ['permission' => 'viewUserList', 'group_id' => 3], ] ]); } + /** + * Purge the settings cache and reset the new display name driver. + */ + protected function recalculateDisplayNameDriver() + { + $this->purgeSettingsCache(); + $container = $this->app()->getContainer(); + $container->forgetInstance('flarum.user.display_name.driver'); + User::setDisplayNameDriver($container->make('flarum.user.display_name.driver')); + } + protected function registerTestPreference() { $this->extend( @@ -52,7 +63,8 @@ class UserTest extends TestCase */ public function username_display_name_driver_used_by_default() { - $this->prepDb(); + $this->app(); + $this->recalculateDisplayNameDriver(); $user = User::find(1); @@ -69,7 +81,8 @@ class UserTest extends TestCase ->displayNameDriver('custom', CustomDisplayNameDriver::class) ); - $this->prepDb(); + $this->app(); + $this->recalculateDisplayNameDriver(); $user = User::find(1); @@ -81,7 +94,8 @@ class UserTest extends TestCase */ public function user_has_permissions_for_expected_groups_if_no_processors_added() { - $this->prepDb(); + $this->app(); + $user = User::find(2); $this->assertContains('viewUserList', $user->getPermissions()); @@ -98,7 +112,8 @@ class UserTest extends TestCase }); })); - $this->prepDb(); + $this->app(); + $user = User::find(2); $this->assertNotContains('viewUserList', $user->getPermissions()); @@ -111,7 +126,8 @@ class UserTest extends TestCase { $this->extend((new Extend\User)->permissionGroups(CustomGroupProcessorClass::class)); - $this->prepDb(); + $this->app(); + $user = User::find(2); $this->assertNotContains('viewUserList', $user->getPermissions()); @@ -123,7 +139,8 @@ class UserTest extends TestCase public function can_add_user_preference() { $this->registerTestPreference(); - $this->prepDb(); + + $this->app(); /** @var User $user */ $user = User::find(2); @@ -136,7 +153,8 @@ class UserTest extends TestCase public function can_store_user_preference() { $this->registerTestPreference(); - $this->prepDb(); + + $this->app(); /** @var User $user */ $user = User::find(2); @@ -152,7 +170,8 @@ class UserTest extends TestCase public function storing_user_preference_modified_by_transformer() { $this->registerTestPreference(); - $this->prepDb(); + + $this->app(); /** @var User $user */ $user = User::find(2); diff --git a/framework/core/tests/integration/setup.php b/framework/core/tests/integration/setup.php index 2a18ca4a6..95739eaac 100644 --- a/framework/core/tests/integration/setup.php +++ b/framework/core/tests/integration/setup.php @@ -56,8 +56,8 @@ $pipeline = $installation ) ->adminUser(new AdminUser( 'admin', - 'secret', - 'admin@flarum.email' + 'password', + 'admin@machine.local' )) ->settings(['mail_driver' => 'log']) ->build(); diff --git a/framework/core/tests/unit/Foundation/ContainerUtilTest.php b/framework/core/tests/unit/Foundation/ContainerUtilTest.php index f7b8a2992..d90764f3b 100644 --- a/framework/core/tests/unit/Foundation/ContainerUtilTest.php +++ b/framework/core/tests/unit/Foundation/ContainerUtilTest.php @@ -17,6 +17,9 @@ class ContainerUtilTest extends TestCase { private $container; + /** + * @inheritDoc + */ protected function setUp() { parent::setUp(); diff --git a/framework/core/tests/unit/Foundation/ErrorHandling/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php b/framework/core/tests/unit/Foundation/ErrorHandling/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php index 0bf6d4441..b3c3273c2 100644 --- a/framework/core/tests/unit/Foundation/ErrorHandling/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php +++ b/framework/core/tests/unit/Foundation/ErrorHandling/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php @@ -20,6 +20,9 @@ class IlluminateValidationExceptionHandlerTest extends TestCase { private $handler; + /** + * @inheritDoc + */ protected function setUp(): void { $this->handler = new IlluminateValidationExceptionHandler; diff --git a/framework/core/tests/unit/Foundation/ErrorHandling/ExceptionHandler/ValidationExceptionHandlerTest.php b/framework/core/tests/unit/Foundation/ErrorHandling/ExceptionHandler/ValidationExceptionHandlerTest.php index edb4c0892..aa8df566f 100644 --- a/framework/core/tests/unit/Foundation/ErrorHandling/ExceptionHandler/ValidationExceptionHandlerTest.php +++ b/framework/core/tests/unit/Foundation/ErrorHandling/ExceptionHandler/ValidationExceptionHandlerTest.php @@ -17,6 +17,9 @@ class ValidationExceptionHandlerTest extends TestCase { private $handler; + /** + * @inheritDoc + */ protected function setUp(): void { $this->handler = new ValidationExceptionHandler; diff --git a/framework/core/tests/unit/Settings/DatabaseSettingsRepositoryTest.php b/framework/core/tests/unit/Settings/DatabaseSettingsRepositoryTest.php index 56de7ffd8..0f3b88d78 100644 --- a/framework/core/tests/unit/Settings/DatabaseSettingsRepositoryTest.php +++ b/framework/core/tests/unit/Settings/DatabaseSettingsRepositoryTest.php @@ -19,6 +19,9 @@ class DatabaseSettingsRepositoryTest extends TestCase private $connection; private $repository; + /** + * @inheritDoc + */ protected function setUp(): void { $this->connection = m::mock(ConnectionInterface::class); diff --git a/framework/core/tests/unit/Settings/MemoryCacheSettingsRepositoryTest.php b/framework/core/tests/unit/Settings/MemoryCacheSettingsRepositoryTest.php index 4011e4d6b..a7d6f1d09 100644 --- a/framework/core/tests/unit/Settings/MemoryCacheSettingsRepositoryTest.php +++ b/framework/core/tests/unit/Settings/MemoryCacheSettingsRepositoryTest.php @@ -19,6 +19,9 @@ class MemoryCacheSettingsRepositoryTest extends TestCase private $baseRepository; private $repository; + /** + * @inheritDoc + */ protected function setUp(): void { $this->baseRepository = m::mock(SettingsRepositoryInterface::class); diff --git a/framework/core/tests/unit/User/AbstractPolicyTest.php b/framework/core/tests/unit/User/AbstractPolicyTest.php index d2ba2efac..0a32eed11 100644 --- a/framework/core/tests/unit/User/AbstractPolicyTest.php +++ b/framework/core/tests/unit/User/AbstractPolicyTest.php @@ -21,6 +21,9 @@ class AbstractPolicyTest extends TestCase private $policy; private $dispatcher; + /** + * @inheritDoc + */ protected function setUp(): void { $this->policy = m::mock(CustomUserPolicy::class)->makePartial(); diff --git a/framework/core/tests/unit/User/AvatarUploaderTest.php b/framework/core/tests/unit/User/AvatarUploaderTest.php index 392bbe70d..a85e3ed9a 100644 --- a/framework/core/tests/unit/User/AvatarUploaderTest.php +++ b/framework/core/tests/unit/User/AvatarUploaderTest.php @@ -24,6 +24,9 @@ class AvatarUploaderTest extends TestCase private $filesystem; private $uploader; + /** + * @inheritDoc + */ protected function setUp(): void { $this->dispatcher = m::mock(Dispatcher::class);