Merge pull request #17203 from artfulrobot/artfulrobot-cleanup-job-improvements
[civicrm-core.git] / tests / phpunit / api / v3 / APIWrapperTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 require_once 'api/Wrapper.php';
13
14 /**
15 * Test class for API functions
16 *
17 * @package CiviCRM_APIv3
18 * @group headless
19 */
20 class api_v3_APIWrapperTest extends CiviUnitTestCase {
21 public $DBResetRequired = FALSE;
22
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.
29 */
30 protected function setUp() {
31 parent::setUp();
32 $this->useTransaction(TRUE);
33 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_apiWrappers', [$this, 'onApiWrappers']);
34 }
35
36 /**
37 * Tears down the fixture, for example, closes a network connection.
38 * This method is called after a test is executed.
39 */
40 protected function tearDown() {
41 parent::tearDown();
42 }
43
44 /**
45 * @param $apiWrappers
46 * @param $apiRequest
47 */
48 public function onApiWrappers(&$apiWrappers, $apiRequest) {
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
56 public function testWrapperHook() {
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".
61 $result = $this->callAPISuccess('contact', 'create', [
62 'contact_type' => 'Invalid',
63 'first_name' => 'First',
64 'last_name' => 'Last',
65 ]);
66 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
67 $this->assertEquals('MUNGE! First Last', $result['values'][$result['id']]['display_name_munged']);
68 }
69
70 }
71
72 /**
73 * Class api_v3_APIWrapperTest_Impl
74 */
75 class api_v3_APIWrapperTest_Impl implements API_Wrapper {
76
77 /**
78 * @inheritDoc
79 */
80 public function fromApiInput($apiRequest) {
81 if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') {
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 /**
90 * @inheritDoc
91 */
92 public function toApiOutput($apiRequest, $result) {
93 if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') {
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 }
101
102 }