blessing-skin-server/tests/PluginControllerTest.php

151 lines
4.9 KiB
PHP
Raw Normal View History

2017-11-16 10:09:58 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2018-08-19 17:39:33 +08:00
use Illuminate\Support\Facades\File;
use Tests\Concerns\GeneratesFakePlugins;
2017-11-16 10:09:58 +08:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PluginControllerTest extends TestCase
{
use DatabaseTransactions;
2018-08-19 17:39:33 +08:00
use GeneratesFakePlugins;
2017-11-16 10:09:58 +08:00
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-16 10:09:58 +08:00
{
parent::setUp();
2017-11-17 07:54:39 +08:00
2018-08-19 17:39:33 +08:00
$this->generateFakePlugin(['name' => 'fake-plugin-for-test', 'version' => '1.1.4']);
$this->generateFakePlugin(['name' => 'fake-plugin-with-config-view', 'version' => '5.1.4', 'config' => 'config.blade.php']);
2019-02-27 23:44:50 +08:00
$this->actAs('superAdmin');
2017-11-16 10:09:58 +08:00
}
public function testShowManage()
{
2018-07-13 15:38:22 +08:00
$this->get('/admin/plugins/manage')
->assertSee(trans('general.plugin-manage'));
2017-11-16 10:09:58 +08:00
}
public function testConfig()
{
// Plugin is disabled
2018-08-19 17:39:33 +08:00
$this->get('/admin/plugins/config/fake-plugin-with-config-view')
->assertNotFound();
2017-11-16 10:09:58 +08:00
// Plugin is enabled but it doesn't have config view
2018-08-19 17:39:33 +08:00
plugin('fake-plugin-for-test')->setEnabled(true);
2017-11-16 10:09:58 +08:00
$this->get('/admin/plugins/config/avatar-api')
2019-02-16 21:12:06 +08:00
->assertSee(trans('admin.plugins.operations.no-config-notice'))
2018-08-19 17:39:33 +08:00
->assertNotFound();
2017-11-16 10:09:58 +08:00
// Plugin has config view
2018-08-19 17:54:00 +08:00
plugin('fake-plugin-with-config-view')->setEnabled(true);
$this->get('/admin/plugins/config/fake-plugin-with-config-view')
2018-07-13 15:38:22 +08:00
->assertSuccessful();
2017-11-16 10:09:58 +08:00
}
public function testManage()
{
// An not-existed plugin
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', ['name' => 'nope'])
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.plugins.operations.not-found'),
2017-11-16 10:09:58 +08:00
]);
// Invalid action
2018-08-19 17:39:33 +08:00
$this->postJson('/admin/plugins/manage', ['name' => 'fake-plugin-for-test'])
2018-07-13 15:38:22 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.invalid-action'),
2017-11-16 10:09:58 +08:00
]);
2018-06-29 18:26:48 +08:00
// Enable a plugin with unsatisfied dependencies
2018-08-19 17:39:33 +08:00
app('plugins')->getPlugin('fake-plugin-for-test')->setRequirements([
2018-09-06 19:31:35 +08:00
'blessing-skin-server' => '^3.4.0 || ^4.0.0',
2018-08-19 17:39:33 +08:00
'fake-plugin-with-config-view' => '^6.6.6',
'whatever' => '^1.0.0',
2018-06-29 18:26:48 +08:00
]);
2018-08-19 17:39:33 +08:00
app('plugins')->enable('fake-plugin-with-config-view');
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2018-08-19 17:39:33 +08:00
'name' => 'fake-plugin-for-test',
'action' => 'enable',
2018-07-13 15:38:22 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.plugins.operations.unsatisfied.notice'),
'reason' => [
2018-06-29 18:26:48 +08:00
trans('admin.plugins.operations.unsatisfied.version', [
2018-08-19 17:39:33 +08:00
'name' => 'fake-plugin-with-config-view',
'constraint' => '^6.6.6',
2018-06-29 18:26:48 +08:00
]),
trans('admin.plugins.operations.unsatisfied.disabled', [
'name' => 'whatever',
]),
],
2018-06-29 18:26:48 +08:00
]);
2017-11-16 10:09:58 +08:00
// Enable a plugin
2018-08-19 17:39:33 +08:00
app('plugins')->getPlugin('fake-plugin-for-test')->setRequirements([]);
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2018-08-19 17:39:33 +08:00
'name' => 'fake-plugin-for-test',
'action' => 'enable',
2018-07-13 15:38:22 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans(
2017-11-16 10:09:58 +08:00
'admin.plugins.operations.enabled',
2018-08-19 17:39:33 +08:00
['plugin' => plugin('fake-plugin-for-test')->title]
),
2017-11-16 10:09:58 +08:00
]);
// Disable a plugin
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2018-08-19 17:39:33 +08:00
'name' => 'fake-plugin-for-test',
'action' => 'disable',
2018-07-13 15:38:22 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans(
2017-11-16 10:09:58 +08:00
'admin.plugins.operations.disabled',
2018-08-19 17:39:33 +08:00
['plugin' => plugin('fake-plugin-for-test')->title]
),
2017-11-16 10:09:58 +08:00
]);
// Delete a plugin
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2018-08-19 17:39:33 +08:00
'name' => 'fake-plugin-for-test',
'action' => 'delete',
2018-07-13 15:38:22 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('admin.plugins.operations.deleted'),
2017-11-16 10:09:58 +08:00
]);
2018-08-19 17:39:33 +08:00
$this->assertFalse(file_exists(base_path('plugins/fake-plugin-for-test/')));
2017-11-16 10:09:58 +08:00
}
public function testGetPluginData()
{
2018-07-13 15:38:22 +08:00
$this->getJson('/admin/plugins/data')
->assertJsonStructure([
2018-08-19 17:39:33 +08:00
[
2017-11-16 10:09:58 +08:00
'name',
'version',
'title',
'description',
2018-08-11 23:36:28 +08:00
'author',
2017-11-16 10:09:58 +08:00
'url',
2018-08-11 23:36:28 +08:00
'enabled',
2018-08-19 17:39:33 +08:00
'config',
'dependencies',
],
2017-11-16 10:09:58 +08:00
]);
}
2018-08-19 17:39:33 +08:00
2019-02-27 23:44:50 +08:00
protected function tearDown(): void
2018-08-19 17:39:33 +08:00
{
// Clean fake plugins
2019-03-14 00:02:00 +08:00
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-plugin-for-test');
2019-03-14 00:13:32 +08:00
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-plugin-with-config-view');
2018-08-19 17:39:33 +08:00
parent::tearDown();
}
2017-11-16 10:09:58 +08:00
}