Update PluginControllerTest

This commit is contained in:
printempw 2018-08-15 23:13:34 +08:00
parent b124c75e4b
commit dafe2583fe
3 changed files with 43 additions and 43 deletions

View File

@ -6,7 +6,6 @@ git:
cache:
directories:
- vendor
- plugins
- node_modules
env:

View File

@ -92,9 +92,22 @@ trait GenerateFakePlugins
mkdir($plugin_dir);
}
// Generate fake config view
if ($config = array_get($info, 'config')) {
$views_path = "$plugin_dir/views";
if (! is_dir($views_path)) {
mkdir($views_path);
}
file_put_contents("$views_path/$config", str_random(64));
}
file_put_contents("$plugin_dir/package.json", json_encode(
$this->generateFakePlguinInfo($info)
));
file_put_contents("$plugin_dir/bootstrap.php", "<?php retrun function () { return '{$info['name']}'; };");
}
/**

View File

@ -6,36 +6,15 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
class PluginControllerTest extends TestCase
{
use GenerateFakePlugins;
use DatabaseTransactions;
protected function setUp()
{
parent::setUp();
$plugins = [
'example-plugin' => 'example-plugin_v1.0.0.zip',
'hello-dolly' => 'hello-dolly_v1.0.0.zip'
];
foreach ($plugins as $plugin_name => $filename) {
if (! file_exists(base_path('plugins/'.$plugin_name))) {
$context = stream_context_create(['http' => [
'method' => 'GET',
'header' => 'User-Agent: '.config('secure.user_agent')
]]);
file_put_contents(
storage_path('testing/'.$filename),
file_get_contents("https://coding.net/u/printempw/p/bs-plugins-archive/git/raw/master/$filename", false, $context)
);
$zip = new ZipArchive();
$zip->open(storage_path('testing/'.$filename));
$zip->extractTo(base_path('plugins/'));
$zip->close();
unlink(storage_path('testing/'.$filename));
}
}
$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']);
return $this->actAs('superAdmin');
}
@ -49,19 +28,19 @@ class PluginControllerTest extends TestCase
public function testConfig()
{
// Plugin is disabled
$this->get('/admin/plugins/config/example-plugin')
$this->get('/admin/plugins/config/fake-plugin-with-config-view')
->see(trans('admin.plugins.operations.no-config-notice'))
->assertResponseStatus(404);
// Plugin is enabled but it doesn't have config view
plugin('hello-dolly')->setEnabled(true);
$this->get('/admin/plugins/config/hello-dolly')
plugin('fake-plugin-for-test')->setEnabled(true);
$this->get('/admin/plugins/config/fake-plugin-for-test')
->see(trans('admin.plugins.operations.no-config-notice'))
->assertResponseStatus(404);
// Plugin has config view
plugin('example-plugin')->setEnabled(true);
$this->get('/admin/plugins/config/example-plugin')
plugin('fake-plugin-with-config-view')->setEnabled(true);
$this->get('/admin/plugins/config/fake-plugin-with-config-view')
->assertResponseStatus(200);
}
@ -75,28 +54,28 @@ class PluginControllerTest extends TestCase
]);
// Invalid action
$this->post('/admin/plugins/manage', ['name' => 'hello-dolly'])
$this->post('/admin/plugins/manage', ['name' => 'fake-plugin-for-test'])
->seeJson([
'errno' => 1,
'msg' => trans('admin.invalid-action')
]);
// Enable a plugin with unsatisfied dependencies
app('plugins')->getPlugin('hello-dolly')->setRequirements([
app('plugins')->getPlugin('fake-plugin-for-test')->setRequirements([
'blessing-skin-server' => '^3.4.0',
'example-plugin' => '^6.6.6',
'fake-plugin-with-config-view' => '^6.6.6',
'whatever' => '^1.0.0'
]);
app('plugins')->enable('example-plugin');
app('plugins')->enable('fake-plugin-with-config-view');
$this->post('/admin/plugins/manage', [
'name' => 'hello-dolly',
'name' => 'fake-plugin-for-test',
'action' => 'enable'
])->seeJson([
'errno' => 1,
'msg' => trans('admin.plugins.operations.unsatisfied.notice'),
'reason' => [
trans('admin.plugins.operations.unsatisfied.version', [
'name' => 'example-plugin',
'name' => 'fake-plugin-with-config-view',
'constraint' => '^6.6.6'
]),
trans('admin.plugins.operations.unsatisfied.disabled', [
@ -106,42 +85,42 @@ class PluginControllerTest extends TestCase
]);
// Enable a plugin
app('plugins')->getPlugin('hello-dolly')->setRequirements([]);
app('plugins')->getPlugin('fake-plugin-for-test')->setRequirements([]);
$this->expectsEvents(App\Events\PluginWasEnabled::class);
$this->post('/admin/plugins/manage', [
'name' => 'hello-dolly',
'name' => 'fake-plugin-for-test',
'action' => 'enable'
])->seeJson([
'errno' => 0,
'msg' => trans(
'admin.plugins.operations.enabled',
['plugin' => plugin('hello-dolly')->title]
['plugin' => plugin('fake-plugin-for-test')->title]
)
]);
// Disable a plugin
$this->post('/admin/plugins/manage', [
'name' => 'hello-dolly',
'name' => 'fake-plugin-for-test',
'action' => 'disable'
])->seeJson([
'errno' => 0,
'msg' => trans(
'admin.plugins.operations.disabled',
['plugin' => plugin('hello-dolly')->title]
['plugin' => plugin('fake-plugin-for-test')->title]
)
]);
$this->expectsEvents(App\Events\PluginWasDisabled::class);
// Delete a plugin
$this->post('/admin/plugins/manage', [
'name' => 'hello-dolly',
'name' => 'fake-plugin-for-test',
'action' => 'delete'
])->seeJson([
'errno' => 0,
'msg' => trans('admin.plugins.operations.deleted')
]);
$this->expectsEvents(App\Events\PluginWasDeleted::class);
$this->assertFalse(file_exists(base_path('plugins/hello-dolly/')));
$this->assertFalse(file_exists(base_path('plugins/fake-plugin-for-test/')));
}
public function testGetPluginData()
@ -162,4 +141,13 @@ class PluginControllerTest extends TestCase
]]
]);
}
protected function tearDown()
{
// Clean fake plugins
File::deleteDirectory(base_path('plugins/fake-plugin-for-test'));
File::deleteDirectory(base_path('plugins/fake-plugin-with-config-view'));
parent::tearDown();
}
}