3 require_once 'CiviTest/CiviUnitTestCase.php';
5 * Test that the API accepts the 'match' and 'match-mandatory' options.
7 class CRM_Utils_API_MatchOptionTest
extends CiviUnitTestCase
{
11 $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_contact WHERE first_name='Jeffrey' and last_name='Lebowski'");
13 // Create noise to ensure we don't accidentally/coincidentally match the first record
14 $this->individualCreate(array('email' => 'ignore1@example.com'));
18 * If there's no pre-existing record, then insert a new one.
20 function testMatch_none() {
21 $result = $this->callAPISuccess('contact', 'create', array(
23 'match' => array('first_name', 'last_name'),
25 'contact_type' => 'Individual',
26 'first_name' => 'Jeffrey',
27 'last_name' => 'Lebowski',
29 'external_identifier' => '1',
31 $this->assertEquals('Jeffrey', $result['values'][$result['id']]['first_name']);
32 $this->assertEquals('Lebowski', $result['values'][$result['id']]['last_name']);
36 * If there's no pre-existing record, then throw an error.
38 function testMatchMandatory_none() {
39 $this->callAPIFailure('contact', 'create', array(
41 'match-mandatory' => array('first_name', 'last_name'),
43 'contact_type' => 'Individual',
44 'first_name' => 'Jeffrey',
45 'last_name' => 'Lebowski',
47 'external_identifier' => '1',
48 ), 'Failed to match existing record');
51 function apiOptionNames() {
54 array('match-mandatory'),
59 * If there's one pre-existing record, then update it.
61 * @dataProvider apiOptionNames
62 * @param string $apiOptionName e.g. "match" or "match-mandatory"
64 function testMatch_one($apiOptionName) {
65 // create basic record
66 $result1 = $this->callAPISuccess('contact', 'create', array(
67 'contact_type' => 'Individual',
68 'first_name' => 'Jeffrey',
69 'last_name' => 'Lebowski',
71 'external_identifier' => '1',
74 $this->individualCreate(array('email' => 'ignore2@example.com')); // more noise!
76 // update the record by matching first/last name
77 $result2 = $this->callAPISuccess('contact', 'create', array(
79 $apiOptionName => array('first_name', 'last_name'),
81 'contact_type' => 'Individual',
82 'first_name' => 'Jeffrey',
83 'last_name' => 'Lebowski',
84 'nick_name' => 'The Dude',
85 'external_identifier' => '2',
88 $this->assertEquals($result1['id'], $result2['id']);
89 $this->assertEquals('Jeffrey', $result2['values'][$result2['id']]['first_name']);
90 $this->assertEquals('Lebowski', $result2['values'][$result2['id']]['last_name']);
91 $this->assertEquals('The Dude', $result2['values'][$result2['id']]['nick_name']);
92 // Make sure it was a real update
93 $this->assertDBQuery(1, "SELECT count(*) FROM civicrm_contact WHERE first_name='Jeffrey' and last_name='Lebowski' AND nick_name = 'The Dude'");
97 * If there's more than one pre-existing record, throw an error.
99 * @dataProvider apiOptionNames
100 * @param string $apiOptionName e.g. "match" or "match-mandatory"
102 function testMatch_many($apiOptionName) {
103 // create the first Lebowski
104 $result1 = $this->callAPISuccess('contact', 'create', array(
105 'contact_type' => 'Individual',
106 'first_name' => 'Jeffrey',
107 'last_name' => 'Lebowski',
108 'nick_name' => 'The Dude',
109 'external_identifier' => '1',
112 // create the second Lebowski
113 $result2 = $this->callAPISuccess('contact', 'create', array(
114 'contact_type' => 'Individual',
115 'first_name' => 'Jeffrey',
116 'last_name' => 'Lebowski',
117 'nick_name' => 'The Big Lebowski',
118 'external_identifier' => '2',
121 $this->individualCreate(array('email' => 'ignore2@example.com')); // more noise!
123 // Try to update - but fail due to ambiguity
124 $result3 = $this->callAPIFailure('contact', 'create', array(
126 $apiOptionName => array('first_name', 'last_name'),
128 'contact_type' => 'Individual',
129 'first_name' => 'Jeffrey',
130 'last_name' => 'Lebowski',
132 'external_identifier' => 'new',
133 ), 'Ambiguous match criteria');