Passed
Push — devel-3.0 ( 09ea81...3c7891 )
by Rubén
03:32
created

CustomFieldRepositoryTest::testGetForModuleById()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 86
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 70
nc 1
nop 0
dl 0
loc 86
rs 8.6545
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Tests\Repositories;
26
27
use SP\Core\Acl\ActionsInterface;
28
use SP\Core\Exceptions\ConstraintException;
29
use SP\DataModel\CustomFieldData;
30
use SP\Repositories\CustomField\CustomFieldRepository;
31
use SP\Storage\Database\DatabaseConnectionData;
32
use SP\Tests\DatabaseTestCase;
33
use function SP\Tests\setupContext;
34
35
/**
36
 * Class CustomFieldRepositoryTest
37
 *
38
 * @package SP\Tests\Repositories
39
 */
40
class CustomFieldRepositoryTest extends DatabaseTestCase
41
{
42
    /**
43
     * @var CustomFieldRepository
44
     */
45
    private static $repository;
46
47
    /**
48
     * @throws \DI\NotFoundException
49
     * @throws \SP\Core\Context\ContextException
50
     * @throws \DI\DependencyException
51
     */
52
    public static function setUpBeforeClass()
53
    {
54
        $dic = setupContext();
55
56
        self::$dataset = 'syspass.xml';
57
58
        // Datos de conexión a la BBDD
59
        self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
60
61
        // Inicializar el repositorio
62
        self::$repository = $dic->get(CustomFieldRepository::class);
63
    }
64
65
    /**
66
     * @throws \SP\Core\Exceptions\ConstraintException
67
     * @throws \SP\Core\Exceptions\QueryException
68
     */
69
    public function testDeleteCustomFieldDataBatch()
70
    {
71
        $this->assertEquals(1, self::$repository->deleteCustomFieldDataBatch([1, 2, 3], ActionsInterface::ACCOUNT));
72
73
        $this->assertEquals(1, self::$repository->deleteCustomFieldDataBatch([1, 2, 3], ActionsInterface::CATEGORY));
74
75
        $this->assertEquals(0, $this->conn->getRowCount('CustomFieldData'));
76
77
        $this->assertEquals(0, self::$repository->deleteCustomFieldDataBatch([], ActionsInterface::CATEGORY));
78
79
        $this->assertEquals(0, self::$repository->deleteCustomFieldDataBatch([], ActionsInterface::USER));
80
81
    }
82
83
    /**
84
     * @throws \SP\Core\Exceptions\ConstraintException
85
     * @throws \SP\Core\Exceptions\QueryException
86
     */
87
    public function testDeleteCustomFieldDataForDefinition()
88
    {
89
        $this->assertEquals(1, self::$repository->deleteCustomFieldDataForDefinition(1, ActionsInterface::ACCOUNT, 1));
90
        $this->assertEquals(0, self::$repository->deleteCustomFieldDataForDefinition(1, ActionsInterface::ACCOUNT, 2));
91
        $this->assertEquals(0, self::$repository->deleteCustomFieldDataForDefinition(10, ActionsInterface::ACCOUNT, 3));
92
93
        $this->assertEquals(1, self::$repository->deleteCustomFieldDataForDefinition(1, ActionsInterface::CATEGORY, 2));
94
        $this->assertEquals(0, self::$repository->deleteCustomFieldDataForDefinition(1, ActionsInterface::CATEGORY, 1));
95
        $this->assertEquals(0, self::$repository->deleteCustomFieldDataForDefinition(10, ActionsInterface::CATEGORY, 3));
96
97
        $this->assertEquals(0, self::$repository->deleteCustomFieldDataForDefinition(1, ActionsInterface::USER, 1));
98
        $this->assertEquals(0, self::$repository->deleteCustomFieldDataForDefinition(1, ActionsInterface::USER, 2));
99
    }
100
101
    /**
102
     * @throws \SP\Core\Exceptions\ConstraintException
103
     * @throws \SP\Core\Exceptions\QueryException
104
     */
105
    public function testCheckExists()
106
    {
107
        $data = new CustomFieldData();
108
        $data->setModuleId(ActionsInterface::ACCOUNT);
109
        $data->setDefinitionId(1);
110
        $data->setId(1);
111
112
        $this->assertFalse(self::$repository->checkExists($data));
113
114
        $data->setModuleId(ActionsInterface::CATEGORY);
115
        $data->setDefinitionId(1);
116
        $data->setId(1);
117
118
        $this->assertFalse(self::$repository->checkExists($data));
119
120
        $data->setModuleId(ActionsInterface::USER);
121
        $data->setDefinitionId(1);
122
        $data->setId(1);
123
124
        $this->assertFalse(self::$repository->checkExists($data));
125
    }
126
127
    /**
128
     * @throws \SP\Core\Exceptions\ConstraintException
129
     * @throws \SP\Core\Exceptions\QueryException
130
     */
131
    public function testGetAllEncrypted()
132
    {
133
        $result = self::$repository->getAllEncrypted();
134
        $this->assertEquals(1, $result->getNumRows());
135
136
        /** @var CustomFieldData[] $data */
137
        $data = $result->getDataAsArray();
138
139
        $this->assertCount(1, $data);
140
        $this->assertInstanceOf(CustomFieldData::class, $data[0]);
141
        $this->assertEquals(1, $data[0]->getItemId());
142
        $this->assertEquals(ActionsInterface::ACCOUNT, $data[0]->getModuleId());
143
        $this->assertEquals(1, $data[0]->getItemId());
144
    }
145
146
    /**
147
     * @throws \SP\Core\Exceptions\ConstraintException
148
     * @throws \SP\Core\Exceptions\QueryException
149
     */
150
    public function testDeleteCustomFieldDefinitionDataBatch()
151
    {
152
        $this->assertEquals(2, self::$repository->deleteCustomFieldDefinitionDataBatch([1, 2, 3]));
153
154
        $this->assertEquals(0, $this->conn->getRowCount('CustomFieldData'));
155
156
        $this->assertEquals(0, self::$repository->deleteCustomFieldDefinitionDataBatch([]));
157
    }
158
159
    /**
160
     * @throws \SP\Core\Exceptions\ConstraintException
161
     * @throws \SP\Core\Exceptions\QueryException
162
     */
163
    public function testGetAll()
164
    {
165
        $result = self::$repository->getAll();
166
        $this->assertEquals(2, $result->getNumRows());
167
168
        $data = $result->getDataAsArray();
169
170
        $this->assertCount(2, $data);
171
        $this->assertInstanceOf(CustomFieldData::class, $data[0]);
172
        $this->assertInstanceOf(CustomFieldData::class, $data[1]);
173
    }
174
175
    /**
176
     * @throws \SP\Core\Exceptions\ConstraintException
177
     * @throws \SP\Core\Exceptions\QueryException
178
     */
179
    public function testDeleteCustomFieldData()
180
    {
181
        $this->assertEquals(1, self::$repository->deleteCustomFieldData(1, ActionsInterface::ACCOUNT));
182
        $this->assertEquals(1, self::$repository->deleteCustomFieldData(1, ActionsInterface::CATEGORY));
183
184
        $this->assertEquals(0, $this->conn->getRowCount('CustomFieldData'));
185
186
        $this->assertEquals(0, self::$repository->deleteCustomFieldData(2, ActionsInterface::ACCOUNT));
187
188
        $this->assertEquals(0, self::$repository->deleteCustomFieldData(2, ActionsInterface::CATEGORY));
189
190
        $this->assertEquals(0, self::$repository->deleteCustomFieldData(2, ActionsInterface::USER));
191
    }
192
193
    /**
194
     * @throws \SP\Core\Exceptions\ConstraintException
195
     * @throws \SP\Core\Exceptions\QueryException
196
     */
197
    public function testGetForModuleById()
198
    {
199
        $result = self::$repository->getForModuleAndItemId(ActionsInterface::ACCOUNT, 1);
200
        $this->assertEquals(1, $result->getNumRows());
201
202
        $data = $result->getDataAsArray();
203
204
        $this->assertCount(1, $data);
205
        $this->assertEquals('Prueba', $data[0]->definitionName);
206
        $this->assertEquals(1, $data[0]->definitionId);
207
        $this->assertEquals(ActionsInterface::ACCOUNT, $data[0]->moduleId);
208
        $this->assertEquals(1, $data[0]->required);
209
        $this->assertEquals(0, $data[0]->showInList);
210
        $this->assertEquals('Ayuda', $data[0]->help);
211
        $this->assertEquals(1, $data[0]->isEncrypted);
212
        $this->assertEquals(1, $data[0]->typeId);
213
        $this->assertEquals('text', $data[0]->typeName);
214
        $this->assertEquals('Texto', $data[0]->typeText);
215
        $this->assertNotEmpty($data[0]->data);
216
        $this->assertNotEmpty($data[0]->key);
217
218
        $result = self::$repository->getForModuleAndItemId(ActionsInterface::ACCOUNT, 2);
219
        $this->assertEquals(1, $result->getNumRows());
220
        $data = $result->getDataAsArray();
221
222
        $this->assertCount(1, $data);
223
        $this->assertEquals('Prueba', $data[0]->definitionName);
224
        $this->assertEquals(1, $data[0]->definitionId);
225
        $this->assertEquals(ActionsInterface::ACCOUNT, $data[0]->moduleId);
226
        $this->assertEquals(1, $data[0]->required);
227
        $this->assertEquals(0, $data[0]->showInList);
228
        $this->assertEquals('Ayuda', $data[0]->help);
229
        $this->assertEquals(1, $data[0]->isEncrypted);
230
        $this->assertEquals(1, $data[0]->typeId);
231
        $this->assertEquals('text', $data[0]->typeName);
232
        $this->assertEquals('Texto', $data[0]->typeText);
233
        $this->assertNull($data[0]->data);
234
        $this->assertNull($data[0]->key);
235
236
        $result = self::$repository->getForModuleAndItemId(ActionsInterface::ACCOUNT, 3);
237
238
        $this->assertEquals(1, $result->getNumRows());
239
240
        $result = self::$repository->getForModuleAndItemId(ActionsInterface::CATEGORY, 1);
241
        $this->assertEquals(2, $result->getNumRows());
242
243
        $data = $result->getDataAsArray();
244
245
        $this->assertCount(2, $data);
246
        $this->assertEquals('RSA', $data[0]->definitionName);
247
        $this->assertEquals(2, $data[0]->definitionId);
248
        $this->assertEquals(ActionsInterface::CATEGORY, $data[0]->moduleId);
249
        $this->assertEquals(0, $data[0]->required);
250
        $this->assertEquals(0, $data[0]->showInList);
251
        $this->assertEquals(null, $data[0]->help);
252
        $this->assertEquals(0, $data[0]->isEncrypted);
253
        $this->assertEquals(2, $data[0]->typeId);
254
        $this->assertEquals('password', $data[0]->typeName);
255
        $this->assertEquals('Clave', $data[0]->typeText);
256
        $this->assertNotEmpty($data[0]->data);
257
        $this->assertNull($data[0]->key);
258
259
        $result = self::$repository->getForModuleAndItemId(ActionsInterface::CATEGORY, 2);
260
        $this->assertEquals(2, $result->getNumRows());
261
262
        $data = $result->getDataAsArray();
263
264
        $this->assertCount(2, $data);
265
        $this->assertEquals('RSA', $data[0]->definitionName);
266
        $this->assertEquals(2, $data[0]->definitionId);
267
        $this->assertEquals(ActionsInterface::CATEGORY, $data[0]->moduleId);
268
        $this->assertEquals(0, $data[0]->required);
269
        $this->assertEquals(0, $data[0]->showInList);
270
        $this->assertEquals(null, $data[0]->help);
271
        $this->assertEquals(0, $data[0]->isEncrypted);
272
        $this->assertEquals(2, $data[0]->typeId);
273
        $this->assertEquals('password', $data[0]->typeName);
274
        $this->assertEquals('Clave', $data[0]->typeText);
275
        $this->assertNull($data[0]->data);
276
        $this->assertNull($data[0]->key);
277
278
        $result = self::$repository->getForModuleAndItemId(ActionsInterface::CATEGORY, 3);
279
        $this->assertEquals(2, $result->getNumRows());
280
281
        $result = self::$repository->getForModuleAndItemId(ActionsInterface::USER, 1);
282
        $this->assertEquals(0, $result->getNumRows());
283
    }
284
285
    /**
286
     * @throws \SP\Core\Exceptions\ConstraintException
287
     * @throws \SP\Core\Exceptions\QueryException
288
     */
289
    public function testCreate()
290
    {
291
        $data = new CustomFieldData();
292
        $data->setId(2);
293
        $data->setModuleId(ActionsInterface::ACCOUNT);
294
        $data->setDefinitionId(1);
295
        $data->setData('cuenta');
296
        $data->setKey('nan');
297
298
        $this->assertEquals(3, self::$repository->create($data));
299
300
        $data = new CustomFieldData();
301
        $data->setId(2);
302
        $data->setModuleId(ActionsInterface::CATEGORY);
303
        $data->setDefinitionId(2);
304
        $data->setData('categoria');
305
        $data->setKey('nan');
306
307
        $this->assertEquals(4, self::$repository->create($data));
308
309
        $this->expectException(ConstraintException::class);
310
311
        $data = new CustomFieldData();
312
        $data->setId(2);
313
        $data->setModuleId(ActionsInterface::ACCOUNT);
314
        $data->setDefinitionId(1);
315
        $data->setData('cuenta');
316
        $data->setKey('nan');
317
318
        self::$repository->create($data);
319
320
        $data->setDefinitionId(3);
321
322
        self::$repository->create($data);
323
324
        $data = new CustomFieldData();
325
        $data->setId(2);
326
        $data->setModuleId(ActionsInterface::CATEGORY);
327
        $data->setDefinitionId(2);
328
        $data->setData('categoria');
329
        $data->setKey('nan');
330
331
        self::$repository->create($data);
332
333
        $data->setDefinitionId(4);
334
335
        self::$repository->create($data);
336
337
        $this->assertEquals(4, $this->conn->getRowCount('CustomFieldData'));
338
    }
339
340
    /**
341
     * @throws ConstraintException
342
     * @throws \SP\Core\Exceptions\QueryException
343
     */
344
    public function testDeleteCustomFieldDefinitionData()
345
    {
346
        $this->assertEquals(1, self::$repository->deleteCustomFieldDefinitionData(1));
347
        $this->assertEquals(1, self::$repository->deleteCustomFieldDefinitionData(2));
348
349
        $this->assertEquals(0, $this->conn->getRowCount('CustomFieldData'));
350
    }
351
352
    /**
353
     * @throws ConstraintException
354
     * @throws \SP\Core\Exceptions\QueryException
355
     */
356
    public function testUpdate()
357
    {
358
        $data = new CustomFieldData();
359
        $data->setId(1);
360
        $data->setModuleId(ActionsInterface::ACCOUNT);
361
        $data->setDefinitionId(1);
362
        $data->setData('cuenta');
363
        $data->setKey('nan');
364
365
        $this->assertEquals(0, self::$repository->update($data));
366
367
        $data = new CustomFieldData();
368
        $data->setId(1);
369
        $data->setModuleId(ActionsInterface::CATEGORY);
370
        $data->setDefinitionId(2);
371
        $data->setData('categoria');
372
        $data->setKey('nan');
373
374
        $this->assertEquals(0, self::$repository->update($data));
375
376
377
        $data = new CustomFieldData();
378
        $data->setId(2);
379
        $data->setModuleId(ActionsInterface::ACCOUNT);
380
        $data->setDefinitionId(1);
381
        $data->setData('cuenta');
382
        $data->setKey('nan');
383
384
        $this->assertEquals(0, self::$repository->update($data));
385
386
        $data = new CustomFieldData();
387
        $data->setId(2);
388
        $data->setModuleId(ActionsInterface::CATEGORY);
389
        $data->setDefinitionId(2);
390
        $data->setData('categoria');
391
        $data->setKey('nan');
392
393
        $this->assertEquals(0, self::$repository->update($data));
394
395
        $this->assertEquals(0, self::$repository->update(new CustomFieldData()));
396
397
        $data = new CustomFieldData();
398
        $data->setId(2);
399
        $data->setModuleId(ActionsInterface::USER);
400
        $data->setDefinitionId(3);
401
        $data->setData('nan');
402
        $data->setKey('nan');
403
404
        $this->assertEquals(0, self::$repository->update($data));
405
406
        $this->assertEquals(2, $this->conn->getRowCount('CustomFieldData'));
407
    }
408
}
409