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