From 06ee7674f0705a3c28c9f07d645573d4ef42dfc2 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Tue, 8 Mar 2022 19:18:11 +0000 Subject: [PATCH] Add test for matching on empty string with API4 save --- tests/phpunit/api/v4/Action/SaveTest.php | 76 ++++++++++++++++++------ 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/tests/phpunit/api/v4/Action/SaveTest.php b/tests/phpunit/api/v4/Action/SaveTest.php index d58b4f4c74..ccbc11841d 100644 --- a/tests/phpunit/api/v4/Action/SaveTest.php +++ b/tests/phpunit/api/v4/Action/SaveTest.php @@ -27,22 +27,20 @@ use Civi\Api4\Contact; */ 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']); @@ -54,15 +52,55 @@ class SaveTest extends UnitTestCase { ->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; + } + } -- 2.25.1