MDL-79338 core: add support for hook callback redirection in tests
This commit is contained in:
@@ -54,6 +54,9 @@ final class manager implements
|
||||
/** @var array list of all deprecated lib.php plugin callbacks. */
|
||||
private $alldeprecations = [];
|
||||
|
||||
/** @var array list of redirected callbacks in PHPUnit tests */
|
||||
private $redirectedcallbacks = [];
|
||||
|
||||
/**
|
||||
* Constructor can be used only from factory methods.
|
||||
*/
|
||||
@@ -89,6 +92,32 @@ final class manager implements
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override hook callbacks for testing purposes.
|
||||
*
|
||||
* @param string $hookname
|
||||
* @param callable $callback
|
||||
* @return void
|
||||
*/
|
||||
public function phpunit_redirect_hook(string $hookname, callable $callback): void {
|
||||
if (!PHPUNIT_TEST) {
|
||||
throw new \coding_exception('Invalid call of manager::phpunit_redirect_hook() outside of tests');
|
||||
}
|
||||
$this->redirectedcallbacks[$hookname] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all redirections of hook callbacks.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function phpunit_stop_redirections(): void {
|
||||
if (!PHPUNIT_TEST) {
|
||||
throw new \coding_exception('Invalid call of manager::phpunit_stop_redirections() outside of tests');
|
||||
}
|
||||
$this->redirectedcallbacks = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns list of callbacks for given hook name.
|
||||
*
|
||||
@@ -215,6 +244,14 @@ final class manager implements
|
||||
return $event;
|
||||
}
|
||||
|
||||
if (PHPUNIT_TEST) {
|
||||
$hookclassname = get_class($event);
|
||||
if (isset($this->redirectedcallbacks[$hookclassname])) {
|
||||
call_user_func($this->redirectedcallbacks[$hookclassname], $event);
|
||||
return $event;
|
||||
}
|
||||
}
|
||||
|
||||
$callbacks = $this->getListenersForEvent($event);
|
||||
|
||||
if (empty($callbacks)) {
|
||||
|
||||
Reference in New Issue
Block a user