Skip to content

Commit

Permalink
Merge pull request #10988 from jmcclelland/CRM-21194
Browse files Browse the repository at this point in the history
CRM-21194 - ensure distinct count of clickable URLs works.
  • Loading branch information
eileenmcnaughton authored Apr 16, 2018
2 parents 0532834 + 12c4780 commit 00e1884
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
25 changes: 16 additions & 9 deletions CRM/Mailing/Event/BAO/TrackableURLOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ public static function getTotalCount(
$mailing = CRM_Mailing_BAO_Mailing::getTableName();
$job = CRM_Mailing_BAO_MailingJob::getTableName();

$distinct = NULL;
if ($is_distinct) {
$distinct = 'DISTINCT ';
}
$query = "
SELECT COUNT($click.id) as opened
SELECT COUNT($distinct $click.event_queue_id) as opened
FROM $click
INNER JOIN $queue
ON $click.event_queue_id = $queue.id
Expand All @@ -168,10 +172,6 @@ public static function getTotalCount(
$query .= " AND $click.trackable_url_id = " . CRM_Utils_Type::escape($url_id, 'Integer');
}

if ($is_distinct) {
$query .= " GROUP BY $queue.id ";
}

// query was missing
$dao->query($query);

Expand Down Expand Up @@ -305,9 +305,16 @@ public static function &getRows(
$query = "
SELECT $contact.display_name as display_name,
$contact.id as contact_id,
$email.email as email,
$click.time_stamp as date,
$url.url as url
$email.email as email,";

if ($is_distinct) {
$query .= "MIN($click.time_stamp) as date,";
}
else {
$query .= "$click.time_stamp as date,";
}

$query .= "$url.url as url
FROM $contact
INNER JOIN $queue
ON $queue.contact_id = $contact.id
Expand Down Expand Up @@ -337,7 +344,7 @@ public static function &getRows(
}

if ($is_distinct) {
$query .= " GROUP BY $queue.id, $click.time_stamp, $url.url ";
$query .= " GROUP BY $queue.id, $url.url ";
}

$orderBy = "sort_name ASC, {$click}.time_stamp DESC";
Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/CRM/Mailing/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,27 @@ public function testOpenedMailingQuery() {
$this->assertEquals(4, count($totalOpenedMail));
}

/**
* CRM-21194: Test accurate count for unique trackable URLs
*/
public function testTrackableUrlMailingQuery() {
$op = new PHPUnit_Extensions_Database_Operation_Insert();
$op->execute($this->_dbconn,
$this->createFlatXMLDataSet(
dirname(__FILE__) . '/queryDataset.xml'
)
);

// ensure that total unique clicked mail count is same while
// fetching rows and row count for mailing_id = 14 and
// trackable_url_id 12
$totalDistinctTrackableUrlCount = CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount(14, NULL, TRUE, 13);
$totalTrackableUrlCount = CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount(14, NULL, FALSE, 13);
$totalTrackableUrlMail = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows(14, NULL, TRUE, 13);

$this->assertEquals(3, $totalDistinctTrackableUrlCount, "Accurately display distinct count of unique trackable URLs");
$this->assertEquals(4, $totalTrackableUrlCount, "Accurately display count of unique trackable URLs");
$this->assertEquals(3, count($totalTrackableUrlMail), "Accurately display list of unique trackable URLs and who clicked them.");
}

}
3 changes: 2 additions & 1 deletion tests/phpunit/CRM/Mailing/BAO/queryDataset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
test05 109 n y n n
test06 110 n y2 n y
test07 111 n y y[dc] n
test08 112 n y y[c] y
test08 112 n y y[c2] y
Mailing 15: Second Test Mailing Events, 2011-05-26
Expand Down Expand Up @@ -131,6 +131,7 @@
<civicrm_mailing_event_trackable_url_open id="8" event_queue_id="51" trackable_url_id="12" time_stamp="2011-05-26 13:21:09"/>
<civicrm_mailing_event_trackable_url_open id="9" event_queue_id="51" trackable_url_id="13" time_stamp="2011-05-26 13:21:13"/>
<civicrm_mailing_event_trackable_url_open id="7" event_queue_id="52" trackable_url_id="13" time_stamp="2011-05-26 13:20:03"/>
<civicrm_mailing_event_trackable_url_open id="12" event_queue_id="52" trackable_url_id="13" time_stamp="2011-05-27 13:20:03"/>
<civicrm_mailing_event_trackable_url_open id="10" event_queue_id="55" trackable_url_id="14" time_stamp="2011-05-26 13:23:54"/>
<civicrm_mailing_event_trackable_url_open id="11" event_queue_id="55" trackable_url_id="15" time_stamp="2011-05-26 13:23:58"/>
</dataset>

0 comments on commit 00e1884

Please sign in to comment.