a0090e6b |
1 | <?php |
2 | |
3 | /** |
4 | * Include dataProvider for tests |
5b5ea9b6 |
5 | * |
a0090e6b |
6 | * @group headless |
7 | */ |
8 | class CRM_Contribute_BAO_QueryTest extends CiviUnitTestCase { |
39b959db |
9 | |
a0090e6b |
10 | public function tearDown() { |
11 | $this->quickCleanUpFinancialEntities(); |
5b5ea9b6 |
12 | parent::tearDown(); |
a0090e6b |
13 | } |
14 | |
15 | /** |
16 | * Check that we get a successful trying to return by pseudo-fields |
17 | * - financial_type. |
18 | * |
19 | * @param string $sort |
124f3c1b |
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. |
a0090e6b |
24 | * |
5b5ea9b6 |
25 | * @throws \CRM_Core_Exception |
26 | * |
a0090e6b |
27 | * @dataProvider getSortFields |
28 | */ |
124f3c1b |
29 | public function testSearchPseudoReturnProperties($sort, $isUseKeySort) { |
a0090e6b |
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); |
124f3c1b |
40 | $sql = $queryObj->getSearchSQL(0, 0, $sort . ' asc'); |
41 | if ($isUseKeySort) { |
42 | $this->assertContains('field(', $sql); |
43 | } |
a0090e6b |
44 | try { |
124f3c1b |
45 | $resultDAO = CRM_Core_DAO::executeQuery($sql); |
a0090e6b |
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 [ |
124f3c1b |
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], |
a0090e6b |
68 | ]; |
69 | } |
70 | |
5b5ea9b6 |
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 | |
a0090e6b |
87 | } |