CRM-13312 - CRM_Utils_API_MatchOption - Add option for updating based on user-specifi...
[civicrm-core.git] / tests / phpunit / CRM / Utils / API / MatchOptionTest.php
1 <?php
2
3 require_once 'CiviTest/CiviUnitTestCase.php';
4 /**
5 * Test that the API accepts the 'match' and 'match-mandatory' options.
6 */
7 class CRM_Utils_API_MatchOptionTest extends CiviUnitTestCase {
8
9 function setUp() {
10 parent::setUp();
11 $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_contact WHERE first_name='Jeffrey' and last_name='Lebowski'");
12
13 // Create noise to ensure we don't accidentally/coincidentally match the first record
14 $this->individualCreate(array('email' => 'ignore1@example.com'));
15 }
16
17 /**
18 * If there's no pre-existing record, then insert a new one.
19 */
20 function testMatch_none() {
21 $result = $this->callAPISuccess('contact', 'create', array(
22 'options' => array(
23 'match' => array('first_name', 'last_name'),
24 ),
25 'contact_type' => 'Individual',
26 'first_name' => 'Jeffrey',
27 'last_name' => 'Lebowski',
28 'nick_name' => '',
29 'external_identifier' => '1',
30 ));
31 $this->assertEquals('Jeffrey', $result['values'][$result['id']]['first_name']);
32 $this->assertEquals('Lebowski', $result['values'][$result['id']]['last_name']);
33 }
34
35 /**
36 * If there's no pre-existing record, then throw an error.
37 */
38 function testMatchMandatory_none() {
39 $this->callAPIFailure('contact', 'create', array(
40 'options' => array(
41 'match-mandatory' => array('first_name', 'last_name'),
42 ),
43 'contact_type' => 'Individual',
44 'first_name' => 'Jeffrey',
45 'last_name' => 'Lebowski',
46 'nick_name' => '',
47 'external_identifier' => '1',
48 ), 'Failed to match existing record');
49 }
50
51 function apiOptionNames() {
52 return array(
53 array('match'),
54 array('match-mandatory'),
55 );
56 }
57
58 /**
59 * If there's one pre-existing record, then update it.
60 *
61 * @dataProvider apiOptionNames
62 * @param string $apiOptionName e.g. "match" or "match-mandatory"
63 */
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',
70 'nick_name' => '',
71 'external_identifier' => '1',
72 ));
73
74 $this->individualCreate(array('email' => 'ignore2@example.com')); // more noise!
75
76 // update the record by matching first/last name
77 $result2 = $this->callAPISuccess('contact', 'create', array(
78 'options' => array(
79 $apiOptionName => array('first_name', 'last_name'),
80 ),
81 'contact_type' => 'Individual',
82 'first_name' => 'Jeffrey',
83 'last_name' => 'Lebowski',
84 'nick_name' => 'The Dude',
85 'external_identifier' => '2',
86 ));
87
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'");
94 }
95
96 /**
97 * If there's more than one pre-existing record, throw an error.
98 *
99 * @dataProvider apiOptionNames
100 * @param string $apiOptionName e.g. "match" or "match-mandatory"
101 */
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',
110 ));
111
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',
119 ));
120
121 $this->individualCreate(array('email' => 'ignore2@example.com')); // more noise!
122
123 // Try to update - but fail due to ambiguity
124 $result3 = $this->callAPIFailure('contact', 'create', array(
125 'options' => array(
126 $apiOptionName => array('first_name', 'last_name'),
127 ),
128 'contact_type' => 'Individual',
129 'first_name' => 'Jeffrey',
130 'last_name' => 'Lebowski',
131 'nick_name' => '',
132 'external_identifier' => 'new',
133 ), 'Ambiguous match criteria');
134 }
135
136 }