Merge pull request #2304 from flarum/fl/tests-in-transaction

Run Backend Tests in Transactions
This commit is contained in:
Alexander Skvortsov 2021-01-12 21:26:59 -05:00 committed by GitHub
commit c1778d5dd6
41 changed files with 476 additions and 600 deletions

View File

@ -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

View File

@ -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 [

View File

@ -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(

View File

@ -0,0 +1,25 @@
<?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;
use Flarum\Settings\SettingsRepositoryInterface;
trait UsesSettings
{
/**
* Removes the settings respository instance from the IoC container.
*
* This allows test cases that add/modify settings to refresh the in-memory settings cache.
*/
protected function purgeSettingsCache()
{
$this->app()->getContainer()->forgetInstance(SettingsRepositoryInterface::class);
}
}

View File

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

View File

@ -17,6 +17,9 @@ class WithTokenTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();

View File

@ -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'],
],

View File

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

View File

@ -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' => '<t><p>foo bar</p></t>'],
],
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}

View File

@ -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' => '<t><p>not in text</p></t>'],
['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>lightsail in text</p></t>'],
]);
$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' => '<t><p>not in text</p></t>'],
['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>lightsail in text</p></t>'],
]);
$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']);
}
}

View File

@ -0,0 +1,130 @@
<?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\api\discussions;
use Carbon\Carbon;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
class ListTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->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' => '<t><p>not in text</p></t>'],
['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>lightsail in text</p></t>'],
]);
// 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']);
}
}

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -16,6 +16,9 @@ class ListTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();

View File

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

View File

@ -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'],
],

View File

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

View File

@ -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', [

View File

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

View File

@ -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' => '<t><p>foo bar</p></t>'],
@ -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,

View File

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

View File

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

View File

@ -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(

View File

@ -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,

View File

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

View File

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

View File

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

View File

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

View File

@ -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', [

View File

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

View File

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

View File

@ -56,8 +56,8 @@ $pipeline = $installation
)
->adminUser(new AdminUser(
'admin',
'secret',
'admin@flarum.email'
'password',
'admin@machine.local'
))
->settings(['mail_driver' => 'log'])
->build();

View File

@ -17,6 +17,9 @@ class ContainerUtilTest extends TestCase
{
private $container;
/**
* @inheritDoc
*/
protected function setUp()
{
parent::setUp();

View File

@ -20,6 +20,9 @@ class IlluminateValidationExceptionHandlerTest extends TestCase
{
private $handler;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->handler = new IlluminateValidationExceptionHandler;

View File

@ -17,6 +17,9 @@ class ValidationExceptionHandlerTest extends TestCase
{
private $handler;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->handler = new ValidationExceptionHandler;

View File

@ -19,6 +19,9 @@ class DatabaseSettingsRepositoryTest extends TestCase
private $connection;
private $repository;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->connection = m::mock(ConnectionInterface::class);

View File

@ -19,6 +19,9 @@ class MemoryCacheSettingsRepositoryTest extends TestCase
private $baseRepository;
private $repository;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->baseRepository = m::mock(SettingsRepositoryInterface::class);

View File

@ -21,6 +21,9 @@ class AbstractPolicyTest extends TestCase
private $policy;
private $dispatcher;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->policy = m::mock(CustomUserPolicy::class)->makePartial();

View File

@ -24,6 +24,9 @@ class AvatarUploaderTest extends TestCase
private $filesystem;
private $uploader;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->dispatcher = m::mock(Dispatcher::class);