Commit | Line | Data |
---|---|---|
3097a0d6 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
3097a0d6 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
3097a0d6 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
3097a0d6 | 27 | |
3097a0d6 TO |
28 | require_once 'api/Wrapper.php'; |
29 | ||
30 | /** | |
31 | * Test class for API functions | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
acb109b7 | 34 | * @group headless |
3097a0d6 TO |
35 | */ |
36 | class api_v3_APIWrapperTest extends CiviUnitTestCase { | |
37 | public $DBResetRequired = FALSE; | |
b7c9bc4c | 38 | |
3097a0d6 TO |
39 | |
40 | protected $_apiversion = 3; | |
41 | ||
42 | /** | |
43 | * Sets up the fixture, for example, opens a network connection. | |
44 | * This method is called before a test is executed. | |
3097a0d6 TO |
45 | */ |
46 | protected function setUp() { | |
47 | parent::setUp(); | |
0ddcaf8a | 48 | $this->useTransaction(TRUE); |
3097a0d6 TO |
49 | CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_apiWrappers', array($this, 'onApiWrappers')); |
50 | } | |
51 | ||
52 | /** | |
53 | * Tears down the fixture, for example, closes a network connection. | |
54 | * This method is called after a test is executed. | |
3097a0d6 TO |
55 | */ |
56 | protected function tearDown() { | |
57 | parent::tearDown(); | |
58 | } | |
59 | ||
4cbe18b8 EM |
60 | /** |
61 | * @param $apiWrappers | |
62 | * @param $apiRequest | |
63 | */ | |
00be9182 | 64 | public function onApiWrappers(&$apiWrappers, $apiRequest) { |
3097a0d6 TO |
65 | $this->assertTrue(is_string($apiRequest['entity']) && !empty($apiRequest['entity'])); |
66 | $this->assertTrue(is_string($apiRequest['action']) && !empty($apiRequest['action'])); | |
67 | $this->assertTrue(is_array($apiRequest['params']) && !empty($apiRequest['params'])); | |
68 | ||
69 | $apiWrappers[] = new api_v3_APIWrapperTest_Impl(); | |
70 | } | |
71 | ||
00be9182 | 72 | public function testWrapperHook() { |
3097a0d6 TO |
73 | // Note: this API call would fail due to missing contact_type, but |
74 | // the wrapper intervenes (fromApiInput) | |
75 | // Note: The output would define "display_name", but the wrapper | |
76 | // intervenes (toApiOutput) and replaces with "display_name_munged". | |
77 | $result = $this->callAPISuccess('contact', 'create', array( | |
78 | 'contact_type' => 'Invalid', | |
79 | 'first_name' => 'First', | |
80 | 'last_name' => 'Last', | |
81 | )); | |
82 | $this->assertEquals('First', $result['values'][$result['id']]['first_name']); | |
83 | $this->assertEquals('MUNGE! First Last', $result['values'][$result['id']]['display_name_munged']); | |
84 | } | |
96025800 | 85 | |
3097a0d6 TO |
86 | } |
87 | ||
4cbe18b8 EM |
88 | /** |
89 | * Class api_v3_APIWrapperTest_Impl | |
90 | */ | |
3097a0d6 TO |
91 | class api_v3_APIWrapperTest_Impl implements API_Wrapper { |
92 | /** | |
e7c15cb6 | 93 | * @inheritDoc |
3097a0d6 TO |
94 | */ |
95 | public function fromApiInput($apiRequest) { | |
4846df91 | 96 | if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') { |
3097a0d6 TO |
97 | if ('Invalid' == CRM_Utils_Array::value('contact_type', $apiRequest['params'])) { |
98 | $apiRequest['params']['contact_type'] = 'Individual'; | |
99 | } | |
100 | } | |
101 | return $apiRequest; | |
102 | } | |
103 | ||
104 | /** | |
e7c15cb6 | 105 | * @inheritDoc |
3097a0d6 TO |
106 | */ |
107 | public function toApiOutput($apiRequest, $result) { | |
4846df91 | 108 | if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') { |
3097a0d6 TO |
109 | if (isset($result['id'], $result['values'][$result['id']]['display_name'])) { |
110 | $result['values'][$result['id']]['display_name_munged'] = 'MUNGE! ' . $result['values'][$result['id']]['display_name']; | |
111 | unset($result['values'][$result['id']]['display_name']); | |
112 | } | |
113 | } | |
114 | return $result; | |
115 | } | |
96025800 | 116 | |
3097a0d6 | 117 | } |