If a template contribution is updated we need to update the amount on the recurring...
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / QueryTest.php
1 <?php
2
3 /**
4 * Include dataProvider for tests
5 *
6 * @group headless
7 */
8 class CRM_Contribute_BAO_QueryTest extends CiviUnitTestCase {
9
10 public function tearDown(): void {
11 $this->quickCleanUpFinancialEntities();
12 parent::tearDown();
13 }
14
15 /**
16 * Check that we get a successful trying to return by pseudo-fields
17 * - financial_type.
18 *
19 * @param string $sort
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.
24 *
25 * @throws \CRM_Core_Exception
26 *
27 * @dataProvider getSortFields
28 */
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');
34
35 $params = [
36 ['financial_type_id', '=', $donationTypeID , 1, 0],
37 ];
38
39 $queryObj = new CRM_Contact_BAO_Query($params);
40 $sql = $queryObj->getSearchSQL(0, 0, $sort . ' asc');
41 if ($isUseKeySort) {
42 $this->assertStringContainsString('field(', $sql);
43 }
44 try {
45 $resultDAO = CRM_Core_DAO::executeQuery($sql);
46 $this->assertTrue($resultDAO->fetch());
47 $this->assertEquals(1, $resultDAO->N);
48 }
49 catch (PEAR_Exception $e) {
50 $err = $e->getCause();
51 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
52
53 }
54 }
55
56 /**
57 * Data provider for sort fields
58 */
59 public function getSortFields() {
60 return [
61 ['financial_type', TRUE],
62 ['payment_instrument', TRUE],
63 ['individual_prefix', TRUE],
64 ['communication_style', TRUE],
65 ['gender', TRUE],
66 ['state_province', FALSE],
67 ['country', FALSE],
68 ];
69 }
70
71 /**
72 * Test receive_date_high, low & relative work.
73 *
74 * @throws \CRM_Core_Exception
75 */
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]);
87 }
88
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',
94 ]);
95 $contribution2 = $this->contributionCreate([
96 'receive_date' => '2017-01-02',
97 'contact_id' => $this->individualCreate(),
98 'honor_contact_id' => $contact2['id'],
99 ]);
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]);
114 }
115
116 }