Merge pull request #17467 from civicrm/5.26
[civicrm-core.git] / tests / phpunit / CRM / Utils / HookTest.php
CommitLineData
c964e214 1<?php
aba1cd8b
EM
2
3/**
4 * Class CRM_Utils_HookTest
acb109b7 5 * @group headless
aba1cd8b 6 */
c964e214
TO
7class CRM_Utils_HookTest extends CiviUnitTestCase {
8
39b9dbb3 9 public static $activeTest = NULL;
c964e214 10
39b959db 11 public $fakeModules;
c964e214 12
39b959db 13 public $log;
c964e214 14
00be9182 15 public function setUp() {
c964e214 16 parent::setUp();
9099cab3 17 $this->fakeModules = [
c964e214
TO
18 'hooktesta',
19 'hooktestb',
20 'hooktestc',
8b3edc18
TO
21 'hooktestd',
22 'hookteste',
9099cab3 23 ];
c964e214
TO
24 // our goal is to test a helper in CRM_Utils_Hook, but we need a concrete class
25 $this->hook = new CRM_Utils_Hook_UnitTests();
9099cab3 26 $this->log = [];
c964e214
TO
27 self::$activeTest = $this;
28 }
29
00be9182 30 public function tearDown() {
c964e214
TO
31 self::$activeTest = $this;
32 parent::tearDown();
33 }
34
35 /**
36 * Verify that runHooks() is reentrant by invoking one hook which calls another hooks
37 */
00be9182 38 public function testRunHooks_reentrancy() {
c964e214 39 $arg1 = 'whatever';
44b06da7 40 $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, CRM_Utils_Hook::$_nullObject);
c964e214 41 $this->assertEquals(
9099cab3 42 [
c964e214
TO
43 'a-outer',
44 'b-outer-1',
45 'a-inner',
46 'b-inner',
47 'b-outer-2',
48 'c-outer',
9099cab3 49 ],
c964e214
TO
50 $this->log
51 );
52 }
8b3edc18
TO
53
54 /**
55 * Verify that the results of runHooks() are correctly merged
56 */
00be9182 57 public function testRunHooks_merge() {
8b3edc18
TO
58 $result = $this->hook->runHooks($this->fakeModules, 'civicrm_testRunHooks_merge', 0, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject);
59 $this->assertEquals(
9099cab3 60 [
8b3edc18
TO
61 'from-module-a1',
62 'from-module-a2',
63 'from-module-e',
9099cab3 64 ],
8b3edc18
TO
65 $result
66 );
67 }
96025800 68
c964e214
TO
69}
70
36e590d4
TO
71/* --- Library of test hook implementations --- */
72
389bcebf 73/**
36e590d4 74 * Implements hook_civicrm_testRunHooks_outer().
389bcebf 75 */
c964e214
TO
76function hooktesta_civicrm_testRunHooks_outer() {
77 $test = CRM_Utils_HookTest::$activeTest;
78 $test->log[] = 'a-outer';
79}
80
81function hooktestb_civicrm_testRunHooks_outer() {
82 $test = CRM_Utils_HookTest::$activeTest;
83 $test->log[] = 'b-outer-1';
44b06da7 84 $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, CRM_Utils_Hook::$_nullObject);
c964e214
TO
85 $test->log[] = 'b-outer-2';
86}
87
88function hooktestc_civicrm_testRunHooks_outer() {
89 $test = CRM_Utils_HookTest::$activeTest;
90 $test->log[] = 'c-outer';
91}
92
93function hooktesta_civicrm_testRunHooks_inner() {
94 $test = CRM_Utils_HookTest::$activeTest;
95 $test->log[] = 'a-inner';
96}
97
98function hooktestb_civicrm_testRunHooks_inner() {
99 $test = CRM_Utils_HookTest::$activeTest;
100 $test->log[] = 'b-inner';
101}
8b3edc18 102
7fe37828
EM
103/**
104 * @return array
105 */
8b3edc18 106function hooktesta_civicrm_testRunHooks_merge() {
9099cab3 107 return ['from-module-a1', 'from-module-a2'];
8b3edc18
TO
108}
109
36e590d4
TO
110// OMIT: function hooktestb_civicrm_testRunHooks_merge
111
389bcebf 112/**
36e590d4 113 * Implements hook_civicrm_testRunHooks_merge().
389bcebf 114 */
8b3edc18 115function hooktestc_civicrm_testRunHooks_merge() {
9099cab3 116 return [];
8b3edc18
TO
117}
118
7fe37828
EM
119/**
120 * @return null
121 */
8b3edc18
TO
122function hooktestd_civicrm_testRunHooks_merge() {
123 return NULL;
124}
125
7fe37828
EM
126/**
127 * @return array
128 */
8b3edc18 129function hookteste_civicrm_testRunHooks_merge() {
9099cab3 130 return ['from-module-e'];
8b3edc18 131}