Commit | Line | Data |
---|---|---|
c964e214 TO |
1 | <?php |
2 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
aba1cd8b EM |
3 | |
4 | /** | |
5 | * Class CRM_Utils_HookTest | |
6 | */ | |
c964e214 TO |
7 | class CRM_Utils_HookTest extends CiviUnitTestCase { |
8 | ||
9 | static $activeTest = NULL; | |
10 | ||
11 | var $fakeModules; | |
12 | ||
13 | var $log; | |
14 | ||
00be9182 | 15 | public function setUp() { |
c964e214 TO |
16 | parent::setUp(); |
17 | $this->fakeModules = array( | |
18 | 'hooktesta', | |
19 | 'hooktestb', | |
20 | 'hooktestc', | |
8b3edc18 TO |
21 | 'hooktestd', |
22 | 'hookteste', | |
c964e214 TO |
23 | ); |
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(); | |
26 | $this->log = array(); | |
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 TO |
41 | $this->assertEquals( |
42 | array( | |
43 | 'a-outer', | |
44 | 'b-outer-1', | |
45 | 'a-inner', | |
46 | 'b-inner', | |
47 | 'b-outer-2', | |
48 | 'c-outer', | |
49 | ), | |
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( | |
60 | array( | |
61 | 'from-module-a1', | |
62 | 'from-module-a2', | |
63 | 'from-module-e', | |
64 | ), | |
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 |
76 | function hooktesta_civicrm_testRunHooks_outer() { |
77 | $test = CRM_Utils_HookTest::$activeTest; | |
78 | $test->log[] = 'a-outer'; | |
79 | } | |
80 | ||
81 | function 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 | ||
88 | function hooktestc_civicrm_testRunHooks_outer() { | |
89 | $test = CRM_Utils_HookTest::$activeTest; | |
90 | $test->log[] = 'c-outer'; | |
91 | } | |
92 | ||
93 | function hooktesta_civicrm_testRunHooks_inner() { | |
94 | $test = CRM_Utils_HookTest::$activeTest; | |
95 | $test->log[] = 'a-inner'; | |
96 | } | |
97 | ||
98 | function 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 TO |
106 | function hooktesta_civicrm_testRunHooks_merge() { |
107 | return array('from-module-a1', 'from-module-a2'); | |
108 | } | |
109 | ||
36e590d4 TO |
110 | // OMIT: function hooktestb_civicrm_testRunHooks_merge |
111 | ||
389bcebf | 112 | /** |
36e590d4 | 113 | * Implements hook_civicrm_testRunHooks_merge(). |
389bcebf | 114 | */ |
8b3edc18 TO |
115 | function hooktestc_civicrm_testRunHooks_merge() { |
116 | return array(); | |
117 | } | |
118 | ||
7fe37828 EM |
119 | /** |
120 | * @return null | |
121 | */ | |
8b3edc18 TO |
122 | function hooktestd_civicrm_testRunHooks_merge() { |
123 | return NULL; | |
124 | } | |
125 | ||
7fe37828 EM |
126 | /** |
127 | * @return array | |
128 | */ | |
8b3edc18 TO |
129 | function hookteste_civicrm_testRunHooks_merge() { |
130 | return array('from-module-e'); | |
131 | } |