From 55c2fb38acad89539b2bedffc554fdd235df816d Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Mon, 20 Nov 2017 19:56:24 +0800 Subject: [PATCH] Update tests for `SetupController` --- app/Http/Controllers/SetupController.php | 15 +- tests/SetupControllerTest.php | 167 +++++++++++++++++++++++ 2 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 tests/SetupControllerTest.php diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 83e49e8a..a9795888 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -37,7 +37,7 @@ class SetupController extends Controller 'site_name' => 'required' ]); - if (isset($_POST['generate_random'])) { + if ($request->has('generate_random')) { // generate new APP_KEY & SALT randomly if (is_writable(app()->environmentFile())) { Artisan::call('key:random'); @@ -48,7 +48,9 @@ class SetupController extends Controller 'salt' => config('secure.salt') ]); } else { + // @codeCoverageIgnoreStart Log::warning("[SetupWizard] Failed to set application key. No write permission."); + // @codeCoverageIgnoreEnd } } @@ -61,7 +63,7 @@ class SetupController extends Controller $siteUrl = url('/'); if (ends_with($siteUrl, '/index.php')) { - $siteUrl = substr($siteUrl, 0, -10); + $siteUrl = substr($siteUrl, 0, -10); // @codeCoverageIgnore } Option::set('site_url', $siteUrl); @@ -143,9 +145,14 @@ class SetupController extends Controller try { Artisan::call('view:clear'); } catch (\Exception $e) { - Log::error('Error occured when processing view:clear', $e); + Log::error('Error occured when processing view:clear', [$e]); - File::cleanDirectory(storage_path('framework/views')); + $files = collect(File::files(storage_path('framework/views'))); + $files->reject(function ($path) { + return ends_with($path, '.gitignore'); + })->each(function ($path) { + File::delete($path); + }); } return view('setup.updates.success', ['tips' => $tips]); diff --git a/tests/SetupControllerTest.php b/tests/SetupControllerTest.php new file mode 100644 index 00000000..ecb9865a --- /dev/null +++ b/tests/SetupControllerTest.php @@ -0,0 +1,167 @@ +dropAllTables(); + } + + protected function tearDown() + { + $this->dropAllTables(); + Mockery::close(); + parent::tearDown(); + } + + protected function dropAllTables() + { + $tables = [ + 'closets', 'migrations', 'options', 'players', 'textures', 'users' + ]; + array_walk($tables, function ($table) { + Schema::dropIfExists($table); + }); + + return $this; + } + + public function testWelcome() + { + $this->visit('/setup') + ->see(config('database.connections.mysql.username')) + ->see(config('database.connections.mysql.host')); + } + + public function testInfo() + { + $this->visit('/setup/info') + ->seePageIs('/setup/info'); + } + + public function testFinish() + { + // Without `email` field + $this->post('/setup/finish') + ->dontSee(trans('setup.wizard.finish.title')); + + // Not an valid email address + $this->post('/setup/finish', ['email' => 'not_an_email']) + ->dontSee(trans('setup.wizard.finish.title')); + + // Without `password` field + $this->post('/setup/finish', [ + 'email' => 'a@b.c' + ])->dontSee(trans('setup.wizard.finish.title')); + + // Password is too short + $this->post('/setup/finish', [ + 'email' => 'a@b.c', + 'password' => '1' + ])->dontSee(trans('setup.wizard.finish.title')); + + // Password is too long + $this->post('/setup/finish', [ + 'email' => 'a@b.c', + 'password' => str_random(17) + ])->dontSee(trans('setup.wizard.finish.title')); + + // Confirmation is not OK + $this->post('/setup/finish', [ + 'email' => 'a@b.c', + 'password' => '12345678', + 'password_confirmation' => '12345679' + ])->dontSee(trans('setup.wizard.finish.title')); + + // Without `site_name` field + $this->post('/setup/finish', [ + 'email' => 'a@b.c', + 'password' => '12345678', + 'password_confirmation' => '12345678' + ])->dontSee(trans('setup.wizard.finish.title')); + + // Regenerate keys + Artisan::shouldReceive('call') + ->with('key:random') + ->once() + ->andReturn(true); + Artisan::shouldReceive('call') + ->with('salt:random') + ->once() + ->andReturn(true); + Artisan::shouldReceive('call') + ->with('migrate', ['--force' => true]) + ->once() + ->andReturnUsing(function () { + $migration = new CreateAllTables(); + $migration->up(); + }); + $this->post('/setup/finish', [ + 'email' => 'a@b.c', + 'password' => '12345678', + 'password_confirmation' => '12345678', + 'site_name' => 'bs', + 'generate_random' => true + ])->see(trans('setup.wizard.finish.title')) + ->see('a@b.c') + ->see('12345678'); + } + + public function testUpdate() + { + $this->visit('/setup/update') + ->see(trans('setup.locked.text')); + + option(['version' => '0.1.0']); + $this->visit('/setup/update') + ->see(trans('setup.updates.welcome.title')); + } + + 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 + + Artisan::shouldReceive('call') + ->with('view:clear') + ->andThrow(new Exception()); + config(['options.new_option' => 'value']); + $this->post('/setup/update') + ->assertViewHas('tips'); + $this->assertEquals('value', option('new_option')); + $this->assertEquals('3.1.1', option('version')); + unlink(database_path("update_scripts/update-$current_version-to-100.0.0.php")); + + option(['version' => '3.0.0']); // Fake old version + $this->post('/setup/update'); + $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()); + } +}