3 require_once 'CiviTest/CiviUnitTestCase.php';
5 * Test that the API accepts the 'reload' option.
7 * To do this, each of our test cases will perform a 'create' call and use hook_civicrm_post
8 * to munge the database. If the reload option is present, then the return value should reflect
9 * the final SQL content (after calling hook_civicrm_post). If the reload option is missing,
10 * 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 * An implementation of hook_civicrm_post used with all our test cases.
93 * @param string $objectName
94 * @param int $objectId
97 public function onPost($op, $objectName, $objectId, &$objectRef) {
98 if ($op == 'create' && $objectName == 'Individual') {
99 CRM_Core_DAO
::executeQuery(
100 "UPDATE civicrm_contact SET nick_name = 'munged' WHERE id = %1",
102 1 => array($objectId, 'Integer'),