1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Gui\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Mezon\Gui\FieldsAlgorithms; |
6
|
|
|
use Mezon\Conf\Conf; |
7
|
|
|
use Mezon\Fs\InMemory; |
8
|
|
|
define('ID_FIELD_NAME', 'id'); |
9
|
|
|
define('TITLE_FIELD_NAME', 'title'); |
10
|
|
|
define('USER_ID_FIELD_NAME', 'user_id'); |
11
|
|
|
define('FIELDS_FIELD_NAME', 'fields'); |
12
|
|
|
define('DISABLED_FIELD_NAME', 'disabled'); |
13
|
|
|
define('STRING_TYPE_NAME', 'string'); |
14
|
|
|
define('INTEGER_TYPE_NAME', 'integer'); |
15
|
|
|
define('DATE_TYPE_NAME', 'date'); |
16
|
|
|
define('EXTERNAL_TYPE_NAME', 'external'); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
21
|
|
|
*/ |
22
|
|
|
class FieldsAlgorithmsUnitTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
* @see TestCase::setUp() |
29
|
|
|
*/ |
30
|
|
|
protected function setUp(): void |
31
|
|
|
{ |
32
|
|
|
Conf::setConfigValue('fs/layer', 'mock'); |
33
|
|
|
InMemory::clearFs(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Post test processing |
38
|
|
|
*/ |
39
|
|
|
public function tearDown(): void |
40
|
|
|
{ |
41
|
|
|
unset($_GET[FIELDS_FIELD_NAME]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Method creates testing data |
46
|
|
|
* |
47
|
|
|
* @return array Testing data |
48
|
|
|
*/ |
49
|
|
|
protected function getFields1(): array |
50
|
|
|
{ |
51
|
|
|
return json_decode(file_get_contents(__DIR__ . '/conf/setup.json'), true); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Method creates testing data |
56
|
|
|
* |
57
|
|
|
* @return array Testing data |
58
|
|
|
*/ |
59
|
|
|
protected function getFields2(): array |
60
|
|
|
{ |
61
|
|
|
// TODO move it to the json config |
62
|
|
|
return [ |
63
|
|
|
ID_FIELD_NAME => [ |
64
|
|
|
'type' => INTEGER_TYPE_NAME, |
65
|
|
|
DISABLED_FIELD_NAME => 1 |
66
|
|
|
], |
67
|
|
|
TITLE_FIELD_NAME => [ |
68
|
|
|
'type' => STRING_TYPE_NAME, |
69
|
|
|
'required' => 1 |
70
|
|
|
] |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Testing invalid construction |
76
|
|
|
*/ |
77
|
|
|
public function testConstructor(): void |
78
|
|
|
{ |
79
|
|
|
// setup and test body |
80
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
81
|
|
|
|
82
|
|
|
// assertions |
83
|
|
|
$this->assertEquals('entity', $fieldsAlgorithms->getEntityName(), 'EntityName was not set'); |
84
|
|
|
$this->assertTrue($fieldsAlgorithms->hasCustomFields(), 'Data was not loaded'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Testing hasCustomFields |
89
|
|
|
*/ |
90
|
|
|
public function testHasNotCustomFields(): void |
91
|
|
|
{ |
92
|
|
|
// setup and test body |
93
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields2(), 'entity'); |
94
|
|
|
|
95
|
|
|
// assertions |
96
|
|
|
$_GET[FIELDS_FIELD_NAME] = TITLE_FIELD_NAME; |
97
|
|
|
$this->assertFalse($fieldsAlgorithms->hasCustomFields(), 'Custom fields are not in the model'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Testing hasCustomFields |
102
|
|
|
*/ |
103
|
|
|
public function testHasCustomFields(): void |
104
|
|
|
{ |
105
|
|
|
// setup and test body |
106
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
107
|
|
|
|
108
|
|
|
// assertions |
109
|
|
|
$this->assertTrue($fieldsAlgorithms->hasCustomFields(), 'Custom fields are in the model'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Testing getTypedValue |
114
|
|
|
*/ |
115
|
|
|
public function testGetTypedValue(): void |
116
|
|
|
{ |
117
|
|
|
// setup and test body |
118
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
119
|
|
|
|
120
|
|
|
// assertions int |
121
|
|
|
$this->assertEquals( |
122
|
|
|
1, |
123
|
|
|
$fieldsAlgorithms->getTypedValue(INTEGER_TYPE_NAME, '1'), |
124
|
|
|
'Type was not casted properly for integer'); |
125
|
|
|
$this->assertTrue( |
126
|
|
|
is_int($fieldsAlgorithms->getTypedValue(INTEGER_TYPE_NAME, '1')), |
127
|
|
|
'Type was not casted properly for integer'); |
128
|
|
|
|
129
|
|
|
// assertions string |
130
|
|
|
$this->assertEquals( |
131
|
|
|
'1', |
132
|
|
|
$fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '1'), |
133
|
|
|
'Type was not casted properly for string'); |
134
|
|
|
$this->assertTrue( |
135
|
|
|
is_string($fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '1')), |
136
|
|
|
'Return type is not correct'); |
137
|
|
|
$this->assertEquals( |
138
|
|
|
'&', |
139
|
|
|
$fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '&'), |
140
|
|
|
'Type was not casted properly for string'); |
141
|
|
|
$this->assertEquals( |
142
|
|
|
'""', |
143
|
|
|
$fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '""'), |
144
|
|
|
'Default brunch for string is not working'); |
145
|
|
|
|
146
|
|
|
// assertions date |
147
|
|
|
$this->assertEquals( |
148
|
|
|
'2019-01-01', |
149
|
|
|
$fieldsAlgorithms->getTypedValue(DATE_TYPE_NAME, '2019-01-01'), |
150
|
|
|
'Type was not casted properly for date'); |
151
|
|
|
$this->assertEquals( |
152
|
|
|
'', |
153
|
|
|
$fieldsAlgorithms->getTypedValue(DATE_TYPE_NAME, '""'), |
154
|
|
|
'Default date for string is not working'); |
155
|
|
|
|
156
|
|
|
// assertions file |
157
|
|
|
$this->assertContains('value', $fieldsAlgorithms->getTypedValue('file', [ |
158
|
|
|
'value' |
159
|
|
|
], false)); |
160
|
|
|
$path = $fieldsAlgorithms->getTypedValue('file', [ |
161
|
|
|
'name' => 'test.txt', |
162
|
|
|
'file' => '1234' |
163
|
|
|
], true); |
164
|
|
|
$this->assertEquals(base64_decode('1234'), InMemory::fileGetContents($path)); |
165
|
|
|
|
166
|
|
|
// assertions external |
167
|
|
|
$typedValue = $fieldsAlgorithms->getTypedValue(EXTERNAL_TYPE_NAME, [ |
168
|
|
|
'1', |
169
|
|
|
'2' |
170
|
|
|
]); |
171
|
|
|
$this->assertContains(1, $typedValue); |
172
|
|
|
$this->assertContains(2, $typedValue); |
173
|
|
|
$this->assertCount(2, $typedValue); |
|
|
|
|
174
|
|
|
|
175
|
|
|
// assertion unexisting |
176
|
|
|
$this->expectException(\Exception::class); |
177
|
|
|
$fieldsAlgorithms->getTypedValue('unexisting', '1'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Test validateFieldExistance method |
182
|
|
|
*/ |
183
|
|
|
public function testValidateFieldExistance(): void |
184
|
|
|
{ |
185
|
|
|
// setup and test body |
186
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
187
|
|
|
|
188
|
|
|
// test body and assertions |
189
|
|
|
$this->expectException(\Exception::class); |
190
|
|
|
$fieldsAlgorithms->validateFieldExistance('unexisting-field'); |
191
|
|
|
|
192
|
|
|
$this->expectException(\Exception::class); |
193
|
|
|
$fieldsAlgorithms->validateFieldExistance('id'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Test getSecureValue method |
198
|
|
|
*/ |
199
|
|
|
public function testGetSecureValue(): void |
200
|
|
|
{ |
201
|
|
|
// setup and test body |
202
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
203
|
|
|
|
204
|
|
|
// test body and assertions |
205
|
|
|
$id = $fieldsAlgorithms->getSecureValue('id', '1'); |
206
|
|
|
|
207
|
|
|
// assertions |
208
|
|
|
$this->assertIsInt($id, 'Invalid secure processing for integer value'); |
209
|
|
|
$this->assertEquals(1, $id, 'Data loss for integer value'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Test getSecureValues method |
214
|
|
|
*/ |
215
|
|
|
public function testGetSecureValues(): void |
216
|
|
|
{ |
217
|
|
|
// setup and test body |
218
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
219
|
|
|
|
220
|
|
|
// test body and assertions |
221
|
|
|
$id = $fieldsAlgorithms->getSecureValues('id', [ |
222
|
|
|
'1', |
223
|
|
|
'2&' |
224
|
|
|
]); |
225
|
|
|
|
226
|
|
|
// assertions |
227
|
|
|
$this->assertIsInt($id[0], 'Invalid secure processing for integer values'); |
228
|
|
|
$this->assertIsInt($id[1], 'Invalid secure processing for integer values'); |
229
|
|
|
|
230
|
|
|
$this->assertEquals(1, $id[0], 'Data loss for integer values'); |
231
|
|
|
$this->assertEquals(2, $id[1], 'Data loss for integer values'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Test getValuesForPrefix method |
236
|
|
|
*/ |
237
|
|
|
public function testGetValuesForPrefix(): void |
238
|
|
|
{ |
239
|
|
|
// setup and test body |
240
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
241
|
|
|
$_POST['prefix-id'] = '1'; |
242
|
|
|
$_POST['prefix-title'] = 'some string'; |
243
|
|
|
|
244
|
|
|
// test body and assertions |
245
|
|
|
$result = $fieldsAlgorithms->getValuesForPrefix('prefix-'); |
246
|
|
|
|
247
|
|
|
// assertions |
248
|
|
|
$this->assertIsInt($result['id'], 'Invalid secure processing for integer prefix'); |
249
|
|
|
$this->assertIsString($result[TITLE_FIELD_NAME], 'Invalid secure processing for string prefix'); |
250
|
|
|
|
251
|
|
|
$this->assertEquals(1, $result['id'], 'Data loss for integer preix'); |
252
|
|
|
$this->assertEquals('some string', $result[TITLE_FIELD_NAME], 'Data loss for string preix'); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Testing 'removeField' method |
257
|
|
|
*/ |
258
|
|
|
public function testRemoveField(): void |
259
|
|
|
{ |
260
|
|
|
// setup |
261
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
262
|
|
|
|
263
|
|
|
// test body |
264
|
|
|
$fieldsAlgorithms->removeField('extensions'); |
265
|
|
|
|
266
|
|
|
// assertions |
267
|
|
|
$this->assertFalse($fieldsAlgorithms->hasCustomFields(), 'Field "extensions" was not removed'); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Testing 'fetchCustomField' method for unexisting field |
272
|
|
|
*/ |
273
|
|
|
public function testFetchCustomFieldUnexistingField(): void |
274
|
|
|
{ |
275
|
|
|
// setup |
276
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
277
|
|
|
$record = []; |
278
|
|
|
|
279
|
|
|
// test body |
280
|
|
|
$result = $fieldsAlgorithms->fetchCustomField($record, 'unexisting'); |
281
|
|
|
|
282
|
|
|
// assertions |
283
|
|
|
$this->assertEquals(0, count($result), 'Something was returned, but should not'); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Testing 'fetchCustomField' method |
288
|
|
|
*/ |
289
|
|
|
public function testFetchCustomField(): void |
290
|
|
|
{ |
291
|
|
|
// setup |
292
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
293
|
|
|
$record = []; |
294
|
|
|
$_POST['entity' . '-balance'] = '11'; |
295
|
|
|
|
296
|
|
|
// test body |
297
|
|
|
$result = $fieldsAlgorithms->fetchCustomField($record, 'extensions'); |
298
|
|
|
|
299
|
|
|
// assertions |
300
|
|
|
$this->assertEquals(11, $result['balance'], 'Invalid field value'); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Testing 'fetchField' method |
305
|
|
|
*/ |
306
|
|
|
public function testFetchField(): void |
307
|
|
|
{ |
308
|
|
|
// setup |
309
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
310
|
|
|
$record = []; |
311
|
|
|
$_POST['entity' . '-id'] = '11'; |
312
|
|
|
$_FILES['entity' . '-avatar'] = [ |
313
|
|
|
'name' => 'test.dat', |
314
|
|
|
'file' => 'content' |
315
|
|
|
]; |
316
|
|
|
$_POST['entity' . '-balance'] = '33'; |
317
|
|
|
|
318
|
|
|
// test body |
319
|
|
|
$fieldsAlgorithms->fetchField($record, 'id'); |
320
|
|
|
$fieldsAlgorithms->fetchField($record, 'avatar'); |
321
|
|
|
$fieldsAlgorithms->fetchField($record, 'extensions'); |
322
|
|
|
|
323
|
|
|
// assertions |
324
|
|
|
$this->assertEquals(11, $record['id'], 'id was not fetched'); |
325
|
|
|
$this->assertEquals(base64_decode('content'), InMemory::fileGetContents($record['avatar'])); |
326
|
|
|
$this->assertEquals(33, $record['balance'], 'balance was not fetched'); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Testing 'getObject' method |
331
|
|
|
*/ |
332
|
|
|
public function testGetObject(): void |
333
|
|
|
{ |
334
|
|
|
// setup |
335
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
336
|
|
|
|
337
|
|
|
// test body |
338
|
|
|
$object = $fieldsAlgorithms->getObject('title'); |
339
|
|
|
|
340
|
|
|
// assertions |
341
|
|
|
$this->assertInstanceOf(\Mezon\Gui\Field\InputText::class, $object); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Testing 'getFieldsNames' method |
346
|
|
|
*/ |
347
|
|
|
public function testGetFieldsNames(): void |
348
|
|
|
{ |
349
|
|
|
// setup |
350
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
351
|
|
|
|
352
|
|
|
// test body |
353
|
|
|
$fields = $fieldsAlgorithms->getFieldsNames(); |
354
|
|
|
|
355
|
|
|
// assertions |
356
|
|
|
$this->assertContains('id', $fields); |
357
|
|
|
$this->assertContains('title', $fields); |
358
|
|
|
$this->assertContains('user_id', $fields); |
359
|
|
|
$this->assertContains('label', $fields); |
360
|
|
|
$this->assertContains('description', $fields); |
361
|
|
|
$this->assertContains('created', $fields); |
362
|
|
|
$this->assertContains('avatar', $fields); |
363
|
|
|
$this->assertContains('parts', $fields); |
364
|
|
|
$this->assertContains('extensions', $fields); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Testing field compilation |
369
|
|
|
*/ |
370
|
|
|
public function testGetCompiledField(): void |
371
|
|
|
{ |
372
|
|
|
// setup |
373
|
|
|
$fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity'); |
374
|
|
|
|
375
|
|
|
// test body |
376
|
|
|
$inputField = $fieldsAlgorithms->getCompiledField('title'); |
377
|
|
|
$textareaField = $fieldsAlgorithms->getCompiledField('description'); |
378
|
|
|
|
379
|
|
|
// assertions |
380
|
|
|
$this->assertStringContainsString('<input ', $inputField); |
381
|
|
|
$this->assertStringContainsString('<textarea ', $textareaField); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Testing exception in constructor |
386
|
|
|
*/ |
387
|
|
|
public function testExceptionInConstructor(): void |
388
|
|
|
{ |
389
|
|
|
// assertions |
390
|
|
|
$this->expectException(\Exception::class); |
391
|
|
|
$this->expectExceptionCode(- 1); |
392
|
|
|
$this->expectExceptionMessage('Can not define control\'s type'); |
393
|
|
|
|
394
|
|
|
// setup and test body |
395
|
|
|
new FieldsAlgorithms([ |
396
|
|
|
[ |
397
|
|
|
'undefined' => [] |
398
|
|
|
] |
399
|
|
|
], 'entity'); |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|