Merge pull request #14922 from civicrm/5.16
[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() {
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->assertContains('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 $this->contributionCreate(['receive_date' => '2018-01-02', 'contact_id' => $this->individualCreate()]);
78 $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 }
86
87 }