2017-11-18 20:36:31 +08:00
|
|
|
<?php
|
|
|
|
|
2018-08-17 15:25:08 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2019-04-04 09:48:04 +08:00
|
|
|
use Cache;
|
2018-08-17 15:25:08 +08:00
|
|
|
use Exception;
|
|
|
|
use ZipArchive;
|
2018-08-18 09:48:39 +08:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-11-18 20:36:31 +08:00
|
|
|
use Illuminate\Support\Facades\File;
|
2018-08-18 09:48:39 +08:00
|
|
|
use Tests\Concerns\MocksGuzzleClient;
|
2018-08-17 15:25:08 +08:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2018-08-18 09:48:39 +08:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
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();
|
2017-11-19 12:49:24 +08:00
|
|
|
|
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')),
|
|
|
|
new RequestException('Connection Error', new Request('GET', 'whatever')),
|
|
|
|
]);
|
|
|
|
$this->get('/admin/update')->assertSee(config('app.version'));
|
|
|
|
|
|
|
|
// New version available
|
|
|
|
$time = time();
|
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3', false, $time));
|
|
|
|
$this->get('/admin/update')
|
|
|
|
->assertSee(config('app.version'))
|
|
|
|
->assertSee('8.9.3')
|
|
|
|
->assertSee('test')
|
|
|
|
->assertSee(Carbon::createFromTimestamp($time)->toDateTimeString());
|
|
|
|
|
|
|
|
// Now using pre-release version
|
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('0.0.1', false, $time));
|
|
|
|
$this->get('/admin/update');
|
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')),
|
|
|
|
new RequestException('Connection Error', new Request('GET', 'whatever')),
|
|
|
|
]);
|
|
|
|
$this->getJson('/admin/update/check')
|
|
|
|
->assertJson([
|
2017-11-18 20:36:31 +08:00
|
|
|
'latest' => null,
|
2019-03-02 22:58:37 +08:00
|
|
|
'available' => false,
|
2017-11-18 20:36:31 +08:00
|
|
|
]);
|
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// New version available
|
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3', false, time()));
|
|
|
|
$this->getJson('/admin/update/check')
|
|
|
|
->assertJson([
|
|
|
|
'latest' => '8.9.3',
|
2019-03-02 22:58:37 +08:00
|
|
|
'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'));
|
|
|
|
|
|
|
|
// Lack write permission
|
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3'));
|
|
|
|
File::deleteDirectory(storage_path('update_cache'));
|
2017-11-18 20:36:31 +08:00
|
|
|
Storage::shouldReceive('disk')
|
2018-07-12 17:18:19 +08:00
|
|
|
->with('root')
|
2018-08-18 09:48:39 +08:00
|
|
|
->once()
|
2017-11-18 20:36:31 +08:00
|
|
|
->andReturnSelf();
|
|
|
|
Storage::shouldReceive('makeDirectory')
|
2018-07-12 17:18:19 +08:00
|
|
|
->with('storage/update_cache')
|
2018-08-18 09:48:39 +08:00
|
|
|
->once()
|
2017-11-18 20:36:31 +08:00
|
|
|
->andReturn(false);
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=prepare-download')
|
|
|
|
->assertJson([
|
|
|
|
'errno' => 1,
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => trans('admin.update.errors.write-permission'),
|
2018-08-18 09:48:39 +08:00
|
|
|
]);
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// Prepare for downloading
|
2017-11-18 20:36:31 +08:00
|
|
|
mkdir(storage_path('update_cache'));
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->appendToGuzzleQueue([
|
|
|
|
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
|
|
|
|
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
|
|
|
|
]);
|
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=prepare-download')
|
|
|
|
->assertJsonStructure(['release_url', 'tmp_path']);
|
2019-04-04 09:48:04 +08:00
|
|
|
$this->assertTrue(Cache::has('tmp_path'));
|
|
|
|
$this->assertFalse(Cache::has('download-progress'));
|
2017-11-18 20:36:31 +08:00
|
|
|
|
|
|
|
// Start downloading
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::flush();
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=start-download')
|
|
|
|
->assertJson([
|
|
|
|
'errno' => 1,
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => 'No temp path available, please try again.',
|
2018-08-18 09:48:39 +08:00
|
|
|
]);
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// Can't download update package
|
|
|
|
$this->appendToGuzzleQueue([
|
|
|
|
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
|
|
|
|
new RequestException('Connection Error', new Request('GET', 'whatever')),
|
|
|
|
]);
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::put('tmp_path', storage_path('update_cache/update.zip'));
|
|
|
|
$this->getJson('/admin/update/download?action=start-download');
|
2018-08-18 09:48:39 +08:00
|
|
|
|
|
|
|
// Download update package
|
|
|
|
$fakeUpdatePackage = $this->generateFakeUpdateFile();
|
|
|
|
$this->appendToGuzzleQueue([
|
|
|
|
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
|
|
|
|
new Response(200, [], fopen($fakeUpdatePackage, 'r')),
|
|
|
|
]);
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::put('tmp_path', storage_path('update_cache/update.zip'));
|
|
|
|
$this->getJson('/admin/update/download?action=start-download')
|
2018-08-18 09:48:39 +08:00
|
|
|
->assertJson([
|
2019-03-02 22:58:37 +08:00
|
|
|
'tmp_path' => storage_path('update_cache/update.zip'),
|
2018-08-18 09:48:39 +08:00
|
|
|
]);
|
|
|
|
$this->assertFileExists(storage_path('update_cache/update.zip'));
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// No download progress available
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::flush();
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=get-progress')
|
|
|
|
->assertJson([]);
|
|
|
|
|
|
|
|
// Get download progress
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::put('download-progress', ['total' => 514, 'downloaded' => 114]);
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=get-progress')
|
|
|
|
->assertJson([
|
2018-08-17 23:24:08 +08:00
|
|
|
'total' => 514,
|
2019-03-02 22:58:37 +08:00
|
|
|
'downloaded' => 114,
|
2017-11-18 20:36:31 +08:00
|
|
|
]);
|
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// No such zip archive
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::put('tmp_path', storage_path('update_cache/nope.zip'));
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=extract')
|
|
|
|
->assertJson([
|
|
|
|
'errno' => 1,
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => 'No file available',
|
2018-08-18 09:48:39 +08:00
|
|
|
]);
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// Can't extract zip archive
|
2017-11-18 20:36:31 +08:00
|
|
|
file_put_contents(storage_path('update_cache/update.zip'), 'text');
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::put('tmp_path', storage_path('update_cache/update.zip'));
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=extract')
|
|
|
|
->assertJson([
|
|
|
|
'errno' => 1,
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => trans('admin.update.errors.unzip').'19',
|
2018-08-18 09:48:39 +08:00
|
|
|
]);
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// Extract
|
|
|
|
copy(storage_path('testing/update.zip'), storage_path('update_cache/update.zip'));
|
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=extract')
|
|
|
|
->assertJson([
|
|
|
|
'errno' => 0,
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => trans('admin.update.complete'),
|
2018-08-18 09:48:39 +08:00
|
|
|
]);
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// Can't overwrite vendor directory, skip
|
2017-11-18 20:36:31 +08:00
|
|
|
mkdir(storage_path('update_cache'));
|
2018-08-18 09:48:39 +08:00
|
|
|
copy(storage_path('testing/update.zip'), storage_path('update_cache/update.zip'));
|
2017-11-18 20:36:31 +08:00
|
|
|
File::shouldReceive('copyDirectory')
|
2018-08-18 09:48:39 +08:00
|
|
|
->with(storage_path('update_cache/8.9.3/vendor'), base_path('vendor'))
|
|
|
|
->andThrow(new Exception);
|
2017-11-18 20:36:31 +08:00
|
|
|
File::shouldReceive('deleteDirectory')
|
2018-08-18 09:48:39 +08:00
|
|
|
->with(storage_path('update_cache/8.9.3/vendor'));
|
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=extract');
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// Can't apply update package
|
2017-11-18 20:36:31 +08:00
|
|
|
File::shouldReceive('copyDirectory')
|
2018-08-18 09:48:39 +08:00
|
|
|
->with(storage_path('update_cache/8.9.3'), base_path())
|
2017-11-18 20:36:31 +08:00
|
|
|
->andThrow(new Exception);
|
|
|
|
File::shouldReceive('deleteDirectory')
|
|
|
|
->with(storage_path('update_cache'));
|
|
|
|
File::shouldReceive('deleteDirectory')
|
|
|
|
->with(storage_path('update_cache'));
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=extract')
|
|
|
|
->assertJson([
|
|
|
|
'errno' => 1,
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => trans('admin.update.errors.overwrite'),
|
2018-08-18 09:48:39 +08:00
|
|
|
]);
|
2017-11-18 20:36:31 +08:00
|
|
|
|
|
|
|
// Invalid action
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->withNewVersionAvailable()
|
|
|
|
->getJson('/admin/update/download?action=no')
|
|
|
|
->assertJson([
|
2017-11-18 20:36:31 +08:00
|
|
|
'errno' => 1,
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => trans('general.illegal-parameters'),
|
2017-11-18 20:36:31 +08:00
|
|
|
]);
|
|
|
|
}
|
2018-07-22 10:50:01 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
protected function withNewVersionAvailable()
|
2018-07-22 10:50:01 +08:00
|
|
|
{
|
2018-08-18 09:48:39 +08:00
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3'));
|
2019-03-02 22:58:37 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function generateFakeUpdateInfo($version, $preview = false, $time = null)
|
|
|
|
{
|
|
|
|
$time = $time ?: time();
|
|
|
|
|
|
|
|
return json_encode([
|
|
|
|
'app_name' => 'blessing-skin-server',
|
|
|
|
'latest_version' => $version,
|
|
|
|
'update_time' => $time,
|
|
|
|
'releases' => [
|
|
|
|
$version => [
|
|
|
|
'version' => $version,
|
|
|
|
'pre_release' => $preview,
|
|
|
|
'release_time' => $time,
|
|
|
|
'release_note' => 'test',
|
2019-03-02 22:58:37 +08:00
|
|
|
'release_url' => "https://whatever.test/$version/update.zip",
|
|
|
|
],
|
|
|
|
],
|
2018-08-18 09:48:39 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function generateFakeUpdateFile()
|
|
|
|
{
|
|
|
|
$zipPath = storage_path('testing/update.zip');
|
|
|
|
|
|
|
|
if (file_exists($zipPath)) {
|
|
|
|
unlink($zipPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
$zip->open($zipPath, ZipArchive::CREATE);
|
|
|
|
$zip->addEmptyDir('coverage');
|
|
|
|
$zip->close();
|
|
|
|
|
|
|
|
return $zipPath;
|
2018-07-22 10:50:01 +08:00
|
|
|
}
|
2017-11-18 20:36:31 +08:00
|
|
|
}
|