From d871af190612ff553c12c36e9b9e7fb535ef0fbd Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Thu, 15 Aug 2019 15:02:40 +0800 Subject: [PATCH] Tweak test --- app/Services/Hook.php | 14 +-- tests/Concerns/GeneratesFakePlugins.php | 118 ------------------------ tests/ServicesTest/HookTest.php | 10 -- 3 files changed, 7 insertions(+), 135 deletions(-) delete mode 100644 tests/Concerns/GeneratesFakePlugins.php diff --git a/app/Services/Hook.php b/app/Services/Hook.php index 3cdb24cc..d4d4649b 100644 --- a/app/Services/Hook.php +++ b/app/Services/Hook.php @@ -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; } diff --git a/tests/Concerns/GeneratesFakePlugins.php b/tests/Concerns/GeneratesFakePlugins.php deleted file mode 100644 index 3395c475..00000000 --- a/tests/Concerns/GeneratesFakePlugins.php +++ /dev/null @@ -1,118 +0,0 @@ - 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", "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()