Add check for plugin updates

This commit is contained in:
printempw 2018-08-12 22:43:21 +08:00
parent 4a2edb2291
commit 6570e3327f
6 changed files with 106 additions and 2 deletions

View File

@ -47,6 +47,19 @@ class MarketController extends Controller
return Datatables::of($plugins)->setRowId('plugin-{{ $name }}')->make(true);
}
public function checkUpdates()
{
$pluginsHaveUpdate = collect($this->getAllAvailablePlugins())->filter(function ($item) {
$plugin = plugin($item['name']);
return $plugin && Comparator::greaterThan($item['version'], $plugin->version);
});
return json([
'available' => !$pluginsHaveUpdate->isEmpty(),
'plugins' => array_values($pluginsHaveUpdate->all())
]);
}
public function download(Request $request, PluginManager $manager)
{
$name = $request->get('name');

View File

@ -682,6 +682,42 @@ describe('tests for "market" module', () => {
expect(toastr.success).toBeCalledWith('success');
expect(reloadTable).toBeCalledWith(null, false);
});
it('check for plugin updates', async () => {
const fetch = jest.fn()
.mockReturnValueOnce(Promise.resolve({
available: false,
plugins: []
}))
.mockReturnValueOnce(Promise.resolve({
available: true,
plugins: [{
name: 'hello-dolly',
version: '8.1.0'
}]
}));
const url = jest.fn(path => path);
window.fetch = fetch;
window.url = url;
document.body.innerHTML = `
<a id="target" href="admin/plugins/market"><i class="fa fa-shopping-bag"></i> <span>Plugin Market</span></a>
`;
const checkForPluginUpdates = require(modulePath).checkForPluginUpdates;
await checkForPluginUpdates();
expect($('#target').html()).toBe(
'<i class="fa fa-shopping-bag"></i> <span>Plugin Market</span>'
);
await checkForPluginUpdates();
expect($('#target').html()).toBe(
'<i class="fa fa-shopping-bag"></i> <span>Plugin Market</span>'+
'<span class="label label-success pull-right">1</span>'
);
});
});
describe('tests for "update" module', () => {

View File

@ -150,8 +150,22 @@ async function updatePlugin(name) {
});
}
async function checkForPluginUpdates() {
try {
const data = await fetch({ url: url('admin/plugins/market/check') });
if (data.available === true) {
const dom = `<span class="label label-success pull-right">${data.plugins.length}</span>`;
$(`[href="${url('admin/plugins/market')}"]`).append(dom);
}
} catch (error) {
//
}
}
if (process.env.NODE_ENV === 'test') {
module.exports = {
checkForPluginUpdates,
initMarketTable,
installPlugin,
updatePlugin,

View File

@ -92,7 +92,12 @@
@include('common.dependencies.script', ['module' => 'admin'])
@if (option('check_update'))
<script>$(document).ready(checkForUpdates);</script>
<script>
$(document).ready(() => {
checkForUpdates();
checkForPluginUpdates();
});
</script>
@endif
@if (option('allow_sending_statistics'))

View File

@ -131,6 +131,7 @@ Route::group(['middleware' => 'admin', 'prefix' => 'admin'], function ()
Route::get ('/market', 'MarketController@showMarket');
Route::post('/market-data', 'MarketController@getMarketData');
Route::get ('/market/check', 'MarketController@checkUpdates');
Route::post('/market/download', 'MarketController@download');
});

View File

@ -32,7 +32,42 @@ class MarketControllerTest extends TestCase
'msg' => trans('admin.plugins.market.install-success')
]);
$this->assertTrue(is_dir(base_path('plugins/hello-dolly')));
$this->assertFileNotExists(base_path('plugins/hello-dolly_v1.0.0.zip'));
$this->assertTrue(empty(glob(base_path('plugins/hello-dolly_*.zip'))));
}
public function testCheckUpdates()
{
$plugin_dir = base_path('plugins/hello-dolly');
if (! is_dir($plugin_dir)) {
mkdir($plugin_dir);
}
$this->get('/admin/plugins/market/check')
->seeJson([
'available' => false,
'plugins' => []
]);
file_put_contents("$plugin_dir/package.json", json_encode([
'name' => 'hello-dolly',
'version' => '0.0.1',
'title' => '',
'description' => '',
'author' => '',
'url' => '',
'namespace' => ''
]));
// Refresh plugin manager
$this->app->singleton('plugins', App\Services\PluginManager::class);
$this->get('/admin/plugins/market/check')
->seeJsonSubset([
'available' => true,
'plugins' => [[
'name' => 'hello-dolly'
]]
]);
}
public function testGetMarketData()