Merge pull request #15878 from civicrm/5.20
[civicrm-core.git] / tests / phpunit / CRM / Utils / API / ReloadOptionTest.php
CommitLineData
0e4d4398
TO
1<?php
2
0e4d4398
TO
3/**
4 * Test that the API accepts the 'reload' option.
5 *
6 * To do this, each of our test cases will perform a 'create' call and use hook_civicrm_post
7 * to munge the database. If the reload option is present, then the return value should reflect
8 * the final SQL content (after calling hook_civicrm_post). If the reload option is missing,
9 * then the return should reflect the inputted (unmodified) data.
acb109b7 10 * @group headless
0e4d4398
TO
11 */
12class CRM_Utils_API_ReloadOptionTest extends CiviUnitTestCase {
13
00be9182 14 public function setUp() {
0e4d4398 15 parent::setUp();
9099cab3 16 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_post', [$this, 'onPost']);
0e4d4398
TO
17 }
18
19 /**
20 * If reload option is missing, then 'create' returns the inputted nick_name -- despite the
21 * fact that the hook manipulated the actual DB content.
22 */
00be9182 23 public function testNoReload() {
9099cab3 24 $result = $this->callAPISuccess('contact', 'create', [
0e4d4398
TO
25 'contact_type' => 'Individual',
26 'first_name' => 'First',
27 'last_name' => 'Last',
28 'nick_name' => 'Firstie',
9099cab3 29 ]);
0e4d4398 30 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
39b959db
SL
31 // munged by hook, but we haven't realized it
32 $this->assertEquals('Firstie', $result['values'][$result['id']]['nick_name']);
0e4d4398
TO
33 }
34
35 /**
36 * When the reload option is unrecognized, generate an error
37 */
00be9182 38 public function testReloadInvalid() {
9099cab3 39 $this->callAPIFailure('contact', 'create', [
0e4d4398
TO
40 'contact_type' => 'Individual',
41 'first_name' => 'First',
42 'last_name' => 'Last',
43 'nick_name' => 'Firstie',
9099cab3 44 'options' => [
0e4d4398 45 'reload' => 'invalid',
9099cab3
CW
46 ],
47 ]);
0e4d4398
TO
48 }
49
50 /**
51 * If reload option is set, then 'create' returns the final nick_name -- even if it
52 * differs from the inputted nick_name.
53 */
00be9182 54 public function testReloadDefault() {
9099cab3 55 $result = $this->callAPISuccess('contact', 'create', [
0e4d4398
TO
56 'contact_type' => 'Individual',
57 'first_name' => 'First',
58 'last_name' => 'Last',
59 'nick_name' => 'Firstie',
9099cab3 60 'options' => [
21dfd5f5 61 'reload' => 1,
9099cab3
CW
62 ],
63 ]);
0e4d4398
TO
64 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
65 $this->assertEquals('munged', $result['values'][$result['id']]['nick_name']);
66 }
67
68 /**
69 * When the reload option is combined with chaining, the reload should munge
70 * the chain results.
71 */
00be9182 72 public function testReloadNoChainInterference() {
9099cab3 73 $result = $this->callAPISuccess('contact', 'create', [
0e4d4398
TO
74 'contact_type' => 'Individual',
75 'first_name' => 'First',
76 'last_name' => 'Last',
77 'nick_name' => 'Firstie',
9099cab3 78 'api.Email.create' => [
0e4d4398 79 'email' => 'test@example.com',
9099cab3
CW
80 ],
81 'options' => [
21dfd5f5 82 'reload' => 1,
9099cab3
CW
83 ],
84 ]);
0e4d4398
TO
85 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
86 $this->assertEquals('munged', $result['values'][$result['id']]['nick_name']);
87 $this->assertAPISuccess($result['values'][$result['id']]['api.Email.create']);
88 }
89
d719a3df
JV
90 /**
91 * When the reload option is combined with chaining, the reload should munge
92 * the chain results, even if sequential=1.
93 */
94 public function testReloadNoChainInterferenceSequential() {
9099cab3 95 $result = $this->callAPISuccess('contact', 'create', [
d719a3df
JV
96 'sequential' => 1,
97 'contact_type' => 'Individual',
98 'first_name' => 'First',
99 'last_name' => 'Last',
100 'nick_name' => 'Firstie',
9099cab3 101 'api.Email.create' => [
d719a3df 102 'email' => 'test@example.com',
9099cab3
CW
103 ],
104 'options' => [
d719a3df 105 'reload' => 1,
9099cab3
CW
106 ],
107 ]);
d719a3df
JV
108 $this->assertEquals('First', $result['values'][0]['first_name']);
109 $this->assertEquals('munged', $result['values'][0]['nick_name']);
110 $this->assertAPISuccess($result['values'][0]['api.Email.create']);
111 }
112
0e4d4398 113}