2017-11-18 20:36:31 +08:00
|
|
|
<?php
|
|
|
|
|
2018-08-17 15:25:08 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2020-10-14 11:56:34 +08:00
|
|
|
use App\Models\User;
|
2020-01-14 10:58:20 +08:00
|
|
|
use App\Services\Unzip;
|
2017-11-18 20:36:31 +08:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2020-03-09 17:18:36 +08:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
class UpdateControllerTest extends TestCase
|
2017-11-18 20:36:31 +08:00
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2019-02-27 23:44:50 +08:00
|
|
|
protected function setUp(): void
|
2017-11-18 20:36:31 +08:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-10-14 11:56:34 +08:00
|
|
|
$this->actingAs(User::factory()->superAdmin()->create());
|
2017-11-18 20:36:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowUpdatePage()
|
|
|
|
{
|
2020-03-09 17:18:36 +08:00
|
|
|
Http::fakeSequence()
|
|
|
|
->pushStatus(404)
|
|
|
|
->push($this->fakeUpdateInfo('8.9.3', ['spec' => 0]))
|
|
|
|
->push($this->fakeUpdateInfo('8.9.3', ['php' => '100.0.0']))
|
|
|
|
->push($this->fakeUpdateInfo('8.9.3'));
|
2018-08-18 09:48:39 +08:00
|
|
|
|
|
|
|
// Can't connect to update source
|
|
|
|
$this->get('/admin/update')->assertSee(config('app.version'));
|
|
|
|
|
2019-04-06 22:52:43 +08:00
|
|
|
// Missing `spec` field
|
2019-07-05 14:48:12 +08:00
|
|
|
$this->get('/admin/update')->assertSee(trans('admin.update.errors.spec'));
|
|
|
|
|
|
|
|
// Low PHP version
|
|
|
|
$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-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 testDownload()
|
|
|
|
{
|
2020-03-09 17:18:36 +08:00
|
|
|
Http::fake([
|
|
|
|
config('app.update_source') => Http::sequence()
|
|
|
|
->push($this->fakeUpdateInfo('1.2.3'))
|
|
|
|
->whenEmpty($this->fakeUpdateInfo('8.9.3')),
|
|
|
|
'https://whatever.test/8.9.3/update.zip' => Http::sequence()
|
|
|
|
->pushStatus(404)
|
|
|
|
->pushStatus(200),
|
|
|
|
]);
|
2017-11-18 20:36:31 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
// Already up-to-date
|
2020-01-13 08:37:57 +08:00
|
|
|
$this->postJson('/admin/update/download')
|
2020-01-06 22:47:29 +08:00
|
|
|
->assertJson([
|
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('admin.update.info.up-to-date'),
|
|
|
|
]);
|
2018-08-18 09:48:39 +08:00
|
|
|
|
2019-04-05 17:23:27 +08:00
|
|
|
// Download
|
2020-01-13 08:37:57 +08:00
|
|
|
$this->postJson('/admin/update/download')->assertJson(['code' => 1]);
|
2020-01-14 10:58:20 +08:00
|
|
|
$this->mock(Unzip::class, function ($mock) {
|
|
|
|
$mock->shouldReceive('extract')->once()->andReturn();
|
2019-08-22 09:11:04 +08:00
|
|
|
});
|
2019-08-26 11:01:49 +08:00
|
|
|
$this->mock(\Illuminate\Filesystem\Filesystem::class, function ($mock) {
|
2019-12-23 23:28:46 +08:00
|
|
|
$mock->shouldReceive('delete')->with(storage_path('options.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
|
|
|
});
|
2020-01-13 08:37:57 +08:00
|
|
|
$this->postJson('/admin/update/download')
|
2019-04-23 11:47:45 +08:00
|
|
|
->assertJson(['code' => 0, 'message' => trans('admin.update.complete')]);
|
2017-11-18 20:36:31 +08:00
|
|
|
}
|
2018-07-22 10:50:01 +08:00
|
|
|
|
2020-03-09 17:18:36 +08:00
|
|
|
protected function fakeUpdateInfo(string $version, $extra = [])
|
2018-08-18 09:48:39 +08:00
|
|
|
{
|
2020-03-09 17:18:36 +08:00
|
|
|
return array_merge([
|
2019-07-05 14:48:12 +08:00
|
|
|
'spec' => 2,
|
2020-10-13 10:10:03 +08:00
|
|
|
'php' => '7.4.0',
|
2019-04-06 22:52:43 +08:00
|
|
|
'latest' => $version,
|
|
|
|
'url' => "https://whatever.test/$version/update.zip",
|
2020-03-09 17:18:36 +08:00
|
|
|
], $extra);
|
2018-08-18 09:48:39 +08:00
|
|
|
}
|
2017-11-18 20:36:31 +08:00
|
|
|
}
|