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

112 lines
2.7 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\CreateUserController;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Illuminate\Support\Arr;
class CreateUserControllerTest extends ApiControllerTestCase
2018-04-13 13:52:39 +08:00
{
protected $controller = CreateUserController::class;
protected $data = [
'username' => 'test',
'password' => 'too-obscure',
'email' => 'test@machine.local'
];
public function setUp()
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
'settings' => [
['key' => 'mail_driver', 'value' => 'log']
]
]);
}
2018-04-13 13:52:39 +08:00
/**
* @test
*/
public function cannot_create_user_without_data()
{
$response = $this->callWith();
$this->assertEquals(422, $response->getStatusCode());
2018-04-13 13:52:39 +08:00
}
/**
* @test
*/
public function can_create_user()
{
$response = $this->callWith($this->data);
$this->assertEquals(201, $response->getStatusCode());
/** @var User $user */
$user = User::where('username', 'test')->firstOrFail();
$this->assertEquals(0, $user->is_activated);
foreach (Arr::except($this->data, 'password') as $property => $value) {
$this->assertEquals($value, $user->{$property});
}
}
/**
* @test
*/
public function admins_can_create_activated_users()
{
$this->actor = User::find(1);
2018-04-13 13:52:39 +08:00
$response = $this->callWith(array_merge($this->data, [
2018-08-24 20:05:04 +08:00
'isEmailConfirmed' => 1
2018-04-13 13:52:39 +08:00
]));
$this->assertEquals(201, $response->getStatusCode());
/** @var User $user */
$user = User::where('username', 'test')->firstOrFail();
$this->assertEquals(1, $user->is_email_confirmed);
2018-04-13 13:52:39 +08:00
}
/**
* @test
*/
public function disabling_sign_up_prevents_user_creation()
{
/** @var SettingsRepositoryInterface $settings */
2018-08-15 06:47:00 +08:00
$settings = app(SettingsRepositoryInterface::class);
2018-04-13 13:52:39 +08:00
$settings->set('allow_sign_up', false);
$response = $this->callWith($this->data);
$this->assertEquals(403, $response->getStatusCode());
$settings->set('allow_sign_up', true);
2018-04-13 13:52:39 +08:00
}
}