Skip to content

Commit a388d78

Browse files
committed
Added unit test, factories, and updates to models
1 parent b42404e commit a388d78

6 files changed

Lines changed: 114 additions & 15 deletions

File tree

‎ProcessMaker/Models/Recommendation.php‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
namespace ProcessMaker\Models;
44

5-
use ProcessMaker\Traits\HasUuids;
65
use Illuminate\Database\Eloquent\Builder;
76

87
class Recommendation extends ProcessMakerModel
98
{
10-
use HasUuids;
11-
129
protected $connection = 'processmaker';
1310

1411
protected $fillable = [
@@ -20,7 +17,7 @@ class Recommendation extends ProcessMakerModel
2017
'min_matches' => 'integer',
2118
'dismiss_for_secs' => 'integer',
2219
'actions' => 'array',
23-
'advanced_filter' => 'json',
20+
'advanced_filter' => 'array',
2421
];
2522

2623
protected $attributes = [

‎ProcessMaker/Models/RecommendationUser.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class RecommendationUser extends ProcessMakerModel
88
{
99
protected $connection = 'processmaker';
1010

11+
protected $guarded = [];
12+
1113
protected $casts = [
1214
'dismissed_until' => 'datetime',
1315
'count' => 'integer',

‎ProcessMaker/RecommendationEngine.php‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public function generate(): void
5757
// the recommendations advanced filter
5858
Filter::filter($query, $recommendation->advanced_filter);
5959

60-
dump($query->count());
61-
6260
// Set up the RecommendationUser query
6361
$recommendationUsersQuery = $recommendation->recommendationUsers()->where('user_id', '=', $this->user->id);
6462

@@ -67,7 +65,8 @@ public function generate(): void
6765

6866
// Check if there are enough results to satisfy the
6967
// minimum matches required by the recommendation
70-
$minimumMatchesMet = ($count = $query->count()) < $recommendation->min_matches;
68+
$count = $query->count();
69+
$minimumMatchesMet = $count >= $recommendation->min_matches;
7170

7271
// If we find the RecommendationUser records, we need
7372
// to make sure they're up-to-date
@@ -86,15 +85,11 @@ public function generate(): void
8685

8786
// Persist the updates
8887
$recommendationUser->save();
89-
90-
dump(['RecommendationUserUpdated' => $recommendationUser]);
9188
} else {
9289
// Otherwise, if the minimum matches are not met, we
9390
// need to delete the existing RecommendationUser
9491
// rows since they are now invalid
9592
$recommendationUsersQuery->delete();
96-
97-
dump(['RecommendationUserDeleted' => $recommendationUser]);
9893
}
9994
}
10095
} else if ($minimumMatchesMet) {
@@ -107,10 +102,6 @@ public function generate(): void
107102
]);
108103

109104
$recommendationUser->save();
110-
111-
dump(['RecommendationUserCreated' => $recommendationUser]);
112-
} else {
113-
dump(['NoRecommendationsForUser' => false]);
114105
}
115106
}
116107
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Database\Factories\ProcessMaker\Models;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
/**
9+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\ProcessMaker\Models\Recommendation>
10+
*/
11+
class RecommendationFactory extends Factory
12+
{
13+
/**
14+
* Define the model's default state.
15+
*
16+
* @return array<string, mixed>
17+
*/
18+
public function definition(): array
19+
{
20+
return [
21+
'uuid' => (string) Str::orderedUuid(),
22+
'status' => 'ACTIVE',
23+
'name' => $this->faker->name(),
24+
'description' => $this->faker->text(),
25+
'min_matches' => 3,
26+
'dismiss_for_secs' => 60 * 60 * 24 * 7,
27+
'advanced_filter' => [],
28+
'actions' => [],
29+
];
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Database\Factories\ProcessMaker\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\ProcessMaker\Models\RecommendationUser>
9+
*/
10+
class RecommendationUserFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition(): array
18+
{
19+
return [];
20+
}
21+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Tests\unit\ProcessMaker;
4+
5+
use Tests\TestCase;
6+
use ProcessMaker\Models\User;
7+
use ProcessMaker\Models\Recommendation;
8+
use ProcessMaker\Models\ProcessRequest;
9+
use ProcessMaker\Events\ActivityAssigned;
10+
use ProcessMaker\Models\RecommendationUser;
11+
use ProcessMaker\Models\ProcessRequestToken;
12+
13+
class RecommendationEngineTest extends TestCase
14+
{
15+
public function testMatchesARecommendation()
16+
{
17+
$user = User::factory()->create([
18+
'status' => 'ACTIVE'
19+
]);
20+
21+
$processRequest = ProcessRequest::factory()->create([
22+
'data' => ['price' => random_int(512, 2048)]
23+
]);
24+
25+
$recommendation = Recommendation::factory()->create([
26+
'advanced_filter' => [
27+
[
28+
'subject' => ['type' => 'Field', 'value' => 'process_id'],
29+
'operator' => '=',
30+
'value' => $processRequest->process_id,
31+
],
32+
[
33+
'subject' => ['type' => 'Field', 'value' => 'data.price'],
34+
'operator' => '>',
35+
'value' => random_int(0, 511),
36+
]
37+
]
38+
]);
39+
40+
$processRequestTokens = ProcessRequestToken::factory(3)->create([
41+
'user_id' => $user->id,
42+
'process_request_id' => $processRequest->id,
43+
'process_id' => $processRequest->process_id,
44+
'status' => 'ACTIVE'
45+
]);
46+
47+
event(new ActivityAssigned($processRequestTokens->first()));
48+
49+
$this->assertEquals(1, RecommendationUser::count());
50+
51+
$recommendationUser = RecommendationUser::first();
52+
53+
$this->assertEquals($recommendation->id, $recommendationUser->recommendation_id);
54+
55+
$this->assertEquals($user->id, $recommendationUser->user_id);
56+
}
57+
}

0 commit comments

Comments
 (0)