Skip to content

Commit

Permalink
Merge pull request #14370 from eileenmcnaughton/exception
Browse files Browse the repository at this point in the history
[REF] dev/core#998 make processDupes testable & add test
  • Loading branch information
mattwire authored Jun 1, 2019
2 parents 892614b + 2d1fefa commit 37ec52f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
51 changes: 35 additions & 16 deletions CRM/Contact/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,22 +624,7 @@ public static function processDupes() {
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]);
}
Expand Down Expand Up @@ -908,6 +893,40 @@ public static function isOrQuery() {
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.
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/CRM/Contact/Page/AjaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public function testGetDedupes() {
$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;
Expand Down

0 comments on commit 37ec52f

Please sign in to comment.