Merge pull request #14326 from civicrm/5.14
[civicrm-core.git] / tests / phpunit / CRM / Mailing / BAO / QueryTest.php
1 <?php
2
3 /**
4 * Include dataProvider for tests
5 * @group headless
6 */
7 class CRM_Mailing_BAO_QueryTest extends CiviUnitTestCase {
8
9 /**
10 * @return CRM_Mailing_BAO_QueryTestDataProvider
11 */
12 public function dataProvider() {
13 return new CRM_Mailing_BAO_QueryTestDataProvider();
14 }
15
16 public function setUp() {
17 parent::setUp();
18 }
19
20 public function tearDown() {
21 $tablesToTruncate = array(
22 'civicrm_mailing_event_bounce',
23 'civicrm_mailing_event_delivered',
24 'civicrm_mailing_event_opened',
25 'civicrm_mailing_event_reply',
26 'civicrm_mailing_event_trackable_url_open',
27 'civicrm_mailing_event_queue',
28 'civicrm_mailing_trackable_url',
29 'civicrm_mailing_job',
30 'civicrm_mailing',
31 'civicrm_mailing_recipients',
32 'civicrm_email',
33 'civicrm_contact',
34 );
35 $this->quickCleanup($tablesToTruncate);
36 }
37
38 /**
39 * Test CRM_Contact_BAO_Query::searchQuery()
40 * @dataProvider dataProvider
41 * @param $fv
42 * @param $count
43 * @param $ids
44 * @param $full
45 */
46 public function testSearch($fv, $count, $ids, $full) {
47 $this->loadXMLDataSet(dirname(__FILE__) . '/queryDataset.xml');
48
49 $params = CRM_Contact_BAO_Query::convertFormValues($fv);
50 $obj = new CRM_Contact_BAO_Query($params);
51
52 // let's set useGroupBy=true, to prevent duplicate records
53 $obj->_useGroupBy = TRUE;
54
55 $dao = $obj->searchQuery();
56
57 $contacts = array();
58 while ($dao->fetch()) {
59 $contacts[] = $dao->contact_id;
60 }
61
62 sort($contacts, SORT_NUMERIC);
63
64 $this->assertEquals($ids, $contacts);
65 }
66
67 /**
68 * CRM-20412: Test accurate count for unique open details
69 */
70 public function testOpenedMailingQuery() {
71
72 $this->loadXMLDataSet(dirname(__FILE__) . '/queryDataset.xml');
73 // ensure that total unique opened mail count is same while
74 // fetching rows and row count for mailing_id = 14
75 $totalOpenedMailCount = CRM_Mailing_Event_BAO_Opened::getTotalCount(14, NULL, TRUE);
76 $totalOpenedMail = CRM_Mailing_Event_BAO_Opened::getRows(14, NULL, TRUE);
77
78 $this->assertEquals(4, $totalOpenedMailCount);
79 $this->assertEquals(4, count($totalOpenedMail));
80 }
81
82 /**
83 * CRM-21194: Test accurate count for unique trackable URLs
84 */
85 public function testTrackableUrlMailingQuery() {
86 $this->loadXMLDataSet(dirname(__FILE__) . '/queryDataset.xml');
87
88 // ensure that total unique clicked mail count is same while
89 // fetching rows and row count for mailing_id = 14 and
90 // trackable_url_id 12
91 $totalDistinctTrackableUrlCount = CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount(14, NULL, TRUE, 13);
92 $totalTrackableUrlCount = CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount(14, NULL, FALSE, 13);
93 $totalTrackableUrlMail = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows(14, NULL, TRUE, 13);
94
95 $this->assertEquals(3, $totalDistinctTrackableUrlCount, "Accurately display distinct count of unique trackable URLs");
96 $this->assertEquals(4, $totalTrackableUrlCount, "Accurately display count of unique trackable URLs");
97 $this->assertEquals(3, count($totalTrackableUrlMail), "Accurately display list of unique trackable URLs and who clicked them.");
98 }
99
100 }