From: eileen Date: Wed, 29 May 2019 05:34:15 +0000 (+1200) Subject: [REF] dev/core#998 make processDupes testable & add test X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2d1fefa0ae61cf8b8700f975801a6e847ac24f09;p=civicrm-core.git [REF] dev/core#998 make processDupes testable & add test I am pretty sure that there is an issue here with flipped contacts mis-comparing but this change is just preliminary - improving testing & preparing to consolidate code so we always go via the api and the same logic always applies --- diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index da32d7329f..97686838a9 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -624,22 +624,7 @@ LIMIT {$offset}, {$rowCount} return; } - $exception = new CRM_Dedupe_DAO_Exception(); - $exception->contact_id1 = $cid; - $exception->contact_id2 = $oid; - //make sure contact2 > contact1. - if ($cid > $oid) { - $exception->contact_id1 = $oid; - $exception->contact_id2 = $cid; - } - $exception->find(TRUE); - $status = NULL; - if ($oper == 'dupe-nondupe') { - $status = $exception->save(); - } - if ($oper == 'nondupe-dupe') { - $status = $exception->delete(); - } + $status = self::markNonDuplicates($cid, $oid, $oper); CRM_Utils_JSON::output(['status' => ($status) ? $oper : $status]); } @@ -908,6 +893,40 @@ LIMIT {$offset}, {$rowCount} return !empty($searchData['value']); } + /** + * Mark not duplicates. + * + * Note this function would sensibly be replaced by an api-call but extracting here to add a test first. + * + * I would have like to make it private but test class accesses it & it doesn't warrant being a BAO class + * as it should feel very endangered. + * + * @param int $cid + * @param int $oid + * @param 'dupe-nondupe'|'nondupe-dupe' $oper + * + * @return \CRM_Core_DAO|mixed|null + */ + public static function markNonDuplicates($cid, $oid, $oper) { + $exception = new CRM_Dedupe_DAO_Exception(); + $exception->contact_id1 = $cid; + $exception->contact_id2 = $oid; + //make sure contact2 > contact1. + if ($cid > $oid) { + $exception->contact_id1 = $oid; + $exception->contact_id2 = $cid; + } + $exception->find(TRUE); + $status = NULL; + if ($oper == 'dupe-nondupe') { + $status = $exception->save(); + } + if ($oper == 'nondupe-dupe') { + $status = $exception->delete(); + } + return $status; + } + /** * Retrieve a PDF Page Format for the PDF Letter form. */ diff --git a/tests/phpunit/CRM/Contact/Page/AjaxTest.php b/tests/phpunit/CRM/Contact/Page/AjaxTest.php index 699cc99617..bbcfbd22b8 100644 --- a/tests/phpunit/CRM/Contact/Page/AjaxTest.php +++ b/tests/phpunit/CRM/Contact/Page/AjaxTest.php @@ -46,6 +46,22 @@ class CRM_Contact_Page_AjaxTest extends CiviUnitTestCase { $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result); } + /** + * Tests the 'guts' of the processDupes function. + * + * @throws \Exception + */ + public function testProcessDupes() { + $contact1 = $this->individualCreate(); + $contact2 = $this->individualCreate(); + $contact3 = $this->individualCreate(); + CRM_Contact_Page_AJAX::markNonDuplicates($contact1, $contact2, 'dupe-nondupe'); + CRM_Contact_Page_AJAX::markNonDuplicates($contact3, $contact1, 'dupe-nondupe'); + $this->callAPISuccessGetSingle('Exception', ['contact_id1' => $contact1, 'contact_id2' => $contact2]); + // Note that in saving the order is changed to have the lowest ID first. + $this->callAPISuccessGetSingle('Exception', ['contact_id1' => $contact1, 'contact_id2' => $contact3]); + } + public function testGetDedupesPostCode() { $_REQUEST['gid'] = 1; $_REQUEST['rgid'] = 1;