Merge pull request #14878 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / Utils / API / ReloadOptionTest.php
1 <?php
2
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.
10 * @group headless
11 */
12 class CRM_Utils_API_ReloadOptionTest extends CiviUnitTestCase {
13
14 public function setUp() {
15 parent::setUp();
16 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_post', [$this, 'onPost']);
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 */
23 public function testNoReload() {
24 $result = $this->callAPISuccess('contact', 'create', [
25 'contact_type' => 'Individual',
26 'first_name' => 'First',
27 'last_name' => 'Last',
28 'nick_name' => 'Firstie',
29 ]);
30 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
31 // munged by hook, but we haven't realized it
32 $this->assertEquals('Firstie', $result['values'][$result['id']]['nick_name']);
33 }
34
35 /**
36 * When the reload option is unrecognized, generate an error
37 */
38 public function testReloadInvalid() {
39 $this->callAPIFailure('contact', 'create', [
40 'contact_type' => 'Individual',
41 'first_name' => 'First',
42 'last_name' => 'Last',
43 'nick_name' => 'Firstie',
44 'options' => [
45 'reload' => 'invalid',
46 ],
47 ]);
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 */
54 public function testReloadDefault() {
55 $result = $this->callAPISuccess('contact', 'create', [
56 'contact_type' => 'Individual',
57 'first_name' => 'First',
58 'last_name' => 'Last',
59 'nick_name' => 'Firstie',
60 'options' => [
61 'reload' => 1,
62 ],
63 ]);
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 */
72 public function testReloadNoChainInterference() {
73 $result = $this->callAPISuccess('contact', 'create', [
74 'contact_type' => 'Individual',
75 'first_name' => 'First',
76 'last_name' => 'Last',
77 'nick_name' => 'Firstie',
78 'api.Email.create' => [
79 'email' => 'test@example.com',
80 ],
81 'options' => [
82 'reload' => 1,
83 ],
84 ]);
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
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() {
95 $result = $this->callAPISuccess('contact', 'create', [
96 'sequential' => 1,
97 'contact_type' => 'Individual',
98 'first_name' => 'First',
99 'last_name' => 'Last',
100 'nick_name' => 'Firstie',
101 'api.Email.create' => [
102 'email' => 'test@example.com',
103 ],
104 'options' => [
105 'reload' => 1,
106 ],
107 ]);
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
113 }