| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Test that the API accepts the 'reload' option. |
| 5 | * |
| 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. |
| 10 | * @group headless |
| 11 | */ |
| 12 | class CRM_Utils_API_ReloadOptionTest extends CiviUnitTestCase { |
| 13 | |
| 14 | public function setUp() { |
| 15 | parent::setUp(); |
| 16 | CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_post', array($this, 'onPost')); |
| 17 | } |
| 18 | |
| 19 | /** |
| 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. |
| 22 | */ |
| 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', |
| 29 | )); |
| 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 |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * When the reload option is unrecognized, generate an error |
| 36 | */ |
| 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', |
| 43 | 'options' => array( |
| 44 | 'reload' => 'invalid', |
| 45 | ), |
| 46 | )); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * If reload option is set, then 'create' returns the final nick_name -- even if it |
| 51 | * differs from the inputted nick_name. |
| 52 | */ |
| 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', |
| 59 | 'options' => array( |
| 60 | 'reload' => 1, |
| 61 | ), |
| 62 | )); |
| 63 | $this->assertEquals('First', $result['values'][$result['id']]['first_name']); |
| 64 | $this->assertEquals('munged', $result['values'][$result['id']]['nick_name']); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * When the reload option is combined with chaining, the reload should munge |
| 69 | * the chain results. |
| 70 | */ |
| 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', |
| 79 | ), |
| 80 | 'options' => array( |
| 81 | 'reload' => 1, |
| 82 | ), |
| 83 | )); |
| 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']); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * When the reload option is combined with chaining, the reload should munge |
| 91 | * the chain results, even if sequential=1. |
| 92 | */ |
| 93 | public function testReloadNoChainInterferenceSequential() { |
| 94 | $result = $this->callAPISuccess('contact', 'create', array( |
| 95 | 'sequential' => 1, |
| 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', |
| 102 | ), |
| 103 | 'options' => array( |
| 104 | 'reload' => 1, |
| 105 | ), |
| 106 | )); |
| 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']); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * An implementation of hook_civicrm_post used with all our test cases. |
| 114 | * |
| 115 | * @param $op |
| 116 | * @param string $objectName |
| 117 | * @param int $objectId |
| 118 | * @param $objectRef |
| 119 | */ |
| 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", |
| 124 | array( |
| 125 | 1 => array($objectId, 'Integer'), |
| 126 | ) |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | } |