From bb303ebc4c83bb3c44340c22632ba39894da27b8 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 12 Jun 2026 17:50:01 +0200 Subject: [PATCH] Some cleanup in Hook API's and tests Clean up duplicated code in include/plugin.php and Zotlabs\Extend\Hook, making the former just wrappers calling the latter. Also removed the unnecessary serialization of arrays in Hook::insert, and cleaned up and expaned the tests a bit. --- Zotlabs/Extend/Hook.php | 4 --- include/plugin.php | 41 +++++----------------- tests/unit/CallHooksTest.php | 66 +++++++++++++++++++++++++++--------- 3 files changed, 58 insertions(+), 53 deletions(-) diff --git a/Zotlabs/Extend/Hook.php b/Zotlabs/Extend/Hook.php index 67d4f2d7b..213616048 100644 --- a/Zotlabs/Extend/Hook.php +++ b/Zotlabs/Extend/Hook.php @@ -167,10 +167,6 @@ class Hook { * would require the hook array to be resorted. */ static public function insert($hook, $fn, $version = 0, $priority = 0) { - if(is_array($fn)) { - $fn = serialize($fn); - } - if(! is_array(App::$hooks)) App::$hooks = array(); diff --git a/include/plugin.php b/include/plugin.php index 988433dce..ea47affdc 100644 --- a/include/plugin.php +++ b/include/plugin.php @@ -5,6 +5,7 @@ * @brief Some functions to handle addons and themes. */ +use Zotlabs\Extend\Hook; use Zotlabs\Lib\Config; /** @@ -342,29 +343,14 @@ function visible_plugin_list() { * @return mixed|bool */ function register_hook($hook, $file, $function, $priority = 0) { - $r = q("SELECT * FROM hook WHERE hook = '%s' AND file = '%s' AND fn = '%s' LIMIT 1", - dbesc($hook), - dbesc($file), - dbesc($function) - ); - if($r) - return true; - - $r = q("INSERT INTO hook (hook, file, fn, priority) VALUES ( '%s', '%s', '%s', '%s' )", - dbesc($hook), - dbesc($file), - dbesc($function), - dbesc($priority) - ); - - return $r; + return Hook::Register($hook, $file, $function, 1, $priority); } /** * @brief unregisters a hook. * - * @see ::Zotlabs::Extend::Hook::unregister + * @see ::Zotlabs::Extend::Hook::unregister() * * @param string $hook the name of the hook * @param string $file the name of the file that hooks into @@ -372,13 +358,7 @@ function register_hook($hook, $file, $function, $priority = 0) { * @return array */ function unregister_hook($hook, $file, $function) { - $r = q("DELETE FROM hook WHERE hook = '%s' AND file = '%s' AND fn = '%s'", - dbesc($hook), - dbesc($file), - dbesc($function) - ); - - return $r; + return Hook::unregister($hook, $file, $function, 1, 0); } /** @@ -428,7 +408,7 @@ function load_hooks() { } /** - * @brief Inserts a hook into a page request. + * Inserts a hook into a page request. * * Insert a short-lived hook into the running page request. * Hooks are normally persistent so that they can be called @@ -439,6 +419,8 @@ function load_hooks() { * which will not persist beyond the life of this page request * or the current process. * + * @see ::Zotlabs::Extend::Hook::insert() + * * @param string $hook * name of hook to attach callback * @param string $fn @@ -447,14 +429,7 @@ function load_hooks() { * @param int $priority (optional) default 0 */ function insert_hook($hook, $fn, $version = 0, $priority = 0) { - - if(! is_array(App::$hooks)) - App::$hooks = array(); - - if(! array_key_exists($hook, App::$hooks)) - App::$hooks[$hook] = array(); - - App::$hooks[$hook][] = array('', $fn, $priority, $version); + Hook::insert($hook, $fn, $version, $priority); } /** diff --git a/tests/unit/CallHooksTest.php b/tests/unit/CallHooksTest.php index 0170f31d0..89a97fd50 100644 --- a/tests/unit/CallHooksTest.php +++ b/tests/unit/CallHooksTest.php @@ -10,8 +10,10 @@ namespace Zotlabs\Tests\Unit; -use PHPUnit\Framework\Attributes\BackupStaticProperties; use App; +use PHPUnit\Framework\Attributes\BackupStaticProperties; +use PHPUnit\Framework\Attributes\DataProvider; +use Zotlabs\Extend\Hook; #[BackupStaticProperties(App::class)] class CallHooksTest extends UnitTestCase { @@ -27,39 +29,71 @@ class CallHooksTest extends UnitTestCase { $this->assertHookInvoked(); } - public function test_static_class_function_as_string(): void { - insert_hook('test_hook', 'Zotlabs\Tests\Unit\CallHooksTest::static_test_hook'); + #[DataProvider('hookProvider')] + public function testOldInsertHookApi(mixed $hook): void { + insert_hook('test_hook', $hook); $this->assertHookInvoked(); } - public function test_static_class_function_as_array(): void { - insert_hook('test_hook', ['Zotlabs\Tests\Unit\CallHooksTest', 'static_test_hook']); + #[DataProvider('hookProvider')] + public function testNewHookInsertApi(mixed $hook): void { + Hook::insert('test_hook', $hook); $this->assertHookInvoked(); } - public function test_static_class_function_as_serialized_array(): void { - insert_hook('test_hook', serialize(['Zotlabs\Tests\Unit\CallHooksTest', 'static_test_hook'])); + #[DataProvider('hookProvider')] + public function testNewHookRegisterApi(mixed $hook): void { + Hook::register('test_hook', __FILE__, $hook); + + load_hooks(); $this->assertHookInvoked(); + + Hook::unregister('test_hook', __FILE__, $hook); + + load_hooks(); + $this->assertNotHookInvoked(); } - public function test_instance_function_as_array(): void { - insert_hook('test_hook', [$this, 'instance_test_hook']); - $this->assertHookInvoked(); - } + // + // Helper functions + // - - public function assertHookInvoked(): void { + private function invokeHook(): bool { $test_hook_args = ['called' => false]; call_hooks('test_hook', $test_hook_args); - $this->assertTrue($test_hook_args['called']); + return $test_hook_args['called']; } - public function instance_test_hook(array &$args): void { - $args['called'] = true; + private function assertHookInvoked(): void { + $this->assertTrue($this->invokeHook()); } + + private function assertNotHookInvoked(): void { + $this->assertFalse($this->invokeHook()); + } + + // + // A static function to invoke via the hook + // + public static function static_test_hook(array &$args): void { $args['called'] = true; } + + // + // Data provider for the hook tests + // + + public static function hookProvider(): array { + return [ + 'hook is static class function as string' => [ + 'Zotlabs\Tests\Unit\CallHooksTest::static_test_hook' + ], + 'hook is static class function as array' => [ + ['Zotlabs\Tests\Unit\CallHooksTest', 'static_test_hook'] + ], + ]; + } }