Enhance rendering notice for unsatisfied plugin dependencies

This commit is contained in:
printempw 2018-06-29 20:54:40 +08:00
parent b4e8b7e8c9
commit 815fef7f3d
6 changed files with 25 additions and 24 deletions

View File

@ -48,22 +48,23 @@ class PluginController extends Controller
switch ($request->get('action')) { switch ($request->get('action')) {
case 'enable': case 'enable':
if (! $plugins->isRequirementsSatisfied($plugin)) { if (! $plugins->isRequirementsSatisfied($plugin)) {
$msg = ''; $reason = [];
foreach ($plugins->getUnsatisfiedRequirements($plugin) as $name => $detail) { foreach ($plugins->getUnsatisfiedRequirements($plugin) as $name => $detail) {
$constraint = $detail['constraint'];
if (! $detail['version']) { if (! $detail['version']) {
$msg .= '<li>'.trans('admin.plugins.operations.unsatisfied.disabled', [ $reason[] = trans('admin.plugins.operations.unsatisfied.disabled', compact('name'));
'name' => "<code>$name</code>"
]).'</li>';
} else { } else {
$msg .= '<li>'.trans('admin.plugins.operations.unsatisfied.version', [ $reason[] = trans('admin.plugins.operations.unsatisfied.version', compact('name', 'constraint'));
'name' => "<code>$name</code>",
'constraint' => "<code>{$detail['constraint']}</code>"
]).'</li>';
} }
} }
return json('<p>'.trans('admin.plugins.operations.unsatisfied.notice')."</p><ul>$msg</ul>", 1); return json([
'errno' => 1,
'msg' => trans('admin.plugins.operations.unsatisfied.notice'),
'reason' => $reason
]);
} }
$plugins->enable($name); $plugins->enable($name);

View File

@ -415,7 +415,7 @@ describe('tests for "plugins" module', () => {
requirements: { 'a': '^1.1.0', 'b': '^2.1.0', 'c': '^3.3.0' }, requirements: { 'a': '^1.1.0', 'b': '^2.1.0', 'c': '^3.3.0' },
unsatisfiedRequirements: { 'c': '^3.3.0' } unsatisfiedRequirements: { 'c': '^3.3.0' }
})) }))
.mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'notice', reason: ['reason1', 'reason2'] }))
.mockReturnValueOnce(Promise.reject()); .mockReturnValueOnce(Promise.reject());
const url = jest.fn(path => path); const url = jest.fn(path => path);
const swal = jest.fn() const swal = jest.fn()
@ -464,7 +464,7 @@ describe('tests for "plugins" module', () => {
expect(reloadTable).toBeCalledWith(null, false); expect(reloadTable).toBeCalledWith(null, false);
await enablePlugin('plugin'); await enablePlugin('plugin');
expect(swal).toBeCalledWith({ type: 'warning', html: 'warning' }); expect(swal).toBeCalledWith({ type: 'warning', html: '<p>notice</p><ul><li>reason1</li><li>reason2</li></ul>' });
await enablePlugin('plugin'); await enablePlugin('plugin');
expect(showAjaxError).toBeCalled(); expect(showAjaxError).toBeCalled();

View File

@ -102,17 +102,18 @@ async function enablePlugin(name) {
}); });
} }
const { errno, msg } = await fetch({ const { errno, msg, reason } = await fetch({
type: 'POST', type: 'POST',
url: url(`admin/plugins/manage?action=enable&name=${name}`), url: url(`admin/plugins/manage?action=enable&name=${name}`),
dataType: 'json' dataType: 'json'
}); });
if (errno === 0) { if (errno === 0) {
toastr.success(msg); toastr.success(msg);
$.pluginsTable.ajax.reload(null, false); $.pluginsTable.ajax.reload(null, false);
} else { } else {
swal({ type: 'warning', html: msg }); swal({ type: 'warning', html: `<p>${msg}</p><ul><li>${reason.join('</li><li>')}</li></ul>` });
} }
} catch (error) { } catch (error) {
showAjaxError(error); showAjaxError(error);

View File

@ -110,8 +110,8 @@ plugins:
enabled: :plugin has been enabled. enabled: :plugin has been enabled.
unsatisfied: unsatisfied:
notice: There are unsatisfied dependencies in the plugin, therefore we can't enable it. Please install or update the plugins listed below. notice: There are unsatisfied dependencies in the plugin, therefore we can't enable it. Please install or update the plugins listed below.
disabled: The :name plugin is not enabled. disabled: The <code>:name</code> plugin is not enabled
version: The version of :name does not satisfies the constraint :constraint version: The version of <code>:name</code> does not satisfies the constraint <code>:constraint</code>
disabled: :plugin has been disabled. disabled: :plugin has been disabled.
deleted: The plugin was deleted successfully. deleted: The plugin was deleted successfully.
no-config-notice: The plugin is not installed or doesn't provide configuration page. no-config-notice: The plugin is not installed or doesn't provide configuration page.

View File

@ -110,8 +110,8 @@ plugins:
enabled: :plugin 已启用 enabled: :plugin 已启用
unsatisfied: unsatisfied:
notice: 无法启用此插件,因为其仍有未满足的依赖关系。请检查以下插件的版本,更新或安装它们: notice: 无法启用此插件,因为其仍有未满足的依赖关系。请检查以下插件的版本,更新或安装它们:
disabled: :name 插件未启用 disabled: <code>:name</code> 插件未启用
version: :name 的版本不符合要求 :constraint version: <code>:name</code> 的版本不符合要求 <code>:constraint</code>
disabled: :plugin 已禁用 disabled: :plugin 已禁用
deleted: 插件已被成功删除 deleted: 插件已被成功删除
no-config-notice: 插件未安装或未提供配置页面 no-config-notice: 插件未安装或未提供配置页面

View File

@ -95,17 +95,16 @@ class PluginControllerTest extends TestCase
'action' => 'enable' 'action' => 'enable'
])->seeJson([ ])->seeJson([
'errno' => 1, 'errno' => 1,
'msg' => sprintf( 'msg' => trans('admin.plugins.operations.unsatisfied.notice'),
'<p>%s</p><ul><li>%s</li><li>%s</li></ul>', 'reason' => [
trans('admin.plugins.operations.unsatisfied.notice'),
trans('admin.plugins.operations.unsatisfied.version', [ trans('admin.plugins.operations.unsatisfied.version', [
'name' => "<code>example-plugin</code>", 'name' => 'example-plugin',
'constraint' => "<code>^6.6.6</code>" 'constraint' => '^6.6.6'
]), ]),
trans('admin.plugins.operations.unsatisfied.disabled', [ trans('admin.plugins.operations.unsatisfied.disabled', [
'name' => "<code>whatever</code>" 'name' => 'whatever'
]) ])
) ]
]); ]);
// Enable a plugin // Enable a plugin