-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathJSONFormsTest.php
More file actions
172 lines (156 loc) · 5.9 KB
/
JSONFormsTest.php
File metadata and controls
172 lines (156 loc) · 5.9 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
<?php
namespace Tests\Unit;
use App\Models\Chamado;
use App\Models\Fila;
use App\Models\Setor;
use App\Models\User;
use App\Utils\JSONForms;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Tests\TestCase;
class JSONFormsTest extends TestCase
{
private $template = [
"predio" => [
"label" => "Prédio",
"type" => "text"
]
];
public function testTemplateFromFila()
{
Setor::factory()->create();
$template = json_encode($this->template);
$fila = Fila::factory()->make(['template' => $template]);
$json = json_decode($fila->template);
$this->assertEquals($json->predio->label, "Prédio");
}
public function testTemplateFromChamado()
{
$template = json_encode($this->template);
# precisamos do fila_id para usar o acessor do chamado
$fila = Fila::factory()->create(['template' => $template]);
$chamado = Chamado::factory()->make([
'fila_id' => $fila->id,
'extras' => '{"predio": "Administração"}'
]);
$key = key(json_decode($chamado->fila->template, true));
$json = json_decode($chamado->extras);
$this->assertEquals($json->$key, 'Administração');
}
public function testGenerateFormNoTemplate()
{
$fila = Fila::factory()->make();
$this->assertEquals(JSONForms::generateForm($fila), []);
}
public function testGenerateFormTemplate()
{
$template = json_encode($this->template);
$fila = Fila::factory()->make(['template' => $template]);
$form = JSONForms::generateForm($fila);
# JSONForms devolve um array de elementos
# um elemento é um label + input + help
$this->assertStringContainsString("extras[predio]", $form[0][1]->toHtml());
}
public function testGenerateFormChamado()
{
$template = json_encode($this->template);
$fila = Fila::factory()->create(['template' => $template]);
$chamado = Chamado::factory()->make([
'fila_id' => $fila->id,
'extras' => '{"predio": "Administração"}'
]);
$form = JSONForms::generateForm($fila, $chamado);
$this->assertStringContainsString("Administração", $form[0][1]->toHtml());
}
public function testGenerateFormCannot()
{
$tmp = $this->template;
$tmp["predio"]["can"] = "admin";
$template = json_encode($tmp);
$fila = Fila::factory()->make(['template' => $template]);
$form = JSONForms::generateForm($fila);
$this->assertEquals(JSONForms::generateForm($fila), []);
}
public function testGenerateFormCan()
{
$tmp = $this->template;
$tmp["predio"]["can"] = "admin";
$template = json_encode($tmp);
$user = User::factory()->make(['is_admin' => 1]);
$fila = Fila::factory()->make(['template' => $template]);
$this->actingAs($user);
session(['is_admin' => 1]);
$form = JSONForms::generateForm($fila);
$this->assertStringContainsString("extras[predio]", $form[0][1]->toHtml());
}
public function testGenerateFormSelect()
{
$tmp = $this->template;
$tmp["predio"]["type"] = "select";
$tmp["predio"]["value"] = [
"1" => "Bloco A",
"2" => "Administração"
];
$template = json_encode($tmp);
$fila = Fila::factory()->make(['template' => $template]);
$form = JSONForms::generateForm($fila);
$this->assertStringContainsString("Bloco A", $form[0][1]->toHtml());
}
public function testGenerateFormSelected()
{
$tmp = $this->template;
$tmp["predio"]["type"] = "select";
$tmp["predio"]["value"] = [
"1" => "Bloco A",
"2" => "Administração"
];
$template = json_encode($tmp);
$fila = Fila::factory()->create(['template' => $template]);
$chamado = Chamado::factory()->make([
'fila_id' => $fila->id,
'extras' => '{"predio": 2}'
]);
$form = JSONForms::generateForm($fila, $chamado);
$this->assertStringContainsString('selected">Admin', $form[0][1]->toHtml());
}
public function testGenerateFormHelp()
{
$tmp = $this->template;
$tmp["predio"]["help"] = "Local de atendimento";
$template = json_encode($tmp);
$fila = Fila::factory()->make(['template' => $template]);
$form = JSONForms::generateForm($fila);
# $form terá 3 elementos
$this->assertStringContainsString("Local de atendimento", $form[0][2]->toHtml());
}
public function testGenerateFormTextMaxlength()
{
$tmp = $this->template;
$tmp["predio"]["maxlength"] = 10;
$template = json_encode($tmp);
$fila = Fila::factory()->make(['template' => $template]);
$form = JSONForms::generateForm($fila);
$this->assertStringContainsString('maxlength="10"', $form[0][1]->toHtml());
}
public function testBuildRules()
{
$tmp = $this->template;
$tmp["predio"]["validate"] = "required";
$template = json_encode($tmp);
$fila = Fila::factory()->make(['template' => $template]);
$request = new Request([], ["extras" => ["predio" => "required"]]);
$validate = JSONForms::buildRules($request, $fila);
$this->assertEquals($validate, ["extras.predio" => "required"]);
}
public function testBuildTextMaxLengthRules()
{
$tmp = $this->template;
$tmp["predio"]["maxlength"] = 10;
$template = json_encode($tmp);
$fila = Fila::factory()->make(['template' => $template]);
$validate = JSONForms::buildTextMaxLengthRules(["predio" => "Administracao"], $fila);
$this->assertEquals($validate, ["extras.predio" => "nullable|string|max:10"]);
}
}