Skip to content

Commit

Permalink
allow l10n of plugins market registry
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Apr 7, 2020
1 parent f0e3b6a commit 1669269
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
7 changes: 6 additions & 1 deletion app/Http/Controllers/MarketController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ public function download(Request $request, PluginManager $manager, Unzip $unzip)

protected function fetch(): Collection
{
$lang = in_array(app()->getLocale(), config('plugins.locales'))
? app()->getLocale()
: config('app.fallback_locale');

$plugins = collect(explode(',', config('plugins.registry')))
->map(function ($registry) {
->map(function ($registry) use ($lang) {
$registry = str_replace('{lang}', $lang, $registry);
$response = Http::withOptions([
'verify' => CaBundle::getSystemCaRootBundlePath(),
])->get(trim($registry));
Expand Down
12 changes: 11 additions & 1 deletion config/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
*/
'registry' => env(
'PLUGINS_REGISTRY',
'https://cdn.jsdelivr.net/gh/bs-community/plugins-dist@latest/registry-preview.json'
'https://cdn.jsdelivr.net/gh/bs-community/plugins-dist@latest/registry-preview_{lang}.json'
),

/*
|--------------------------------------------------------------------------
| Plugins Market Localization
|--------------------------------------------------------------------------
|
| Supported languages of plugins market registry will be listed here.
|
*/
'locales' => ['en', 'zh_CN'],
];
73 changes: 48 additions & 25 deletions tests/HttpTest/ControllersTest/MarketControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ protected function setUp(): void

public function testDownload()
{
$registryUrl = str_replace('{lang}', 'en', config('plugins.registry'));
Http::fake([
config('plugins.registry') => Http::sequence()
$registryUrl => Http::sequence()
->push(['version' => 1, 'packages' => []])
->push([
'version' => 1,
Expand Down Expand Up @@ -82,44 +83,49 @@ public function testDownload()

public function testMarketData()
{
$registry = [
'version' => 1,
'packages' => [
[
'name' => 'fake1',
'title' => 'Fake',
'version' => '1.0.0',
'description' => '',
'author' => '',
'dist' => [],
'require' => [],
],
[
'name' => 'fake2',
'title' => 'Fake',
'version' => '0.0.0',
'description' => '',
'author' => '',
'dist' => [],
'require' => [],
],
],
];

Http::fakeSequence()
->pushStatus(404)
->push([
'version' => 1,
'packages' => [
[
'name' => 'fake1',
'title' => 'Fake',
'version' => '1.0.0',
'description' => '',
'author' => '',
'dist' => [],
'require' => [],
],
[
'name' => 'fake2',
'title' => 'Fake',
'version' => '0.0.0',
'description' => '',
'author' => '',
'dist' => [],
'require' => [],
],
],
]);
->push($registry)
->push($registry);

$this->getJson('/admin/plugins/market/list')->assertStatus(500);

$this->mock(PluginManager::class, function ($mock) {
$mock->shouldReceive('get')
->with('fake1')
->atLeast()
->once()
->andReturn(new Plugin('', ['name' => 'fake1', 'version' => '0.0.1']));
$mock->shouldReceive('get')
->with('fake2')
->atLeast()
->once()
->andReturn(null);
$mock->shouldReceive('getUnsatisfied')->twice();
$mock->shouldReceive('getUnsatisfied')->atLeast()->once();
});
$this->getJson('/admin/plugins/market/list')
->assertJsonStructure([
Expand All @@ -134,5 +140,22 @@ public function testMarketData()
'dependencies',
],
]);

// with fallback locale
app()->setLocale('es_ES');
$this->getJson('/admin/plugins/market/list')
->assertJsonStructure([
[
'name',
'title',
'version',
'installed',
'description',
'author',
'dist',
'dependencies',
],
]);
app()->setLocale('en');
}
}

0 comments on commit 1669269

Please sign in to comment.