Swap out some additional assigns for tokens, improve tests
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / QueryTest.php
1 <?php
2
3 /**
4 * @group headless
5 */
6 class CRM_Event_BAO_QueryTest extends CiviUnitTestCase {
7
8 public function setUp(): void {
9 parent::setUp();
10 }
11
12 /**
13 * Test searching for participant note.
14 *
15 * @throws \CRM_Core_Exception
16 */
17 public function testParticipantNote() {
18 $event = $this->eventCreate();
19 $this->individualCreate([
20 'api.participant.create' => [
21 'event_id' => $event['id'],
22 'note' => 'some_note',
23 ],
24 ]);
25 $this->individualCreate([
26 'api.participant.create' => [
27 'event_id' => $event['id'],
28 'note' => 'some_other_note',
29 ],
30 ]);
31 $params = [
32 [
33 0 => 'participant_note',
34 1 => '=',
35 2 => 'some_note',
36 3 => 1,
37 4 => 0,
38 ],
39 ];
40
41 $query = new CRM_Contact_BAO_Query($params, NULL, NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS);
42 $sql = $query->query(FALSE);
43 $result = CRM_Core_DAO::executeQuery(implode(' ', $sql));
44 $this->assertEquals(1, $result->N);
45 }
46
47 /**
48 * Unit test to check if participant search retrieves correct event type id.
49 *
50 * @throws \CRM_Core_Exception
51 */
52 public function testEventType() {
53 $event = $this->eventCreate();
54 $contactId = $this->individualCreate([
55 'api.participant.create' => [
56 'event_id' => $event['id'],
57 ],
58 ]);
59 $params = [
60 [
61 0 => 'event_id',
62 1 => '=',
63 2 => $event['id'],
64 3 => 1,
65 4 => 0,
66 ],
67 ];
68
69 $returnProperties = [
70 'event_type_id' => 1,
71 'contact_id' => 1,
72 'event_id' => 1,
73 ];
74
75 $query = new CRM_Contact_BAO_Query(
76 $params, $returnProperties, NULL,
77 FALSE, FALSE, CRM_Contact_BAO_Query::MODE_EVENT
78 );
79 $sql = $query->query(FALSE);
80 $result = CRM_Core_DAO::executeQuery(implode(' ', $sql));
81
82 $this->assertEquals(1, $result->N);
83 $result->fetch();
84
85 $this->assertEquals($contactId, $result->contact_id);
86 $this->assertEquals($event['id'], $result->event_id);
87 $eventTypeId = $this->callAPISuccessGetValue('Event', [
88 'id' => $event['id'],
89 'return' => 'event_type_id',
90 ]);
91 $this->assertEquals($eventTypeId, $result->event_type_id);
92 }
93
94 /**
95 * Test provided event search parameters.
96 *
97 * @dataProvider getEventSearchParameters
98 *
99 * @throws \CRM_Core_Exception
100 */
101 public function testParameters($parameters, $expected) {
102 $query = new CRM_Contact_BAO_Query(
103 $parameters, NULL, NULL,
104 FALSE, FALSE, CRM_Contact_BAO_Query::MODE_EVENT
105 );
106 $query->query(FALSE);
107 $this->assertEquals($expected['where'], trim($query->_whereClause));
108 $this->assertEquals($expected['qill'], trim($query->_qill[0][0]));
109 }
110
111 /**
112 * @return array
113 */
114 public function getEventSearchParameters() {
115 return [
116 [
117 [['participant_status_id', '=', 1, 0, 0]],
118 [
119 'where' => '( civicrm_participant.status_id = 1 )',
120 'qill' => 'Status ID = Registered',
121 ],
122 ],
123 [
124 [['participant_status_id', 'IN', [1, 2], 0, 0]],
125 [
126 'where' => '( civicrm_participant.status_id IN ("1", "2") )',
127 'qill' => 'Status ID In Registered, Attended',
128 ],
129 ],
130 ];
131 }
132
133 }