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

AccountSearchServiceTest::checkString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 0
loc 20
rs 9.9
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\Account;
26
27
use SP\Core\Context\ContextInterface;
28
use SP\DataModel\UserPreferencesData;
29
use SP\Mvc\Model\QueryCondition;
30
use SP\Services\Account\AccountSearchFilter;
31
use SP\Services\Account\AccountSearchService;
32
use SP\Services\User\UserLoginResponse;
33
use SP\Storage\Database\DatabaseConnectionData;
34
use SP\Storage\Database\QueryResult;
35
use SP\Tests\DatabaseTestCase;
36
use function SP\Tests\setupContext;
37
38
/**
39
 * Class AccountSearchServiceTest
40
 *
41
 * @package SP\Tests\Services
42
 */
43
class AccountSearchServiceTest extends DatabaseTestCase
44
{
45
    /**
46
     * @var AccountSearchService
47
     */
48
    private static $service;
49
    /**
50
     * @var \Closure
51
     */
52
    private static $setupUser;
53
54
    /**
55
     * @throws \DI\NotFoundException
56
     * @throws \SP\Core\Context\ContextException
57
     * @throws \DI\DependencyException
58
     */
59
    public static function setUpBeforeClass()
60
    {
61
        $dic = setupContext();
62
63
        self::$dataset = 'syspass_accountSearch.xml';
64
65
        // Datos de conexión a la BBDD
66
        self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
67
68
        // Inicializar el servicio
69
        self::$service = $dic->get(AccountSearchService::class);
70
71
        $context = $dic->get(ContextInterface::class);
72
73
        self::$setupUser = function (UserLoginResponse $response) use ($context) {
74
            $response->setLastUpdate(time());
75
76
            $context->setUserData($response);
77
        };
78
    }
79
80
    /**
81
     * @throws \SP\Core\Exceptions\ConstraintException
82
     * @throws \SP\Core\Exceptions\QueryException
83
     * @throws \SP\Core\Exceptions\SPException
84
     */
85
    public function testProcessSearchResultsForUserAdmin()
86
    {
87
        $userData = new UserLoginResponse();
88
        $userData->setId(1);
89
        $userData->setUserGroupId(1);
90
        $userData->setIsAdminApp(1);
91
        $userData->setPreferences(new UserPreferencesData());
92
93
        self::$setupUser->call($this, $userData);
94
95
        $this->checkCategoryById(1, [1]);
96
        $this->checkNonExistantCategory();
97
        $this->checkClientById(1, [1]);
98
        $this->checkClientById(2, [2]);
99
        $this->checkClientAndCategory(2, 2, [2]);
100
        $this->checkClientAndCategory(2, 1, [2, 1], QueryCondition::CONDITION_OR);
101
        $this->checkNonExistantClient();
102
        $this->checkString('apple.com', [2]);
103
        $this->checkString('aaaa', [1]);
104
        $this->checkString('github');
105
        $this->checkString('google', [1]);
106
        $this->checkString('slack');
107
        $this->checkString('is:private');
108
        $this->checkString('not:private', [2, 1]);
109
        $this->checkString('user:admin', [2, 1]);
110
        $this->checkString('user:user_a', [2, 1]);
111
        $this->checkString('owner:user_a');
112
        $this->checkString('owner:user_b');
113
        $this->checkString('group:Admins', [2, 1]);
114
        $this->checkString('group:Usuarios', [2]);
115
        $this->checkString('maingroup:Admins', [2, 1]);
116
        $this->checkString('maingroup:Usuarios');
117
        $this->checkString('file:"Clock 3"', [2]);
118
        $this->checkString('file:"syspass"', [1]);
119
        $this->checkString('id:1', [1]);
120
        $this->checkString('id:3');
121
        $this->checkFavorites(1, [1]);
122
        $this->checkTags([1, 3], [2]);
123
        $this->checkTags([1, 3], [2, 1], QueryCondition::CONDITION_OR);
124
        $this->checkTags([2], [1]);
125
    }
126
127
    /**
128
     * @param int   $id Category Id
129
     * @param array $accountsId
130
     *
131
     * @throws \SP\Core\Exceptions\ConstraintException
132
     * @throws \SP\Core\Exceptions\QueryException
133
     * @throws \SP\Core\Exceptions\SPException
134
     */
135
    private function checkCategoryById($id, array $accountsId = [])
136
    {
137
        $rows = count($accountsId);
138
139
        $searchFilter = new \SP\Services\Account\AccountSearchFilter();
140
        $searchFilter->setLimitCount(10);
141
        $searchFilter->setCategoryId($id);
142
143
        // Comprobar un Id de categoría
144
        $result = self::$service->processSearchResults($searchFilter);
145
        $this->assertInstanceOf(QueryResult::class, $result);
146
147
        if ($rows > 0) {
148
            /** @var \SP\Services\Account\AccountSearchItem[] $data */
149
            $data = $result->getDataAsArray();
150
151
            $i = 0;
152
153
            foreach ($data as $searchItem) {
154
                $this->assertEquals($accountsId[$i], $searchItem->getAccountSearchVData()->getId());
155
                $i++;
156
            }
157
        }
158
    }
159
160
    /**
161
     * @throws \SP\Core\Exceptions\ConstraintException
162
     * @throws \SP\Core\Exceptions\QueryException
163
     * @throws \SP\Core\Exceptions\SPException
164
     */
165
    private function checkNonExistantCategory()
166
    {
167
        $searchFilter = new AccountSearchFilter();
168
        $searchFilter->setLimitCount(10);
169
        $searchFilter->setCategoryId(10);
170
171
        $result = self::$service->processSearchResults($searchFilter);
172
        $this->assertInstanceOf(QueryResult::class, $result);
173
        $this->assertEquals(0, $result->getNumRows());
174
        $this->assertCount(0, $result->getDataAsArray());
175
    }
176
177
    /**
178
     * @param int   $id Client Id
179
     * @param array $accountsId
180
     *
181
     * @throws \SP\Core\Exceptions\ConstraintException
182
     * @throws \SP\Core\Exceptions\QueryException
183
     * @throws \SP\Core\Exceptions\SPException
184
     */
185
    private function checkClientById($id, array $accountsId = [])
186
    {
187
        $rows = count($accountsId);
188
189
        $searchFilter = new \SP\Services\Account\AccountSearchFilter();
190
        $searchFilter->setLimitCount(10);
191
        $searchFilter->setClientId($id);
192
193
        $result = self::$service->processSearchResults($searchFilter);
194
        $this->assertInstanceOf(QueryResult::class, $result);
195
        $this->assertEquals($rows, $result->getNumRows());
196
197
        if ($rows > 0) {
198
            /** @var \SP\Services\Account\AccountSearchItem[] $data */
199
            $data = $result->getDataAsArray();
200
201
            $i = 0;
202
203
            foreach ($data as $searchItem) {
204
                $this->assertEquals($accountsId[$i], $searchItem->getAccountSearchVData()->getId());
205
                $i++;
206
            }
207
        }
208
    }
209
210
    /**
211
     * @param int    $clientId
212
     * @param int    $categoryId
213
     * @param array  $accountsId
214
     * @param string $operator
215
     *
216
     * @throws \SP\Core\Exceptions\ConstraintException
217
     * @throws \SP\Core\Exceptions\QueryException
218
     * @throws \SP\Core\Exceptions\SPException
219
     */
220
    private function checkClientAndCategory($clientId, $categoryId, array $accountsId = [], $operator = null)
221
    {
222
        $rows = count($accountsId);
223
224
        $searchFilter = new \SP\Services\Account\AccountSearchFilter();
225
        $searchFilter->setLimitCount(10);
226
        $searchFilter->setFilterOperator($operator);
227
        $searchFilter->setClientId($clientId);
228
        $searchFilter->setCategoryId($categoryId);
229
230
        $result = self::$service->processSearchResults($searchFilter);
231
        $this->assertInstanceOf(QueryResult::class, $result);
232
        $this->assertEquals($rows, $result->getNumRows());
233
234
        $i = 0;
235
236
        /** @var \SP\Services\Account\AccountSearchItem $item */
237
        foreach ($result->getDataAsArray() as $item) {
238
            $this->assertEquals($accountsId[$i], $item->getAccountSearchVData()->getId());
239
            $i++;
240
        }
241
    }
242
243
    /**
244
     * @throws \SP\Core\Exceptions\ConstraintException
245
     * @throws \SP\Core\Exceptions\QueryException
246
     * @throws \SP\Core\Exceptions\SPException
247
     */
248
    private function checkNonExistantClient()
249
    {
250
        $searchFilter = new \SP\Services\Account\AccountSearchFilter();
251
        $searchFilter->setLimitCount(10);
252
        $searchFilter->setClientId(10);
253
254
        $result = self::$service->processSearchResults($searchFilter);
255
        $this->assertInstanceOf(QueryResult::class, $result);
256
        $this->assertEquals(0, $result->getNumRows());
257
        $this->assertCount(0, $result->getDataAsArray());
258
    }
259
260
    /**
261
     * @param string $string
262
     * @param array  $accountsId
263
     *
264
     * @throws \SP\Core\Exceptions\ConstraintException
265
     * @throws \SP\Core\Exceptions\QueryException
266
     * @throws \SP\Core\Exceptions\SPException
267
     */
268
    private function checkString($string, array $accountsId = [])
269
    {
270
        $rows = count($accountsId);
271
272
        $searchFilter = new \SP\Services\Account\AccountSearchFilter();
273
        $searchFilter->setLimitCount(10);
274
        $searchFilter->setTxtSearch($string);
275
276
        $result = self::$service->processSearchResults($searchFilter);
277
        $this->assertInstanceOf(QueryResult::class, $result);
278
279
        $this->assertEquals($rows, $result->getNumRows());
280
281
        $i = 0;
282
283
        /** @var \SP\Services\Account\AccountSearchItem $item */
284
        foreach ($result->getDataAsArray() as $item) {
285
            $this->assertEquals($accountsId[$i], $item->getAccountSearchVData()->getId());
286
287
            $i++;
288
        }
289
    }
290
291
    /**
292
     * @param int   $rows Expected rows
293
     * @param array $accountsId
294
     *
295
     * @throws \SP\Core\Exceptions\ConstraintException
296
     * @throws \SP\Core\Exceptions\QueryException
297
     * @throws \SP\Core\Exceptions\SPException
298
     */
299
    private function checkFavorites($rows, array $accountsId = [])
300
    {
301
        $searchFilter = new \SP\Services\Account\AccountSearchFilter();
302
        $searchFilter->setLimitCount(10);
303
        $searchFilter->setSearchFavorites(true);
304
305
        $result = self::$service->processSearchResults($searchFilter);
306
307
        $this->assertInstanceOf(QueryResult::class, $result);
308
        $this->assertEquals($rows, $result->getNumRows());
309
310
        $i = 0;
311
312
        /** @var \SP\Services\Account\AccountSearchItem $item */
313
        foreach ($result->getDataAsArray() as $item) {
314
            $this->assertEquals($accountsId[$i], $item->getAccountSearchVData()->getId());
315
            $i++;
316
        }
317
    }
318
319
    /**
320
     * @param array  $tagsId
321
     * @param array  $accountsId
322
     * @param string $operator
323
     *
324
     * @throws \SP\Core\Exceptions\ConstraintException
325
     * @throws \SP\Core\Exceptions\QueryException
326
     * @throws \SP\Core\Exceptions\SPException
327
     */
328
    private function checkTags(array $tagsId, array $accountsId = [], $operator = null)
329
    {
330
        $rows = count($accountsId);
331
332
        $searchFilter = new \SP\Services\Account\AccountSearchFilter();
333
        $searchFilter->setLimitCount(10);
334
        $searchFilter->setFilterOperator($operator);
335
        $searchFilter->setTagsId($tagsId);
336
337
        $result = self::$service->processSearchResults($searchFilter);
338
        $this->assertInstanceOf(QueryResult::class, $result);
339
340
        /** @var \SP\Services\Account\AccountSearchItem[] $data */
341
        $data = $result->getDataAsArray();
342
343
        $this->assertEquals($rows, $result->getNumRows());
344
        $this->assertCount($rows, $data);
345
346
        $i = 0;
347
348
        foreach ($data as $item) {
349
            $this->assertEquals($accountsId[$i], $item->getAccountSearchVData()->getId());
350
            $i++;
351
        }
352
    }
353
354
    /**
355
     * @throws \SP\Core\Exceptions\ConstraintException
356
     * @throws \SP\Core\Exceptions\QueryException
357
     * @throws \SP\Core\Exceptions\SPException
358
     */
359
    public function testProcessSearchResultsForUserDemo()
360
    {
361
        \SP\Services\Account\AccountSearchItem::$publicLinkEnabled = false;
362
363
        $userData = new UserLoginResponse();
364
        $userData->setId(2);
365
        $userData->setUserGroupId(2);
366
        $userData->setPreferences(new UserPreferencesData());
367
368
        self::$setupUser->call($this, $userData);
369
370
        $this->checkCategoryById(1, [1]);
371
        $this->checkNonExistantCategory();
372
        $this->checkClientById(1, [1]);
373
        $this->checkClientById(2, [2]);
374
        $this->checkClientAndCategory(2, 2, [2]);
375
        $this->checkClientAndCategory(2, 1, [2, 1], QueryCondition::CONDITION_OR);
376
        $this->checkNonExistantClient();
377
        $this->checkString('apple.com', [2]);
378
        $this->checkString('github');
379
        $this->checkString('google', [1]);
380
        $this->checkString('slack');
381
        $this->checkString('is:private');
382
        $this->checkString('not:private', [2, 1]);
383
        $this->checkString('user:admin', [2, 1]);
384
        $this->checkString('user:user_a', [2, 1]);
385
        $this->checkString('owner:user_a');
386
        $this->checkString('owner:user_b');
387
        $this->checkString('group:Admins', [2, 1]);
388
        $this->checkString('group:Usuarios', [2]);
389
        $this->checkString('maingroup:Admins', [2, 1]);
390
        $this->checkString('maingroup:Usuarios');
391
        $this->checkString('file:"Clock 3"', [2]);
392
        $this->checkString('file:"syspass"', [1]);
393
        $this->checkString('id:1', [1]);
394
        $this->checkString('id:3');
395
        $this->checkFavorites(1, [2]);
396
        $this->checkTags([1, 3], [2]);
397
        $this->checkTags([1, 3], [2, 1], QueryCondition::CONDITION_OR);
398
        $this->checkTags([2], [1]);
399
    }
400
401
    /**
402
     * @throws \SP\Core\Exceptions\ConstraintException
403
     * @throws \SP\Core\Exceptions\QueryException
404
     * @throws \SP\Core\Exceptions\SPException
405
     */
406
    public function testProcessSearchResultsForUserA()
407
    {
408
        \SP\Services\Account\AccountSearchItem::$publicLinkEnabled = false;
409
410
        $userData = new UserLoginResponse();
411
        $userData->setId(3);
412
        $userData->setUserGroupId(3);
413
        $userData->setPreferences(new UserPreferencesData());
414
415
        self::$setupUser->call($this, $userData);
416
417
        $this->checkCategoryById(1, [1]);
418
        $this->checkNonExistantCategory();
419
        $this->checkClientById(1, [1]);
420
        $this->checkClientById(2, [2, 3]);
421
        $this->checkClientAndCategory(2, 2, [2, 3]);
422
        $this->checkClientAndCategory(2, 1, [2, 3, 1], QueryCondition::CONDITION_OR);
423
        $this->checkNonExistantClient();
424
        $this->checkString('apple.com', [2]);
425
        $this->checkString('github', [3]);
426
        $this->checkString('google', [1]);
427
        $this->checkString('slack', [4]);
428
        $this->checkString('is:private', [3, 4]);
429
        $this->checkString('user:admin', [2, 1]);
430
        $this->checkString('user:user_a', [2, 3, 1, 4]);
431
        $this->checkString('owner:user_a', [3, 4]);
432
        $this->checkString('owner:user_b');
433
        $this->checkString('group:Admins', [2, 1]);
434
        $this->checkString('group:Usuarios', [2, 3, 4]);
435
        $this->checkString('maingroup:Admins', [2, 1]);
436
        $this->checkString('maingroup:Usuarios', [3, 4]);
437
        $this->checkString('file:"Clock 3"', [2]);
438
        $this->checkString('file:"syspass"', [1]);
439
        $this->checkString('id:1', [1]);
440
        $this->checkString('id:3', [3]);
441
        $this->checkFavorites(2, [2, 1]);
442
        $this->checkTags([1, 3], [2]);
443
        $this->checkTags([1, 3], [2, 1], QueryCondition::CONDITION_OR);
444
        $this->checkTags([2], [1]);
445
    }
446
447
    /**
448
     * @throws \SP\Core\Exceptions\ConstraintException
449
     * @throws \SP\Core\Exceptions\QueryException
450
     * @throws \SP\Core\Exceptions\SPException
451
     */
452
    public function testProcessSearchResultsForUserB()
453
    {
454
        \SP\Services\Account\AccountSearchItem::$publicLinkEnabled = false;
455
456
        $userData = new UserLoginResponse();
457
        $userData->setId(4);
458
        $userData->setUserGroupId(3);
459
        $userData->setPreferences(new UserPreferencesData());
460
461
        self::$setupUser->call($this, $userData);
462
463
        $this->checkCategoryById(1);
464
        $this->checkNonExistantCategory();
465
        $this->checkClientById(1);
466
        $this->checkClientById(2, [2]);
467
        $this->checkClientAndCategory(2, 2, [2]);
468
        $this->checkClientAndCategory(2, 1, [2], QueryCondition::CONDITION_OR);
469
        $this->checkNonExistantClient();
470
        $this->checkString('apple.com', [2]);
471
        $this->checkString('github');
472
        $this->checkString('google');
473
        $this->checkString('slack', [4]);
474
        $this->checkString('is:private', [4]);
475
        $this->checkString('not:private', [2]);
476
        $this->checkString('user:admin', [2]);
477
        $this->checkString('user:user_a', [2, 4]);
478
        $this->checkString('owner:user_a', [4]);
479
        $this->checkString('owner:user_b');
480
        $this->checkString('group:Admins', [2]);
481
        $this->checkString('group:Usuarios', [2, 4]);
482
        $this->checkString('maingroup:Admins', [2]);
483
        $this->checkString('maingroup:Usuarios', [4]);
484
        $this->checkString('file:"Clock 3"', [2]);
485
        $this->checkString('file:"syspass"');
486
        $this->checkString('id:1');
487
        $this->checkString('id:3');
488
        $this->checkFavorites(0);
489
        $this->checkTags([1, 3], [2]);
490
        $this->checkTags([1, 3], [2], QueryCondition::CONDITION_OR);
491
        $this->checkTags([2]);
492
    }
493
}
494