2017-11-20 19:56:24 +08:00
|
|
|
<?php
|
|
|
|
|
2018-08-17 15:25:08 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
use Mockery;
|
|
|
|
use Exception;
|
|
|
|
use CreateAllTables;
|
2019-02-27 23:44:50 +08:00
|
|
|
use Illuminate\Support\Str;
|
2018-08-21 11:14:22 +08:00
|
|
|
use AddVerificationToUsersTable;
|
2018-08-17 15:25:08 +08:00
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
2017-11-20 19:56:24 +08:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2019-03-02 22:58:37 +08:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2017-11-20 19:56:24 +08:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
|
|
|
class SetupControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2019-02-27 23:44:50 +08:00
|
|
|
protected function tearDown(): void
|
2017-11-20 19:56:24 +08:00
|
|
|
{
|
|
|
|
$this->dropAllTables();
|
|
|
|
Mockery::close();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dropAllTables()
|
|
|
|
{
|
|
|
|
$tables = [
|
2019-03-30 11:38:30 +08:00
|
|
|
'user_closet',
|
|
|
|
'migrations',
|
|
|
|
'options',
|
|
|
|
'players',
|
|
|
|
'textures',
|
|
|
|
'users',
|
|
|
|
'reports',
|
2019-07-03 16:19:13 +08:00
|
|
|
'oauth_auth_codes',
|
|
|
|
'oauth_access_tokens',
|
|
|
|
'oauth_clients',
|
|
|
|
'oauth_personal_access_clients',
|
|
|
|
'oauth_refresh_tokens',
|
|
|
|
'notifications',
|
2019-07-05 22:48:23 +08:00
|
|
|
'jobs',
|
2017-11-20 19:56:24 +08:00
|
|
|
];
|
|
|
|
array_walk($tables, function ($table) {
|
|
|
|
Schema::dropIfExists($table);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWelcome()
|
|
|
|
{
|
2019-07-05 12:40:14 +08:00
|
|
|
$this->dropAllTables();
|
2018-07-21 16:55:36 +08:00
|
|
|
$this->get('/setup')->assertViewIs('setup.wizard.welcome');
|
|
|
|
}
|
2018-02-23 09:51:23 +08:00
|
|
|
|
2018-07-21 16:55:36 +08:00
|
|
|
public function testDatabase()
|
|
|
|
{
|
2019-07-05 12:40:14 +08:00
|
|
|
$this->dropAllTables();
|
2018-07-21 16:55:36 +08:00
|
|
|
$fake = [
|
2018-11-25 14:32:14 +08:00
|
|
|
'type' => env('DB_CONNECTION'),
|
2018-09-06 16:26:03 +08:00
|
|
|
'host' => env('DB_HOST'),
|
2018-11-25 14:32:14 +08:00
|
|
|
'port' => env('DB_PORT'),
|
2018-07-21 16:55:36 +08:00
|
|
|
'db' => env('DB_DATABASE'),
|
|
|
|
'username' => env('DB_USERNAME'),
|
|
|
|
'password' => env('DB_PASSWORD'),
|
|
|
|
'prefix' => '',
|
|
|
|
];
|
2019-03-31 10:33:03 +08:00
|
|
|
File::shouldReceive('get')->with(base_path('.env'))->andReturn('');
|
|
|
|
File::shouldReceive('put')->with(base_path('.env'), '');
|
2018-07-21 16:55:36 +08:00
|
|
|
$this->post('/setup/database', $fake)->assertRedirect('/setup/info');
|
2019-03-17 12:05:31 +08:00
|
|
|
|
|
|
|
$this->get('/setup/database')->assertRedirect('/setup/info');
|
2018-07-21 16:55:36 +08:00
|
|
|
}
|
2018-02-23 09:51:23 +08:00
|
|
|
|
2018-07-21 16:55:36 +08:00
|
|
|
public function testReportDatabaseConnectionError()
|
|
|
|
{
|
2019-07-05 12:40:14 +08:00
|
|
|
$this->dropAllTables();
|
2018-07-24 09:58:10 +08:00
|
|
|
$this->post('/setup/database', ['type' => 'sqlite', 'host' => 'placeholder', 'db' => 'test'])
|
2018-07-21 16:55:36 +08:00
|
|
|
->assertSee(trans('setup.database.connection-error', [
|
|
|
|
'type' => 'SQLite',
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => 'Database (test) does not exist.',
|
2018-07-21 16:55:36 +08:00
|
|
|
]));
|
2017-11-20 19:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInfo()
|
|
|
|
{
|
2019-07-05 12:40:14 +08:00
|
|
|
$this->dropAllTables();
|
2019-04-25 23:24:24 +08:00
|
|
|
$this->get('/setup/info')->assertViewIs('setup.wizard.info');
|
2018-03-11 12:01:14 +08:00
|
|
|
Artisan::call('migrate:refresh');
|
|
|
|
Schema::drop('users');
|
2018-07-13 15:26:19 +08:00
|
|
|
$this->get('/setup/info')->assertSee('already exist');
|
2017-11-20 19:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFinish()
|
|
|
|
{
|
2019-07-05 12:40:14 +08:00
|
|
|
$this->dropAllTables();
|
2017-11-20 19:56:24 +08:00
|
|
|
// Without `email` field
|
|
|
|
$this->post('/setup/finish')
|
2018-07-13 15:26:19 +08:00
|
|
|
->assertDontSee(trans('setup.wizard.finish.title'));
|
2017-11-20 19:56:24 +08:00
|
|
|
|
|
|
|
// Not an valid email address
|
|
|
|
$this->post('/setup/finish', ['email' => 'not_an_email'])
|
2018-07-13 15:26:19 +08:00
|
|
|
->assertDontSee(trans('setup.wizard.finish.title'));
|
2017-11-20 19:56:24 +08:00
|
|
|
|
2018-07-20 17:23:54 +08:00
|
|
|
// Empty nickname
|
2017-11-20 19:56:24 +08:00
|
|
|
$this->post('/setup/finish', [
|
2019-03-02 22:58:37 +08:00
|
|
|
'email' => 'a@b.c',
|
2018-07-13 15:26:19 +08:00
|
|
|
])->assertDontSee(trans('setup.wizard.finish.title'));
|
2017-11-20 19:56:24 +08:00
|
|
|
|
2018-07-20 17:23:54 +08:00
|
|
|
// Invalid characters in nickname
|
|
|
|
$this->post('/setup/finish', [
|
|
|
|
'email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'nickname' => '\\',
|
2018-07-20 17:23:54 +08:00
|
|
|
])->assertDontSee(trans('setup.wizard.finish.title'));
|
|
|
|
|
|
|
|
// Too long nickname
|
|
|
|
$this->post('/setup/finish', [
|
|
|
|
'email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'nickname' => Str::random(256),
|
2018-07-20 17:23:54 +08:00
|
|
|
])->assertDontSee(trans('setup.wizard.finish.title'));
|
|
|
|
|
|
|
|
// Without `password` field
|
|
|
|
$this->post('/setup/finish', [
|
|
|
|
'email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'nickname' => 'nickname',
|
2018-07-20 17:23:54 +08:00
|
|
|
])->assertDontSee(trans('setup.wizard.finish.title'));
|
|
|
|
|
2017-11-20 19:56:24 +08:00
|
|
|
// Password is too short
|
|
|
|
$this->post('/setup/finish', [
|
|
|
|
'email' => 'a@b.c',
|
2018-07-20 17:23:54 +08:00
|
|
|
'nickname' => 'nickname',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '1',
|
2018-07-13 15:26:19 +08:00
|
|
|
])->assertDontSee(trans('setup.wizard.finish.title'));
|
2017-11-20 19:56:24 +08:00
|
|
|
|
|
|
|
// Password is too long
|
|
|
|
$this->post('/setup/finish', [
|
|
|
|
'email' => 'a@b.c',
|
2018-07-20 17:23:54 +08:00
|
|
|
'nickname' => 'nickname',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => Str::random(17),
|
2018-07-13 15:26:19 +08:00
|
|
|
])->assertDontSee(trans('setup.wizard.finish.title'));
|
2017-11-20 19:56:24 +08:00
|
|
|
|
|
|
|
// Confirmation is not OK
|
|
|
|
$this->post('/setup/finish', [
|
|
|
|
'email' => 'a@b.c',
|
2018-07-20 17:23:54 +08:00
|
|
|
'nickname' => 'nickname',
|
2017-11-20 19:56:24 +08:00
|
|
|
'password' => '12345678',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password_confirmation' => '12345679',
|
2018-07-13 15:26:19 +08:00
|
|
|
])->assertDontSee(trans('setup.wizard.finish.title'));
|
2017-11-20 19:56:24 +08:00
|
|
|
|
|
|
|
// Without `site_name` field
|
|
|
|
$this->post('/setup/finish', [
|
|
|
|
'email' => 'a@b.c',
|
2018-07-20 17:23:54 +08:00
|
|
|
'nickname' => 'nickname',
|
2017-11-20 19:56:24 +08:00
|
|
|
'password' => '12345678',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password_confirmation' => '12345678',
|
2018-07-13 15:26:19 +08:00
|
|
|
])->assertDontSee(trans('setup.wizard.finish.title'));
|
2017-11-20 19:56:24 +08:00
|
|
|
|
|
|
|
// Regenerate keys
|
|
|
|
Artisan::shouldReceive('call')
|
2019-08-09 11:20:36 +08:00
|
|
|
->with('key:generate')
|
2017-11-20 19:56:24 +08:00
|
|
|
->once()
|
|
|
|
->andReturn(true);
|
|
|
|
Artisan::shouldReceive('call')
|
|
|
|
->with('salt:random')
|
|
|
|
->once()
|
|
|
|
->andReturn(true);
|
2019-04-23 10:05:58 +08:00
|
|
|
Artisan::shouldReceive('call')
|
|
|
|
->with('jwt:secret', ['--no-interaction' => true])
|
|
|
|
->once()
|
|
|
|
->andReturn(true);
|
2019-04-25 23:24:24 +08:00
|
|
|
Artisan::shouldReceive('call')
|
|
|
|
->with('passport:keys', ['--no-interaction' => true])
|
|
|
|
->once()
|
|
|
|
->andReturn(true);
|
2017-11-20 19:56:24 +08:00
|
|
|
Artisan::shouldReceive('call')
|
2019-06-30 23:25:48 +08:00
|
|
|
->with('migrate', [
|
|
|
|
'--force' => true,
|
|
|
|
'--path' => [
|
|
|
|
'database/migrations',
|
2019-08-15 23:27:29 +08:00
|
|
|
'vendor/laravel/passport/database/migrations',
|
|
|
|
],
|
2019-06-30 23:25:48 +08:00
|
|
|
])
|
2017-11-20 19:56:24 +08:00
|
|
|
->once()
|
|
|
|
->andReturnUsing(function () {
|
|
|
|
$migration = new CreateAllTables();
|
|
|
|
$migration->up();
|
2018-08-21 11:14:22 +08:00
|
|
|
|
|
|
|
$migration = new AddVerificationToUsersTable();
|
|
|
|
$migration->up();
|
2017-11-20 19:56:24 +08:00
|
|
|
});
|
|
|
|
$this->post('/setup/finish', [
|
|
|
|
'email' => 'a@b.c',
|
2018-07-20 17:23:54 +08:00
|
|
|
'nickname' => 'nickname',
|
2017-11-20 19:56:24 +08:00
|
|
|
'password' => '12345678',
|
|
|
|
'password_confirmation' => '12345678',
|
|
|
|
'site_name' => 'bs',
|
2019-03-02 22:58:37 +08:00
|
|
|
'generate_random' => true,
|
2018-07-13 15:26:19 +08:00
|
|
|
])->assertSee(trans('setup.wizard.finish.title'))
|
|
|
|
->assertSee('a@b.c')
|
|
|
|
->assertSee('12345678');
|
2018-07-20 17:23:54 +08:00
|
|
|
$superAdmin = \App\Models\User::find(1);
|
|
|
|
$this->assertEquals('a@b.c', $superAdmin->email);
|
|
|
|
$this->assertTrue($superAdmin->verifyPassword('12345678'));
|
|
|
|
$this->assertEquals('nickname', $superAdmin->nickname);
|
|
|
|
$this->assertEquals('bs', option('site_name'));
|
2017-11-20 19:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdate()
|
|
|
|
{
|
2019-07-05 12:40:14 +08:00
|
|
|
$this->actAs('superAdmin')
|
|
|
|
->get('/setup/update')
|
2018-07-13 15:26:19 +08:00
|
|
|
->assertSee(trans('setup.locked.text'));
|
2017-11-20 19:56:24 +08:00
|
|
|
|
|
|
|
option(['version' => '0.1.0']);
|
2018-07-13 15:26:19 +08:00
|
|
|
$this->get('/setup/update')
|
|
|
|
->assertSee(trans('setup.updates.welcome.title'));
|
2017-11-20 19:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoUpdate()
|
|
|
|
{
|
|
|
|
$current_version = config('app.version');
|
|
|
|
config(['app.version' => '100.0.0']);
|
|
|
|
copy(
|
|
|
|
database_path('update_scripts/update-3.1-to-3.1.1.php'),
|
|
|
|
database_path("update_scripts/update-$current_version-to-100.0.0.php")
|
|
|
|
); // Just a fixture
|
|
|
|
|
|
|
|
config(['options.new_option' => 'value']);
|
2019-07-05 12:40:14 +08:00
|
|
|
$this->actAs('superAdmin')->get('/setup/exec-update')->assertViewHas('tips');
|
2017-11-20 19:56:24 +08:00
|
|
|
$this->assertEquals('value', option('new_option'));
|
2019-04-22 22:56:24 +08:00
|
|
|
$this->assertEquals('100.0.0', option('version'));
|
2017-11-20 19:56:24 +08:00
|
|
|
unlink(database_path("update_scripts/update-$current_version-to-100.0.0.php"));
|
|
|
|
|
|
|
|
option(['version' => '3.0.0']); // Fake old version
|
2019-05-03 15:04:02 +08:00
|
|
|
$this->get('/setup/exec-update');
|
2017-11-20 19:56:24 +08:00
|
|
|
$this->assertEquals('100.0.0', option('version'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckDirectories()
|
|
|
|
{
|
|
|
|
Storage::shouldReceive('disk')
|
|
|
|
->with('root')
|
|
|
|
->andReturnSelf();
|
|
|
|
Storage::shouldReceive('has')
|
|
|
|
->with('storage/textures')
|
|
|
|
->andReturn(false);
|
|
|
|
Storage::shouldReceive('makeDirectory')
|
|
|
|
->with('storage/textures')
|
|
|
|
->andThrow(new Exception());
|
|
|
|
|
|
|
|
$this->assertFalse(\App\Http\Controllers\SetupController::checkDirectories());
|
|
|
|
}
|
|
|
|
}
|