mirror of
https://github.com/flarum/framework.git
synced 2025-02-17 02:02:47 +08:00
Improve discussions API tests
This commit is contained in:
parent
0ad1b9784f
commit
106c51071b
|
@ -1,6 +1,8 @@
|
|||
<?php
|
||||
use \ApiTester;
|
||||
|
||||
use Laracasts\TestDummy\Factory;
|
||||
|
||||
class DiscussionsResourceCest {
|
||||
|
||||
protected $endpoint = '/api/discussions';
|
||||
|
@ -9,24 +11,37 @@ class DiscussionsResourceCest {
|
|||
{
|
||||
$I->wantTo('get discussions via API');
|
||||
|
||||
$id = $I->haveRecord('discussions', ['title' => 'Game of Thrones', 'last_time' => date('c')]);
|
||||
$id2 = $I->haveRecord('discussions', ['title' => 'Lord of the Rings', 'last_time' => date('c')]);
|
||||
$discussions = Factory::times(2)->create('Flarum\Core\Discussions\Discussion');
|
||||
|
||||
$I->sendGET($this->endpoint);
|
||||
$I->seeResponseCodeIs(200);
|
||||
$I->seeResponseIsJson();
|
||||
|
||||
$I->expect('both items are in response');
|
||||
$I->seeResponseContainsJson(['id' => (string) $id, 'title' => 'Game of Thrones']);
|
||||
$I->seeResponseContainsJson(['id' => (string) $id2, 'title' => 'Lord of the Rings']);
|
||||
$I->expect('there are two discussions in the response');
|
||||
$I->assertEquals(2, count($I->grabDataFromJsonResponse('discussions')));
|
||||
|
||||
$I->expect('both items are in root discussions array');
|
||||
$I->seeResponseContainsJson(['discussions' => [['id' => (string) $id], ['id' => (string) $id2]]]);
|
||||
$I->expect('they are the discussions we created');
|
||||
$I->seeResponseContainsJson(['id' => (string) $discussions[0]->id, 'title' => $discussions[0]->title]);
|
||||
$I->seeResponseContainsJson(['id' => (string) $discussions[1]->id, 'title' => $discussions[1]->title]);
|
||||
}
|
||||
|
||||
public function showDiscussion(ApiTester $I)
|
||||
{
|
||||
$I->wantTo('show a single discussion via API');
|
||||
|
||||
$discussion = Factory::create('Flarum\Core\Discussions\Discussion');
|
||||
|
||||
$I->sendGET($this->endpoint.'/'.$discussion->id);
|
||||
$I->seeResponseCodeIs(200);
|
||||
$I->seeResponseIsJson();
|
||||
|
||||
$I->seeResponseContainsJson(['discussions' => ['id' => (string) $discussion->id, 'title' => $discussion->title]]);
|
||||
}
|
||||
|
||||
public function createDiscussion(ApiTester $I)
|
||||
{
|
||||
$I->wantTo('create a discussion via API');
|
||||
|
||||
$I->haveHttpHeader('Authorization', 'Token 123456');
|
||||
|
||||
$I->sendPOST($this->endpoint, ['discussions' => ['title' => 'foo', 'content' => 'bar']]);
|
||||
|
@ -38,4 +53,36 @@ class DiscussionsResourceCest {
|
|||
$id = $I->grabDataFromJsonResponse('discussions.id');
|
||||
$I->seeRecord('discussions', ['id' => $id, 'title' => 'foo']);
|
||||
}
|
||||
|
||||
public function updateDiscussion(ApiTester $I)
|
||||
{
|
||||
$I->wantTo('update a discussion via API');
|
||||
|
||||
$I->haveHttpHeader('Authorization', 'Token 123456');
|
||||
|
||||
$discussion = Factory::create('Flarum\Core\Discussions\Discussion');
|
||||
|
||||
$I->sendPUT($this->endpoint.'/'.$discussion->id, ['discussions' => ['title' => 'foo']]);
|
||||
$I->seeResponseCodeIs(200);
|
||||
$I->seeResponseIsJson();
|
||||
$I->seeResponseContainsJson(['title' => 'foo']);
|
||||
|
||||
$id = $I->grabDataFromJsonResponse('discussions.id');
|
||||
$I->seeRecord('discussions', ['id' => $id, 'title' => 'foo']);
|
||||
}
|
||||
|
||||
public function deleteDiscussion(ApiTester $I)
|
||||
{
|
||||
$I->wantTo('delete a discussion via API');
|
||||
|
||||
$I->haveHttpHeader('Authorization', 'Token 123456');
|
||||
|
||||
$discussion = Factory::create('Flarum\Core\Discussions\Discussion');
|
||||
|
||||
$I->sendDELETE($this->endpoint.'/'.$discussion->id);
|
||||
$I->seeResponseCodeIs(204);
|
||||
$I->seeResponseEquals('');
|
||||
|
||||
$I->dontSeeRecord('discussions', ['id' => $discussion->id]);
|
||||
}
|
||||
}
|
|
@ -1,2 +1,33 @@
|
|||
<?php
|
||||
// Here you can initialize variables that will be available to your tests
|
||||
|
||||
// Set up the default permissions.
|
||||
// @todo this should be moved to an installation migration-like object which should be called upon
|
||||
$permissions = [
|
||||
|
||||
// Guests can view the forum
|
||||
['group.2' , 'forum' , 'view'],
|
||||
|
||||
// Members can create and reply to discussions + edit their own stuff
|
||||
['group.3' , 'forum' , 'startDiscussion'],
|
||||
['group.3' , 'discussion' , 'editOwn'],
|
||||
['group.3' , 'discussion' , 'reply'],
|
||||
['group.3' , 'post' , 'editOwn'],
|
||||
|
||||
// Moderators can edit + delete stuff and suspend users
|
||||
['group.4' , 'discussion' , 'delete'],
|
||||
['group.4' , 'discussion' , 'edit'],
|
||||
['group.4' , 'post' , 'delete'],
|
||||
['group.4' , 'post' , 'edit'],
|
||||
['group.4' , 'user' , 'suspend'],
|
||||
|
||||
];
|
||||
foreach ($permissions as &$permission) {
|
||||
$permission = [
|
||||
'grantee' => $permission[0],
|
||||
'entity' => $permission[1],
|
||||
'permission' => $permission[2]
|
||||
];
|
||||
}
|
||||
\DB::table('permissions')->truncate();
|
||||
\DB::table('permissions')->insert($permissions);
|
Loading…
Reference in New Issue
Block a user