tests/phpunit - Declare `@group headless`
[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();
836f9779 16 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_post', array($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() {
0e4d4398
TO
24 $result = $this->callAPISuccess('contact', 'create', array(
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 $this->assertEquals('Firstie', $result['values'][$result['id']]['nick_name']); // munged by hook, but we haven't realized it
32 }
33
34 /**
35 * When the reload option is unrecognized, generate an error
36 */
00be9182 37 public function testReloadInvalid() {
0e4d4398
TO
38 $this->callAPIFailure('contact', 'create', array(
39 'contact_type' => 'Individual',
40 'first_name' => 'First',
41 'last_name' => 'Last',
42 'nick_name' => 'Firstie',
43 'options' => array(
44 'reload' => 'invalid',
45 ),
46 ));
47 }
48
49 /**
50 * If reload option is set, then 'create' returns the final nick_name -- even if it
51 * differs from the inputted nick_name.
52 */
00be9182 53 public function testReloadDefault() {
0e4d4398
TO
54 $result = $this->callAPISuccess('contact', 'create', array(
55 'contact_type' => 'Individual',
56 'first_name' => 'First',
57 'last_name' => 'Last',
58 'nick_name' => 'Firstie',
59 'options' => array(
21dfd5f5 60 'reload' => 1,
0e4d4398
TO
61 ),
62 ));
63 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
64 $this->assertEquals('munged', $result['values'][$result['id']]['nick_name']);
65 }
66
67 /**
68 * When the reload option is combined with chaining, the reload should munge
69 * the chain results.
70 */
00be9182 71 public function testReloadNoChainInterference() {
0e4d4398
TO
72 $result = $this->callAPISuccess('contact', 'create', array(
73 'contact_type' => 'Individual',
74 'first_name' => 'First',
75 'last_name' => 'Last',
76 'nick_name' => 'Firstie',
77 'api.Email.create' => array(
78 'email' => 'test@example.com',
79 ),
80 'options' => array(
21dfd5f5 81 'reload' => 1,
0e4d4398
TO
82 ),
83 ));
84 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
85 $this->assertEquals('munged', $result['values'][$result['id']]['nick_name']);
86 $this->assertAPISuccess($result['values'][$result['id']]['api.Email.create']);
87 }
88
d719a3df
JV
89 /**
90 * When the reload option is combined with chaining, the reload should munge
91 * the chain results, even if sequential=1.
92 */
93 public function testReloadNoChainInterferenceSequential() {
94 $result = $this->callAPISuccess('contact', 'create', array(
95 'sequential' => 1,
96 'contact_type' => 'Individual',
97 'first_name' => 'First',
98 'last_name' => 'Last',
99 'nick_name' => 'Firstie',
100 'api.Email.create' => array(
101 'email' => 'test@example.com',
102 ),
103 'options' => array(
104 'reload' => 1,
105 ),
106 ));
107 $this->assertEquals('First', $result['values'][0]['first_name']);
108 $this->assertEquals('munged', $result['values'][0]['nick_name']);
109 $this->assertAPISuccess($result['values'][0]['api.Email.create']);
110 }
111
0e4d4398 112 /**
2241036a 113 * An implementation of hook_civicrm_post used with all our test cases.
0e4d4398
TO
114 *
115 * @param $op
100fef9d
CW
116 * @param string $objectName
117 * @param int $objectId
0e4d4398
TO
118 * @param $objectRef
119 */
00be9182 120 public function onPost($op, $objectName, $objectId, &$objectRef) {
0e4d4398
TO
121 if ($op == 'create' && $objectName == 'Individual') {
122 CRM_Core_DAO::executeQuery(
123 "UPDATE civicrm_contact SET nick_name = 'munged' WHERE id = %1",
124 array(
21dfd5f5 125 1 => array($objectId, 'Integer'),
0e4d4398
TO
126 )
127 );
128 }
129 }
96025800 130
0e4d4398 131}