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

SyspassImportTest::checkImportedData()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 126
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 83
nc 4
nop 0
dl 0
loc 126
rs 8.3709
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\Import;
26
27
use DI\Container;
28
use SP\Core\Crypt\Crypt;
29
use SP\Services\Account\AccountService;
30
use SP\Services\Category\CategoryService;
31
use SP\Services\Client\ClientService;
32
use SP\Services\Import\FileImport;
33
use SP\Services\Import\ImportException;
34
use SP\Services\Import\ImportParams;
35
use SP\Services\Import\SyspassImport;
36
use SP\Services\Import\XmlFileImport;
37
use SP\Storage\Database\DatabaseConnectionData;
38
use SP\Tests\DatabaseTestCase;
39
use function SP\Tests\setupContext;
40
41
/**
42
 * Class SyspassImportTest
43
 *
44
 * @package SP\Tests\Services\Import
45
 */
46
class SyspassImportTest extends DatabaseTestCase
47
{
48
    /**
49
     * @var Container
50
     */
51
    protected static $dic;
52
53
    /**
54
     * @throws \DI\NotFoundException
55
     * @throws \SP\Core\Context\ContextException
56
     * @throws \DI\DependencyException
57
     */
58
    public static function setUpBeforeClass()
59
    {
60
        self::$dic = setupContext();
61
62
        self::$dataset = 'syspass_import.xml';
63
64
        // Datos de conexión a la BBDD
65
        self::$databaseConnectionData = self::$dic->get(DatabaseConnectionData::class);
66
    }
67
68
    /**
69
     * @throws \DI\DependencyException
70
     * @throws \DI\NotFoundException
71
     * @throws \Defuse\Crypto\Exception\CryptoException
72
     * @throws \SP\Core\Exceptions\ConstraintException
73
     * @throws \SP\Core\Exceptions\QueryException
74
     * @throws \SP\Repositories\NoSuchItemException
75
     * @throws \SP\Services\Import\ImportException
76
     * @throws \SP\Storage\File\FileException
77
     */
78
    public function testDoImport()
79
    {
80
        $file = RESOURCE_DIR . DIRECTORY_SEPARATOR . 'import' . DIRECTORY_SEPARATOR . 'data_syspass.xml';
81
82
        $params = new ImportParams();
83
        $params->setDefaultUser(1);
84
        $params->setDefaultGroup(2);
85
86
        $import = new SyspassImport(self::$dic, new XmlFileImport(FileImport::fromFilesystem($file)), $params);
87
        $import->doImport();
88
89
        $this->checkImportedData();
90
    }
91
92
    /**
93
     * @throws \DI\DependencyException
94
     * @throws \DI\NotFoundException
95
     * @throws \Defuse\Crypto\Exception\CryptoException
96
     * @throws \SP\Core\Exceptions\ConstraintException
97
     * @throws \SP\Core\Exceptions\QueryException
98
     * @throws \SP\Repositories\NoSuchItemException
99
     */
100
    private function checkImportedData()
101
    {
102
        // Checkout categories
103
        $this->assertEquals('CSV Category 1', self::$dic->get(CategoryService::class)->getByName('CSV Category 1')->getName());
104
105
        $this->assertEquals(5, $this->conn->getRowCount('Category'));
106
107
        // Checkout clients
108
        $this->assertEquals('CSV Client 1', self::$dic->get(ClientService::class)->getByName('CSV Client 1')->getName());
109
110
        $this->assertEquals(5, $this->conn->getRowCount('Client'));
111
112
        // Checkout accounts
113
        $accountService = self::$dic->get(AccountService::class);
114
115
        // 1st account
116
        $result = $accountService->getById(3);
117
        $data = $result->getAccountVData();
118
119
        $this->assertEquals(3, $data->getId());
120
        $this->assertEquals(1, $data->getUserId());
121
        $this->assertEquals(2, $data->getUserGroupId());
122
        $this->assertEquals('Google', $data->getName());
123
        $this->assertEquals('Google', $data->getClientName());
124
        $this->assertEquals('Web', $data->getCategoryName());
125
        $this->assertEquals('https://google.com', $data->getUrl());
126
        $this->assertEmpty($data->getNotes());
127
        $this->assertEquals('admin', $data->getLogin());
128
129
        $accountService->withTagsById($result);
130
131
        $expectedTags = [7, 8, 9];
132
        $i = 0;
133
134
        foreach ($result->getTags() as $tag) {
135
            $this->assertEquals($expectedTags[$i], $tag->getId());
136
            $i++;
137
        }
138
139
        $pass = $accountService->getPasswordForId($data->getId());
140
141
        $this->assertEquals('-{?^··\mjC<c', Crypt::decrypt($pass->getPass(), $pass->getKey(), '12345678900'));
142
143
        // 1st account
144
        $result = $accountService->getById(4);
145
        $data = $result->getAccountVData();
146
147
        $this->assertEquals(4, $data->getId());
148
        $this->assertEquals(1, $data->getUserId());
149
        $this->assertEquals(2, $data->getUserGroupId());
150
        $this->assertEquals('Google', $data->getName());
151
        $this->assertEquals('Google', $data->getClientName());
152
        $this->assertEquals('Web', $data->getCategoryName());
153
        $this->assertEquals('https://google.com', $data->getUrl());
154
        $this->assertEquals('blablacar', $data->getNotes());
155
        $this->assertEquals('admin', $data->getLogin());
156
157
        $accountService->withTagsById($result);
158
159
        $expectedTags = [8, 9, 1];
160
        $i = 0;
161
162
        foreach ($result->getTags() as $tag) {
163
            $this->assertEquals($expectedTags[$i], $tag->getId());
164
            $i++;
165
        }
166
167
        $pass = $accountService->getPasswordForId($data->getId());
168
169
        $this->assertEquals('\'ynHRMJy-fRa', Crypt::decrypt($pass->getPass(), $pass->getKey(), '12345678900'));
170
171
        // 1st account
172
        $result = $accountService->getById(5);
173
        $data = $result->getAccountVData();
174
175
        $this->assertEquals(5, $data->getId());
176
        $this->assertEquals(1, $data->getUserId());
177
        $this->assertEquals(2, $data->getUserGroupId());
178
        $this->assertEquals('Test CSV 1', $data->getName());
179
        $this->assertEquals('CSV Client 1', $data->getClientName());
180
        $this->assertEquals('CSV Category 1', $data->getCategoryName());
181
        $this->assertEquals('http://test.me', $data->getUrl());
182
        $this->assertEquals('CSV Notes', $data->getNotes());
183
        $this->assertEquals('csv_login1', $data->getLogin());
184
185
        $pass = $accountService->getPasswordForId($data->getId());
186
187
        $this->assertEquals('csv_pass1', Crypt::decrypt($pass->getPass(), $pass->getKey(), '12345678900'));
188
189
        // 2nd account
190
        $result = $accountService->getById(6);
191
        $data = $result->getAccountVData();
192
193
        $this->assertEquals(6, $data->getId());
194
        $this->assertEquals(1, $data->getUserId());
195
        $this->assertEquals(2, $data->getUserGroupId());
196
        $this->assertEquals('Test CSV 2', $data->getName());
197
        $this->assertEquals('Google', $data->getClientName());
198
        $this->assertEquals('Linux', $data->getCategoryName());
199
        $this->assertEquals('http://linux.org', $data->getUrl());
200
        $this->assertEquals("CSV Notes 2\nbla\nbla\ncar\n", $data->getNotes());
201
        $this->assertEquals('csv_login2', $data->getLogin());
202
203
        $pass = $accountService->getPasswordForId($data->getId());
204
205
        $this->assertEquals('csv_pass2', Crypt::decrypt($pass->getPass(), $pass->getKey(), '12345678900'));
206
207
        // 3rd account
208
        $result = $accountService->getById(7);
209
        $data = $result->getAccountVData();
210
211
        $this->assertEquals(7, $data->getId());
212
        $this->assertEquals(1, $data->getUserId());
213
        $this->assertEquals(2, $data->getUserGroupId());
214
        $this->assertEquals('Test CSV 3', $data->getName());
215
        $this->assertEquals('Apple', $data->getClientName());
216
        $this->assertEquals('SSH', $data->getCategoryName());
217
        $this->assertEquals('http://apple.com', $data->getUrl());
218
        $this->assertEquals('CSV Notes 3', $data->getNotes());
219
        $this->assertEquals('csv_login2', $data->getLogin());
220
221
        $pass = $accountService->getPasswordForId($data->getId());
222
223
        $this->assertEquals('csv_pass3', Crypt::decrypt($pass->getPass(), $pass->getKey(), '12345678900'));
224
225
        $this->assertEquals(7, $this->conn->getRowCount('Account'));
226
    }
227
228
    /**
229
     * @throws ImportException
230
     * @throws \SP\Storage\File\FileException
231
     */
232
    public function testDoImportInvalidData()
233
    {
234
        $file = RESOURCE_DIR . DIRECTORY_SEPARATOR . 'import' . DIRECTORY_SEPARATOR . 'data_syspass_invalid.xml';
235
236
        $params = new ImportParams();
237
        $params->setDefaultUser(1);
238
        $params->setDefaultGroup(1);
239
240
        $import = new SyspassImport(self::$dic, new XmlFileImport(FileImport::fromFilesystem($file)), $params);
241
242
        $this->expectException(ImportException::class);
243
244
        $import->doImport();
245
    }
246
}
247