From c964e21467e9be59675b0f17aeb631cd20604517 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 23 Jul 2013 15:23:50 -0700 Subject: [PATCH] CRM-13103 - CRM_Utils_HookTest Add unit test in which one hook calls another. Note that this unit-test doesn't actually reproduce the issue for me (eg it incorrectly shows the test as passing on code that doesn't work for me in real situations), but it's still valuable in that it gives us better test coverage. ---------------------------------------- * CRM-13103: Hook execution terminates prematurely http://issues.civicrm.org/jira/browse/CRM-13103 --- tests/phpunit/CRM/Utils/HookTest.php | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/phpunit/CRM/Utils/HookTest.php diff --git a/tests/phpunit/CRM/Utils/HookTest.php b/tests/phpunit/CRM/Utils/HookTest.php new file mode 100644 index 0000000000..1a190eecc7 --- /dev/null +++ b/tests/phpunit/CRM/Utils/HookTest.php @@ -0,0 +1,76 @@ +fakeModules = array( + 'hooktesta', + 'hooktestb', + 'hooktestc', + ); + // our goal is to test a helper in CRM_Utils_Hook, but we need a concrete class + $this->hook = new CRM_Utils_Hook_UnitTests(); + $this->log = array(); + self::$activeTest = $this; + } + + function tearDown() { + self::$activeTest = $this; + parent::tearDown(); + } + + /** + * Verify that runHooks() is reentrant by invoking one hook which calls another hooks + */ + function testRunHooks_reentrancy() { + $arg1 = 'whatever'; + $this->hook->runHooks($this->fakeModules, 'civicrm_testRunHooks_outer', 1, $arg1, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject); + $this->assertEquals( + array( + 'a-outer', + 'b-outer-1', + 'a-inner', + 'b-inner', + 'b-outer-2', + 'c-outer', + ), + $this->log + ); + } +} + +/* --- Library of test hook implementations ---- */ + +function hooktesta_civicrm_testRunHooks_outer() { + $test = CRM_Utils_HookTest::$activeTest; + $test->log[] = 'a-outer'; +} + +function hooktestb_civicrm_testRunHooks_outer() { + $test = CRM_Utils_HookTest::$activeTest; + $test->log[] = 'b-outer-1'; + $test->hook->runHooks($test->fakeModules, 'civicrm_testRunHooks_inner', 0, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject); + $test->log[] = 'b-outer-2'; +} + +function hooktestc_civicrm_testRunHooks_outer() { + $test = CRM_Utils_HookTest::$activeTest; + $test->log[] = 'c-outer'; +} + +function hooktesta_civicrm_testRunHooks_inner() { + $test = CRM_Utils_HookTest::$activeTest; + $test->log[] = 'a-inner'; +} + +function hooktestb_civicrm_testRunHooks_inner() { + $test = CRM_Utils_HookTest::$activeTest; + $test->log[] = 'b-inner'; +} -- 2.25.1