Commit | Line | Data |
---|---|---|
3097a0d6 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
3097a0d6 | 5 | | | |
7d61e75f TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
3097a0d6 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
3097a0d6 | 11 | |
3097a0d6 TO |
12 | require_once 'api/Wrapper.php'; |
13 | ||
14 | /** | |
15 | * Test class for API functions | |
16 | * | |
17 | * @package CiviCRM_APIv3 | |
acb109b7 | 18 | * @group headless |
3097a0d6 TO |
19 | */ |
20 | class api_v3_APIWrapperTest extends CiviUnitTestCase { | |
21 | public $DBResetRequired = FALSE; | |
b7c9bc4c | 22 | |
3097a0d6 TO |
23 | |
24 | protected $_apiversion = 3; | |
25 | ||
26 | /** | |
27 | * Sets up the fixture, for example, opens a network connection. | |
28 | * This method is called before a test is executed. | |
3097a0d6 TO |
29 | */ |
30 | protected function setUp() { | |
31 | parent::setUp(); | |
0ddcaf8a | 32 | $this->useTransaction(TRUE); |
9099cab3 | 33 | CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_apiWrappers', [$this, 'onApiWrappers']); |
3097a0d6 TO |
34 | } |
35 | ||
36 | /** | |
37 | * Tears down the fixture, for example, closes a network connection. | |
38 | * This method is called after a test is executed. | |
3097a0d6 TO |
39 | */ |
40 | protected function tearDown() { | |
41 | parent::tearDown(); | |
42 | } | |
43 | ||
4cbe18b8 EM |
44 | /** |
45 | * @param $apiWrappers | |
46 | * @param $apiRequest | |
47 | */ | |
00be9182 | 48 | public function onApiWrappers(&$apiWrappers, $apiRequest) { |
3097a0d6 TO |
49 | $this->assertTrue(is_string($apiRequest['entity']) && !empty($apiRequest['entity'])); |
50 | $this->assertTrue(is_string($apiRequest['action']) && !empty($apiRequest['action'])); | |
51 | $this->assertTrue(is_array($apiRequest['params']) && !empty($apiRequest['params'])); | |
52 | ||
53 | $apiWrappers[] = new api_v3_APIWrapperTest_Impl(); | |
54 | } | |
55 | ||
00be9182 | 56 | public function testWrapperHook() { |
3097a0d6 TO |
57 | // Note: this API call would fail due to missing contact_type, but |
58 | // the wrapper intervenes (fromApiInput) | |
59 | // Note: The output would define "display_name", but the wrapper | |
60 | // intervenes (toApiOutput) and replaces with "display_name_munged". | |
9099cab3 | 61 | $result = $this->callAPISuccess('contact', 'create', [ |
3097a0d6 TO |
62 | 'contact_type' => 'Invalid', |
63 | 'first_name' => 'First', | |
64 | 'last_name' => 'Last', | |
9099cab3 | 65 | ]); |
3097a0d6 TO |
66 | $this->assertEquals('First', $result['values'][$result['id']]['first_name']); |
67 | $this->assertEquals('MUNGE! First Last', $result['values'][$result['id']]['display_name_munged']); | |
68 | } | |
96025800 | 69 | |
3097a0d6 TO |
70 | } |
71 | ||
4cbe18b8 EM |
72 | /** |
73 | * Class api_v3_APIWrapperTest_Impl | |
74 | */ | |
3097a0d6 | 75 | class api_v3_APIWrapperTest_Impl implements API_Wrapper { |
39b959db | 76 | |
3097a0d6 | 77 | /** |
e7c15cb6 | 78 | * @inheritDoc |
3097a0d6 TO |
79 | */ |
80 | public function fromApiInput($apiRequest) { | |
4846df91 | 81 | if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') { |
3097a0d6 TO |
82 | if ('Invalid' == CRM_Utils_Array::value('contact_type', $apiRequest['params'])) { |
83 | $apiRequest['params']['contact_type'] = 'Individual'; | |
84 | } | |
85 | } | |
86 | return $apiRequest; | |
87 | } | |
88 | ||
89 | /** | |
e7c15cb6 | 90 | * @inheritDoc |
3097a0d6 TO |
91 | */ |
92 | public function toApiOutput($apiRequest, $result) { | |
4846df91 | 93 | if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') { |
3097a0d6 TO |
94 | if (isset($result['id'], $result['values'][$result['id']]['display_name'])) { |
95 | $result['values'][$result['id']]['display_name_munged'] = 'MUNGE! ' . $result['values'][$result['id']]['display_name']; | |
96 | unset($result['values'][$result['id']]['display_name']); | |
97 | } | |
98 | } | |
99 | return $result; | |
100 | } | |
96025800 | 101 | |
3097a0d6 | 102 | } |