mc-blessing-skin/tests/HttpTest/ControllersTest/SetupControllerTest.php

157 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2017-11-20 19:56:24 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2019-09-07 10:18:24 +08:00
use Illuminate\Contracts\Console\Kernel as Artisan;
2019-12-14 11:10:37 +08:00
use Illuminate\Filesystem\Filesystem;
2017-11-20 19:56:24 +08:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2019-12-14 11:10:37 +08:00
use Illuminate\Support\Str;
2017-11-20 19:56:24 +08:00
class SetupControllerTest extends TestCase
{
use DatabaseTransactions;
2019-09-07 10:18:24 +08:00
public function setUp(): void
{
parent::setUp();
}
2018-07-21 16:55:36 +08:00
public function testDatabase()
{
2019-09-06 23:53:47 +08:00
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('exists')
->with(storage_path('install.lock'))
->atLeast(1)
->andReturn(false);
$mock->shouldReceive('put')
->with(storage_path('install.lock'), '')
->atLeast(1)
->andReturn(true);
2019-09-07 10:18:24 +08:00
$mock->shouldReceive('get')
->with(base_path('.env'))
->once()
2019-09-12 19:06:18 +08:00
->andReturn('DB_CONNECTION=abc');
2019-09-07 10:18:24 +08:00
$mock->shouldReceive('put')
2019-09-12 19:06:18 +08:00
->with(base_path('.env'), 'DB_CONNECTION='.env('DB_CONNECTION'))
2019-09-07 10:18:24 +08:00
->once()
->andReturn(true);
2019-09-06 23:53:47 +08:00
});
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' => '',
];
$this->post('/setup/database', $fake)->assertRedirect('/setup/info');
$this->get('/setup/database')->assertRedirect('/setup/info');
2018-02-23 09:51:23 +08:00
2019-09-07 10:18:24 +08:00
$this->mock(\Illuminate\Database\DatabaseManager::class, function ($mock) {
$mock->shouldReceive('connection')->andThrow(new \Exception())->once();
2019-09-06 23:53:47 +08:00
});
2019-09-07 10:18:24 +08:00
$this->post('/setup/database', ['type' => 'sqlite'])
->assertSee(
trans('setup.database.connection-error', ['type' => 'SQLite', 'msg' => ''])
);
2019-09-06 23:53:47 +08:00
2019-09-07 10:18:24 +08:00
$this->mock(\Illuminate\Database\Connection::class, function ($mock) {
$mock->shouldReceive('getPdo')->andThrow(new \Exception());
});
$this->get('/setup/database')->assertViewIs('setup.wizard.database');
2017-11-20 19:56:24 +08:00
}
public function testFinish()
{
2019-09-06 23:53:47 +08:00
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('exists')
->with(storage_path('install.lock'))
->atLeast(1)
->andReturn(false);
$mock->shouldReceive('put')
->with(storage_path('install.lock'), '')
->atLeast(1)
->andReturn(true);
});
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', [
'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
// Without `password` field
$this->post('/setup/finish', [
'email' => 'a@b.c',
'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',
'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',
'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',
'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',
'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
2019-09-07 10:18:24 +08:00
$this->spy(Artisan::class, function ($spy) {
$spy->shouldReceive('call')
->with('passport:keys', ['--no-interaction' => true])
->once();
$spy->shouldReceive('call')
->with('migrate', [
'--force' => true,
'--path' => [
'database/migrations',
'vendor/laravel/passport/database/migrations',
],
])
->once();
});
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',
2020-02-01 12:44:39 +08:00
])->assertSee(trans('setup.wizard.finish.title'));
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
}
}