blessing-skin-server/tests/UpdateControllerTest.php

112 lines
3.8 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;
2018-08-18 09:48:39 +08:00
use Tests\Concerns\MocksGuzzleClient;
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();
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-04-05 17:23:27 +08:00
app()->instance(PackageManager::class, new Concerns\FakePackageManager(null, true));
$this->getJson('/admin/update/download?action=download')
2019-04-23 11:47:45 +08:00
->assertJson(['code' => 1]);
2019-04-05 17:23:27 +08:00
app()->bind(PackageManager::class, Concerns\FakePackageManager::class);
$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-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,
2019-08-09 22:56:54 +08:00
'php' => '7.2.12',
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
}