Tweak test

This commit is contained in:
Pig Fang 2019-08-15 15:02:40 +08:00
parent eb836357b8
commit d871af1906
3 changed files with 7 additions and 135 deletions

View File

@ -16,7 +16,7 @@ class Hook
/**
* Add an item to menu.
*
* @param string $category 'user' or 'admin'
* @param string $category 'user' or 'admin' or 'explore'
* @param int $position Where to insert the given item, start from 0.
* @param array $menu e.g.
* [
@ -63,17 +63,17 @@ class Hook
});
}
public static function registerPluginTransScripts(string $id, $pages = ['*'], $priority = 999): void
public static function registerPluginTransScripts(string $name, $pages = ['*'], $priority = 999): void
{
Event::listen(Events\RenderingFooter::class, function ($event) use ($id, $pages) {
Event::listen(Events\RenderingFooter::class, function ($event) use ($name, $pages) {
foreach ($pages as $pattern) {
if (! app('request')->is($pattern)) {
if (! request()->is($pattern)) {
continue;
}
// We will determine current locale in the event callback,
// otherwise the locale is not properly detected.
$basepath = config('plugins.url') ?: url('plugins').'/'.$id.'/';
$basepath = config('plugins.url') ?: url('plugins').'/'.$name.'/';
$relative = 'lang/'.config('app.locale').'/locale.js';
$event->addContent(
@ -89,7 +89,7 @@ class Hook
{
Event::listen(Events\RenderingHeader::class, function ($event) use ($urls, $pages) {
foreach ($pages as $pattern) {
if (! app('request')->is($pattern)) {
if (! request()->is($pattern)) {
continue;
}
@ -106,7 +106,7 @@ class Hook
{
Event::listen(Events\RenderingFooter::class, function ($event) use ($urls, $pages) {
foreach ($pages as $pattern) {
if (! app('request')->is($pattern)) {
if (! request()->is($pattern)) {
continue;
}

View File

@ -1,118 +0,0 @@
<?php
namespace Tests\Concerns;
use ZipArchive;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
trait GeneratesFakePlugins
{
/**
* Generate fake content of a plugin's package.json.
*
* @param array $info
* @return array
*/
protected function generateFakePlguinInfo($info = [])
{
return array_replace([
'name' => Str::random(10),
'version' => '0.0.'.rand(1, 9),
'title' => Str::random(20),
'description' => Str::random(60),
'author' => Arr::get($info, 'author', Str::random(10)),
'url' => 'https://'.Str::random(10).'.test',
'namespace' => Str::random(10),
'require' => [
'blessing-skin-server' => '^3.4.0 || ^4.0.0',
],
], $info);
}
/**
* Generate plugin information for plugins registry (with "dist" field).
*
* @param array $info
* @return array
*/
protected function generateFakePluginsRegistryPackage($info = [])
{
return $this->generateFakePlguinInfo(array_replace([
'dist' => [
'type' => 'zip',
'url' => 'https://plugins-registry.test/'.Str::random(10).'.zip',
'shasum' => strtolower(Str::random(40)),
],
], $info));
}
/**
* Generate fake content of a plugins registry.
* You can also pass two arguments (name and version) as a shortcut.
* If no argument is passed, we will randomly generate 10 fake plugins.
*
* @param array $plugins An array of plugin information.
* @return string JSON encoded content.
*/
protected function generateFakePluginsRegistry($plugins = [])
{
if (func_num_args() == 2) {
$plugins = [
[
'name' => func_get_arg(0),
'version' => func_get_arg(1),
],
];
}
$packages = [];
if (count($plugins) == 0) {
// Randomly generate 10 fake plugins
for ($i = 0; $i < 10; $i++) {
$packages[] = $this->generateFakePluginsRegistryPackage();
}
} else {
foreach ($plugins as $info) {
$packages[] = $this->generateFakePluginsRegistryPackage($info);
}
}
return json_encode([
'packages' => $packages,
]);
}
/**
* Generate a fake plugin in plugins directory with given information.
*
* @param array $info The "name" field is required.
* @return void
*/
protected function generateFakePlugin($info)
{
$plugin_dir = config('plugins.directory').DIRECTORY_SEPARATOR.$info['name'];
if (! is_dir($plugin_dir)) {
mkdir($plugin_dir);
}
// Generate fake config view
if ($config = Arr::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 return function () { return '{$info['name']}'; };");
}
}

View File

@ -4,13 +4,9 @@ namespace Tests;
use App\Models\User;
use App\Services\Hook;
use Illuminate\Support\Facades\File;
use Tests\Concerns\GeneratesFakePlugins;
class HookTest extends TestCase
{
use GeneratesFakePlugins;
public function testAddMenuItem()
{
Hook::addMenuItem('user', 0, [
@ -49,15 +45,9 @@ class HookTest extends TestCase
public function testRegisterPluginTransScripts()
{
$this->generateFakePlugin(['name' => 'fake-plugin-with-i18n', 'version' => '0.0.1']);
@mkdir($path = config('plugins.directory').DIRECTORY_SEPARATOR.'fake-plugin-with-i18n/lang/en', 0755, true);
file_put_contents("$path/locale.js", '');
Hook::registerPluginTransScripts('fake-plugin-with-i18n', ['/']);
$this->get('/')->assertSee('fake-plugin-with-i18n/lang/en/locale.js');
$this->get('/skinlib')->assertDontSee('fake-plugin-with-i18n/lang/en/locale.js');
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-plugin-with-i18n');
}
public function testAddStyleFileToPage()