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