4 * Test that the API accepts the 'reload' option.
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.
11 class CRM_Utils_API_ReloadOptionTest
extends CiviUnitTestCase
{
13 public function setUp() {
15 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_post', array($this, 'onPost'));
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.
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',
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
34 * When the reload option is unrecognized, generate an error
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',
43 'reload' => 'invalid',
49 * If reload option is set, then 'create' returns the final nick_name -- even if it
50 * differs from the inputted nick_name.
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',
62 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
63 $this->assertEquals('munged', $result['values'][$result['id']]['nick_name']);
67 * When the reload option is combined with chaining, the reload should munge
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',
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']);
89 * When the reload option is combined with chaining, the reload should munge
90 * the chain results, even if sequential=1.
92 public function testReloadNoChainInterferenceSequential() {
93 $result = $this->callAPISuccess('contact', 'create', array(
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',
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']);
112 * An implementation of hook_civicrm_post used with all our test cases.
115 * @param string $objectName
116 * @param int $objectId
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",
124 1 => array($objectId, 'Integer'),