blessing-skin-server/tests/PluginControllerTest.php

237 lines
7.3 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;
2019-08-13 18:42:17 +08:00
use App\Services\Plugin;
use App\Services\PluginManager;
2017-11-16 10:09:58 +08:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PluginControllerTest extends TestCase
{
use DatabaseTransactions;
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-16 10:09:58 +08:00
{
parent::setUp();
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()
{
2019-08-13 18:42:17 +08:00
option(['plugins_enabled' => json_encode([
['name' => 'fake3', 'version' => '0.0.0'],
['name' => 'fake4', 'version' => '0.0.0'],
])]);
$this->mock(PluginManager::class, function ($mock) {
$mock->shouldReceive('get')
->with('fake1')
->once()
->andReturn(null);
$mock->shouldReceive('get')
->with('fake2')
->once()
->andReturn(new Plugin('', []));
$mock->shouldReceive('get')
->with('fake3')
->once()
->andReturn(new Plugin('', []));
$plugin = new Plugin(resource_path(''), ['config' => 'common/favicon.blade.php']);
$plugin->setEnabled(true);
$mock->shouldReceive('get')
->with('fake4')
->once()
->andReturn($plugin);
});
// No such plugin.
$this->get('/admin/plugins/config/fake1')
->assertNotFound();
2017-11-16 10:09:58 +08:00
// Plugin is disabled
2019-08-13 18:42:17 +08:00
$this->get('/admin/plugins/config/fake2')
2018-08-19 17:39:33 +08:00
->assertNotFound();
2017-11-16 10:09:58 +08:00
// Plugin is enabled but it doesn't have config view
2019-08-13 18:42:17 +08:00
$this->get('/admin/plugins/config/fake3')
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
2019-08-13 18:42:17 +08:00
$this->get('/admin/plugins/config/fake4')
2018-07-13 15:38:22 +08:00
->assertSuccessful();
2019-08-13 18:42:17 +08:00
option(['plugins_enabled' => '[]']);
2017-11-16 10:09:58 +08:00
}
public function testManage()
{
2019-08-13 18:42:17 +08:00
$this->mock(PluginManager::class, function ($mock) {
$mock->shouldReceive('get')
->with('nope')
->once()
->andReturn(null);
$mock->shouldReceive('get')
->with('fake1')
->once()
->andReturn(new Plugin('', []));
$mock->shouldReceive('get')
->with('fake2')
->once()
->andReturn(new Plugin('', ['name' => 'fake2']));
$mock->shouldReceive('getUnsatisfied')
->withArgs(function ($plugin) {
$this->assertEquals('fake2', $plugin->name);
return true;
})
->once()
->andReturn(collect([
'dep' => ['version' => '0.0.0', 'constraint' => '^6.6.6'],
'whatever' => ['version' => null, 'constraint' => '^1.2.3'],
]));
$mock->shouldReceive('get')
->with('fake3')
->once()
->andReturn(new Plugin('', ['name' => 'fake3', 'title' => 'Fake']));
$mock->shouldReceive('enable')
->with('fake3')
->once();
$mock->shouldReceive('getUnsatisfied')
->withArgs(function ($plugin) {
$this->assertEquals('fake3', $plugin->name);
return true;
})
->once()
->andReturn(collect([]));
$mock->shouldReceive('get')
->with('fake4')
->once()
->andReturn(new Plugin('', ['name' => 'fake4', 'title' => 'Fake']));
$mock->shouldReceive('disable')
->with('fake4')
->once();
$mock->shouldReceive('get')
->with('fake5')
->once()
->andReturn(new Plugin('', ['name' => 'fake5', 'title' => 'Fake']));
$mock->shouldReceive('delete')
->with('fake5')
->once();
});
2017-11-16 10:09:58 +08:00
// 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
2019-08-13 18:42:17 +08:00
$this->postJson('/admin/plugins/manage', ['name' => 'fake1'])
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-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2019-08-13 18:42:17 +08:00
'name' => 'fake2',
'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'),
2019-04-23 19:14:41 +08:00
'data' => [
'reason' => [
trans('admin.plugins.operations.unsatisfied.version', [
2019-08-13 18:42:17 +08:00
'name' => 'dep',
2019-04-23 19:14:41 +08:00
'constraint' => '^6.6.6',
]),
trans('admin.plugins.operations.unsatisfied.disabled', [
'name' => 'whatever',
]),
],
2019-05-19 13:49:44 +08:00
],
2018-06-29 18:26:48 +08:00
]);
2017-11-16 10:09:58 +08:00
// Enable a plugin
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2019-08-13 18:42:17 +08:00
'name' => 'fake3',
'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',
2019-08-13 18:42:17 +08:00
['plugin' => 'Fake']
),
2017-11-16 10:09:58 +08:00
]);
// Disable a plugin
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2019-08-13 18:42:17 +08:00
'name' => 'fake4',
'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',
2019-08-13 18:42:17 +08:00
['plugin' => 'Fake']
),
2017-11-16 10:09:58 +08:00
]);
// Delete a plugin
2018-07-13 15:38:22 +08:00
$this->postJson('/admin/plugins/manage', [
2019-08-13 18:42:17 +08:00
'name' => 'fake5',
'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
]);
}
public function testGetPluginData()
{
2019-08-13 18:42:17 +08:00
$this->mock(PluginManager::class, function ($mock) {
$mock->shouldReceive('all')
->once()
->andReturn(collect([new Plugin('', [
'name' => 'a',
'version' => '0.0.0',
'title' => ''
])]));
$mock->shouldReceive('getUnsatisfied')
->withArgs(function ($plugin) {
$this->assertEquals('a', $plugin->name);
return true;
})
->once()
->andReturn(collect(['b' => null]));
});
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
]);
}
}