4 * Include dataProvider for tests
8 class CRM_Contribute_BAO_QueryTest
extends CiviUnitTestCase
{
10 public function tearDown(): void
{
11 $this->quickCleanUpFinancialEntities();
16 * Check that we get a successful trying to return by pseudo-fields
20 * @param bool $isUseKeySort
21 * Does the order by use a key sort. A key sort uses the mysql 'field' function to
22 * order by a passed in list. It makes sense for option groups & small sets
23 * but may not do for long lists like states - performance testing not done on that yet.
25 * @throws \CRM_Core_Exception
27 * @dataProvider getSortFields
29 public function testSearchPseudoReturnProperties($sort, $isUseKeySort) {
30 $contactID = $this->individualCreate();
31 $this->contributionCreate(['contact_id' => $contactID, 'financial_type_id' => 'Campaign Contribution']);
32 $this->contributionCreate(['contact_id' => $contactID, 'financial_type_id' => 'Donation']);
33 $donationTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation');
36 ['financial_type_id', '=', $donationTypeID , 1, 0],
39 $queryObj = new CRM_Contact_BAO_Query($params);
40 $sql = $queryObj->getSearchSQL(0, 0, $sort . ' asc');
42 $this->assertStringContainsString('field(', $sql);
45 $resultDAO = CRM_Core_DAO
::executeQuery($sql);
46 $this->assertTrue($resultDAO->fetch());
47 $this->assertEquals(1, $resultDAO->N
);
49 catch (PEAR_Exception
$e) {
50 $err = $e->getCause();
51 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);
57 * Data provider for sort fields
59 public function getSortFields() {
61 ['financial_type', TRUE],
62 ['payment_instrument', TRUE],
63 ['individual_prefix', TRUE],
64 ['communication_style', TRUE],
66 ['state_province', FALSE],
72 * Test receive_date_high, low & relative work.
74 * @throws \CRM_Core_Exception
76 public function testRelativeContributionDates() {
77 $contribution1 = $this->contributionCreate(['receive_date' => '2018-01-02', 'contact_id' => $this->individualCreate()]);
78 $contribution2 = $this->contributionCreate(['receive_date' => '2017-01-02', 'contact_id' => $this->individualCreate()]);
79 $queryObj = new CRM_Contact_BAO_Query([['receive_date_low', '=', 20170101, 1, 0]]);
80 $this->assertEquals(2, $queryObj->searchQuery(0, 0, NULL, TRUE));
81 $queryObj = new CRM_Contact_BAO_Query([['receive_date_low', '=', 20180101, 1, 0]]);
82 $this->assertEquals(1, $queryObj->searchQuery(0, 0, NULL, TRUE));
83 $queryObj = new CRM_Contact_BAO_Query([['receive_date_high', '=', 20180101, 1, 0]]);
84 $this->assertEquals(1, $queryObj->searchQuery(0, 0, NULL, TRUE));
85 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution1]);
86 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution2]);
89 public function testContributionWithoutSoftCredits() {
90 $contribution1 = $this->contributionCreate(['receive_date' => '2018-01-02', 'contact_id' => $this->individualCreate()]);
91 $contact2 = $this->callAPISuccess('Contact', 'create', [
92 'display_name' => 'superman',
93 'contact_type' => 'Individual',
95 $contribution2 = $this->contributionCreate([
96 'receive_date' => '2017-01-02',
97 'contact_id' => $this->individualCreate(),
98 'honor_contact_id' => $contact2['id'],
100 $queryObj = new CRM_Contact_BAO_Query([['contribution_or_softcredits', '=', 'only_contribs_unsoftcredited', 1, 0]], NULL, NULL, FALSE, FALSE, CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
);
101 $this->assertEquals(1, $queryObj->searchQuery(0, 0, NULL, TRUE));
102 $this->assertContains('contribution_search_scredit_combined.filter_id IS NULL', $queryObj->_where
[1]);
103 $queryObj = new CRM_Contact_BAO_Query([['contribution_or_softcredits', '=', 'only_scredits', 1, 0]], NULL, NULL, FALSE, FALSE, CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
);
104 $this->assertEquals(1, $queryObj->searchQuery(0, 0, NULL, TRUE));
105 $this->assertContains('contribution_search_scredit_combined.scredit_id IS NOT NULL', $queryObj->_where
[1]);
106 $queryObj = new CRM_Contact_BAO_Query([['contribution_or_softcredits', '=', 'both_related', 1, 0]], NULL, NULL, FALSE, FALSE, CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
);
107 $this->assertEquals(2, $queryObj->searchQuery(0, 0, NULL, TRUE));
108 $this->assertContains('contribution_search_scredit_combined.filter_id IS NOT NULL', $queryObj->_where
[1]);
109 $queryObj = new CRM_Contact_BAO_Query([['contribution_or_softcredits', '=', 'both', 1, 0]], NULL, NULL, FALSE, FALSE, CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
);
110 $this->assertEquals(3, $queryObj->searchQuery(0, 0, NULL, TRUE));
111 $this->assertEmpty($queryObj->_where
[0]);
112 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution1]);
113 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution2]);