blessing-skin-server/tests/HttpTest/ControllersTest/UpdateControllerTest.php

170 lines
6.0 KiB
PHP
Raw Normal View History

2017-11-18 20:36:31 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2018-08-18 09:48:39 +08:00
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
2019-04-05 17:23:27 +08:00
use App\Services\PackageManager;
2019-12-10 23:40:32 +08:00
use Illuminate\Filesystem\Filesystem;
2018-08-18 09:48:39 +08:00
use Tests\Concerns\MocksGuzzleClient;
2019-12-10 23:40:32 +08:00
use Symfony\Component\Finder\SplFileInfo;
2018-08-18 09:48:39 +08:00
use GuzzleHttp\Exception\RequestException;
2019-12-10 23:40:32 +08:00
use Illuminate\Contracts\Console\Kernel as Artisan;
2017-11-18 20:36:31 +08:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2018-08-18 09:48:39 +08:00
class UpdateControllerTest extends TestCase
2017-11-18 20:36:31 +08:00
{
use DatabaseTransactions;
2018-08-18 09:48:39 +08:00
use MocksGuzzleClient;
2017-11-18 20:36:31 +08:00
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-18 20:36:31 +08:00
{
parent::setUp();
2019-02-27 23:44:50 +08:00
$this->actAs('superAdmin');
2017-11-18 20:36:31 +08:00
}
public function testShowUpdatePage()
{
2018-08-18 09:48:39 +08:00
$this->setupGuzzleClientMock();
// Can't connect to update source
$this->appendToGuzzleQueue([
new RequestException('Connection Error', new Request('GET', 'whatever')),
]);
$this->get('/admin/update')->assertSee(config('app.version'));
2019-04-06 22:52:43 +08:00
// Missing `spec` field
$this->appendToGuzzleQueue([
2019-07-05 14:48:12 +08:00
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3', ['spec' => 0])),
// Weird. Don't remove the following line, or the tests will fail.
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3', ['php' => '100.0.0'])),
2019-04-06 22:52:43 +08:00
]);
2019-07-05 14:48:12 +08:00
$this->get('/admin/update')->assertSee(trans('admin.update.errors.spec'));
// Low PHP version
$this->appendToGuzzleQueue([
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3', ['php' => '100.0.0'])),
]);
$this->get('/admin/update')->assertSee(trans('admin.update.errors.php', ['version' => '100.0.0']));
2019-04-06 22:52:43 +08:00
2018-08-18 09:48:39 +08:00
// New version available
2019-04-06 22:52:43 +08:00
$this->appendToGuzzleQueue([
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
]);
2019-04-05 22:53:07 +08:00
$this->get('/admin/update')->assertSee(config('app.version'))->assertSee('8.9.3');
2017-11-18 20:36:31 +08:00
}
public function testCheckUpdates()
{
2018-08-18 09:48:39 +08:00
$this->setupGuzzleClientMock();
// Update source is unavailable
$this->appendToGuzzleQueue([
new RequestException('Connection Error', new Request('GET', 'whatever')),
]);
2019-04-06 22:52:43 +08:00
$this->getJson('/admin/update/check')->assertJson(['available' => false]);
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// New version available
2019-04-06 22:52:43 +08:00
$this->appendToGuzzleQueue(200, [], $this->mockFakeUpdateInfo('8.9.3'));
$this->getJson('/admin/update/check')->assertJson(['available' => true]);
2017-11-18 20:36:31 +08:00
}
public function testDownload()
{
2018-08-18 09:48:39 +08:00
$this->setupGuzzleClientMock();
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Already up-to-date
$this->getJson('/admin/update/download')
->assertDontSee(trans('general.illegal-parameters'));
2019-04-05 17:23:27 +08:00
// Download
2018-08-18 09:48:39 +08:00
$this->appendToGuzzleQueue([
2019-04-06 22:52:43 +08:00
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
2018-08-18 09:48:39 +08:00
]);
2019-08-22 09:11:04 +08:00
$this->mock(PackageManager::class, function ($mock) {
$mock->shouldReceive('download')->andThrow(new \Exception('ddd'));
});
2019-04-05 17:23:27 +08:00
$this->getJson('/admin/update/download?action=download')
2019-04-23 11:47:45 +08:00
->assertJson(['code' => 1]);
2019-08-22 09:11:04 +08:00
$this->mock(PackageManager::class, function ($mock) {
$mock->shouldReceive('download')->andReturnSelf();
$mock->shouldReceive('extract')->andReturn(true);
$mock->shouldReceive('progress');
});
2019-08-26 11:01:49 +08:00
$this->mock(\Illuminate\Filesystem\Filesystem::class, function ($mock) {
$mock->shouldReceive('delete')->with(storage_path('options/cache.php'))->once();
2019-09-06 23:53:47 +08:00
$mock->shouldReceive('exists')->with(storage_path('install.lock'))->andReturn(true);
2019-08-26 11:01:49 +08:00
});
2019-04-05 17:23:27 +08:00
$this->getJson('/admin/update/download?action=download')
2019-04-23 11:47:45 +08:00
->assertJson(['code' => 0, 'message' => trans('admin.update.complete')]);
2018-08-18 09:48:39 +08:00
// Get download progress
2019-04-05 17:23:27 +08:00
$this->getJson('/admin/update/download?action=progress')
->assertSee('0');
2017-11-18 20:36:31 +08:00
// Invalid action
2019-04-06 22:52:43 +08:00
$this->appendToGuzzleQueue(200, [], $this->mockFakeUpdateInfo('8.9.3'));
2019-04-05 17:23:27 +08:00
$this->getJson('/admin/update/download?action=no')
2018-08-18 09:48:39 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('general.illegal-parameters'),
2017-11-18 20:36:31 +08:00
]);
}
2018-07-22 10:50:01 +08:00
2019-12-10 23:40:32 +08:00
public function testUpdate()
{
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('exists')
->with(storage_path('install.lock'))
->andReturn(true);
$mock->shouldReceive('put')
->with(storage_path('install.lock'), '')
->once()
->andReturn(true);
$mock->shouldReceive('files')
->with(database_path('update_scripts'))
->once()
->andReturn([
new SplFileInfo('/1.0.0.php', '', ''),
new SplFileInfo('/99.0.0.php', '', ''),
new SplFileInfo('/100.0.0.php', '', ''),
]);
$mock->shouldNotReceive('getRequire')->with('/1.0.0.php');
$mock->shouldReceive('getRequire')
->with('/99.0.0.php')
->once();
$mock->shouldReceive('getRequire')
->with('/100.0.0.php')
->once();
});
$this->spy(Artisan::class, function ($spy) {
$spy->shouldReceive('call')
->with('migrate', ['--force' => true])
->once();
$spy->shouldReceive('call')->with('view:clear')->once();
});
config(['app.version' => '100.0.0']);
$this->actAs('superAdmin')
->get('/setup/exec-update')
->assertViewIs('setup.updates.success');
$this->assertEquals('100.0.0', option('version'));
}
2019-07-05 14:48:12 +08:00
protected function mockFakeUpdateInfo(string $version, $extra = [])
2018-08-18 09:48:39 +08:00
{
2019-07-05 14:48:12 +08:00
return json_encode(array_merge([
'spec' => 2,
'php' => '7.2.0',
2019-04-06 22:52:43 +08:00
'latest' => $version,
'url' => "https://whatever.test/$version/update.zip",
2019-07-05 14:48:12 +08:00
], $extra));
2018-08-18 09:48:39 +08:00
}
2017-11-18 20:36:31 +08:00
}