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.
12 class CRM_Utils_API_ReloadOptionTest
extends CiviUnitTestCase
{
14 public function setUp() {
16 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_post', array($this, 'onPost'));
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.
23 public function testNoReload() {
24 $result = $this->callAPISuccess('contact', 'create', array(
25 'contact_type' => 'Individual',
26 'first_name' => 'First',
27 'last_name' => 'Last',
28 'nick_name' => 'Firstie',
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
35 * When the reload option is unrecognized, generate an error
37 public function testReloadInvalid() {
38 $this->callAPIFailure('contact', 'create', array(
39 'contact_type' => 'Individual',
40 'first_name' => 'First',
41 'last_name' => 'Last',
42 'nick_name' => 'Firstie',
44 'reload' => 'invalid',
50 * If reload option is set, then 'create' returns the final nick_name -- even if it
51 * differs from the inputted nick_name.
53 public function testReloadDefault() {
54 $result = $this->callAPISuccess('contact', 'create', array(
55 'contact_type' => 'Individual',
56 'first_name' => 'First',
57 'last_name' => 'Last',
58 'nick_name' => 'Firstie',
63 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
64 $this->assertEquals('munged', $result['values'][$result['id']]['nick_name']);
68 * When the reload option is combined with chaining, the reload should munge
71 public function testReloadNoChainInterference() {
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',
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']);
90 * When the reload option is combined with chaining, the reload should munge
91 * the chain results, even if sequential=1.
93 public function testReloadNoChainInterferenceSequential() {
94 $result = $this->callAPISuccess('contact', 'create', array(
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',
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']);
113 * An implementation of hook_civicrm_post used with all our test cases.
116 * @param string $objectName
117 * @param int $objectId
120 public function onPost($op, $objectName, $objectId, &$objectRef) {
121 if ($op == 'create' && $objectName == 'Individual') {
122 CRM_Core_DAO
::executeQuery(
123 "UPDATE civicrm_contact SET nick_name = 'munged' WHERE id = %1",
125 1 => array($objectId, 'Integer'),