Commit | Line | Data |
---|---|---|
3097a0d6 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
3097a0d6 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
34 | */ | |
35 | class api_v3_APIWrapperTest extends CiviUnitTestCase { | |
36 | public $DBResetRequired = FALSE; | |
b7c9bc4c | 37 | |
3097a0d6 TO |
38 | |
39 | protected $_apiversion = 3; | |
40 | ||
41 | /** | |
42 | * Sets up the fixture, for example, opens a network connection. | |
43 | * This method is called before a test is executed. | |
3097a0d6 TO |
44 | */ |
45 | protected function setUp() { | |
46 | parent::setUp(); | |
0ddcaf8a | 47 | $this->useTransaction(TRUE); |
3097a0d6 TO |
48 | CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_apiWrappers', array($this, 'onApiWrappers')); |
49 | } | |
50 | ||
51 | /** | |
52 | * Tears down the fixture, for example, closes a network connection. | |
53 | * This method is called after a test is executed. | |
3097a0d6 TO |
54 | */ |
55 | protected function tearDown() { | |
56 | parent::tearDown(); | |
57 | } | |
58 | ||
4cbe18b8 EM |
59 | /** |
60 | * @param $apiWrappers | |
61 | * @param $apiRequest | |
62 | */ | |
00be9182 | 63 | public function onApiWrappers(&$apiWrappers, $apiRequest) { |
3097a0d6 TO |
64 | $this->assertTrue(is_string($apiRequest['entity']) && !empty($apiRequest['entity'])); |
65 | $this->assertTrue(is_string($apiRequest['action']) && !empty($apiRequest['action'])); | |
66 | $this->assertTrue(is_array($apiRequest['params']) && !empty($apiRequest['params'])); | |
67 | ||
68 | $apiWrappers[] = new api_v3_APIWrapperTest_Impl(); | |
69 | } | |
70 | ||
00be9182 | 71 | public function testWrapperHook() { |
3097a0d6 TO |
72 | // Note: this API call would fail due to missing contact_type, but |
73 | // the wrapper intervenes (fromApiInput) | |
74 | // Note: The output would define "display_name", but the wrapper | |
75 | // intervenes (toApiOutput) and replaces with "display_name_munged". | |
76 | $result = $this->callAPISuccess('contact', 'create', array( | |
77 | 'contact_type' => 'Invalid', | |
78 | 'first_name' => 'First', | |
79 | 'last_name' => 'Last', | |
80 | )); | |
81 | $this->assertEquals('First', $result['values'][$result['id']]['first_name']); | |
82 | $this->assertEquals('MUNGE! First Last', $result['values'][$result['id']]['display_name_munged']); | |
83 | } | |
96025800 | 84 | |
3097a0d6 TO |
85 | } |
86 | ||
4cbe18b8 EM |
87 | /** |
88 | * Class api_v3_APIWrapperTest_Impl | |
89 | */ | |
3097a0d6 TO |
90 | class api_v3_APIWrapperTest_Impl implements API_Wrapper { |
91 | /** | |
e7c15cb6 | 92 | * @inheritDoc |
3097a0d6 TO |
93 | */ |
94 | public function fromApiInput($apiRequest) { | |
4846df91 | 95 | if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') { |
3097a0d6 TO |
96 | if ('Invalid' == CRM_Utils_Array::value('contact_type', $apiRequest['params'])) { |
97 | $apiRequest['params']['contact_type'] = 'Individual'; | |
98 | } | |
99 | } | |
100 | return $apiRequest; | |
101 | } | |
102 | ||
103 | /** | |
e7c15cb6 | 104 | * @inheritDoc |
3097a0d6 TO |
105 | */ |
106 | public function toApiOutput($apiRequest, $result) { | |
4846df91 | 107 | if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') { |
3097a0d6 TO |
108 | if (isset($result['id'], $result['values'][$result['id']]['display_name'])) { |
109 | $result['values'][$result['id']]['display_name_munged'] = 'MUNGE! ' . $result['values'][$result['id']]['display_name']; | |
110 | unset($result['values'][$result['id']]['display_name']); | |
111 | } | |
112 | } | |
113 | return $result; | |
114 | } | |
96025800 | 115 | |
3097a0d6 | 116 | } |