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