forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedPMQLTest.php
More file actions
171 lines (138 loc) · 5.8 KB
/
ExtendedPMQLTest.php
File metadata and controls
171 lines (138 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Tests\Feature;
use Carbon\Carbon;
use Faker\Factory;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
class ExtendedPMQLTest extends TestCase
{
use RequestHelper;
public function testHandleFieldAlias()
{
// Instantiate Faker
$faker = Factory::create();
// Set our fake date
$date = $faker->dateTime();
// Create a process request with our fake created_at date
$processRequest = ProcessRequest::factory()->create([
'created_at' => $date,
]);
// Construct & run a PMQL query using the "created" field alias
$query = 'created = "' . $date->format('Y-m-d H:i:s') . '"';
$pmqlResult = ProcessRequest::pmql($query)->first();
// Assert that the models match
$this->assertTrue($processRequest->is($pmqlResult));
}
public function testHandleValueAlias()
{
// Create a process request
$processRequest = ProcessRequest::factory()->create([
'status' => 'ACTIVE',
]);
// Construct & run a PMQL query using the "status" value alias
$query = 'status = "In Progress"';
$pmqlResult = ProcessRequest::pmql($query)->get();
// Assert that the model is returned in our search
$ids = $pmqlResult->pluck('id');
$this->assertContains($processRequest->id, $ids);
}
public function testHandleFieldWildcard()
{
$this->markTestSkipped('PMQL does not yet support JSON fields on Microsoft SQL Server.');
// Instantiate Faker
$faker = Factory::create();
// Generate fake data
$data = [
'first_name' => $faker->firstName(),
'last_name' => $faker->lastName(),
];
// Create a process request
$processRequest = ProcessRequest::factory()->create([
'data' => $data,
]);
// Create a process request token tied to the above request
$processRequestToken = ProcessRequestToken::factory()->create([
'process_request_id' => $processRequest->id,
]);
// Construct & run a PMQL query using the "data" field wildcard
$query = "data.first_name = \"{$data['first_name']}\" AND data.last_name = \"{$data['last_name']}\"";
$pmqlResult = ProcessRequestToken::pmql($query)->first();
// Assert that the models match
$this->assertTrue($processRequestToken->is($pmqlResult));
}
public function testInUsersTimezone()
{
// Ensure the mysql server timezone is set to UTC
$this->assertContains(\DB::select('select @@time_zone as tz')[0]->tz, ['+00:00', 'UTC']);
// Ensure the app timezone is set to UTC
config(['app.timezone' => 'UTC']);
$this->user->timezone = 'America/Los_Angeles';
$this->user->save();
$processRequest1 = ProcessRequest::factory()->create([
'completed_at' => '2021-10-05 16:00:00', // UTC
]);
$processRequest2 = ProcessRequest::factory()->create([
'completed_at' => '2021-10-05 18:00:00', // UTC
]);
$url = route('api.requests.index', ['pmql' => 'completed_at > "2021-10-05 10:00:00"']); // America/Los_Angeles
$result = $this->apiCall('GET', $url);
$this->assertCount(1, $result->json()['data']); // Match only the one created at 11am Los Angeles Time (18:00/6pm UTC)
$this->assertEquals($processRequest2->id, $result->json()['data'][0]['id']);
}
public function testRelativeDate()
{
$processRequest1 = ProcessRequest::factory()->create([
'data' => ['date' => Carbon::parse('-10 minutes')->toDateTimeString()],
]);
$processRequest2 = ProcessRequest::factory()->create([
'data' => ['date' => Carbon::parse('-2 hours')->toDateTimeString()],
]);
$url = route('api.requests.index', ['pmql' => 'data.date > now -1 hour']);
$result = $this->apiCall('GET', $url);
$this->assertCount(1, $result->json()['data']); // Match only the one that completed 10 minutes ago
$this->assertEquals($processRequest1->id, $result->json()['data'][0]['id']);
}
public function testCharComparison()
{
$processRequest1 = ProcessRequest::factory()->create([
'data' => ['gender' => 'F'],
]);
$processRequest2 = ProcessRequest::factory()->create([
'data' => ['gender' => 'M'],
]);
$url = route('api.requests.index', ['pmql' => 'data.gender = "F"']);
$result = $this->apiCall('GET', $url);
$this->assertCount(1, $result->json()['data']); // Match only F
$this->assertEquals($processRequest1->id, $result->json()['data'][0]['id']);
}
public function testFilterUsernameWithNumbers()
{
$user = User::factory()->create([
'username' => 'W0584',
]);
$processRequest = ProcessRequest::factory()->create([
'user_id' => $user->id,
]);
$url = route('api.requests.index', ['pmql' => 'requester = "W0584"']);
$result = $this->apiCall('GET', $url);
$requesterId = $result->json()['data'][0]['user_id'];
$this->assertEquals($requesterId, $user->id);
}
public function testLowerFunction()
{
ProcessRequest::factory()->create([
'data' => ['YQP_CLIENT_NAME' => 'Teresa Roldan HC'],
]);
$pmqlSearch = 'lower(data.YQP_CLIENT_NAME) LIKE "%teresa roldan hc%"';
$route = route('api.requests.index', [
'include' => 'data',
'pmql' => $pmqlSearch,
]);
$response = $this->apiCall('GET', $route);
$response->assertStatus(200);
$this->assertCount(1, $response->json()['data']);
}
}