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 // munged by hook, but we haven't realized it
32 $this->assertEquals('Firstie', $result['values'][$result['id']]['nick_name']);
36 * When the reload option is unrecognized, generate an error
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',
45 'reload' => 'invalid',
51 * If reload option is set, then 'create' returns the final nick_name -- even if it
52 * differs from the inputted nick_name.
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',
64 $this->assertEquals('First', $result['values'][$result['id']]['first_name']);
65 $this->assertEquals('munged', $result['values'][$result['id']]['nick_name']);
69 * When the reload option is combined with chaining, the reload should munge
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',
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']);
91 * When the reload option is combined with chaining, the reload should munge
92 * the chain results, even if sequential=1.
94 public function testReloadNoChainInterferenceSequential() {
95 $result = $this->callAPISuccess('contact', 'create', array(
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',
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']);