framework/tests/integration/api/Controller/CreateGroupControllerTest.php

90 lines
2.2 KiB
PHP
Raw Normal View History

2018-04-13 13:52:39 +08:00
<?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\integration\api\Controller;
2018-04-13 13:52:39 +08:00
use Flarum\Api\Controller\CreateGroupController;
use Flarum\Group\Group;
use Flarum\User\User;
2019-07-06 07:11:19 +08:00
use Illuminate\Support\Arr;
2018-04-13 13:52:39 +08:00
use Illuminate\Support\Str;
class CreateGroupControllerTest extends ApiControllerTestCase
2018-04-13 13:52:39 +08:00
{
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],
],
]);
}
2018-04-13 13:52:39 +08:00
/**
* @test
*/
public function admin_cannot_create_group_without_data()
{
$this->actor = User::find(1);
2018-04-13 13:52:39 +08:00
$this->assertEquals(422, $this->callWith()->getStatusCode());
2018-04-13 13:52:39 +08:00
}
/**
* @test
*/
public function admin_can_create_group()
{
$this->actor = User::find(1);
2018-04-13 13:52:39 +08:00
$response = $this->callWith($this->data);
$this->assertEquals(201, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
2018-04-13 13:52:39 +08:00
$group = Group::where('icon', $this->data['icon'])->firstOrFail();
foreach ($this->data as $property => $value) {
2019-07-06 07:11:19 +08:00
$this->assertEquals($value, Arr::get($data, "data.attributes.$property"), "$property not matching to json response");
2018-04-13 13:52:39 +08:00
$property = Str::snake($property);
$this->assertEquals($value, $group->{$property}, "$property not matching to database result");
2018-04-13 13:52:39 +08:00
}
}
/**
* @test
*/
public function unauthorized_user_cannot_create_group()
{
$this->actor = User::find(2);
2018-04-13 13:52:39 +08:00
$this->assertEquals(403, $this->callWith($this->data)->getStatusCode());
2018-04-13 13:52:39 +08:00
}
}