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

testDeleteEditByAccountId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
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\Repositories;
26
27
use DI\DependencyException;
28
use SP\Core\Exceptions\ConstraintException;
29
use SP\DataModel\ItemData;
30
use SP\Repositories\Account\AccountToUserRepository;
31
use SP\Services\Account\AccountRequest;
32
use SP\Storage\Database\DatabaseConnectionData;
33
use SP\Tests\DatabaseTestCase;
34
use function SP\Tests\setupContext;
35
36
/**
37
 * Class AccountToUserRepositoryTest
38
 *
39
 * Tests de integración para la comprobación de operaciones de usuarios asociados a cuentas
40
 *
41
 * @package SP\Tests
42
 */
43
class AccountToUserRepositoryTest extends DatabaseTestCase
44
{
45
    /**
46
     * @var AccountToUserRepository
47
     */
48
    private static $repository;
49
50
    /**
51
     * @throws DependencyException
52
     * @throws \DI\NotFoundException
53
     * @throws \SP\Core\Context\ContextException
54
     */
55
    public static function setUpBeforeClass()
56
    {
57
        $dic = setupContext();
58
59
        self::$dataset = 'syspass.xml';
60
61
        // Datos de conexión a la BBDD
62
        self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
63
64
        // Inicializar el repositorio
65
        self::$repository = $dic->get(AccountToUserRepository::class);
66
    }
67
68
    /**
69
     * Comprobar la obtención de usuarios por Id de cuenta
70
     *
71
     * @throws ConstraintException
72
     * @throws \SP\Core\Exceptions\QueryException
73
     */
74
    public function testGetUsersByAccountId()
75
    {
76
        $users = self::$repository->getUsersByAccountId(1);
77
78
        $this->assertCount(1, $users);
79
        $this->assertInstanceOf(ItemData::class, $users[0]);
80
81
        $usersView = array_filter($users, function ($user) {
82
            return (int)$user->isEdit === 0;
83
        });
84
85
        $this->assertCount(0, $usersView);
86
87
        $usersEdit = array_filter($users, function ($user) {
88
            return (int)$user->isEdit === 1;
89
        });
90
91
        $this->assertCount(1, $usersEdit);
92
93
        $users = self::$repository->getUsersByAccountId(2);
94
95
        $this->assertCount(1, $users);
96
        $this->assertInstanceOf(ItemData::class, $users[0]);
97
98
        $usersView = array_filter($users, function ($user) {
99
            return (int)$user->isEdit === 0;
100
        });
101
102
        $this->assertCount(1, $usersView);
103
104
        $usersEdit = array_filter($users, function ($user) {
105
            return (int)$user->isEdit === 1;
106
        });
107
108
        $this->assertCount(0, $usersEdit);
109
110
        $users = self::$repository->getUsersByAccountId(3);
111
112
        $this->assertCount(0, $users);
113
    }
114
115
    /**
116
     * Comprobar la actualización de usuarios por Id de cuenta
117
     *
118
     * @throws \SP\Core\Exceptions\ConstraintException
119
     * @throws \SP\Core\Exceptions\QueryException
120
     */
121
    public function testUpdate()
122
    {
123
        $accountRequest = new AccountRequest();
124
        $accountRequest->id = 1;
125
        $accountRequest->usersView = [1, 2, 3];
126
127
        self::$repository->update($accountRequest);
128
129
        $users = self::$repository->getUsersByAccountId($accountRequest->id);
130
131
        $this->assertCount(3, $users);
132
        $this->assertInstanceOf(ItemData::class, $users[0]);
133
        $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...
134
        $this->assertInstanceOf(ItemData::class, $users[1]);
135
        $this->assertEquals(0, (int)$users[1]->isEdit);
136
        $this->assertInstanceOf(ItemData::class, $users[2]);
137
        $this->assertEquals(0, (int)$users[2]->isEdit);
138
139
        $this->expectException(ConstraintException::class);
140
141
        $accountRequest->usersView = [10];
142
143
        self::$repository->update($accountRequest);
144
145
        $accountRequest->id = 3;
146
        $accountRequest->usersView = [1, 2, 3];
147
148
        self::$repository->update($accountRequest);
149
    }
150
151
    /**
152
     * Comprobar la actualización de usuarios con permisos de modificación por Id de cuenta
153
     *
154
     * @throws \SP\Core\Exceptions\ConstraintException
155
     * @throws \SP\Core\Exceptions\QueryException
156
     */
157
    public function testUpdateEdit()
158
    {
159
        $accountRequest = new AccountRequest();
160
        $accountRequest->id = 2;
161
        $accountRequest->usersEdit = [2, 3];
162
163
        self::$repository->updateEdit($accountRequest);
164
165
        $users = self::$repository->getUsersByAccountId($accountRequest->id);
166
167
        $this->assertCount(2, $users);
168
        $this->assertInstanceOf(ItemData::class, $users[0]);
169
        $this->assertEquals(1, (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...
170
        $this->assertInstanceOf(ItemData::class, $users[1]);
171
        $this->assertEquals(1, (int)$users[1]->isEdit);
172
173
        $this->expectException(ConstraintException::class);
174
175
        // Comprobar que se lanza excepción al añadir usuarios no existentes
176
        $accountRequest->usersEdit = [10];
177
178
        self::$repository->updateEdit($accountRequest);
179
180
        // Comprobar que se lanza excepción al añadir usuarios a cuenta no existente
181
        $accountRequest->id = 3;
182
        $accountRequest->usersEdit = [2, 3];
183
184
        self::$repository->updateEdit($accountRequest);
185
    }
186
187
    /**
188
     * Comprobar la eliminación de usuarios por Id de cuenta
189
     *
190
     * @throws \SP\Core\Exceptions\ConstraintException
191
     * @throws \SP\Core\Exceptions\QueryException
192
     */
193
    public function testDeleteByAccountId()
194
    {
195
        $this->assertEquals(1, self::$repository->deleteByAccountId(1));
196
        $this->assertCount(0, self::$repository->getUsersByAccountId(1));
197
198
        $this->assertEquals(0, self::$repository->deleteByAccountId(10));
199
200
        $this->assertEquals(1, $this->conn->getRowCount('AccountToUser'));
201
    }
202
203
    /**
204
     * Comprobar la insercción de usuarios con permisos de modificación por Id de cuenta
205
     *
206
     * @throws \SP\Core\Exceptions\ConstraintException
207
     * @throws \SP\Core\Exceptions\QueryException
208
     */
209
    public function testAddEdit()
210
    {
211
        $accountRequest = new AccountRequest();
212
        $accountRequest->id = 2;
213
        $accountRequest->usersEdit = [1, 2, 3];
214
215
        self::$repository->addEdit($accountRequest);
216
217
        $users = self::$repository->getUsersByAccountId($accountRequest->id);
218
219
        $this->assertCount(3, $users);
220
        $this->assertInstanceOf(ItemData::class, $users[0]);
221
        $this->assertInstanceOf(ItemData::class, $users[1]);
222
        $this->assertInstanceOf(ItemData::class, $users[2]);
223
224
        $this->expectException(ConstraintException::class);
225
226
        // Comprobar que se lanza excepción al añadir usuarios no existentes
227
        $accountRequest->usersEdit = [10];
228
229
        self::$repository->addEdit($accountRequest);
230
231
        // Comprobar que se lanza excepción al añadir usuarios a cuenta no existente
232
        $accountRequest->id = 3;
233
        $accountRequest->usersEdit = [1, 2, 3];
234
235
        self::$repository->addEdit($accountRequest);
236
    }
237
238
    /**
239
     * Comprobar la insercción de usuarios por Id de cuenta
240
     *
241
     * @throws \SP\Core\Exceptions\ConstraintException
242
     * @throws \SP\Core\Exceptions\QueryException
243
     */
244
    public function testAdd()
245
    {
246
        $accountRequest = new AccountRequest();
247
        $accountRequest->id = 2;
248
        $accountRequest->usersView = [1, 2, 3];
249
250
        self::$repository->add($accountRequest);
251
252
        $users = self::$repository->getUsersByAccountId($accountRequest->id);
253
254
        $this->assertCount(3, $users);
255
        $this->assertInstanceOf(ItemData::class, $users[0]);
256
        $this->assertInstanceOf(ItemData::class, $users[1]);
257
        $this->assertInstanceOf(ItemData::class, $users[2]);
258
259
        $this->expectException(ConstraintException::class);
260
261
        // Comprobar que se lanza excepción al añadir usuarios no existentes
262
        $accountRequest->usersView = [10];
263
264
        self::$repository->add($accountRequest);
265
266
        // Comprobar que se lanza excepción al añadir usuarios a cuenta no existente
267
        $accountRequest->id = 3;
268
        $accountRequest->usersView = [1, 2, 3];
269
270
        self::$repository->add($accountRequest);
271
    }
272
273
    /**
274
     * Comprobar la eliminación de usuarios con permisos de modificación por Id de cuenta
275
     *
276
     * @throws \SP\Core\Exceptions\ConstraintException
277
     * @throws \SP\Core\Exceptions\QueryException
278
     */
279
    public function testDeleteEditByAccountId()
280
    {
281
        $this->assertEquals(1, self::$repository->deleteEditByAccountId(1));
282
        $this->assertCount(0, self::$repository->getUsersByAccountId(1));
283
284
        $this->assertEquals(0, self::$repository->deleteEditByAccountId(10));
285
286
        $this->assertEquals(1, $this->conn->getRowCount('AccountToUser'));
287
    }
288
}
289