Skip to content

Commit

Permalink
Add test for matching on empty string with API4 save
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Mar 9, 2022
1 parent 26c8e1f commit 06ee767
Showing 1 changed file with 57 additions and 19 deletions.
76 changes: 57 additions & 19 deletions tests/phpunit/api/v4/Action/SaveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,20 @@
*/
class SaveTest extends UnitTestCase {

public function testSaveWithMatchingCriteria() {
$records = [
['first_name' => 'One', 'last_name' => 'Test', 'external_identifier' => 'abc'],
['first_name' => 'Two', 'last_name' => 'Test', 'external_identifier' => 'def'],
];

/**
* @dataProvider getMatchingCriteriaDataProvider
* @return void
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testSaveWithMatchingCriteria($matchCriteria, $records, $changes, $expected) {
$contacts = Contact::save(FALSE)
->setRecords($records)
->execute();

$records[0]['last_name'] = $records[1]['last_name'] = 'Changed';
$records[0]['external_identifier'] = 'ghi';

$modified = Contact::save(FALSE)
->setRecords($records)
->setMatch(['first_name', 'external_identifier'])
->setRecords($changes)
->setMatch($matchCriteria)
->execute();

$this->assertGreaterThan($contacts[0]['id'], $modified[0]['id']);
Expand All @@ -54,15 +52,55 @@ public function testSaveWithMatchingCriteria() {
->addWhere('id', 'IN', $ids)
->addOrderBy('id')
->execute();
$expected = [
// Original insert
['id' => $contacts[0]['id'], 'first_name' => 'One', 'last_name' => 'Test', 'external_identifier' => 'abc'],
// Match+update
['id' => $contacts[1]['id'], 'first_name' => 'Two', 'last_name' => 'Changed', 'external_identifier' => 'def'],
// Subsequent insert
['id' => $modified[0]['id'], 'first_name' => 'One', 'last_name' => 'Changed', 'external_identifier' => 'ghi'],
];

for ($index = 0; $index < count($expected); $index++) {
$expected[$index]['id'] = $contacts[0]['id'] + $index;
}
$this->assertEquals($expected, (array) $get);
}

public function getMatchingCriteriaDataProvider() {
// data = [ match criteria, records, modifiedRecords, expected results ]
$data[] = [
['first_name', 'external_identifier'],
[
['first_name' => 'One', 'last_name' => 'Test', 'external_identifier' => 'abc'],
['first_name' => 'Two', 'last_name' => 'Test', 'external_identifier' => 'def'],
],
[
['first_name' => 'One', 'last_name' => 'Changed', 'external_identifier' => 'ghi'],
['first_name' => 'Two', 'last_name' => 'Changed', 'external_identifier' => 'def'],
],
[
// Original insert
['first_name' => 'One', 'last_name' => 'Test', 'external_identifier' => 'abc'],
// Match+update
['first_name' => 'Two', 'last_name' => 'Changed', 'external_identifier' => 'def'],
// Subsequent insert
['first_name' => 'One', 'last_name' => 'Changed', 'external_identifier' => 'ghi'],
],
];
// Test that we get a match on an empty string (eg. external_identifier => '')
$data[] = [
['first_name', 'last_name', 'external_identifier'],
[
['first_name' => 'One', 'last_name' => 'Test', 'external_identifier' => 'abc'],
['first_name' => 'Two', 'last_name' => 'Test', 'external_identifier' => ''],
],
[
['first_name' => 'One', 'last_name' => 'Test', 'external_identifier' => 'ghi'],
['first_name' => 'Two', 'last_name' => 'Test', 'external_identifier' => ''],
],
[
// Original insert
['first_name' => 'One', 'last_name' => 'Test', 'external_identifier' => 'abc'],
// Match+update
['first_name' => 'Two', 'last_name' => 'Test', 'external_identifier' => ''],
// Subsequent insert
['first_name' => 'One', 'last_name' => 'Test', 'external_identifier' => 'ghi'],
],
];
return $data;
}

}

0 comments on commit 06ee767

Please sign in to comment.