Repository navigation
Expand file tree
/
Copy pathRecommendationEngineTest.php
More file actions
240 lines (188 loc) · 7.61 KB
/
Copy pathRecommendationEngineTest.php
File metadata and controls
240 lines (188 loc) · 7.61 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Tests\unit\ProcessMaker;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Queue;
use ProcessMaker\Events\ActivityAssigned;
use ProcessMaker\Jobs\GenerateUserRecommendations;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\Recommendation;
use ProcessMaker\Models\RecommendationUser;
use ProcessMaker\Models\User;
use ProcessMaker\RecommendationEngine;
use ProcessMaker\SyncRecommendations;
use Tests\TestCase;
class RecommendationEngineTest extends TestCase
{
private static array $generatedModelUuids;
private SyncRecommendations $syncRecommendations;
public function setUp(): void
{
parent::setUp();
$this->syncRecommendations = app(SyncRecommendations::class);
}
public function testRecommendationMatchesAndAccuracy(): void
{
$user = User::factory()->create([
'status' => 'ACTIVE',
]);
$processRequest = ProcessRequest::factory()->create([
'data' => ['price' => random_int(512, 2048)],
]);
$recommendation = Recommendation::factory()->create([
'advanced_filter' => [
[
'subject' => ['type' => 'Field', 'value' => 'process_id'],
'operator' => '=',
'value' => $processRequest->process_id,
],
[
'subject' => ['type' => 'Field', 'value' => 'data.price'],
'operator' => '>',
'value' => random_int(0, 511),
],
],
]);
$processRequestTokens = ProcessRequestToken::factory(3)->create([
'user_id' => $user->id,
'process_request_id' => $processRequest->id,
'process_id' => $processRequest->process_id,
'status' => 'active',
]);
event(new ActivityAssigned($processRequestTokens->first()));
$this->assertEquals(1, RecommendationUser::count());
$recommendationUser = RecommendationUser::first();
$this->assertTrue($recommendation->is($recommendationUser->recommendation));
$this->assertTrue($user->is($recommendationUser->user));
$this->assertEquals(1, $recommendation->recommendationUsers()->count());
$recommendationUser->dismissed_until = Carbon::parse($recommendationUser->dismissed_until)
->subtract('year', 1);
$recommendationUser->save();
$this->assertTrue($recommendationUser->isExpired());
$recommendationUser->dismiss();
$this->assertFalse($recommendationUser->isExpired());
}
public function testUserHasDisabledRecommendations(): void
{
Queue::fake([
GenerateUserRecommendations::class,
]);
$user = User::factory()->create([
'status' => 'ACTIVE',
'meta' => [
'disableRecommendations' => true,
],
]);
$processRequestToken = ProcessRequestToken::factory()->create([
'user_id' => $user->id,
]);
event(new ActivityAssigned($processRequestToken));
Queue::assertNotPushed(GenerateUserRecommendations::class);
}
public function testSystemHasDisabledRecommendations(): void
{
config(['app.recommendations_enabled' => false]);
Queue::fake([
GenerateUserRecommendations::class,
]);
$user = User::factory()->create(['status' => 'ACTIVE']);
$processRequestToken = ProcessRequestToken::factory()->create([
'user_id' => $user->id,
]);
event(new ActivityAssigned($processRequestToken));
Queue::assertNotPushed(GenerateUserRecommendations::class);
}
public function testRecommendationsSync(): void
{
Http::preventStrayRequests();
// Set up the fake responses for the
Http::fake([
// Index file contains all directories/filenames in the repo
'https://api.github.com/repos/processmaker/pm4-recommendations/contents*' => Http::response([
[
'name' => 'default',
'type' => 'dir',
'url' => 'https://repo.test/default',
],
[
'name' => 'localhost',
'type' => 'dir',
'url' => 'https://repo.test/localhost',
],
]),
// Default directory
'https://repo.test/default' => Http::response([
[
'download_url' => 'https://repo.test/default/global_test.json',
],
]),
// Matches with the instance domain
// the unit tests are running on
'https://repo.test/localhost' => Http::response([
[
'download_url' => 'https://repo.test/localhost/instance_test.json',
],
]),
'https://repo.test/default/global_test.json' => Http::response($this->generateModelData()),
'https://repo.test/localhost/instance_test.json' => Http::response($this->generateModelData()),
]);
// Run the sync
$this->syncRecommendations->sync();
// Build a query to count the recommendation
// persisted with the uuids we generated
$persistedRecommendationQuery = Recommendation::query()->whereIn('uuid', static::$generatedModelUuids);
// Compare the number of recommendations we
// persisted from the generated model data
$this->assertCount($persistedRecommendationQuery->count(), static::$generatedModelUuids);
}
/**
* Use a factory to generate a Recommendation model's data
* without persisting it to use as mock JSON responses
*/
protected function generateModelData(): array
{
// Generate the mock data
$model_data = Recommendation::factory()->make([
'advanced_filter' => '[{"subject":{"type":"Field","value":"process_request_id"},"operator":"!=","value":1}]',
]);
// Save the generated uuid to check if the recommendation
// was persisted during the test
static::$generatedModelUuids[] = $model_data->uuid;
// Return an anonymous object, which will represent
// the json body of the mock http response
return $model_data->toArray();
}
public function testHandleUserSettingChanges(): void
{
Queue::fake([
GenerateUserRecommendations::class,
]);
$user = User::factory()->create([
'meta' => (object) [
'disableRecommendations' => true,
],
]);
$original = [
'meta' => null,
];
RecommendationUser::factory()->create([
'user_id' => $user->id,
]);
$this->assertCount(1, RecommendationUser::all());
RecommendationEngine::handleUserSettingChanges($user, $original);
// User disabled so all recommendations should be deleted
$this->assertCount(0, RecommendationUser::all());
Queue::assertNotPushed(GenerateUserRecommendations::class);
$user->meta = null;
$user->save();
$original['meta'] = (object) [
'disableRecommendations' => true,
];
RecommendationEngine::handleUserSettingChanges($user, $original);
// User enabled so recommendations should be generated
Queue::assertPushed(GenerateUserRecommendations::class);
}
}