mirror of
https://github.com/flarum/framework.git
synced 2024-11-27 02:53:37 +08:00
Convert more controller tests to request tests
This commit is contained in:
parent
a70acdf9cb
commit
b3dc2c7571
|
@ -1,89 +0,0 @@
|
||||||
<?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\Controller;
|
|
||||||
|
|
||||||
use Flarum\Api\Controller\CreateDiscussionController;
|
|
||||||
use Flarum\Discussion\Discussion;
|
|
||||||
use Flarum\User\User;
|
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
|
|
||||||
class CreateDiscussionControllerTest extends ApiControllerTestCase
|
|
||||||
{
|
|
||||||
protected $controller = CreateDiscussionController::class;
|
|
||||||
|
|
||||||
protected $data = [
|
|
||||||
'title' => 'test - too-obscure',
|
|
||||||
'content' => 'predetermined content for automated testing - too-obscure'
|
|
||||||
];
|
|
||||||
|
|
||||||
public function setUp()
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
$this->prepareDatabase([
|
|
||||||
'discussions' => [],
|
|
||||||
'posts' => [],
|
|
||||||
'users' => [
|
|
||||||
$this->adminUser(),
|
|
||||||
],
|
|
||||||
'groups' => [
|
|
||||||
$this->adminGroup(),
|
|
||||||
],
|
|
||||||
'group_user' => [
|
|
||||||
['user_id' => 1, 'group_id' => 1],
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function can_create_discussion()
|
|
||||||
{
|
|
||||||
$this->actor = User::find(1);
|
|
||||||
|
|
||||||
$response = $this->callWith($this->data);
|
|
||||||
|
|
||||||
$this->assertEquals(201, $response->getStatusCode());
|
|
||||||
|
|
||||||
/** @var Discussion $discussion */
|
|
||||||
$discussion = Discussion::where('title', $this->data['title'])->firstOrFail();
|
|
||||||
$data = json_decode($response->getBody()->getContents(), true);
|
|
||||||
|
|
||||||
$this->assertEquals($this->data['title'], $discussion->title);
|
|
||||||
$this->assertEquals($this->data['title'], Arr::get($data, 'data.attributes.title'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function cannot_create_discussion_without_content()
|
|
||||||
{
|
|
||||||
$this->actor = User::find(1);
|
|
||||||
|
|
||||||
$data = Arr::except($this->data, 'content');
|
|
||||||
$response = $this->callWith($data);
|
|
||||||
|
|
||||||
$this->assertEquals(422, $response->getStatusCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function cannot_create_discussion_without_title()
|
|
||||||
{
|
|
||||||
$this->actor = User::find(1);
|
|
||||||
|
|
||||||
$data = Arr::except($this->data, 'title');
|
|
||||||
$response = $this->callWith($data);
|
|
||||||
|
|
||||||
$this->assertEquals(422, $response->getStatusCode());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
<?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 Flarum\Discussion\Discussion;
|
||||||
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
class CreationTest extends TestCase
|
||||||
|
{
|
||||||
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->prepareDatabase([
|
||||||
|
'discussions' => [],
|
||||||
|
'posts' => [],
|
||||||
|
'users' => [
|
||||||
|
$this->adminUser(),
|
||||||
|
],
|
||||||
|
'groups' => [
|
||||||
|
$this->adminGroup(),
|
||||||
|
],
|
||||||
|
'group_user' => [
|
||||||
|
['user_id' => 1, 'group_id' => 1],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function cannot_create_discussion_without_content()
|
||||||
|
{
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('POST', '/api/discussions', [
|
||||||
|
'authenticatedAs' => 1,
|
||||||
|
'json' => [
|
||||||
|
'data' => [
|
||||||
|
'attributes' => [
|
||||||
|
'title' => 'Test post',
|
||||||
|
'content' => '',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(422, $response->getStatusCode());
|
||||||
|
|
||||||
|
// The response body should contain details about the failed validation
|
||||||
|
$body = (string) $response->getBody();
|
||||||
|
$this->assertJson($body);
|
||||||
|
$this->assertEquals([
|
||||||
|
'errors' => [
|
||||||
|
[
|
||||||
|
'status' => '422',
|
||||||
|
'code' => 'validation_error',
|
||||||
|
'detail' => 'validation.required',
|
||||||
|
'source' => ['pointer' => '/data/attributes/content'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
], json_decode($body, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function cannot_create_discussion_without_title()
|
||||||
|
{
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('POST', '/api/discussions', [
|
||||||
|
'authenticatedAs' => 1,
|
||||||
|
'json' => [
|
||||||
|
'data' => [
|
||||||
|
'attributes' => [
|
||||||
|
'title' => '',
|
||||||
|
'content' => 'Test post',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(422, $response->getStatusCode());
|
||||||
|
|
||||||
|
// The response body should contain details about the failed validation
|
||||||
|
$body = (string) $response->getBody();
|
||||||
|
$this->assertJson($body);
|
||||||
|
$this->assertEquals([
|
||||||
|
'errors' => [
|
||||||
|
[
|
||||||
|
'status' => '422',
|
||||||
|
'code' => 'validation_error',
|
||||||
|
'detail' => 'validation.required',
|
||||||
|
'source' => ['pointer' => '/data/attributes/title'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
], json_decode($body, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function can_create_discussion()
|
||||||
|
{
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('POST', '/api/discussions', [
|
||||||
|
'authenticatedAs' => 1,
|
||||||
|
'json' => [
|
||||||
|
'data' => [
|
||||||
|
'attributes' => [
|
||||||
|
'title' => 'test - too-obscure',
|
||||||
|
'content' => 'predetermined content for automated testing - too-obscure',
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(201, $response->getStatusCode());
|
||||||
|
|
||||||
|
/** @var Discussion $discussion */
|
||||||
|
$discussion = Discussion::firstOrFail();
|
||||||
|
$data = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
$this->assertEquals('test - too-obscure', $discussion->title);
|
||||||
|
$this->assertEquals('test - too-obscure', Arr::get($data, 'data.attributes.title'));
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,15 +7,15 @@
|
||||||
* LICENSE file that was distributed with this source code.
|
* LICENSE file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Flarum\Tests\integration\api\Controller;
|
namespace Flarum\Tests\integration\api\discussions;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Flarum\Api\Controller\DeleteDiscussionController;
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
use Flarum\User\User;
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
|
||||||
class DeleteDiscussionControllerTest extends ApiControllerTestCase
|
class DeletionTest extends TestCase
|
||||||
{
|
{
|
||||||
protected $controller = DeleteDiscussionController::class;
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
|
@ -46,22 +46,17 @@ class DeleteDiscussionControllerTest extends ApiControllerTestCase
|
||||||
*/
|
*/
|
||||||
public function admin_can_delete()
|
public function admin_can_delete()
|
||||||
{
|
{
|
||||||
$this->actor = User::find(1);
|
$response = $this->send(
|
||||||
|
$this->request('DELETE', '/api/discussions/1', [
|
||||||
$response = $this->callWith([], ['id' => 1]);
|
'authenticatedAs' => 1,
|
||||||
|
'json' => [],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(204, $response->getStatusCode());
|
$this->assertEquals(204, $response->getStatusCode());
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function deleting_discussions_deletes_their_posts()
|
|
||||||
{
|
|
||||||
$this->actor = User::find(1);
|
|
||||||
|
|
||||||
$this->callWith([], ['id' => 1]);
|
|
||||||
|
|
||||||
|
// Ensure both the database and the corresponding post are deleted
|
||||||
|
$this->assertNull($this->database()->table('discussions')->find(1), 'Discussion exists in the DB');
|
||||||
$this->assertNull($this->database()->table('posts')->find(1), 'Post exists in the DB');
|
$this->assertNull($this->database()->table('posts')->find(1), 'Post exists in the DB');
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user