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

CustomFieldServiceTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
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\Services\CustomField;
26
27
use SP\Core\Acl\ActionsInterface;
28
use SP\Core\Crypt\Crypt;
29
use SP\Core\Exceptions\ConstraintException;
30
use SP\DataModel\CustomFieldData;
31
use SP\Repositories\NoSuchItemException;
32
use SP\Services\CustomField\CustomFieldService;
33
use SP\Storage\Database\DatabaseConnectionData;
34
use SP\Tests\DatabaseTestCase;
35
use SP\Tests\Services\Account\AccountCryptServiceTest;
36
use function SP\Tests\setupContext;
37
38
/**
39
 * Class CustomFieldServiceTest
40
 *
41
 * @package SP\Tests\Services\CustomField
42
 */
43
class CustomFieldServiceTest extends DatabaseTestCase
44
{
45
    /**
46
     * @var CustomFieldService
47
     */
48
    private static $service;
49
50
    /**
51
     * @throws \DI\NotFoundException
52
     * @throws \SP\Core\Context\ContextException
53
     * @throws \DI\DependencyException
54
     */
55
    public static function setUpBeforeClass()
56
    {
57
        $dic = setupContext();
58
59
        self::$dataset = 'syspass_customField.xml';
60
61
        // Datos de conexión a la BBDD
62
        self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
63
64
        // Inicializar el repositorio
65
        self::$service = $dic->get(CustomFieldService::class);
66
    }
67
68
    /**
69
     * @throws \SP\Core\Exceptions\ConstraintException
70
     * @throws \SP\Core\Exceptions\QueryException
71
     */
72
    public function testDeleteCustomFieldDefinitionDataBatch()
73
    {
74
        $this->assertEquals(3, self::$service->deleteCustomFieldDefinitionDataBatch([1, 2, 3]));
75
76
        $this->assertEquals(0, $this->conn->getRowCount('CustomFieldData'));
77
78
        $this->assertEquals(0, self::$service->deleteCustomFieldDefinitionDataBatch([]));
79
    }
80
81
    /**
82
     * @throws \Defuse\Crypto\Exception\CryptoException
83
     * @throws \SP\Core\Exceptions\ConstraintException
84
     * @throws \SP\Core\Exceptions\QueryException
85
     * @throws \SP\Services\ServiceException
86
     */
87
    public function testUpdateMasterPass()
88
    {
89
        $customFields = self::$service->getAllEncrypted();
90
91
        foreach ($customFields as $customField) {
92
            $data = Crypt::decrypt(
93
                $customField->getData(),
94
                $customField->getKey(),
95
                AccountCryptServiceTest::CURRENT_MASTERPASS);
96
97
            $customField->setData($data);
98
99
            $this->assertEquals(1, self::$service->updateMasterPass($customField, AccountCryptServiceTest::NEW_MASTERPASS));
100
        }
101
    }
102
103
    /**
104
     * @throws \SP\Core\Exceptions\ConstraintException
105
     * @throws \SP\Core\Exceptions\QueryException
106
     */
107
    public function testGetAllEncrypted()
108
    {
109
        $data = self::$service->getAllEncrypted();
110
111
        $this->assertCount(2, $data);
112
        $this->assertEquals(1, $data[0]->getDefinitionId());
113
        $this->assertEquals(1, $data[0]->getItemId());
114
        $this->assertEquals(1, $data[1]->getDefinitionId());
115
        $this->assertEquals(2, $data[1]->getItemId());
116
    }
117
118
    /**
119
     * @throws \SP\Core\Exceptions\ConstraintException
120
     * @throws \SP\Core\Exceptions\QueryException
121
     */
122
    public function testDeleteCustomFieldDataBatch()
123
    {
124
        $this->assertEquals(2, self::$service->deleteCustomFieldDataBatch([1, 2, 3], ActionsInterface::ACCOUNT));
125
126
        $this->assertEquals(1, self::$service->deleteCustomFieldDataBatch([1, 2, 3], ActionsInterface::CATEGORY));
127
128
        $this->assertEquals(0, $this->conn->getRowCount('CustomFieldData'));
129
130
        $this->assertEquals(0, self::$service->deleteCustomFieldDataBatch([], ActionsInterface::CATEGORY));
131
132
        $this->assertEquals(0, self::$service->deleteCustomFieldDataBatch([], ActionsInterface::USER));
133
    }
134
135
    /**
136
     * @throws \Defuse\Crypto\Exception\CryptoException
137
     * @throws \SP\Core\Exceptions\ConstraintException
138
     * @throws \SP\Core\Exceptions\QueryException
139
     * @throws \SP\Core\Exceptions\SPException
140
     */
141
    public function testUpdateOrCreateData()
142
    {
143
        $data = new CustomFieldData();
144
        $data->setItemId(1);
145
        $data->setModuleId(ActionsInterface::ACCOUNT);
146
        $data->setDefinitionId(1);
147
        $data->setData('cuenta');
148
149
        $this->assertTrue(self::$service->updateOrCreateData($data));
150
151
        $data = new CustomFieldData();
152
        $data->setItemId(1);
153
        $data->setModuleId(ActionsInterface::CATEGORY);
154
        $data->setDefinitionId(2);
155
        $data->setData('categoria');
156
157
        $this->assertTrue(self::$service->updateOrCreateData($data));
158
159
        $data = new CustomFieldData();
160
        $data->setItemId(2);
161
        $data->setModuleId(ActionsInterface::ACCOUNT);
162
        $data->setDefinitionId(1);
163
        $data->setData('cuenta');
164
165
        $this->assertTrue(self::$service->updateOrCreateData($data));
166
167
        $data = new CustomFieldData();
168
        $data->setItemId(2);
169
        $data->setModuleId(ActionsInterface::CATEGORY);
170
        $data->setDefinitionId(2);
171
        $data->setData('categoria');
172
173
        $this->assertTrue(self::$service->updateOrCreateData($data));
174
175
        $this->assertTrue(self::$service->updateOrCreateData(new CustomFieldData()));
176
177
        $data = new CustomFieldData();
178
        $data->setItemId(2);
179
        $data->setModuleId(ActionsInterface::USER);
180
        $data->setDefinitionId(3);
181
        $data->setData('nan');
182
183
        $this->assertEquals(true, self::$service->updateOrCreateData($data));
184
185
        $this->assertEquals(5, $this->conn->getRowCount('CustomFieldData'));
186
    }
187
188
    /**
189
     * @throws \SP\Core\Exceptions\ConstraintException
190
     * @throws \SP\Core\Exceptions\QueryException
191
     */
192
    public function testGetForModuleAndItemId()
193
    {
194
        $result = self::$service->getForModuleAndItemId(ActionsInterface::ACCOUNT, 1);
195
196
        $this->assertCount(1, $result);
197
        $this->assertEquals('Prueba', $result[0]->definitionName);
198
        $this->assertEquals(1, $result[0]->definitionId);
199
        $this->assertEquals(ActionsInterface::ACCOUNT, $result[0]->moduleId);
200
        $this->assertEquals(1, $result[0]->required);
201
        $this->assertEquals(0, $result[0]->showInList);
202
        $this->assertEquals('Ayuda', $result[0]->help);
203
        $this->assertEquals(1, $result[0]->isEncrypted);
204
        $this->assertEquals(1, $result[0]->typeId);
205
        $this->assertEquals('text', $result[0]->typeName);
206
        $this->assertEquals('Texto', $result[0]->typeText);
207
        $this->assertNotEmpty($result[0]->data);
208
        $this->assertNotEmpty($result[0]->key);
209
210
        $result = self::$service->getForModuleAndItemId(ActionsInterface::ACCOUNT, 2);
211
212
        $this->assertCount(1, $result);
213
        $this->assertEquals('Prueba', $result[0]->definitionName);
214
        $this->assertEquals(1, $result[0]->definitionId);
215
        $this->assertEquals(ActionsInterface::ACCOUNT, $result[0]->moduleId);
216
        $this->assertEquals(1, $result[0]->required);
217
        $this->assertEquals(0, $result[0]->showInList);
218
        $this->assertEquals('Ayuda', $result[0]->help);
219
        $this->assertEquals(1, $result[0]->isEncrypted);
220
        $this->assertEquals(1, $result[0]->typeId);
221
        $this->assertEquals('text', $result[0]->typeName);
222
        $this->assertEquals('Texto', $result[0]->typeText);
223
        $this->assertNotEmpty($result[0]->data);
224
        $this->assertNotEmpty($result[0]->key);
225
226
        $result = self::$service->getForModuleAndItemId(ActionsInterface::ACCOUNT, 3);
227
228
        $this->assertCount(1, $result);
229
230
        $result = self::$service->getForModuleAndItemId(ActionsInterface::CATEGORY, 1);
231
232
        $this->assertCount(2, $result);
233
        $this->assertEquals('SSL', $result[0]->definitionName);
234
        $this->assertEquals(3, $result[0]->definitionId);
235
        $this->assertEquals(ActionsInterface::CATEGORY, $result[0]->moduleId);
236
        $this->assertEquals(0, $result[0]->required);
237
        $this->assertEquals(0, $result[0]->showInList);
238
        $this->assertEquals(null, $result[0]->help);
239
        $this->assertEquals(1, $result[0]->isEncrypted);
240
        $this->assertEquals(10, $result[0]->typeId);
241
        $this->assertEquals('textarea', $result[0]->typeName);
242
        $this->assertEquals('Área de Texto', $result[0]->typeText);
243
        $this->assertNull($result[0]->data);
244
        $this->assertNull($result[0]->key);
245
246
        $this->assertEquals('RSA', $result[1]->definitionName);
247
        $this->assertEquals(2, $result[1]->definitionId);
248
        $this->assertEquals(ActionsInterface::CATEGORY, $result[1]->moduleId);
249
        $this->assertEquals(0, $result[1]->required);
250
        $this->assertEquals(0, $result[1]->showInList);
251
        $this->assertEquals(null, $result[1]->help);
252
        $this->assertEquals(0, $result[1]->isEncrypted);
253
        $this->assertEquals(2, $result[1]->typeId);
254
        $this->assertEquals('password', $result[1]->typeName);
255
        $this->assertEquals('Clave', $result[1]->typeText);
256
        $this->assertNotEmpty($result[1]->data);
257
        $this->assertNull($result[1]->key);
258
259
        $result = self::$service->getForModuleAndItemId(ActionsInterface::CATEGORY, 2);
260
261
        $this->assertEquals('SSL', $result[0]->definitionName);
262
        $this->assertEquals(3, $result[0]->definitionId);
263
        $this->assertEquals(ActionsInterface::CATEGORY, $result[0]->moduleId);
264
        $this->assertEquals(0, $result[0]->required);
265
        $this->assertEquals(0, $result[0]->showInList);
266
        $this->assertEquals(null, $result[0]->help);
267
        $this->assertEquals(1, $result[0]->isEncrypted);
268
        $this->assertEquals(10, $result[0]->typeId);
269
        $this->assertEquals('textarea', $result[0]->typeName);
270
        $this->assertEquals('Área de Texto', $result[0]->typeText);
271
        $this->assertNull($result[0]->data);
272
        $this->assertNull($result[0]->key);
273
274
        $this->assertCount(2, $result);
275
        $this->assertEquals('RSA', $result[1]->definitionName);
276
        $this->assertEquals(2, $result[1]->definitionId);
277
        $this->assertEquals(ActionsInterface::CATEGORY, $result[1]->moduleId);
278
        $this->assertEquals(0, $result[1]->required);
279
        $this->assertEquals(0, $result[1]->showInList);
280
        $this->assertEquals(null, $result[1]->help);
281
        $this->assertEquals(0, $result[1]->isEncrypted);
282
        $this->assertEquals(2, $result[1]->typeId);
283
        $this->assertEquals('password', $result[1]->typeName);
284
        $this->assertEquals('Clave', $result[1]->typeText);
285
        $this->assertNull($result[1]->data);
286
        $this->assertNull($result[1]->key);
287
288
        $result = self::$service->getForModuleAndItemId(ActionsInterface::CATEGORY, 3);
289
290
        $this->assertCount(2, $result);
291
292
        $result = self::$service->getForModuleAndItemId(ActionsInterface::USER, 1);
293
294
        $this->assertCount(0, $result);
295
    }
296
297
    /**
298
     * @throws \SP\Core\Exceptions\SPException
299
     */
300
    public function testDeleteCustomFieldData()
301
    {
302
        $this->assertEquals(1, self::$service->deleteCustomFieldData(1, ActionsInterface::ACCOUNT));
303
        $this->assertEquals(1, self::$service->deleteCustomFieldData(2, ActionsInterface::ACCOUNT));
304
        $this->assertEquals(1, self::$service->deleteCustomFieldData(1, ActionsInterface::CATEGORY));
305
306
        $this->assertEquals(0, $this->conn->getRowCount('CustomFieldData'));
307
308
        $this->assertEquals(0, self::$service->deleteCustomFieldData(2, ActionsInterface::ACCOUNT));
309
310
        $this->assertEquals(0, self::$service->deleteCustomFieldData(2, ActionsInterface::CATEGORY));
311
312
        $this->assertEquals(0, self::$service->deleteCustomFieldData(2, ActionsInterface::USER));
313
    }
314
315
    /**
316
     * @throws \SP\Core\Exceptions\ConstraintException
317
     * @throws \SP\Core\Exceptions\QueryException
318
     */
319
    public function testDeleteCustomFieldDefinitionData()
320
    {
321
        $this->assertEquals(2, self::$service->deleteCustomFieldDefinitionData(1));
322
        $this->assertEquals(1, self::$service->deleteCustomFieldDefinitionData(2));
323
        $this->assertEquals(0, self::$service->deleteCustomFieldDefinitionData(3));
324
325
        $this->assertEquals(0, $this->conn->getRowCount('CustomFieldData'));
326
    }
327
328
    /**
329
     * @throws \SP\Core\Exceptions\ConstraintException
330
     * @throws \SP\Core\Exceptions\QueryException
331
     */
332
    public function testGetAll()
333
    {
334
        $result = self::$service->getAll();
335
336
        $this->assertCount(3, $result);
337
        $this->assertInstanceOf(CustomFieldData::class, $result[0]);
338
        $this->assertInstanceOf(CustomFieldData::class, $result[1]);
339
    }
340
341
    /**
342
     * @throws ConstraintException
343
     * @throws \Defuse\Crypto\Exception\CryptoException
344
     * @throws \SP\Core\Exceptions\QueryException
345
     * @throws \SP\Repositories\NoSuchItemException
346
     * @throws \SP\Services\ServiceException
347
     */
348
    public function testCreate()
349
    {
350
        $data = new CustomFieldData();
351
        $data->setId(2);
352
        $data->setModuleId(ActionsInterface::ACCOUNT);
353
        $data->setDefinitionId(1);
354
        $data->setData('cuenta');
355
        $data->setKey('nan');
356
357
        $this->assertEquals(3, self::$service->create($data));
358
359
        $data = new CustomFieldData();
360
        $data->setId(2);
361
        $data->setModuleId(ActionsInterface::CATEGORY);
362
        $data->setDefinitionId(2);
363
        $data->setData('categoria');
364
        $data->setKey('nan');
365
366
        $this->assertEquals(4, self::$service->create($data));
367
368
        $this->expectException(NoSuchItemException::class);
369
370
        $data = new CustomFieldData();
371
        $data->setId(2);
372
        $data->setModuleId(ActionsInterface::ACCOUNT);
373
        $data->setDefinitionId(1);
374
        $data->setData('cuenta');
375
        $data->setKey('nan');
376
377
        self::$service->create($data);
378
379
        $data->setDefinitionId(3);
380
381
        self::$service->create($data);
382
383
        $data = new CustomFieldData();
384
        $data->setId(2);
385
        $data->setModuleId(ActionsInterface::CATEGORY);
386
        $data->setDefinitionId(2);
387
        $data->setData('categoria');
388
        $data->setKey('nan');
389
390
        self::$service->create($data);
391
392
        $data->setDefinitionId(4);
393
394
        self::$service->create($data);
395
396
        $this->assertEquals(4, $this->conn->getRowCount('CustomFieldData'));
397
    }
398
}
399