mirror of
https://github.com/flarum/core.git
synced 2025-10-11 23:14:29 +02:00
Convert more controller tests to request tests
This commit is contained in:
@@ -1,87 +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\Controller\CreateGroupController;
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateGroupControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
protected $controller = CreateGroupController::class;
|
||||
|
||||
protected $data = [
|
||||
'nameSingular' => 'flarumite',
|
||||
'namePlural' => 'flarumites',
|
||||
'icon' => 'test',
|
||||
'color' => null
|
||||
];
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->prepareDatabase([
|
||||
'users' => [
|
||||
$this->adminUser(),
|
||||
$this->normalUser(),
|
||||
],
|
||||
'groups' => [
|
||||
$this->adminGroup(),
|
||||
],
|
||||
'group_user' => [
|
||||
['user_id' => 1, 'group_id' => 1],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function admin_cannot_create_group_without_data()
|
||||
{
|
||||
$this->actor = User::find(1);
|
||||
|
||||
$this->assertEquals(422, $this->callWith()->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function admin_can_create_group()
|
||||
{
|
||||
$this->actor = User::find(1);
|
||||
|
||||
$response = $this->callWith($this->data);
|
||||
|
||||
$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, Arr::get($data, "data.attributes.$property"), "$property not matching to json response");
|
||||
$property = Str::snake($property);
|
||||
$this->assertEquals($value, $group->{$property}, "$property not matching to database result");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function normal_user_cannot_create_group()
|
||||
{
|
||||
$this->actor = User::find(2);
|
||||
|
||||
$this->assertEquals(403, $this->callWith($this->data)->getStatusCode());
|
||||
}
|
||||
}
|
@@ -1,64 +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 Carbon\Carbon;
|
||||
use Flarum\Api\Controller\CreatePostController;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class CreatePostControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
protected $controller = CreatePostController::class;
|
||||
|
||||
protected $data = [
|
||||
'content' => 'reply with predetermined content for automated testing - too-obscure'
|
||||
];
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->prepareDatabase([
|
||||
'discussions' => [
|
||||
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2],
|
||||
],
|
||||
'posts' => [],
|
||||
'users' => [
|
||||
$this->normalUser(),
|
||||
],
|
||||
'groups' => [
|
||||
$this->memberGroup(),
|
||||
],
|
||||
'group_user' => [
|
||||
['user_id' => 2, 'group_id' => 3],
|
||||
],
|
||||
'group_permission' => [
|
||||
['permission' => 'viewDiscussions', 'group_id' => 3],
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function can_create_reply()
|
||||
{
|
||||
$this->actor = User::find(2);
|
||||
|
||||
$body = [];
|
||||
Arr::set($body, 'data.attributes', $this->data);
|
||||
Arr::set($body, 'data.relationships.discussion.data.id', 1);
|
||||
|
||||
$response = $this->callWith($body);
|
||||
|
||||
$this->assertEquals(201, $response->getStatusCode());
|
||||
}
|
||||
}
|
@@ -1,31 +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\Controller\ListGroupsController;
|
||||
use Flarum\Group\Group;
|
||||
|
||||
class ListGroupsControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
protected $controller = ListGroupsController::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(Group::count(), count($data['data']));
|
||||
}
|
||||
}
|
@@ -1,73 +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\Controller\UpdateUserController;
|
||||
use Flarum\User\User;
|
||||
|
||||
class UpdateUserControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
protected $controller = UpdateUserController::class;
|
||||
|
||||
protected $data = [
|
||||
'email' => 'newemail@machine.local',
|
||||
];
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->prepareDatabase([
|
||||
'users' => [
|
||||
$this->adminUser(),
|
||||
$this->normalUser(),
|
||||
],
|
||||
'groups' => [
|
||||
$this->adminGroup(),
|
||||
$this->memberGroup(),
|
||||
],
|
||||
'group_user' => [
|
||||
['user_id' => 1, 'group_id' => 1],
|
||||
['user_id' => 2, 'group_id' => 3],
|
||||
],
|
||||
'group_permission' => [
|
||||
['permission' => 'viewUserList', 'group_id' => 3],
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function users_can_see_their_private_information()
|
||||
{
|
||||
$this->actor = User::find(2);
|
||||
|
||||
$response = $this->callWith([], ['id' => 2]);
|
||||
|
||||
// Test for successful response and that the email is included in the response
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertContains('normal@machine.local', (string) $response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function users_can_not_see_other_users_private_information()
|
||||
{
|
||||
$this->actor = User::find(2);
|
||||
|
||||
$response = $this->callWith([], ['id' => 1]);
|
||||
|
||||
// Make sure sensitive information is not made public
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertNotContains('admin@machine.local', (string) $response->getBody());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user