Skip to content

Commit 0db0c90

Browse files
committed
Add test to get the list of processes to start, organized by category.
1 parent 16f04a0 commit 0db0c90

4 files changed

Lines changed: 131 additions & 6 deletions

File tree

‎ProcessMaker/Http/Controllers/Api/ProcessCategoryController.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function index(Request $request)
3333
$request->input('order_by', 'name'),
3434
$request->input('order_direction', 'asc')
3535
);
36+
$include = $this->getRequestInclude($request);
37+
$query->with($include);
3638
$response = $query->paginate($request->input('per_page', 10));
3739
return new ApiCollection($response);
3840
}

‎ProcessMaker/Models/Process.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,16 @@ public function getStartEvents()
295295
}
296296
return $response;
297297
}
298+
299+
/**
300+
* Process events relationship.
301+
*
302+
* @return \ProcessMaker\Models\ProcessEvents
303+
*/
304+
public function events()
305+
{
306+
$query = $this->newQuery();
307+
$query->where('uuid', $this->uuid);
308+
return new ProcessEvents($query, $this);
309+
}
298310
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
namespace ProcessMaker\Models;
3+
4+
use Illuminate\Database\Eloquent\Relations\Relation;
5+
use Illuminate\Database\Eloquent\Collection;
6+
7+
/**
8+
* Process events relationship
9+
*
10+
*/
11+
class ProcessEvents extends Relation
12+
{
13+
14+
/**
15+
* @var Process[] $processModels
16+
*/
17+
private $processModels = [];
18+
19+
/**
20+
* Set the base constraints on the relation query.
21+
*
22+
* @return void
23+
*/
24+
public function addConstraints()
25+
{
26+
//No Constraints
27+
}
28+
29+
/**
30+
* Set the constraints for an eager load of the relation.
31+
*
32+
* @param array $models
33+
* @return void
34+
*/
35+
public function addEagerConstraints(array $models)
36+
{
37+
$this->processModels = $models;
38+
}
39+
40+
/**
41+
* Get the results of the relationship.
42+
*
43+
* @return mixed
44+
*/
45+
public function getResults()
46+
{
47+
return $this->parent->getStartEvents();
48+
}
49+
50+
/**
51+
* Initialize the relation on a set of models.
52+
*
53+
* @param array $models
54+
* @param string $relation
55+
* @return array
56+
*/
57+
public function initRelation(array $models, $relation)
58+
{
59+
return $models;
60+
}
61+
62+
/**
63+
* Match the eagerly loaded results to their parents.
64+
*
65+
* @param array $models
66+
* @param \Illuminate\Database\Eloquent\Collection $results
67+
* @param string $relation
68+
* @return array
69+
*/
70+
public function match(array $models, Collection $results, $relation)
71+
{
72+
foreach ($models as $model) {
73+
$events = collect($model->getStartEvents());
74+
$model->setRelation($relation, $events);
75+
}
76+
return $models;
77+
}
78+
}

‎tests/Feature/Api/ProcessExecutionTest.php‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ protected function setUp()
5656
/**
5757
* Create a single task process assigned to $this->user
5858
*/
59-
private function createTestProcess()
59+
private function createTestProcess(array $data = [])
6060
{
61-
$process = factory(Process::class)->create([
62-
'bpmn' => Process::getProcessTemplate('SingleTask.bpmn')
63-
]);
61+
$data['bpmn'] = Process::getProcessTemplate('SingleTask.bpmn');
62+
$process = factory(Process::class)->create($data);
6463
//Assign the task to $this->user
6564
$taskUuid = 'UserTaskUID';
6665
factory(ProcessTaskAssignment::class)->create([
@@ -107,10 +106,10 @@ public function testExecuteAProcess()
107106
*/
108107
public function testGetListOfStartEvents()
109108
{
110-
$route = route('processes.show', [$this->process->uuid_text, 'include'=>'events']);
109+
$route = route('processes.show', [$this->process->uuid_text, 'include' => 'events']);
111110
$response = $this->json('GET', $route);
112111
//Check the inclusion of events
113-
$response->assertJsonStructure(['events'=>[['id','name']]]);
112+
$response->assertJsonStructure(['events' => [['id', 'name']]]);
114113
}
115114

116115
/**
@@ -194,4 +193,38 @@ public function testUpdateTaskInvalidStatus()
194193
$response = $this->json('PUT', $route, $data);
195194
$response->assertStatus(422);
196195
}
196+
197+
/**
198+
* Get the list of processes to start with categories and start events.
199+
*/
200+
public function testGetListProcessesToStart()
201+
{
202+
//Create two additional processes
203+
$this->createTestProcess();
204+
$uncProcess = $this->createTestProcess(['process_category_uuid' => null]);
205+
//Get the start event id
206+
$uncProcessEvents = $uncProcess->events;
207+
//Get the list of processes (with and without category) and its start events
208+
$route = route('processes.index', ['include' => 'events,category', 'order_by' => 'name']);
209+
$response = $this->json('GET', $route);
210+
//Check the inclusion of events
211+
$response->assertJsonStructure(['data' => ['*' => ['events' => [['id', 'name']], 'category']]]);
212+
$data = $response->json('data');
213+
$list = ['Uncategorized' => []];
214+
foreach ($data as $process) {
215+
$categoryName = $process['category'] ? $process['category']['name'] : 'Uncategorized';
216+
$list[$categoryName][] = $process;
217+
}
218+
$this->assertArraySubset(
219+
[
220+
'Uncategorized' => [
221+
[
222+
'name' => $uncProcess->name,
223+
'events' => [
224+
['id' => $uncProcessEvents[0]['id']]
225+
]
226+
]
227+
]
228+
], $list);
229+
}
197230
}

0 commit comments

Comments
 (0)