2018-08-19 11:39:14 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2019-04-05 17:23:27 +08:00
|
|
|
use App\Services\PackageManager;
|
2018-08-19 11:39:14 +08:00
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
use Tests\Concerns\MocksGuzzleClient;
|
|
|
|
use Tests\Concerns\GeneratesFakePlugins;
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
|
|
|
|
class MarketControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
use MocksGuzzleClient;
|
|
|
|
use GeneratesFakePlugins;
|
|
|
|
|
2019-02-27 23:44:50 +08:00
|
|
|
protected function setUp(): void
|
2018-08-19 11:39:14 +08:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-02-27 23:44:50 +08:00
|
|
|
$this->actAs('superAdmin');
|
2018-08-19 11:39:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDownload()
|
|
|
|
{
|
|
|
|
$this->setupGuzzleClientMock();
|
|
|
|
|
|
|
|
// Try to download a non-existent plugin
|
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakePluginsRegistry());
|
|
|
|
$this->postJson('/admin/plugins/market/download', [
|
2019-03-02 22:58:37 +08:00
|
|
|
'name' => 'non-existent-plugin',
|
2018-08-19 11:39:14 +08:00
|
|
|
])->assertJson([
|
|
|
|
'errno' => 1,
|
2019-03-02 22:58:37 +08:00
|
|
|
'msg' => trans('admin.plugins.market.non-existent', ['plugin' => 'non-existent-plugin']),
|
2018-08-19 11:39:14 +08:00
|
|
|
]);
|
|
|
|
|
2019-04-05 17:23:27 +08:00
|
|
|
// Download
|
|
|
|
$fakeRegistry = $this->generateFakePluginsRegistry('fake-test-download', '0.0.1');
|
|
|
|
$this->appendToGuzzleQueue([new Response(200, [], $fakeRegistry)]);
|
|
|
|
app()->instance(PackageManager::class, new Concerns\FakePackageManager(null, true));
|
2018-08-19 11:39:14 +08:00
|
|
|
$this->postJson('/admin/plugins/market/download', [
|
2019-03-02 22:58:37 +08:00
|
|
|
'name' => 'fake-test-download',
|
2019-04-05 17:23:27 +08:00
|
|
|
])->assertJson(['errno' => 1]);
|
2018-08-19 11:39:14 +08:00
|
|
|
|
2019-04-05 17:23:27 +08:00
|
|
|
$this->appendToGuzzleQueue([new Response(200, [], $fakeRegistry)]);
|
|
|
|
app()->bind(PackageManager::class, Concerns\FakePackageManager::class);
|
2018-08-19 11:39:14 +08:00
|
|
|
$this->postJson('/admin/plugins/market/download', [
|
2019-03-02 22:58:37 +08:00
|
|
|
'name' => 'fake-test-download',
|
2019-04-05 17:23:27 +08:00
|
|
|
])->assertJson(['errno' => 0, 'msg' => trans('admin.plugins.market.install-success')]);
|
2018-08-19 11:39:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckUpdates()
|
|
|
|
{
|
|
|
|
$this->setupGuzzleClientMock();
|
|
|
|
|
|
|
|
// Not installed
|
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakePluginsRegistry('fake-test-update', '0.0.1'));
|
|
|
|
$this->getJson('/admin/plugins/market/check')
|
|
|
|
->assertJson([
|
|
|
|
'available' => false,
|
2019-03-02 22:58:37 +08:00
|
|
|
'plugins' => [],
|
2018-08-19 11:39:14 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Generate fake plugin and refresh plugin manager
|
|
|
|
$this->generateFakePlugin(['name' => 'fake-test-update', 'version' => '0.0.1']);
|
|
|
|
$this->app->singleton('plugins', \App\Services\PluginManager::class);
|
|
|
|
|
|
|
|
// Plugin up-to-date
|
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakePluginsRegistry('fake-test-update', '0.0.1'));
|
|
|
|
$this->getJson('/admin/plugins/market/check')
|
|
|
|
->assertJson([
|
|
|
|
'available' => false,
|
2019-03-02 22:58:37 +08:00
|
|
|
'plugins' => [],
|
2018-08-19 11:39:14 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// New version available
|
|
|
|
$this->appendToGuzzleQueue(200, [], $this->generateFakePluginsRegistry('fake-test-update', '2.3.3'));
|
|
|
|
$this->getJson('/admin/plugins/market/check')
|
|
|
|
->assertJson([
|
|
|
|
'available' => true,
|
|
|
|
'plugins' => [[
|
2019-03-02 22:58:37 +08:00
|
|
|
'name' => 'fake-test-update',
|
|
|
|
]],
|
2018-08-19 11:39:14 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMarketData()
|
|
|
|
{
|
2018-08-19 18:28:38 +08:00
|
|
|
$registry = $this->generateFakePluginsRegistry();
|
|
|
|
$package = json_decode($registry, true)['packages'][0];
|
|
|
|
$this->generateFakePlugin($package);
|
2018-08-19 11:39:14 +08:00
|
|
|
$this->setupGuzzleClientMock([
|
|
|
|
new RequestException('Connection Error', new Request('POST', 'whatever')),
|
2018-08-19 18:28:38 +08:00
|
|
|
new Response(200, [], $registry),
|
2018-08-19 11:39:14 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Expected an exception, but unable to be asserted.
|
|
|
|
$this->getJson('/admin/plugins/market-data');
|
|
|
|
|
|
|
|
$this->getJson('/admin/plugins/market-data')
|
|
|
|
->assertJsonStructure([
|
|
|
|
[
|
|
|
|
'name',
|
|
|
|
'title',
|
|
|
|
'version',
|
|
|
|
'installed',
|
|
|
|
'description',
|
|
|
|
'author',
|
|
|
|
'dist',
|
2019-03-02 22:58:37 +08:00
|
|
|
'dependencies',
|
|
|
|
],
|
2018-08-19 11:39:14 +08:00
|
|
|
]);
|
2018-08-19 18:38:05 +08:00
|
|
|
|
2019-03-14 00:13:32 +08:00
|
|
|
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.$package['name']);
|
2018-08-19 11:39:14 +08:00
|
|
|
}
|
|
|
|
|
2019-02-27 23:44:50 +08:00
|
|
|
protected function tearDown(): void
|
2018-08-19 11:39:14 +08:00
|
|
|
{
|
|
|
|
// Clean fake plugins
|
2019-03-14 00:02:00 +08:00
|
|
|
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-test-download');
|
|
|
|
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-test-update');
|
|
|
|
File::delete(config('plugins.directory').DIRECTORY_SEPARATOR.'whatever');
|
2018-08-19 11:39:14 +08:00
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
}
|