4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Action
;
22 use Civi\Api4\Activity
;
23 use Civi\Api4\Contact
;
24 use Civi\Api4\Contribution
;
25 use Civi\Api4\Relationship
;
26 use api\v
4\UnitTestCase
;
31 class DateTest
extends UnitTestCase
{
33 public function testRelationshipDate() {
34 $c1 = Contact
::create()
35 ->addValue('first_name', 'c')
36 ->addValue('last_name', 'one')
39 $c2 = Contact
::create()
40 ->addValue('first_name', 'c')
41 ->addValue('last_name', 'two')
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')
52 $result = Relationship
::get()
53 ->addWhere('start_date', '=', 'now')
54 ->addWhere('end_date', '>', 'now + 1 day')
57 $this->assertArrayHasKey($r, $result);
58 $result = Relationship
::get()
59 ->addWhere('start_date', '<', 'now')
62 $this->assertArrayNotHasKey($r, $result);
65 public function testRelativeDateRanges() {
66 $c1 = Contact
::create()
67 ->addValue('first_name', 'c')
68 ->addValue('last_name', 'one')
72 // Avoid problems with `strtotime(<date arithmetic expression>)` giving
73 // impossible dates like April 31 which roll over and then don't match.
74 $thisMonth = (int) date('m');
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'));
80 $act = Activity
::save()
81 ->setDefaults(['activity_type_id:name' => 'Meeting', 'source_contact_id' => $c1])
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')])
85 ->addRecord(['activity_date_time' => 'now'])
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')])
89 ->execute()->column('id');
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);
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);
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);
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);
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);
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);
147 public function testJoinOnRelativeDate() {
148 $c1 = Contact
::create(FALSE)
149 ->addValue('first_name', 'Contributor')
150 ->addValue('last_name', 'One')
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)
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)
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)
178 // Find contribution from last year
179 $contact = \Civi\Api4\Contact
::get()
180 ->addSelect('id', 'contribution.total_amount')
182 ['Contribution AS contribution', FALSE, NULL, ['contribution.receive_date', '=', '"previous.year"']],
184 ->addWhere('id', '=', $c1)
186 $this->assertCount(1, $contact);
187 $this->assertEquals(12, $contact[0]['contribution.total_amount']);
189 // Find contributions not from last year
190 $contact = \Civi\Api4\Contact
::get()
191 ->addSelect('id', 'contribution.total_amount')
193 ['Contribution AS contribution', FALSE, NULL, ['contribution.receive_date', '!=', '"previous.year"']],
195 ->addWhere('id', '=', $c1)
197 $this->assertCount(2, $contact);