Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
7d61e75f | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
7d61e75f TO |
7 | | This work is published under the GNU AGPLv3 license with some | |
8 | | permitted exceptions and without any warranty. For full license | | |
9 | | and copyright information, see https://civicrm.org/licensing | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
19 | ||
19b53e5b C |
20 | namespace api\v4\Action; |
21 | ||
3dd9e4a0 | 22 | use Civi\Api4\Activity; |
19b53e5b | 23 | use Civi\Api4\Contact; |
e062cce3 | 24 | use Civi\Api4\Contribution; |
19b53e5b C |
25 | use Civi\Api4\Relationship; |
26 | use api\v4\UnitTestCase; | |
27 | ||
28 | /** | |
29 | * @group headless | |
30 | */ | |
31 | class DateTest extends UnitTestCase { | |
32 | ||
33 | public function testRelationshipDate() { | |
34 | $c1 = Contact::create() | |
35 | ->addValue('first_name', 'c') | |
36 | ->addValue('last_name', 'one') | |
37 | ->execute() | |
38 | ->first()['id']; | |
39 | $c2 = Contact::create() | |
40 | ->addValue('first_name', 'c') | |
41 | ->addValue('last_name', 'two') | |
42 | ->execute() | |
43 | ->first()['id']; | |
44 | $r = Relationship::create() | |
45 | ->addValue('contact_id_a', $c1) | |
46 | ->addValue('contact_id_b', $c2) | |
47 | ->addValue('relationship_type_id', 1) | |
48 | ->addValue('start_date', 'now') | |
49 | ->addValue('end_date', 'now + 1 week') | |
50 | ->execute() | |
51 | ->first()['id']; | |
52 | $result = Relationship::get() | |
53 | ->addWhere('start_date', '=', 'now') | |
54 | ->addWhere('end_date', '>', 'now + 1 day') | |
55 | ->execute() | |
56 | ->indexBy('id'); | |
57 | $this->assertArrayHasKey($r, $result); | |
58 | $result = Relationship::get() | |
59 | ->addWhere('start_date', '<', 'now') | |
60 | ->execute() | |
61 | ->indexBy('id'); | |
62 | $this->assertArrayNotHasKey($r, $result); | |
63 | } | |
64 | ||
3dd9e4a0 CW |
65 | public function testRelativeDateRanges() { |
66 | $c1 = Contact::create() | |
67 | ->addValue('first_name', 'c') | |
68 | ->addValue('last_name', 'one') | |
69 | ->execute() | |
70 | ->first()['id']; | |
90e1fed1 | 71 | |
72 | // Avoid problems with `strtotime(<date arithmetic expression>)` giving | |
73 | // impossible dates like April 31 which roll over and then don't match. | |
35fed907 | 74 | $thisMonth = (int) date('m'); |
90e1fed1 | 75 | $lastMonth = ($thisMonth === 1 ? 12 : $thisMonth - 1); |
76 | $nextMonth = ($thisMonth === 12 ? 1 : $thisMonth + 1); | |
77 | $lastMonthsYear = ($thisMonth === 1 ? date('Y') - 1 : date('Y')); | |
78 | $nextMonthsYear = ($thisMonth === 12 ? date('Y') + 1 : date('Y')); | |
79 | ||
3dd9e4a0 CW |
80 | $act = Activity::save() |
81 | ->setDefaults(['activity_type_id:name' => 'Meeting', 'source_contact_id' => $c1]) | |
90e1fed1 | 82 | ->addRecord(['activity_date_time' => (date('Y') - 3) . '-' . date('m-01 H:i:s')]) |
83 | ->addRecord(['activity_date_time' => (date('Y') - 1) . '-' . date('m-01 H:i:s')]) | |
84 | ->addRecord(['activity_date_time' => "{$lastMonthsYear}-{$lastMonth}-01 " . date('H:i:s')]) | |
3dd9e4a0 | 85 | ->addRecord(['activity_date_time' => 'now']) |
90e1fed1 | 86 | ->addRecord(['activity_date_time' => "{$nextMonthsYear}-{$nextMonth}-01 " . date('H:i:s')]) |
87 | ->addRecord(['activity_date_time' => (date('Y') + 1) . '-' . date('m-01 H:i:s')]) | |
88 | ->addRecord(['activity_date_time' => (date('Y') + 3) . '-' . date('m-01 H:i:s')]) | |
3dd9e4a0 CW |
89 | ->execute()->column('id'); |
90 | ||
91 | $result = Activity::get(FALSE)->addSelect('id') | |
92 | ->addWhere('activity_date_time', '>', 'previous.year') | |
93 | ->execute()->column('id'); | |
94 | $this->assertNotContains($act[0], $result); | |
95 | $this->assertContains($act[3], $result); | |
96 | $this->assertContains($act[4], $result); | |
97 | $this->assertContains($act[5], $result); | |
98 | $this->assertContains($act[6], $result); | |
99 | ||
100 | $result = Activity::get(FALSE)->addSelect('id') | |
101 | ->addWhere('activity_date_time', '>', 'this.year') | |
102 | ->execute()->column('id'); | |
103 | $this->assertNotContains($act[0], $result); | |
104 | $this->assertNotContains($act[1], $result); | |
105 | $this->assertNotContains($act[2], $result); | |
106 | $this->assertNotContains($act[3], $result); | |
107 | $this->assertContains($act[5], $result); | |
108 | $this->assertContains($act[6], $result); | |
109 | ||
110 | $result = Activity::get(FALSE)->addSelect('id') | |
111 | ->addWhere('activity_date_time', '>=', 'this.year') | |
112 | ->execute()->column('id'); | |
113 | $this->assertNotContains($act[0], $result); | |
114 | $this->assertNotContains($act[1], $result); | |
115 | $this->assertContains($act[3], $result); | |
116 | $this->assertContains($act[4], $result); | |
117 | $this->assertContains($act[5], $result); | |
118 | $this->assertContains($act[6], $result); | |
119 | ||
120 | $result = Activity::get(FALSE)->addSelect('id') | |
121 | ->addWhere('activity_date_time', '<', 'previous.year') | |
122 | ->execute()->column('id'); | |
123 | $this->assertContains($act[0], $result); | |
124 | $this->assertNotContains($act[4], $result); | |
125 | $this->assertNotContains($act[5], $result); | |
126 | $this->assertNotContains($act[6], $result); | |
127 | ||
128 | $result = Activity::get(FALSE)->addSelect('id') | |
129 | ->addWhere('activity_date_time', '=', 'next.month') | |
130 | ->execute()->column('id'); | |
131 | $this->assertNotContains($act[0], $result); | |
132 | $this->assertNotContains($act[1], $result); | |
133 | $this->assertNotContains($act[2], $result); | |
134 | $this->assertContains($act[4], $result); | |
135 | $this->assertNotContains($act[5], $result); | |
136 | $this->assertNotContains($act[6], $result); | |
137 | ||
138 | $result = Activity::get(FALSE)->addSelect('id') | |
139 | ->addWhere('activity_date_time', 'BETWEEN', ['previous.year', 'this.year']) | |
140 | ->execute()->column('id'); | |
141 | $this->assertContains($act[2], $result); | |
142 | $this->assertContains($act[3], $result); | |
143 | $this->assertNotContains($act[0], $result); | |
144 | $this->assertNotContains($act[6], $result); | |
145 | } | |
146 | ||
e062cce3 CW |
147 | public function testJoinOnRelativeDate() { |
148 | $c1 = Contact::create(FALSE) | |
149 | ->addValue('first_name', 'Contributor') | |
150 | ->addValue('last_name', 'One') | |
151 | ->execute() | |
152 | ->first()['id']; | |
153 | ||
154 | // Contribution from last year | |
155 | Contribution::create(FALSE) | |
156 | ->addValue('contact_id', $c1) | |
157 | ->addValue('receive_date', (date('Y') - 1) . '-06-01') | |
158 | ->addValue('financial_type_id', 1) | |
159 | ->addValue('total_amount', 12) | |
160 | ->execute(); | |
161 | ||
162 | // Contribution from this year | |
163 | Contribution::create(FALSE) | |
164 | ->addValue('contact_id', $c1) | |
165 | ->addValue('receive_date', date('Y') . '-06-01') | |
166 | ->addValue('financial_type_id', 1) | |
167 | ->addValue('total_amount', 6) | |
168 | ->execute(); | |
169 | ||
170 | // Contribution from 2 years ago | |
171 | Contribution::create(FALSE) | |
172 | ->addValue('contact_id', $c1) | |
173 | ->addValue('receive_date', (date('Y') - 2) . '-06-01') | |
174 | ->addValue('financial_type_id', 1) | |
175 | ->addValue('total_amount', 24) | |
176 | ->execute(); | |
177 | ||
178 | // Find contribution from last year | |
179 | $contact = \Civi\Api4\Contact::get() | |
180 | ->addSelect('id', 'contribution.total_amount') | |
181 | ->setJoin([ | |
182 | ['Contribution AS contribution', FALSE, NULL, ['contribution.receive_date', '=', '"previous.year"']], | |
183 | ]) | |
184 | ->addWhere('id', '=', $c1) | |
185 | ->execute(); | |
186 | $this->assertCount(1, $contact); | |
187 | $this->assertEquals(12, $contact[0]['contribution.total_amount']); | |
188 | ||
189 | // Find contributions not from last year | |
190 | $contact = \Civi\Api4\Contact::get() | |
191 | ->addSelect('id', 'contribution.total_amount') | |
192 | ->setJoin([ | |
193 | ['Contribution AS contribution', FALSE, NULL, ['contribution.receive_date', '!=', '"previous.year"']], | |
194 | ]) | |
195 | ->addWhere('id', '=', $c1) | |
196 | ->execute(); | |
197 | $this->assertCount(2, $contact); | |
198 | } | |
199 | ||
19b53e5b | 200 | } |