blessing-skin-server/tests/PluginControllerTest.php

177 lines
5.8 KiB
PHP
Raw Normal View History

2017-11-16 10:09:58 +08:00
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PluginControllerTest extends TestCase
{
use DatabaseTransactions;
protected function setUp()
{
parent::setUp();
2017-11-17 07:54:39 +08:00
$plugins = [
2018-02-25 11:23:07 +08:00
'example-plugin' => 'example-plugin_v1.0.0.zip',
2018-07-23 10:11:04 +08:00
'avatar-api' => 'avatar-api_v1.1.1.zip'
2017-11-17 07:54:39 +08:00
];
2017-11-17 07:54:39 +08:00
foreach ($plugins as $plugin_name => $filename) {
2018-02-16 17:53:10 +08:00
if (! file_exists(base_path('plugins/'.$plugin_name))) {
$user_agent = menv('USER_AGENT', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36');
2018-02-16 17:53:10 +08:00
$context = stream_context_create(['http' => [
'method' => 'GET',
'header' => "User-Agent: $user_agent"
2018-02-16 17:53:10 +08:00
]]);
2017-11-17 07:54:39 +08:00
file_put_contents(
storage_path('testing/'.$filename),
2018-02-16 17:53:10 +08:00
file_get_contents("https://coding.net/u/printempw/p/bs-plugins-archive/git/raw/master/$filename", false, $context)
2017-11-17 07:54:39 +08:00
);
$zip = new ZipArchive();
$zip->open(storage_path('testing/'.$filename));
$zip->extractTo(base_path('plugins/'));
$zip->close();
unlink(storage_path('testing/'.$filename));
}
}
2017-11-16 10:09:58 +08:00
return $this->actAs('admin');
}
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
$this->get('/admin/plugins/config/example-plugin')
2018-07-13 15:38:22 +08:00
->assertStatus(404);
2017-11-16 10:09:58 +08:00
// Plugin is enabled but it doesn't have config view
plugin('avatar-api')->setEnabled(true);
$this->get('/admin/plugins/config/avatar-api')
2018-07-13 15:38:22 +08:00
->assertStatus(404);
2017-11-16 10:09:58 +08:00
// Plugin has config view
plugin('example-plugin')->setEnabled(true);
$this->get('/admin/plugins/config/example-plugin')
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([
2017-11-16 10:09:58 +08:00
'errno' => 1,
'msg' => trans('admin.plugins.operations.not-found')
]);
// Invalid action
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', ['name' => 'avatar-api'])
->assertJson([
2017-11-16 10:09:58 +08:00
'errno' => 1,
'msg' => trans('admin.invalid-action')
]);
2018-07-23 10:11:04 +08:00
// Retrieve requirements
$this->postJson('/admin/plugins/manage', [
'name' => 'avatar-api',
'action' => 'requirements'
])->assertJson([
'isRequirementsSatisfied' => true,
'requirements' => [],
'unsatisfiedRequirements' => []
]);
2018-06-29 18:26:48 +08:00
// Enable a plugin with unsatisfied dependencies
app('plugins')->getPlugin('avatar-api')->setRequirements([
'blessing-skin-server' => '^3.4.0',
'example-plugin' => '^6.6.6',
'whatever' => '^1.0.0'
]);
app('plugins')->enable('example-plugin');
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2018-06-29 18:26:48 +08:00
'name' => 'avatar-api',
'action' => 'enable'
2018-07-13 15:38:22 +08:00
])->assertJson([
2018-06-29 18:26:48 +08:00
'errno' => 1,
'msg' => trans('admin.plugins.operations.unsatisfied.notice'),
'reason' => [
2018-06-29 18:26:48 +08:00
trans('admin.plugins.operations.unsatisfied.version', [
'name' => 'example-plugin',
'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
])
]
2018-06-29 18:26:48 +08:00
]);
2017-11-16 10:09:58 +08:00
// Enable a plugin
2018-06-29 18:26:48 +08:00
app('plugins')->getPlugin('avatar-api')->setRequirements([]);
2017-11-16 10:09:58 +08:00
$this->expectsEvents(App\Events\PluginWasEnabled::class);
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2017-11-16 10:09:58 +08:00
'name' => 'avatar-api',
'action' => 'enable'
2018-07-13 15:38:22 +08:00
])->assertJson([
2017-11-16 10:09:58 +08:00
'errno' => 0,
'msg' => trans(
'admin.plugins.operations.enabled',
['plugin' => plugin('avatar-api')->title]
)
]);
// Disable a plugin
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2017-11-16 10:09:58 +08:00
'name' => 'avatar-api',
'action' => 'disable'
2018-07-13 15:38:22 +08:00
])->assertJson([
2017-11-16 10:09:58 +08:00
'errno' => 0,
'msg' => trans(
'admin.plugins.operations.disabled',
['plugin' => plugin('avatar-api')->title]
)
]);
$this->expectsEvents(App\Events\PluginWasDisabled::class);
// Delete a plugin
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2017-11-16 10:09:58 +08:00
'name' => 'avatar-api',
'action' => 'delete'
2018-07-13 15:38:22 +08:00
])->assertJson([
2017-11-16 10:09:58 +08:00
'errno' => 0,
'msg' => trans('admin.plugins.operations.deleted')
]);
$this->expectsEvents(App\Events\PluginWasDeleted::class);
$this->assertFalse(file_exists(base_path('plugins/avatar-api/')));
}
public function testGetPluginData()
{
2018-07-13 15:38:22 +08:00
$this->getJson('/admin/plugins/data')
->assertJsonStructure([
2017-11-16 10:09:58 +08:00
'data' => [[
'name',
'version',
'path',
'title',
'description',
'author' => ['author', 'url'],
'url',
'namespace',
'status',
2018-06-29 18:26:48 +08:00
'dependencies',
2017-11-16 10:09:58 +08:00
'operations' => ['enabled', 'hasConfigView']
]]
]);
}
}