1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 08:27:42 +02:00

Convert more controller tests to request tests

This commit is contained in:
Franz Liedke
2020-03-20 18:54:20 +01:00
parent bc80085ce4
commit b10a17529d
3 changed files with 153 additions and 107 deletions

View File

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

View File

@@ -0,0 +1,62 @@
<?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 DeletionTest extends TestCase
{
use RetrievesAuthorizedUsers;
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'discussions' => [
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2],
],
'posts' => [
['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],
],
]);
}
/**
* @test
*/
public function admin_can_delete()
{
$response = $this->send(
$this->request('DELETE', '/api/discussions/1', [
'authenticatedAs' => 1,
'json' => [],
])
);
$this->assertEquals(204, $response->getStatusCode());
// 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');
}
}