138195031fa9875078b64197006271bb6f4ed9e0
[civicrm-core.git] / tests / phpunit / CRM / Activity / Form / SearchTest.php
1 <?php
2
3 /**
4 * Include dataProvider for tests
5 * @group headless
6 */
7 class CRM_Activity_Form_SearchTest extends CiviUnitTestCase {
8
9 public function setUp(): void {
10 parent::setUp();
11 $this->individualID = $this->individualCreate();
12 $this->contributionCreate([
13 'contact_id' => $this->individualID,
14 'receive_date' => '2017-01-30',
15 ]);
16 }
17
18 public function tearDown(): void {
19 $tablesToTruncate = [
20 'civicrm_activity',
21 'civicrm_activity_contact',
22 ];
23 $this->quickCleanup($tablesToTruncate);
24 parent::tearDown();
25 }
26
27 /**
28 * Test submitted the search form.
29 *
30 * @throws \CRM_Core_Exception
31 * @throws \CiviCRM_API3_Exception
32 */
33 public function testSearch(): void {
34
35 $form = new CRM_Activity_Form_Search();
36 $_SERVER['REQUEST_METHOD'] = 'GET';
37 $form->controller = new CRM_Activity_Controller_Search();
38 $form->preProcess();
39 $form->postProcess();
40 $qfKey = $form->controller->_key;
41 $rows = $form->controller->get('rows');
42 $this->assertEquals([
43 [
44 'contact_id' => '3',
45 'contact_type' => '<a href="/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=3" data-tooltip-url="/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4&amp;is_show_email_task=1" class="crm-summary-link"><div class="icon crm-icon Individual-icon"></div></a>',
46 'sort_name' => 'Anderson, Anthony',
47 'display_name' => 'Mr. Anthony Anderson II',
48 'activity_id' => '1',
49 'activity_date_time' => '2017-01-30 00:00:00',
50 'activity_status_id' => '2',
51 'activity_status' => 'Completed',
52 'activity_subject' => '$ 100.00 - SSF',
53 'source_record_id' => '1',
54 'activity_type_id' => '6',
55 'activity_type' => 'Contribution',
56 'activity_is_test' => '0',
57 'target_contact_name' => [],
58 'assignee_contact_name' => [],
59 'source_contact_id' => '3',
60 'source_contact_name' => 'Anderson, Anthony',
61 'checkbox' => 'mark_x_1',
62 'mailingId' => '',
63 'action' => '<span><a href="/index.php?q=civicrm/contact/view/contribution&amp;action=view&amp;reset=1&amp;id=1&amp;cid=3&amp;context=search&amp;searchContext=activity&amp;key=' . $qfKey . '" class="action-item crm-hover-button" title=\'View Activity\' >View</a></span>',
64 'campaign' => NULL,
65 'campaign_id' => NULL,
66 'repeat' => '',
67 'contact_sub_type' => NULL,
68 'activity_campaign_id' => NULL,
69 'activity_engagement_level' => NULL,
70 'recipients' => '',
71 ],
72 ], $rows);
73 }
74
75 /**
76 * Test the Qill for activity Date time.
77 */
78 public function testQill() {
79 foreach ($this->getSearchCriteria() as $test_name => $data) {
80 $selector = new CRM_Activity_Selector_Search($data['search_criteria']);
81 $this->assertEquals($data['expected_qill'], $selector->getQILL(), "Failed for data set: $test_name");
82 }
83 }
84
85 /**
86 * Get criteria for activity testing.
87 */
88 public function getSearchCriteria() {
89 $format = \Civi::settings()->get('dateformatDatetime');
90 $dates['ending_60.day'] = CRM_Utils_Date::getFromTo('ending_60.day', NULL, NULL);
91 $dates['earlier.year'] = CRM_Utils_Date::getFromTo('earlier.year', NULL, NULL);
92 $dates['greater.year'] = CRM_Utils_Date::getFromTo('greater.year', NULL, NULL);
93 return [
94 'last 60' => [
95 'search_criteria' => [
96 ['activity_date_time_relative', '=', 'ending_60.day', 0, 0],
97 ],
98 'expected_qill' => [['Activity Date is Last 60 days including today (between ' . CRM_Utils_Date::customFormat($dates['ending_60.day'][0], $format) . ' and ' . CRM_Utils_Date::customFormat($dates['ending_60.day'][1], $format) . ')']],
99 ],
100 'end of previous year' => [
101 'search_criteria' => [
102 ['activity_date_time_relative', '=', 'earlier.year', 0, 0],
103 ],
104 'expected_qill' => [['Activity Date is To end of previous calendar year (to ' . CRM_Utils_Date::customFormat($dates['earlier.year'][1], $format) . ')']],
105 ],
106 'start of current year' => [
107 'search_criteria' => [
108 ['activity_date_time_relative', '=', 'greater.year', 0, 0],
109 ],
110 'expected_qill' => [['Activity Date is From start of current calendar year (from ' . CRM_Utils_Date::customFormat($dates['greater.year'][0], $format) . ')']],
111 ],
112 'between' => [
113 'search_criteria' => [
114 ['activity_date_time_low', '=', '2019-03-05', 0, 0],
115 ['activity_date_time_high', '=', '2019-03-27', 0, 0],
116 ],
117 'expected_qill' => [['Activity Date - greater than or equal to "March 5th, 2019 12:00 AM" AND less than or equal to "March 27th, 2019 11:59 PM"']],
118 ],
119 'status is one of' => [
120 'search_criteria' => [
121 ['activity_status_id', '=', ['IN' => ['1', '2']], 0, 0],
122 ],
123 'expected_qill' => [['Activity Status In Scheduled, Completed']],
124 ],
125 ];
126 }
127
128 /**
129 * This just checks there's no errors. It doesn't perform any tasks.
130 * It's a little bit like choosing an action from the dropdown.
131 * @dataProvider taskControllerProvider
132 * @param int $task
133 */
134 public function testTaskController(int $task) {
135 // It gets the task from the POST var
136 $oldtask = $_POST['task'] ?? NULL;
137 $_POST['task'] = $task;
138
139 // yes it's the string 'null'
140 new CRM_Activity_Controller_Search('Find Activities', TRUE, 'null');
141
142 // clean up
143 if (is_null($oldtask)) {
144 unset($_POST['task']);
145 }
146 else {
147 $_POST['task'] = $oldtask;
148 }
149 }
150
151 /**
152 * dataprovider for testTaskController
153 * @return array
154 */
155 public function taskControllerProvider(): array {
156 return [
157 [CRM_Activity_Task::TASK_DELETE],
158 [CRM_Activity_Task::TASK_PRINT],
159 [CRM_Activity_Task::TASK_EXPORT],
160 [CRM_Activity_Task::BATCH_UPDATE],
161 [CRM_Activity_Task::TASK_EMAIL],
162 [CRM_Activity_Task::PDF_LETTER],
163 [CRM_Activity_Task::TASK_SMS],
164 [CRM_Activity_Task::TAG_ADD],
165 [CRM_Activity_Task::TAG_REMOVE],
166 ];
167 }
168
169 }