Merge pull request #6337 from jitendrapurohit/CRM-16792master
[civicrm-core.git] / tests / phpunit / CRM / Utils / API / ReloadOptionTest.php
1 <?php
2
3 require_once 'CiviTest/CiviUnitTestCase.php';
4
5 /**
6 * Test that the API accepts the 'reload' option.
7 *
8 * To do this, each of our test cases will perform a 'create' call and use hook_civicrm_post
9 * to munge the database. If the reload option is present, then the return value should reflect
10 * the final SQL content (after calling hook_civicrm_post). If the reload option is missing,
11 * then the return should reflect the inputted (unmodified) data.
12 */
13 class CRM_Utils_API_ReloadOptionTest extends CiviUnitTestCase {
14
15 public function setUp() {
16 parent::setUp();
17 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_post', array($this, 'onPost'));
18 }
19
20 /**
21 * If reload option is missing, then 'create' returns the inputted nick_name -- despite the
22 * fact that the hook manipulated the actual DB content.
23 */
24 public function testNoReload() {
25 $result = $this->callAPISuccess('contact', 'create', array(
26 'contact_type' => 'Individual',
27 'first_name' => 'First',
28 'last_name' => 'Last',
29 'nick_name' => 'Firstie',
30 ));
31 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
32 $this->assertEquals('Firstie', $result['values'][$result['id']]['nick_name']); // munged by hook, but we haven't realized it
33 }
34
35 /**
36 * When the reload option is unrecognized, generate an error
37 */
38 public function testReloadInvalid() {
39 $this->callAPIFailure('contact', 'create', array(
40 'contact_type' => 'Individual',
41 'first_name' => 'First',
42 'last_name' => 'Last',
43 'nick_name' => 'Firstie',
44 'options' => array(
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', array(
56 'contact_type' => 'Individual',
57 'first_name' => 'First',
58 'last_name' => 'Last',
59 'nick_name' => 'Firstie',
60 'options' => array(
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', array(
74 'contact_type' => 'Individual',
75 'first_name' => 'First',
76 'last_name' => 'Last',
77 'nick_name' => 'Firstie',
78 'api.Email.create' => array(
79 'email' => 'test@example.com',
80 ),
81 'options' => array(
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', array(
96 'sequential' => 1,
97 'contact_type' => 'Individual',
98 'first_name' => 'First',
99 'last_name' => 'Last',
100 'nick_name' => 'Firstie',
101 'api.Email.create' => array(
102 'email' => 'test@example.com',
103 ),
104 'options' => array(
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 /**
114 * An implementation of hook_civicrm_post used with all our test cases.
115 *
116 * @param $op
117 * @param string $objectName
118 * @param int $objectId
119 * @param $objectRef
120 */
121 public function onPost($op, $objectName, $objectId, &$objectRef) {
122 if ($op == 'create' && $objectName == 'Individual') {
123 CRM_Core_DAO::executeQuery(
124 "UPDATE civicrm_contact SET nick_name = 'munged' WHERE id = %1",
125 array(
126 1 => array($objectId, 'Integer'),
127 )
128 );
129 }
130 }
131
132 }