mirror of
https://github.com/flarum/core.git
synced 2025-07-19 15:51:16 +02:00
Convert last two controller tests to request tests
This commit is contained in:
@@ -1,56 +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\Client;
|
|
||||||
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
|
||||||
use Flarum\Tests\integration\TestCase;
|
|
||||||
use Flarum\User\Guest;
|
|
||||||
use Flarum\User\User;
|
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
|
||||||
|
|
||||||
abstract class ApiControllerTestCase extends TestCase
|
|
||||||
{
|
|
||||||
use RetrievesAuthorizedUsers;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var RequestHandlerInterface
|
|
||||||
*/
|
|
||||||
protected $controller;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var null|User
|
|
||||||
*/
|
|
||||||
protected $actor = null;
|
|
||||||
|
|
||||||
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,
|
|
||||||
$queryParams,
|
|
||||||
$body
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface
|
|
||||||
{
|
|
||||||
/** @var Client $api */
|
|
||||||
$api = $this->app()->getContainer()->make(Client::class);
|
|
||||||
|
|
||||||
return $api->send($controller, $actor ?? new Guest, $queryParams, $body);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -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\ListDiscussionsController;
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
use Flarum\User\User;
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
|
||||||
class ListDiscussionsControllerTest extends ApiControllerTestCase
|
class ListTest extends TestCase
|
||||||
{
|
{
|
||||||
protected $controller = ListDiscussionsController::class;
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
@@ -46,7 +46,9 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function shows_index_for_guest()
|
public function shows_index_for_guest()
|
||||||
{
|
{
|
||||||
$response = $this->callWith();
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions')
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
$data = json_decode($response->getBody()->getContents(), true);
|
$data = json_decode($response->getBody()->getContents(), true);
|
||||||
@@ -59,14 +61,13 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function can_search_for_author()
|
public function can_search_for_author()
|
||||||
{
|
{
|
||||||
$user = User::find(2);
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions')
|
||||||
$response = $this->callWith([], [
|
->withQueryParams([
|
||||||
'filter' => [
|
'filter' => ['q' => 'author:normal foo'],
|
||||||
'q' => 'author:'.$user->username.' foo'
|
'include' => 'mostRelevantPost',
|
||||||
],
|
])
|
||||||
'include' => 'mostRelevantPost'
|
);
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
}
|
}
|
||||||
@@ -86,10 +87,14 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase
|
|||||||
['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>lightsail in text</p></t>'],
|
['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>lightsail in text</p></t>'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response = $this->callWith([], [
|
$response = $this->send(
|
||||||
'filter' => ['q' => 'lightsail'],
|
$this->request('GET', '/api/discussions')
|
||||||
'include' => 'mostRelevantPost'
|
->withQueryParams([
|
||||||
]);
|
'filter' => ['q' => 'lightsail'],
|
||||||
|
'include' => 'mostRelevantPost',
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
$data = json_decode($response->getBody()->getContents(), true);
|
$data = json_decode($response->getBody()->getContents(), true);
|
||||||
$ids = array_map(function ($row) {
|
$ids = array_map(function ($row) {
|
||||||
return $row['id'];
|
return $row['id'];
|
||||||
@@ -114,10 +119,14 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase
|
|||||||
['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>lightsail in text</p></t>'],
|
['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>lightsail in text</p></t>'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response = $this->callWith([], [
|
$response = $this->send(
|
||||||
'filter' => ['q' => 'lightsail+'],
|
$this->request('GET', '/api/discussions')
|
||||||
'include' => 'mostRelevantPost'
|
->withQueryParams([
|
||||||
]);
|
'filter' => ['q' => 'lightsail+'],
|
||||||
|
'include' => 'mostRelevantPost',
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
$data = json_decode($response->getBody()->getContents(), true);
|
$data = json_decode($response->getBody()->getContents(), true);
|
||||||
$ids = array_map(function ($row) {
|
$ids = array_map(function ($row) {
|
||||||
return $row['id'];
|
return $row['id'];
|
||||||
@@ -132,17 +141,25 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function search_for_special_characters_gives_empty_result()
|
public function search_for_special_characters_gives_empty_result()
|
||||||
{
|
{
|
||||||
$response = $this->callWith([], [
|
$response = $this->send(
|
||||||
'filter' => ['q' => '*'],
|
$this->request('GET', '/api/discussions')
|
||||||
'include' => 'mostRelevantPost'
|
->withQueryParams([
|
||||||
]);
|
'filter' => ['q' => '*'],
|
||||||
|
'include' => 'mostRelevantPost',
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
$data = json_decode($response->getBody()->getContents(), true);
|
$data = json_decode($response->getBody()->getContents(), true);
|
||||||
$this->assertEquals([], $data['data']);
|
$this->assertEquals([], $data['data']);
|
||||||
|
|
||||||
$response = $this->callWith([], [
|
$response = $this->send(
|
||||||
'filter' => ['q' => '@'],
|
$this->request('GET', '/api/discussions')
|
||||||
'include' => 'mostRelevantPost'
|
->withQueryParams([
|
||||||
]);
|
'filter' => ['q' => '@'],
|
||||||
|
'include' => 'mostRelevantPost',
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
$data = json_decode($response->getBody()->getContents(), true);
|
$data = json_decode($response->getBody()->getContents(), true);
|
||||||
$this->assertEquals([], $data['data']);
|
$this->assertEquals([], $data['data']);
|
||||||
}
|
}
|
@@ -7,24 +7,18 @@
|
|||||||
* 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\ShowDiscussionController;
|
|
||||||
use Flarum\Discussion\Discussion;
|
|
||||||
use Flarum\Event\ScopeModelVisibility;
|
use Flarum\Event\ScopeModelVisibility;
|
||||||
use Flarum\User\User;
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
|
use Flarum\Tests\integration\TestCase;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
class ShowDiscussionControllerTest extends ApiControllerTestCase
|
class ShowTest extends TestCase
|
||||||
{
|
{
|
||||||
protected $controller = ShowDiscussionController::class;
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Discussion
|
|
||||||
*/
|
|
||||||
protected $discussion;
|
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
@@ -63,9 +57,11 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function author_can_see_discussion()
|
public function author_can_see_discussion()
|
||||||
{
|
{
|
||||||
$this->actor = User::find(2);
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions/1', [
|
||||||
$response = $this->callWith([], ['id' => 1]);
|
'authenticatedAs' => 2,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
}
|
}
|
||||||
@@ -75,7 +71,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function guest_cannot_see_empty_discussion()
|
public function guest_cannot_see_empty_discussion()
|
||||||
{
|
{
|
||||||
$response = $this->callWith([], ['id' => 1]);
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions/1')
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(404, $response->getStatusCode());
|
$this->assertEquals(404, $response->getStatusCode());
|
||||||
}
|
}
|
||||||
@@ -85,7 +83,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function guest_cannot_see_hidden_posts()
|
public function guest_cannot_see_hidden_posts()
|
||||||
{
|
{
|
||||||
$response = $this->callWith([], ['id' => 4]);
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions/4')
|
||||||
|
);
|
||||||
|
|
||||||
$json = json_decode($response->getBody()->getContents(), true);
|
$json = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
@@ -97,9 +97,11 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function author_can_see_hidden_posts()
|
public function author_can_see_hidden_posts()
|
||||||
{
|
{
|
||||||
$this->actor = User::find(2);
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions/4', [
|
||||||
$response = $this->callWith([], ['id' => 4]);
|
'authenticatedAs' => 2,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
$json = json_decode($response->getBody()->getContents(), true);
|
$json = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
@@ -120,7 +122,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$response = $this->callWith([], ['id' => 4]);
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions/4')
|
||||||
|
);
|
||||||
|
|
||||||
$json = json_decode($response->getBody()->getContents(), true);
|
$json = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
@@ -132,7 +136,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function guest_can_see_discussion()
|
public function guest_can_see_discussion()
|
||||||
{
|
{
|
||||||
$response = $this->callWith([], ['id' => 2]);
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions/2')
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
}
|
}
|
||||||
@@ -142,7 +148,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
|||||||
*/
|
*/
|
||||||
public function guests_cannot_see_private_discussion()
|
public function guests_cannot_see_private_discussion()
|
||||||
{
|
{
|
||||||
$response = $this->callWith([], ['id' => 3]);
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api/discussions/3')
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(404, $response->getStatusCode());
|
$this->assertEquals(404, $response->getStatusCode());
|
||||||
}
|
}
|
Reference in New Issue
Block a user