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