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

AccountServiceTest::testCreate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 72
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 57
nc 1
nop 0
dl 0
loc 72
rs 8.9381
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\Services\Account;
26
27
use SP\Core\Crypt\Crypt;
28
use SP\Core\Exceptions\ConstraintException;
29
use SP\DataModel\AccountData;
30
use SP\DataModel\AccountVData;
31
use SP\DataModel\ItemSearchData;
32
use SP\Repositories\NoSuchItemException;
33
use SP\Services\Account\AccountHistoryService;
34
use SP\Services\Account\AccountPasswordRequest;
35
use SP\Services\Account\AccountRequest;
36
use SP\Services\Account\AccountSearchFilter;
37
use SP\Services\Account\AccountService;
38
use SP\Services\ServiceException;
39
use SP\Storage\Database\DatabaseConnectionData;
40
use SP\Tests\DatabaseTestCase;
41
use SP\Util\Util;
42
use function SP\Tests\setupContext;
43
44
/**
45
 * Class AccountServiceTest
46
 *
47
 * @package SP\Tests\Services
48
 */
49
class AccountServiceTest extends DatabaseTestCase
50
{
51
    const SECURE_KEY_PASSWORD = '12345678900';
52
    /**
53
     * @var AccountHistoryService
54
     */
55
    protected static $accountHistoryService;
56
    /**
57
     * @var AccountService
58
     */
59
    private static $service;
60
61
    /**
62
     * @throws \DI\NotFoundException
63
     * @throws \SP\Core\Context\ContextException
64
     * @throws \DI\DependencyException
65
     */
66
    public static function setUpBeforeClass()
67
    {
68
        $dic = setupContext();
69
70
        self::$dataset = 'syspass_account.xml';
71
72
        // Datos de conexión a la BBDD
73
        self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
74
75
        // Inicializar el servicio
76
        self::$service = $dic->get(AccountService::class);
77
        self::$accountHistoryService = $dic->get(AccountHistoryService::class);
78
    }
79
80
    /**
81
     * @throws \SP\Core\Exceptions\ConstraintException
82
     * @throws \SP\Core\Exceptions\QueryException
83
     * @throws \SP\Core\Exceptions\SPException
84
     * @throws \Defuse\Crypto\Exception\CryptoException
85
     */
86
    public function testCreate()
87
    {
88
        $accountRequest = new AccountRequest();
89
        $accountRequest->name = 'Prueba 2';
90
        $accountRequest->login = 'admin';
91
        $accountRequest->url = 'http://syspass.org';
92
        $accountRequest->notes = 'notas';
93
        $accountRequest->userEditId = 1;
94
        $accountRequest->passDateChange = time() + 3600;
95
        $accountRequest->clientId = 1;
96
        $accountRequest->categoryId = 1;
97
        $accountRequest->isPrivate = 0;
98
        $accountRequest->isPrivateGroup = 0;
99
        $accountRequest->parentId = 0;
100
        $accountRequest->userId = 1;
101
        $accountRequest->userGroupId = 2;
102
        $accountRequest->pass = '1234abc';
103
        $accountRequest->tags = [2, 3];
104
        $accountRequest->usersView = [2, 4];
105
        $accountRequest->usersEdit = [3, 4];
106
        $accountRequest->userGroupsView = [2, 3];
107
        $accountRequest->userGroupsEdit = [2];
108
109
        $this->assertEquals(3, self::$service->create($accountRequest));
110
111
        $result = self::$service->getById(3);
112
113
        self::$service->withTagsById($result);
114
        self::$service->withUsersById($result);
115
        self::$service->withUserGroupsById($result);
116
117
        $data = $result->getAccountVData();
118
119
        $this->assertEquals(3, $result->getId());
120
        $this->assertEquals($accountRequest->name, $data->getName());
121
        $this->assertEquals($accountRequest->login, $data->getLogin());
122
        $this->assertEquals($accountRequest->url, $data->getUrl());
123
        $this->assertEquals($accountRequest->notes, $data->getNotes());
124
        $this->assertEquals($accountRequest->userId, $data->getUserId());
125
        $this->assertEquals($accountRequest->userGroupId, $data->getUserGroupId());
126
        $this->assertEquals($accountRequest->userEditId, $data->getUserEditId());
127
        $this->assertEquals($accountRequest->passDateChange, $data->getPassDateChange());
128
        $this->assertEquals($accountRequest->clientId, $data->getClientId());
129
        $this->assertEquals($accountRequest->categoryId, $data->getCategoryId());
130
        $this->assertEquals($accountRequest->isPrivate, $data->getIsPrivate());
131
        $this->assertEquals($accountRequest->isPrivateGroup, $data->getIsPrivateGroup());
132
        $this->assertEquals($accountRequest->parentId, $data->getParentId());
133
134
        $tags = $result->getTags();
135
136
        $this->assertEquals(3, $tags[0]->getId());
137
        $this->assertEquals(2, $tags[1]->getId());
138
139
        $users = $result->getUsers();
140
141
        $this->assertEquals(2, $users[0]->getId());
142
        $this->assertEquals(0, (int)$users[0]->isEdit);
0 ignored issues
show
Bug introduced by
The property isEdit does not seem to exist on SP\DataModel\ItemData.
Loading history...
143
        $this->assertEquals(3, $users[1]->getId());
144
        $this->assertEquals(1, (int)$users[1]->isEdit);
145
        $this->assertEquals(4, $users[2]->getId());
146
        $this->assertEquals(1, (int)$users[2]->isEdit);
147
148
        $groups = $result->getUserGroups();
149
150
        $this->assertEquals(2, $groups[0]->getId());
151
        $this->assertEquals(1, (int)$groups[0]->isEdit);
152
        $this->assertEquals(3, $groups[1]->getId());
153
        $this->assertEquals(0, (int)$groups[1]->isEdit);
154
155
        $data = self::$service->getPasswordForId(3);
156
157
        $this->assertEquals('1234abc', Crypt::decrypt($data->getPass(), $data->getKey(), self::SECURE_KEY_PASSWORD));
158
    }
159
160
    /**
161
     * @throws ServiceException
162
     */
163
    public function testDelete()
164
    {
165
        // Comprobar registros iniciales
166
        $this->assertEquals(2, $this->conn->getRowCount('Account'));
167
168
        // Eliminar registros y comprobar el total de registros
169
        self::$service->delete(1);
170
        self::$service->delete(2);
171
172
        $this->assertEquals(0, $this->conn->getRowCount('Account'));
173
174
        $this->expectException(NoSuchItemException::class);
175
176
        // Eliminar un registro no existente
177
        $this->assertEquals(0, self::$service->delete(100));
178
    }
179
180
    /**
181
     * @throws \Defuse\Crypto\Exception\CryptoException
182
     * @throws \SP\Core\Exceptions\ConstraintException
183
     * @throws \SP\Core\Exceptions\QueryException
184
     * @throws NoSuchItemException
185
     */
186
    public function testUpdatePasswordMasterPass()
187
    {
188
        $accountRequest = new AccountPasswordRequest();
189
        $accountRequest->id = 2;
190
        $accountRequest->key = Crypt::makeSecuredKey(self::SECURE_KEY_PASSWORD);
191
        $accountRequest->pass = Crypt::encrypt('1234', $accountRequest->key, self::SECURE_KEY_PASSWORD);
192
193
        // Comprobar que la modificación de la clave es correcta
194
        $this->assertTrue(self::$service->updatePasswordMasterPass($accountRequest));
195
196
        $data = self::$service->getPasswordForId(2);
197
        $clearPassword = Crypt::decrypt($data->pass, $data->key, self::SECURE_KEY_PASSWORD);
198
199
        // Comprobar que la clave obtenida es igual a la encriptada anteriormente
200
        $this->assertEquals('1234', $clearPassword);
201
    }
202
203
    /**
204
     * @throws \SP\Core\Exceptions\ConstraintException
205
     * @throws \SP\Core\Exceptions\QueryException
206
     */
207
    public function testGetTotalNumAccounts()
208
    {
209
        $this->assertEquals(7, self::$service->getTotalNumAccounts());
210
    }
211
212
    /**
213
     * @throws NoSuchItemException
214
     * @throws \SP\Core\Exceptions\ConstraintException
215
     * @throws \SP\Core\Exceptions\QueryException
216
     */
217
    public function testGetDataForLink()
218
    {
219
        $data = self::$service->getDataForLink(1);
220
221
        $this->assertEquals(1, $data->getId());
222
        $this->assertEquals('Google', $data->getName());
223
        $this->assertEquals('admin', $data->getLogin());
224
        $this->assertEquals(pack('H*', '6465663530323030656135663361636362366237656462653536343938666234313231616635323237363539663162346532383963386361346565323732656530636238333632316436393736353665373631393435623033353236616164333730336662306531333535626437333638653033666137623565633364306365323634663863643436393436633365353234316534373338376130393133663935303736396364613365313234643432306636393834386434613262316231306138'), $data->getPass());
225
        $this->assertEquals(pack('H*', '6465663130303030646566353032303065646434636466636231333437613739616166313734343462343839626362643364353664376664356562373233363235653130316261666432323539343633336664626639326630613135373461653562613562323535353230393236353237623863633534313862653363376361376536366139356366353366356162663031623064343236613234336162643533643837643239636633643165326532663732626664396433366133653061343534656664373134633661366237616338363966636263366435303166613964316338386365623264303861333438626633656638653135356538633865353838623938636465653061306463313835646636366535393138393831653366303464323139386236383738333539616563653034376434643637663835313235636661313237633138373865643530616630393434613934616363356265316130323566623065633362663831613933626365366365343734336164363562656638353131343466343332323837356438323339303236656363613866643862376330396563356465373233666466313636656166386336356539666537353436333535333664393766383366316366663931396530386339373730636166633136376661656364306366656262323931666334343831333238333662366432'), $data->getKey());
226
        $this->assertEquals('http://google.com', $data->getUrl());
227
        $this->assertEquals('aaaa', $data->getNotes());
228
        $this->assertEquals('Google', $data->getClientName());
229
        $this->assertEquals('Web', $data->getCategoryName());
230
231
        $this->expectException(NoSuchItemException::class);
232
233
        self::$service->getDataForLink(10);
234
    }
235
236
    /**
237
     * @throws \SP\Core\Exceptions\ConstraintException
238
     * @throws \SP\Core\Exceptions\QueryException
239
     */
240
    public function testGetAccountsPassData()
241
    {
242
        $this->assertCount(2, self::$service->getAccountsPassData());
243
    }
244
245
    /**
246
     * @throws \Exception
247
     */
248
    public function testEditRestore()
249
    {
250
        self::$service->editRestore(3, 1);
251
252
        $this->expectException(ServiceException::class);
253
254
        self::$service->editRestore(1, 1);
255
        self::$service->editRestore(3, 10);
256
257
        $this->assertEquals(6, $this->conn->getRowCount('AccountHistory'));
258
    }
259
260
    /**
261
     * @throws \SP\Core\Exceptions\ConstraintException
262
     * @throws \SP\Core\Exceptions\QueryException
263
     */
264
    public function testGetLinked()
265
    {
266
        $result = self::$service->getLinked(1);
267
268
        $this->assertCount(1, $result);
269
        $this->assertEquals(2, $result[0]->id);
270
        $this->assertEquals('Apple', $result[0]->name);
271
272
        $this->assertCount(0, self::$service->getLinked(2));
273
    }
274
275
    /**
276
     * @throws ServiceException
277
     * @throws \Defuse\Crypto\Exception\CryptoException
278
     */
279
    public function testGetPasswordEncrypted()
280
    {
281
        $data = self::$service->getPasswordEncrypted('123abc');
282
283
        $this->assertEquals('123abc', Crypt::decrypt($data['pass'], $data['key'], self::SECURE_KEY_PASSWORD));
284
285
        $randomKeyPass = Util::generateRandomBytes();
286
287
        $data = self::$service->getPasswordEncrypted('123abc', $randomKeyPass);
288
289
        $this->assertEquals('123abc', Crypt::decrypt($data['pass'], $data['key'], $randomKeyPass));
290
    }
291
292
    /**
293
     * @throws NoSuchItemException
294
     * @throws \Defuse\Crypto\Exception\CryptoException
295
     * @throws \SP\Core\Exceptions\ConstraintException
296
     * @throws \SP\Core\Exceptions\QueryException
297
     * @throws \Exception
298
     */
299
    public function testEditPassword()
300
    {
301
        $accountRequest = new AccountRequest();
302
        $accountRequest->pass = '123abc';
303
        $accountRequest->id = 2;
304
        $accountRequest->userEditId = 1;
305
        $accountRequest->passDateChange = time() + 3600;
306
307
        // Comprobar que la modificación de la clave es correcta
308
        self::$service->editPassword($accountRequest);
309
310
        $data = self::$service->getPasswordForId(2);
311
312
        $clearPassword = Crypt::decrypt($data->pass, $data->key, self::SECURE_KEY_PASSWORD);
313
314
        // Comprobar que la clave obtenida es igual a la encriptada anteriormente
315
        $this->assertEquals('123abc', $clearPassword);
316
317
        $this->expectException(NoSuchItemException::class);
318
319
        // Comprobar que no devuelve resultados
320
        self::$service->getPasswordForId(10);
321
    }
322
323
    /**
324
     * @throws NoSuchItemException
325
     * @throws \SP\Core\Exceptions\ConstraintException
326
     * @throws \SP\Core\Exceptions\QueryException
327
     * @throws \SP\Core\Exceptions\SPException
328
     * @throws \Exception
329
     */
330
    public function testUpdate()
331
    {
332
        $accountRequest = new AccountRequest();
333
        $accountRequest->id = 1;
334
        $accountRequest->name = 'Prueba 1';
335
        $accountRequest->login = 'admin';
336
        $accountRequest->url = 'http://syspass.org';
337
        $accountRequest->notes = 'notas';
338
        $accountRequest->userEditId = 1;
339
        $accountRequest->passDateChange = time() + 3600;
340
        $accountRequest->clientId = 1;
341
        $accountRequest->categoryId = 1;
342
        $accountRequest->isPrivate = 0;
343
        $accountRequest->isPrivateGroup = 0;
344
        $accountRequest->parentId = 0;
345
        $accountRequest->userGroupId = 2;
346
        $accountRequest->tags = [2, 3];
347
        $accountRequest->usersView = [2, 4];
348
        $accountRequest->usersEdit = [3, 4];
349
        $accountRequest->userGroupsView = [2, 3];
350
        $accountRequest->userGroupsEdit = [2];
351
        $accountRequest->updateTags = true;
352
        $accountRequest->updateUserPermissions = true;
353
        $accountRequest->updateUserGroupPermissions = true;
354
355
        self::$service->update($accountRequest);
356
357
        $result = self::$service->getById(1);
358
359
        self::$service->withTagsById($result);
360
        self::$service->withUsersById($result);
361
        self::$service->withUserGroupsById($result);
362
363
        $data = $result->getAccountVData();
364
365
        $this->assertEquals(1, $result->getId());
366
        $this->assertEquals($accountRequest->name, $data->getName());
367
        $this->assertEquals($accountRequest->login, $data->getLogin());
368
        $this->assertEquals($accountRequest->url, $data->getUrl());
369
        $this->assertEquals($accountRequest->notes, $data->getNotes());
370
        $this->assertEquals(1, $data->getUserId());
371
        $this->assertEquals($accountRequest->userGroupId, $data->getUserGroupId());
372
        $this->assertEquals($accountRequest->userEditId, $data->getUserEditId());
373
        $this->assertEquals($accountRequest->passDateChange, $data->getPassDateChange());
374
        $this->assertEquals($accountRequest->clientId, $data->getClientId());
375
        $this->assertEquals($accountRequest->categoryId, $data->getCategoryId());
376
        $this->assertEquals($accountRequest->isPrivate, $data->getIsPrivate());
377
        $this->assertEquals($accountRequest->isPrivateGroup, $data->getIsPrivateGroup());
378
        $this->assertEquals($accountRequest->parentId, $data->getParentId());
379
380
        $tags = $result->getTags();
381
382
        $this->assertEquals(3, $tags[0]->getId());
383
        $this->assertEquals(2, $tags[1]->getId());
384
385
        $users = $result->getUsers();
386
387
        $this->assertEquals(2, $users[0]->getId());
388
        $this->assertEquals(0, (int)$users[0]->isEdit);
0 ignored issues
show
Bug introduced by
The property isEdit does not seem to exist on SP\DataModel\ItemData.
Loading history...
389
        $this->assertEquals(3, $users[1]->getId());
390
        $this->assertEquals(1, (int)$users[1]->isEdit);
391
        $this->assertEquals(4, $users[2]->getId());
392
        $this->assertEquals(1, (int)$users[2]->isEdit);
393
394
        $groups = $result->getUserGroups();
395
396
        $this->assertEquals(2, $groups[0]->getId());
397
        $this->assertEquals(1, (int)$groups[0]->isEdit);
398
        $this->assertEquals(3, $groups[1]->getId());
399
        $this->assertEquals(0, (int)$groups[1]->isEdit);
400
401
        $accountRequest = new AccountRequest();
402
        $accountRequest->id = 3;
403
404
        self::$service->update($accountRequest);
405
    }
406
407
    /**
408
     * @throws ConstraintException
409
     * @throws \SP\Core\Exceptions\QueryException
410
     */
411
    public function testGetForUser()
412
    {
413
        $this->assertCount(2, self::$service->getForUser());
414
        $this->assertCount(0, self::$service->getForUser(1));
415
    }
416
417
    /**
418
     * @throws ConstraintException
419
     * @throws NoSuchItemException
420
     * @throws \SP\Core\Exceptions\QueryException
421
     */
422
    public function testGetById()
423
    {
424
        $this->expectException(NoSuchItemException::class);
425
426
        self::$service->getById(10);
427
    }
428
429
    /**
430
     * @throws ServiceException
431
     * @throws \SP\Core\Exceptions\SPException
432
     */
433
    public function testDeleteByIdBatch()
434
    {
435
        // Comprobar registros iniciales
436
        $this->assertEquals(2, $this->conn->getRowCount('Account'));
437
438
        self::$service->deleteByIdBatch([1, 2, 100]);
439
440
        // Comprobar registros tras eliminación
441
        $this->assertEquals(0, $this->conn->getRowCount('Account'));
442
443
        $this->expectException(ServiceException::class);
444
445
        self::$service->deleteByIdBatch([100]);
446
    }
447
448
    /**
449
     * @throws ConstraintException
450
     * @throws \SP\Core\Exceptions\QueryException
451
     * @throws \SP\Core\Exceptions\SPException
452
     */
453
    public function testGetByFilter()
454
    {
455
        $searchFilter = new AccountSearchFilter();
456
        $searchFilter->setLimitCount(10);
457
        $searchFilter->setCategoryId(1);
458
459
        // Comprobar un Id de categoría
460
        $result = self::$service->getByFilter($searchFilter);
461
462
        $this->assertCount(1, $result);
463
464
        // Comprobar un Id de categoría no existente
465
        $searchFilter->reset();
466
        $searchFilter->setLimitCount(10);
467
        $searchFilter->setCategoryId(10);
468
469
        $result = self::$service->getByFilter($searchFilter);
470
471
        $this->assertCount(0, $result);
472
473
        // Comprobar un Id de cliente
474
        $searchFilter->reset();
475
        $searchFilter->setLimitCount(10);
476
        $searchFilter->setClientId(1);
477
478
        $result = self::$service->getByFilter($searchFilter);
479
480
        $this->assertCount(1, $result);
481
482
        // Comprobar un Id de cliente no existente
483
        $searchFilter->reset();
484
        $searchFilter->setLimitCount(10);
485
        $searchFilter->setClientId(10);
486
487
        $result = self::$service->getByFilter($searchFilter);
488
489
        $this->assertCount(0, $result);
490
491
        // Comprobar una cadena de texto
492
        $searchFilter->reset();
493
        $searchFilter->setLimitCount(10);
494
        $searchFilter->setCleanTxtSearch('apple.com');
495
496
        $result = self::$service->getByFilter($searchFilter);
497
498
        $this->assertCount(1, $result);
499
        $this->assertEquals(2, $result[0]->getId());
500
501
        // Comprobar los favoritos
502
        $searchFilter->reset();
503
        $searchFilter->setLimitCount(10);
504
        $searchFilter->setSearchFavorites(true);
505
506
        $result = self::$service->getByFilter($searchFilter);
507
508
        $this->assertCount(0, $result);
509
510
        // Comprobar las etiquetas
511
        $searchFilter->reset();
512
        $searchFilter->setLimitCount(10);
513
        $searchFilter->setTagsId([1]);
514
515
        $result = self::$service->getByFilter($searchFilter);
516
517
        $this->assertCount(1, $result);
518
        $this->assertEquals(1, $result[0]->getId());
519
    }
520
521
    /**
522
     * @throws ConstraintException
523
     * @throws \SP\Core\Exceptions\QueryException
524
     */
525
    public function testSearch()
526
    {
527
        // Comprobar búsqueda con el texto Google Inc
528
        $itemSearchData = new ItemSearchData();
529
        $itemSearchData->setSeachString('Google');
530
        $itemSearchData->setLimitCount(10);
531
532
        $result = self::$service->search($itemSearchData);
533
        $data = $result->getDataAsArray();
534
535
        $this->assertCount(1, $data);
536
        $this->assertEquals(1, $result->getNumRows());
537
        $this->assertInstanceOf(\stdClass::class, $data[0]);
538
        $this->assertEquals(1, $data[0]->id);
539
        $this->assertEquals('Google', $data[0]->name);
540
541
        // Comprobar búsqueda con el texto Apple
542
        $itemSearchData = new ItemSearchData();
543
        $itemSearchData->setSeachString('Apple');
544
        $itemSearchData->setLimitCount(1);
545
546
        $result = self::$service->search($itemSearchData);
547
        $data = $result->getDataAsArray();
548
549
        $this->assertCount(1, $data);
550
        $this->assertEquals(1, $result->getNumRows());
551
        $this->assertInstanceOf(\stdClass::class, $data[0]);
552
        $this->assertEquals(2, $data[0]->id);
553
        $this->assertEquals('Apple', $data[0]->name);
554
    }
555
556
    /**
557
     * @throws ConstraintException
558
     * @throws NoSuchItemException
559
     * @throws \SP\Core\Exceptions\QueryException
560
     */
561
    public function testIncrementDecryptCounter()
562
    {
563
        /** @var AccountVData $accountBefore */
564
        $accountBefore = self::$service->getById(1)->getAccountVData();
565
566
        $this->assertTrue(self::$service->incrementDecryptCounter(1));
567
568
        /** @var AccountVData $accountAfter */
569
        $accountAfter = self::$service->getById(1)->getAccountVData();
570
571
        $this->assertEquals($accountBefore->getCountDecrypt() + 1, $accountAfter->getCountDecrypt());
572
    }
573
574
    /**
575
     * @throws ConstraintException
576
     * @throws NoSuchItemException
577
     * @throws \SP\Core\Exceptions\QueryException
578
     */
579
    public function testIncrementViewCounter()
580
    {
581
        /** @var AccountVData $accountBefore */
582
        $accountBefore = self::$service->getById(1)->getAccountVData();
583
584
        $this->assertTrue(self::$service->incrementViewCounter(1));
585
586
        /** @var AccountVData $accountAfter */
587
        $accountAfter = self::$service->getById(1)->getAccountVData();
588
589
        $this->assertEquals($accountBefore->getCountView() + 1, $accountAfter->getCountView());
590
    }
591
592
    /**
593
     * @throws ConstraintException
594
     * @throws NoSuchItemException
595
     * @throws \SP\Core\Exceptions\QueryException
596
     */
597
    public function testGetPasswordHistoryForId()
598
    {
599
        $data = self::$service->getPasswordHistoryForId(3);
600
601
        $this->assertEquals(3, $data->getId());
602
        $this->assertEquals('Google', $data->getName());
603
        $this->assertEquals('admin', $data->getLogin());
604
        $this->assertNull($data->getParentId());
605
        $this->assertEquals(pack('H*', '646566353032303064396362643366376662646536326637663732663861383732623430613839386131643134333933663662623033316664343362366461643762626564643634386437363964346634616234386638336636653236396166623734636261383134313363626162326461393733343934613231653934666331616664633637313732316562356666396562646132613665313937626233333563613632383830393934333863643731333230383132316430366433303838'), $data->getPass());
606
        $this->assertEquals(pack('H*', '6465663130303030646566353032303032636635623034396437656539356531653838663166613438643061616132663133613163663766346238316165663837326134373665316461653661353865316666626438346130383166303062633138646136373265653935643234626564336565303063333262646262303433336633356534323263616337613238363532336233313666316137333462616337343839346631333632643863376430373861373862396135633064396239653061353537626562666336636566623766363166376330393734356461623536373762303436313865343936383434663932666364303634316330303935636239363938336361336631363161623134663339643536636233653938333833613062396464356365383736333334376364363933313563306436343362623937366139383831376632346431303364316533353133306262393862353034353262346334663934663162323531383632356530653331346438343430323362666334306264616265376437386238663632326535353338636537663431626261616461613138646333333662623762636565333030656565333734616537356365303131363731323239383132383964346634383661376635303136303835336138663335653366393230383632386162373332343335633037656432616234'), $data->getKey());
607
        $this->assertEquals(pack('H*', '24327924313024787473754E325055766753482F306D7266426C73624F4163745667436A596371447143364C3354395172614E785A43345258475961'), $data->getMPassHash());
608
609
        $this->expectException(NoSuchItemException::class);
610
611
        self::$service->getPasswordHistoryForId(1);
612
    }
613
614
    /**
615
     * @throws ConstraintException
616
     * @throws \SP\Core\Exceptions\QueryException
617
     */
618
    public function testGetAllBasic()
619
    {
620
        $data = self::$service->getAllBasic();
621
622
        $this->assertCount(2, $data);
623
        $this->assertInstanceOf(AccountData::class, $data[0]);
624
        $this->assertEquals(1, $data[0]->getId());
625
        $this->assertInstanceOf(AccountData::class, $data[1]);
626
        $this->assertEquals(2, $data[1]->getId());
627
    }
628
629
    /**
630
     * @throws \SP\Core\Exceptions\SPException
631
     */
632
    public function testCreateFromHistory()
633
    {
634
        $data = self::$accountHistoryService->getById(7);
635
636
        $this->assertEquals(3, self::$service->createFromHistory($data));
637
638
        $result = self::$service->getById(3);
639
        $resultData = $result->getAccountVData();
640
641
        $this->assertEquals($data->getName(), $resultData->getName());
642
        $this->assertEquals($data->getCategoryId(), $resultData->getCategoryId());
643
        $this->assertEquals($data->getClientId(), $resultData->getClientId());
644
        $this->assertEquals($data->getUrl(), $resultData->getUrl());
645
        $this->assertEquals($data->getLogin(), $resultData->getLogin());
646
        $this->assertEquals($data->getNotes(), $resultData->getNotes());
647
        $this->assertEquals($data->getPassDateChange(), $resultData->getPassDateChange());
648
        $this->assertEquals($data->getUserId(), $resultData->getUserId());
649
        $this->assertEquals($data->getUserGroupId(), $resultData->getUserGroupId());
650
        $this->assertEquals($data->getParentId(), $resultData->getParentId());
651
        $this->assertEquals($data->getIsPrivate(), $resultData->getIsPrivate());
652
        $this->assertEquals($data->getIsPrivateGroup(), $resultData->getIsPrivateGroup());
653
654
        $resultData = self::$service->getPasswordForId(3);
655
656
        $this->assertEquals($data->getPass(), $resultData->getPass());
657
        $this->assertEquals($data->getKey(), $resultData->getKey());
658
    }
659
}
660