mirror of
https://framagit.org/hubzilla/core.git
synced 2026-06-21 00:52:33 -04:00
Merge branch 'cleanup-hooks' into 'dev'
Some cleanup in Hook API's and tests See merge request hubzilla/core!2293
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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']
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user