diff --git a/framework/core/tests/Api/Controller/ApiControllerTestCase.php b/framework/core/tests/Api/Controller/ApiControllerTestCase.php index 61172d212..fa8695fb2 100644 --- a/framework/core/tests/Api/Controller/ApiControllerTestCase.php +++ b/framework/core/tests/Api/Controller/ApiControllerTestCase.php @@ -14,6 +14,7 @@ namespace Flarum\Tests\Api\Controller; use Flarum\Http\Controller\ControllerInterface; use Flarum\Tests\Test\TestCase; use Flarum\User\User; +use Illuminate\Support\Arr; use Psr\Http\Message\ResponseInterface; abstract class ApiControllerTestCase extends TestCase @@ -28,13 +29,17 @@ abstract class ApiControllerTestCase extends TestCase */ 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( $this->controller, $this->actor, - [], - $body ? ['data' => ['attributes' => $body]] : [] + $queryParams, + $body ); } diff --git a/framework/core/tests/Api/Controller/CreatePostControllerTest.php b/framework/core/tests/Api/Controller/CreatePostControllerTest.php new file mode 100644 index 000000000..e0d30feb6 --- /dev/null +++ b/framework/core/tests/Api/Controller/CreatePostControllerTest.php @@ -0,0 +1,55 @@ + + * + * 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()); + } +} diff --git a/framework/core/tests/Api/Controller/DeleteDiscussionControllerTest.php b/framework/core/tests/Api/Controller/DeleteDiscussionControllerTest.php new file mode 100644 index 000000000..e4c68dde6 --- /dev/null +++ b/framework/core/tests/Api/Controller/DeleteDiscussionControllerTest.php @@ -0,0 +1,43 @@ + + * + * 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()); + } +} diff --git a/framework/core/tests/Api/Controller/ListDiscussionControllerTest.php b/framework/core/tests/Api/Controller/ListDiscussionControllerTest.php new file mode 100644 index 000000000..695b378c2 --- /dev/null +++ b/framework/core/tests/Api/Controller/ListDiscussionControllerTest.php @@ -0,0 +1,33 @@ + + * + * 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'])); + } +} diff --git a/framework/core/tests/Test/Concerns/RetrievesAuthorizedUsers.php b/framework/core/tests/Test/Concerns/RetrievesAuthorizedUsers.php index ca4bf581c..c11e9c911 100644 --- a/framework/core/tests/Test/Concerns/RetrievesAuthorizedUsers.php +++ b/framework/core/tests/Test/Concerns/RetrievesAuthorizedUsers.php @@ -28,7 +28,7 @@ trait RetrievesAuthorizedUsers public function getNormalUser() { - User::unguarded(function () { + return User::unguarded(function () { return User::firstOrCreate([ 'username' => $this->userAttributes['username'] ], $this->userAttributes);