mirror of
https://github.com/flarum/core.git
synced 2025-07-26 03:01:22 +02:00
adds a few additional api controller tests (#1429)
* added CreatePostControllerTest * added DeleteDiscussionControllerTest * added ListDiscussionControllerTest * Apply fixes from StyleCI [ci skip] [skip ci]
This commit is contained in:
@@ -14,6 +14,7 @@ namespace Flarum\Tests\Api\Controller;
|
|||||||
use Flarum\Http\Controller\ControllerInterface;
|
use Flarum\Http\Controller\ControllerInterface;
|
||||||
use Flarum\Tests\Test\TestCase;
|
use Flarum\Tests\Test\TestCase;
|
||||||
use Flarum\User\User;
|
use Flarum\User\User;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
abstract class ApiControllerTestCase extends TestCase
|
abstract class ApiControllerTestCase extends TestCase
|
||||||
@@ -28,13 +29,17 @@ abstract class ApiControllerTestCase extends TestCase
|
|||||||
*/
|
*/
|
||||||
protected $actor = null;
|
protected $actor = null;
|
||||||
|
|
||||||
protected function callWith(array $body = []): ResponseInterface
|
protected function callWith(array $body = [], array $queryParams = []): ResponseInterface
|
||||||
{
|
{
|
||||||
|
if (! Arr::get($body, 'data') && Arr::isAssoc($body)) {
|
||||||
|
$body = ['data' => ['attributes' => $body]];
|
||||||
|
}
|
||||||
|
|
||||||
return $this->call(
|
return $this->call(
|
||||||
$this->controller,
|
$this->controller,
|
||||||
$this->actor,
|
$this->actor,
|
||||||
[],
|
$queryParams,
|
||||||
$body ? ['data' => ['attributes' => $body]] : []
|
$body
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Tests\Api\Controller;
|
||||||
|
|
||||||
|
use Flarum\Api\Controller\CreatePostController;
|
||||||
|
use Flarum\Discussion\Discussion;
|
||||||
|
use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
class CreatePostControllerTest extends ApiControllerTestCase
|
||||||
|
{
|
||||||
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
|
protected $controller = CreatePostController::class;
|
||||||
|
|
||||||
|
protected $data = [
|
||||||
|
'content' => 'reply with predetermined content for automated testing - too-obscure'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Discussion
|
||||||
|
*/
|
||||||
|
protected $discussion;
|
||||||
|
|
||||||
|
protected function init()
|
||||||
|
{
|
||||||
|
$this->actor = $this->getNormalUser();
|
||||||
|
$this->discussion = Discussion::start(__CLASS__, $this->actor);
|
||||||
|
|
||||||
|
$this->discussion->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function can_create_reply()
|
||||||
|
{
|
||||||
|
$body = [];
|
||||||
|
Arr::set($body, 'data.attributes', $this->data);
|
||||||
|
Arr::set($body, 'data.relationships.discussion.data.id', $this->discussion->id);
|
||||||
|
|
||||||
|
$response = $this->callWith($body);
|
||||||
|
|
||||||
|
$this->assertEquals(201, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Tests\Api\Controller;
|
||||||
|
|
||||||
|
use Flarum\Api\Controller\DeleteDiscussionController;
|
||||||
|
use Flarum\Discussion\Discussion;
|
||||||
|
use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
|
class DeleteDiscussionControllerTest extends ApiControllerTestCase
|
||||||
|
{
|
||||||
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
|
protected $controller = DeleteDiscussionController::class;
|
||||||
|
protected $discussion;
|
||||||
|
|
||||||
|
protected function init()
|
||||||
|
{
|
||||||
|
$this->discussion = Discussion::start(__CLASS__, $this->getNormalUser());
|
||||||
|
|
||||||
|
$this->discussion->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function admin_can_delete()
|
||||||
|
{
|
||||||
|
$this->actor = $this->getAdminUser();
|
||||||
|
|
||||||
|
$response = $this->callWith([], ['id' => $this->discussion->id]);
|
||||||
|
|
||||||
|
$this->assertEquals(204, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Tests\Api\Controller;
|
||||||
|
|
||||||
|
use Flarum\Api\Controller\ListDiscussionsController;
|
||||||
|
use Flarum\Discussion\Discussion;
|
||||||
|
|
||||||
|
class ListDiscussionControllerTest extends ApiControllerTestCase
|
||||||
|
{
|
||||||
|
protected $controller = ListDiscussionsController::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shows_index_for_guest()
|
||||||
|
{
|
||||||
|
$response = $this->callWith();
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
|
$data = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
$this->assertEquals(Discussion::count(), count($data['data']));
|
||||||
|
}
|
||||||
|
}
|
@@ -28,7 +28,7 @@ trait RetrievesAuthorizedUsers
|
|||||||
|
|
||||||
public function getNormalUser()
|
public function getNormalUser()
|
||||||
{
|
{
|
||||||
User::unguarded(function () {
|
return User::unguarded(function () {
|
||||||
return User::firstOrCreate([
|
return User::firstOrCreate([
|
||||||
'username' => $this->userAttributes['username']
|
'username' => $this->userAttributes['username']
|
||||||
], $this->userAttributes);
|
], $this->userAttributes);
|
||||||
|
Reference in New Issue
Block a user