Skip to content

Commit

Permalink
MDL-79338 core: add support for hook callback redirection in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Sep 14, 2023
1 parent f42cc76 commit b2a2d3d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/classes/hook/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -89,6 +92,32 @@ public static function phpunit_get_instance(array $componentfiles): manager {
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.
*
Expand Down Expand Up @@ -215,6 +244,14 @@ public function dispatch(object $event): object {
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)) {
Expand Down
20 changes: 20 additions & 0 deletions lib/phpunit/classes/advanced_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,26 @@ public function redirectEvents() {
return phpunit_util::start_event_redirection();
}

/**
* Override hook callbacks.
*
* @param string $hookname
* @param callable $callback
* @return void
*/
public function redirectHook(string $hookname, callable $callback): void {
\core\hook\manager::get_instance()->phpunit_redirect_hook($hookname, $callback);
}

/**
* Remove all hook overrides.
*
* @return void
*/
public function stopHookRedirections(): void {
\core\hook\manager::get_instance()->phpunit_stop_redirections();
}

/**
* Reset all database tables, restore global state and clear caches and optionally purge dataroot dir.
*
Expand Down
3 changes: 3 additions & 0 deletions lib/phpunit/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public static function initialise_cfg() {
public static function reset_all_data($detectchanges = false) {
global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $FULLME, $FILTERLIB_PRIVATE;

// Stop all hook redirections.
\core\hook\manager::get_instance()->phpunit_stop_redirections();

// Stop any message redirection.
self::stop_message_redirection();

Expand Down

0 comments on commit b2a2d3d

Please sign in to comment.