useTransaction(TRUE); CRM_Utils_Hook::singleton()->setHook('civicrm_selectWhereClause', [$this, 'hook_civicrm_selectWhereClause']); } public function testHookPhoneClause() { $person1 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester']); $cid = $person1['id']; for ($number = 1; $number < 6; ++$number) { $this->callAPISuccess('Phone', 'create', [ 'contact_id' => $cid, 'phone' => $number, ]); } $this->hookEntity = 'Phone'; $this->hookCondition = [ 'phone' => ['= 3'], ]; $phone = $this->callAPISuccessGetSingle('Phone', ['contact_id' => $cid, 'check_permissions' => 1]); $this->assertEquals(3, $phone['phone']); } public function testHookContactClause() { $person1 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester', 'email' => 'bob@test.er']); $person2 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Tom', 'last_name' => 'Tester', 'email' => 'tom@test.er']); $person3 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Tim', 'last_name' => 'Tester', 'email' => 'tim@test.er']); $this->hookEntity = 'Contact'; $this->hookCondition = ['id' => ['= ' . $person2['id']]]; $email = $this->callAPISuccessGetSingle('Email', ['check_permissions' => 1]); $this->assertEquals($person2['id'], $email['contact_id']); } /** * Implements hook_civicrm_selectWhereClause(). */ public function hook_civicrm_selectWhereClause($entity, &$clauses) { if ($entity == $this->hookEntity) { foreach ($this->hookCondition as $field => $clause) { $clauses[$field] = array_merge(CRM_Utils_Array::value($field, $clauses, []), $clause); } } } }