From 6570e3327f54814f7cb6feccba9f2a13b4070889 Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 12 Aug 2018 22:43:21 +0800 Subject: [PATCH] Add check for plugin updates --- app/Http/Controllers/MarketController.php | 13 +++++++ .../assets/src/js/__tests__/admin.test.js | 36 ++++++++++++++++++ resources/assets/src/js/admin/market.js | 14 +++++++ resources/views/admin/master.blade.php | 7 +++- routes/web.php | 1 + tests/MarketControllerTest.php | 37 ++++++++++++++++++- 6 files changed, 106 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/MarketController.php b/app/Http/Controllers/MarketController.php index 35d7964c..04fa0d8a 100644 --- a/app/Http/Controllers/MarketController.php +++ b/app/Http/Controllers/MarketController.php @@ -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'); diff --git a/resources/assets/src/js/__tests__/admin.test.js b/resources/assets/src/js/__tests__/admin.test.js index 7a5cb7c1..f7a37121 100644 --- a/resources/assets/src/js/__tests__/admin.test.js +++ b/resources/assets/src/js/__tests__/admin.test.js @@ -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 = ` + Plugin Market + `; + + const checkForPluginUpdates = require(modulePath).checkForPluginUpdates; + + await checkForPluginUpdates(); + expect($('#target').html()).toBe( + ' Plugin Market' + ); + + await checkForPluginUpdates(); + expect($('#target').html()).toBe( + ' Plugin Market'+ + '1' + ); + }); }); describe('tests for "update" module', () => { diff --git a/resources/assets/src/js/admin/market.js b/resources/assets/src/js/admin/market.js index ed157f91..b408adac 100644 --- a/resources/assets/src/js/admin/market.js +++ b/resources/assets/src/js/admin/market.js @@ -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 = `${data.plugins.length}`; + + $(`[href="${url('admin/plugins/market')}"]`).append(dom); + } + } catch (error) { + // + } +} + if (process.env.NODE_ENV === 'test') { module.exports = { + checkForPluginUpdates, initMarketTable, installPlugin, updatePlugin, diff --git a/resources/views/admin/master.blade.php b/resources/views/admin/master.blade.php index 6583417b..e43df192 100644 --- a/resources/views/admin/master.blade.php +++ b/resources/views/admin/master.blade.php @@ -92,7 +92,12 @@ @include('common.dependencies.script', ['module' => 'admin']) @if (option('check_update')) - + @endif @if (option('allow_sending_statistics')) diff --git a/routes/web.php b/routes/web.php index c24a90bc..633a30ff 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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'); }); diff --git a/tests/MarketControllerTest.php b/tests/MarketControllerTest.php index 4ec546b3..bfc10018 100644 --- a/tests/MarketControllerTest.php +++ b/tests/MarketControllerTest.php @@ -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()