3 require_once 'CiviTest/CiviUnitTestCase.php';
6 * Test that the API accepts the 'reload' option.
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.
13 class CRM_Utils_API_ReloadOptionTest
extends CiviUnitTestCase
{
15 public function setUp() {
17 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_post', array($this, 'onPost'));
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.
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',
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
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']);
114 * An implementation of hook_civicrm_post used with all our test cases.
117 * @param string $objectName
118 * @param int $objectId
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",
126 1 => array($objectId, 'Integer'),