-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkflowTest.php
More file actions
124 lines (105 loc) · 4.2 KB
/
Copy pathWorkflowTest.php
File metadata and controls
124 lines (105 loc) · 4.2 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
<?php
/**
* Unit tests for the workflow state-machine engine (Emanager_Workflow).
*
* @package eManager
*/
declare( strict_types=1 );
use PHPUnit\Framework\TestCase;
/**
* Exercises the pure (no-DB) parts of the workflow engine.
*/
final class WorkflowTest extends TestCase {
/**
* A representative two-state module with a gated transition.
*
* @var array
*/
private array $module;
protected function setUp(): void {
$GLOBALS['em_test_caps'] = array();
$GLOBALS['em_test_uid'] = 1;
$GLOBALS['em_test_user_meta'] = array();
$this->module = array(
'id' => 'pcos',
'fields' => array(
array(
'name' => 'answer',
'label' => 'Owner answer',
),
),
'workflow' => array(
'states' => array(
'Owner Review' => array(
'transitions' => array(
array(
'to' => 'Assign Contracts',
'label' => 'Approve & assign',
'party' => array( 'owner', 'rep' ),
'requires' => array( 'answer' ),
),
),
),
'Assign Contracts' => array( 'transitions' => array() ),
),
),
);
}
public function test_has_and_get_detect_workflow(): void {
$this->assertTrue( Emanager_Workflow::has( $this->module ) );
$this->assertNotNull( Emanager_Workflow::get( $this->module ) );
$plain = array( 'id' => 'notes' );
$this->assertFalse( Emanager_Workflow::has( $plain ) );
$this->assertNull( Emanager_Workflow::get( $plain ) );
}
public function test_transitions_from_known_and_unknown_state(): void {
$transitions = Emanager_Workflow::transitions_from( $this->module, 'Owner Review' );
$this->assertCount( 1, $transitions );
$this->assertSame( 'Assign Contracts', $transitions[0]['to'] );
$this->assertSame( array(), Emanager_Workflow::transitions_from( $this->module, 'Nonexistent' ) );
$this->assertSame( array(), Emanager_Workflow::transitions_from( $this->module, 'Assign Contracts' ) );
}
public function test_find_transition(): void {
$found = Emanager_Workflow::find_transition( $this->module, 'Owner Review', 'Assign Contracts' );
$this->assertIsArray( $found );
$this->assertSame( 'Approve & assign', $found['label'] );
$this->assertNull( Emanager_Workflow::find_transition( $this->module, 'Owner Review', 'Bogus' ) );
}
public function test_missing_requirements_reports_field_label(): void {
// Empty value -> the human label of the missing field is returned.
$missing = Emanager_Workflow::missing_requirements(
$this->module,
array( 'answer' => '' ),
$this->module['workflow']['states']['Owner Review']['transitions'][0]
);
$this->assertSame( array( 'Owner answer' ), $missing );
// Filled value -> requirement satisfied.
$ok = Emanager_Workflow::missing_requirements(
$this->module,
array( 'answer' => 'Approved' ),
$this->module['workflow']['states']['Owner Review']['transitions'][0]
);
$this->assertSame( array(), $ok );
}
public function test_available_transitions_gate_on_party_and_cap(): void {
$transition = $this->module['workflow']['states']['Owner Review']['transitions'][0];
// Wrong party + no cap -> blocked, with a human reason.
$GLOBALS['em_test_caps'] = array();
$GLOBALS['em_test_user_meta'][1]['emanager_party_role'] = 'subcontractor';
$blocked = Emanager_Workflow::available_transitions( $this->module, 'Owner Review' );
$this->assertFalse( $blocked[0]['allowed'] );
$this->assertStringContainsString( 'Owner', $blocked[0]['reason'] );
// Correct party + create cap -> allowed.
$GLOBALS['em_test_caps'] = array( 'emanager_create' );
$GLOBALS['em_test_user_meta'][1]['emanager_party_role'] = 'owner';
$allowed = Emanager_Workflow::available_transitions( $this->module, 'Owner Review' );
$this->assertTrue( $allowed[0]['allowed'] );
// Manager bypasses every party gate.
$GLOBALS['em_test_caps'] = array( 'emanager_create', 'emanager_manage' );
$GLOBALS['em_test_user_meta'][1]['emanager_party_role'] = 'subcontractor';
$manager = Emanager_Workflow::available_transitions( $this->module, 'Owner Review' );
$this->assertTrue( $manager[0]['allowed'] );
// Keep the unused local referenced for clarity.
$this->assertSame( 'Assign Contracts', $transition['to'] );
}
}