diff --git a/framework/core/src/Api/Client.php b/framework/core/src/Api/Client.php index 774f0dadc..18309d3de 100644 --- a/framework/core/src/Api/Client.php +++ b/framework/core/src/Api/Client.php @@ -16,6 +16,7 @@ use Flarum\Foundation\Application; use Flarum\Http\Controller\ControllerInterface; use Flarum\User\User; use InvalidArgumentException; +use Psr\Http\Message\ResponseInterface; use Zend\Diactoros\ServerRequestFactory; class Client @@ -46,10 +47,10 @@ class Client * @param User|null $actor * @param array $queryParams * @param array $body - * @return \Psr\Http\Message\ResponseInterface + * @return ResponseInterface * @throws Exception */ - public function send($controller, $actor, array $queryParams = [], array $body = []) + public function send($controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface { $request = ServerRequestFactory::fromGlobals(null, $queryParams, $body); diff --git a/framework/core/tests/Api/Controller/CreateDiscussionControllerTest.php b/framework/core/tests/Api/Controller/CreateDiscussionControllerTest.php new file mode 100644 index 000000000..fb6f6ba8e --- /dev/null +++ b/framework/core/tests/Api/Controller/CreateDiscussionControllerTest.php @@ -0,0 +1,85 @@ + + * + * 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\CreateDiscussionController; +use Flarum\Discussion\Discussion; +use Flarum\Post\Post; +use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers; +use Illuminate\Support\Arr; + +class CreateDiscussionControllerTest extends ApiControllerTestCase +{ + use RetrievesAuthorizedUsers; + + protected $controller = CreateDiscussionController::class; + + protected $data = [ + 'title' => 'test - too-obscure', + 'content' => 'predetermined content for automated testing - too-obscure' + ]; + + /** + * @test + */ + public function can_create_discussion() + { + $this->actor = $this->getAdminUser(); + + $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'], array_get($data, "data.attributes.title")); + } + + /** + * @test + * @expectedException \Illuminate\Validation\ValidationException + * @expectedExceptionMessage The given data was invalid. + */ + public function cannot_create_discussion_without_content() + { + $this->actor = $this->getAdminUser(); + + $data = Arr::except($this->data, 'content'); + + $this->callWith($data); + } + + /** + * @test + * @expectedException \Illuminate\Validation\ValidationException + * @expectedExceptionMessage The given data was invalid. + */ + public function cannot_create_discussion_without_title() + { + $this->actor = $this->getAdminUser(); + + $data = Arr::except($this->data, 'title'); + + $this->callWith($data); + } + + public function tearDown() + { + Discussion::where('title', $this->data['title'])->delete(); + // Prevent floodgate from kicking in. + Post::where('user_id', $this->getAdminUser()->id)->delete(); + parent::tearDown(); + } +} diff --git a/framework/core/tests/Api/Controller/CreateGroupControllerTestTestCase.php b/framework/core/tests/Api/Controller/CreateGroupControllerTest.php similarity index 83% rename from framework/core/tests/Api/Controller/CreateGroupControllerTestTestCase.php rename to framework/core/tests/Api/Controller/CreateGroupControllerTest.php index bf6399642..b553f43eb 100644 --- a/framework/core/tests/Api/Controller/CreateGroupControllerTestTestCase.php +++ b/framework/core/tests/Api/Controller/CreateGroupControllerTest.php @@ -16,7 +16,7 @@ use Flarum\Group\Group; use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers; use Illuminate\Support\Str; -class CreateGroupControllerTestTestCase extends ApiControllerTestCase +class CreateGroupControllerTest extends ApiControllerTestCase { use RetrievesAuthorizedUsers; @@ -52,11 +52,13 @@ class CreateGroupControllerTestTestCase extends ApiControllerTestCase $this->assertEquals(201, $response->getStatusCode()); + $data = json_decode($response->getBody()->getContents(), true); $group = Group::where('icon', $this->data['icon'])->firstOrFail(); foreach ($this->data as $property => $value) { + $this->assertEquals($value, array_get($data, "data.attributes.$property"), "$property not matching to json response"); $property = Str::snake($property); - $this->assertEquals($value, $group->{$property}); + $this->assertEquals($value, $group->{$property}, "$property not matching to database result"); } } diff --git a/framework/core/tests/Api/Controller/CreateUserControllerTestTestCase.php b/framework/core/tests/Api/Controller/CreateUserControllerTest.php similarity index 97% rename from framework/core/tests/Api/Controller/CreateUserControllerTestTestCase.php rename to framework/core/tests/Api/Controller/CreateUserControllerTest.php index e0f85bf2d..285b2a53a 100644 --- a/framework/core/tests/Api/Controller/CreateUserControllerTestTestCase.php +++ b/framework/core/tests/Api/Controller/CreateUserControllerTest.php @@ -17,7 +17,7 @@ use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers; use Flarum\User\User; use Illuminate\Support\Arr; -class CreateUserControllerTestTestCase extends ApiControllerTestCase +class CreateUserControllerTest extends ApiControllerTestCase { use RetrievesAuthorizedUsers; diff --git a/framework/core/tests/Test/Concerns/MakesApiRequests.php b/framework/core/tests/Test/Concerns/MakesApiRequests.php index 270e501fb..bcf69f819 100644 --- a/framework/core/tests/Test/Concerns/MakesApiRequests.php +++ b/framework/core/tests/Test/Concerns/MakesApiRequests.php @@ -15,12 +15,14 @@ use Flarum\Api\ApiServiceProvider; use Flarum\Api\Client; use Flarum\User\Guest; use Flarum\User\User; +use Flarum\User\UserServiceProvider; use Psr\Http\Message\ResponseInterface; trait MakesApiRequests { public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface { + $this->app->register(UserServiceProvider::class); $this->app->register(ApiServiceProvider::class); $this->app->make('flarum.api.middleware'); /** @var Client $api */