Passed
Push — master ( e0e6ef...9b5d15 )
by Maurício
08:18
created

GitTest::testCheckGitRevisionPacksFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 65
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 48
nc 2
nop 0
dl 0
loc 65
rs 9.1344
c 1
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
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Tests;
6
7
use PhpMyAdmin\Git;
8
9
use function file_put_contents;
10
use function mkdir;
11
use function mt_getrandmax;
12
use function random_int;
13
use function rmdir;
14
use function sys_get_temp_dir;
15
use function unlink;
16
17
use const DIRECTORY_SEPARATOR;
18
19
/**
20
 * @covers \PhpMyAdmin\Git
21
 * @group git-revision
22
 */
23
class GitTest extends AbstractTestCase
24
{
25
    protected Git $object;
26
27
    protected string $testDir;
28
29
    /**
30
     * Sets up the fixture, for example, opens a network connection.
31
     * This method is called before a test is executed.
32
     */
33
    protected function setUp(): void
34
    {
35
        parent::setUp();
36
37
        parent::setProxySettings();
38
39
        $this->testDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR
40
                        . 'gittempdir_' . random_int(0, mt_getrandmax()) . DIRECTORY_SEPARATOR;
41
        $this->object = new Git(true, $this->testDir);
42
43
        unset($_SESSION['git_location']);
44
        unset($_SESSION['is_git_revision']);
45
        mkdir($this->testDir);
46
    }
47
48
    /**
49
     * Tears down the fixture, for example, closes a network connection.
50
     * This method is called after a test is executed.
51
     */
52
    protected function tearDown(): void
53
    {
54
        rmdir($this->testDir);
55
56
        parent::tearDown();
57
58
        unset($this->object);
59
    }
60
61
    /**
62
     * Test for isGitRevision
63
     */
64
    public function testIsGitRevision(): void
65
    {
66
        $_SESSION['git_location'] = '.cachedgitlocation';
67
        $_SESSION['is_git_revision'] = true;
68
69
        $gitLocation = '';
70
71
        $this->assertTrue($this->object->isGitRevision($gitLocation));
72
73
        $this->assertFalse($this->object->hasGitInformation());
74
75
        $this->assertEquals('.cachedgitlocation', $gitLocation);
76
    }
77
78
    /**
79
     * Test for isGitRevision
80
     */
81
    public function testIsGitRevisionSkipped(): void
82
    {
83
        $this->object = new Git(false);
84
        $this->assertFalse(
85
            $this->object->isGitRevision($gitLocation),
86
        );
87
    }
88
89
    /**
90
     * Test for isGitRevision
91
     *
92
     * @group git-revision
93
     */
94
    public function testIsGitRevisionLocalGitDir(): void
95
    {
96
        $this->assertFalse(
97
            $this->object->isGitRevision(),
98
        );
99
100
        $this->assertFalse($this->object->hasGitInformation());
101
102
        unset($_SESSION['git_location']);
103
        unset($_SESSION['is_git_revision']);
104
105
        mkdir($this->testDir . '.git');
106
107
        $this->assertFalse(
108
            $this->object->isGitRevision(),
109
        );
110
111
        $this->assertFalse($this->object->hasGitInformation());
112
113
        unset($_SESSION['git_location']);
114
        unset($_SESSION['is_git_revision']);
115
116
        file_put_contents($this->testDir . '.git/config', '');
117
118
        $this->assertTrue($this->object->isGitRevision());
119
120
        $this->assertFalse($this->object->hasGitInformation());
121
122
        unlink($this->testDir . '.git/config');
123
        rmdir($this->testDir . '.git');
124
    }
125
126
    /**
127
     * Test for isGitRevision
128
     *
129
     * @group git-revision
130
     */
131
    public function testIsGitRevisionExternalGitDir(): void
132
    {
133
        file_put_contents($this->testDir . '.git', 'gitdir: ' . $this->testDir . '.customgitdir');
134
        $this->assertFalse(
135
            $this->object->isGitRevision(),
136
        );
137
138
        $this->assertFalse($this->object->hasGitInformation());
139
140
        unset($_SESSION['git_location']);
141
        unset($_SESSION['is_git_revision']);
142
143
        mkdir($this->testDir . '.customgitdir');
144
145
        $this->assertTrue($this->object->isGitRevision());
146
147
        $this->assertFalse($this->object->hasGitInformation());
148
149
        unset($_SESSION['git_location']);
150
        unset($_SESSION['is_git_revision']);
151
152
        file_put_contents($this->testDir . '.git', 'random data here');
153
154
        $this->assertFalse(
155
            $this->object->isGitRevision(),
156
        );
157
158
        $this->assertFalse($this->object->hasGitInformation());
159
160
        unlink($this->testDir . '.git');
161
        rmdir($this->testDir . '.customgitdir');
162
    }
163
164
    /**
165
     * Test for checkGitRevision packs folder
166
     *
167
     * @group git-revision
168
     */
169
    public function testCheckGitRevisionPacksFolder(): void
170
    {
171
        mkdir($this->testDir . '.git');
172
        file_put_contents($this->testDir . '.git/config', '');
173
174
        $commit = $this->object->checkGitRevision();
175
176
        $this->assertNull($commit);
177
        $this->assertFalse($this->object->hasGitInformation());
178
179
        file_put_contents($this->testDir . '.git/HEAD', 'ref: refs/remotes/origin/master');
180
181
        $commit = $this->object->checkGitRevision();
182
183
        $this->assertNull($commit);
184
185
        file_put_contents(
186
            $this->testDir . '.git/packed-refs',
187
            '# pack-refs with: peeled fully-peeled sorted' . "\n" .
188
            'c1f2ff2eb0c3fda741f859913fd589379f4e4a8f refs/tags/4.3.10' . "\n" .
189
            '^6f2e60343b0a324c65f2d1411bf4bd03e114fb98' . "\n" .
190
            '8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e refs/remotes/origin/master' . "\n",
191
        );
192
        mkdir($this->testDir . '.git/objects/pack', 0777, true);//default = 0777, recursive mode
193
194
        $commit = $this->object->checkGitRevision();
195
196
        if (
197
            $commit === null
198
            && ! isset($_SESSION['PMA_VERSION_REMOTECOMMIT_8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e'])
199
        ) {
200
            $this->markTestSkipped('Unable to get remote commit information.');
201
        }
202
203
        $this->assertIsArray($commit);
204
        $this->assertSame('8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e', $commit['hash']);
205
        $this->assertSame('master', $commit['branch']);
206
        $this->assertSame(
207
            'Update po files' . "\n\n" . '[ci skip]' . "\n\n" . 'Signed-off-by: phpMyAdmin bot <[email protected]>',
208
            $commit['message'],
209
        );
210
        $this->assertTrue($commit['is_remote_commit']);
211
        $this->assertTrue($commit['is_remote_branch']);
212
        $this->assertSame('phpMyAdmin bot', $commit['author']['name']);
213
        $this->assertSame('[email protected]', $commit['author']['email']);
214
        $this->assertSame('2023-05-14T00:19:49Z', $commit['author']['date']);
215
        $this->assertSame('phpMyAdmin bot', $commit['committer']['name']);
216
        $this->assertSame('[email protected]', $commit['committer']['email']);
217
        $this->assertSame('2023-05-14T00:19:49Z', $commit['committer']['date']);
218
219
        rmdir($this->testDir . '.git/objects/pack');
220
        rmdir($this->testDir . '.git/objects');
221
        unlink($this->testDir . '.git/packed-refs');
222
        unlink($this->testDir . '.git/HEAD');
223
        unlink($this->testDir . '.git/config');
224
        rmdir($this->testDir . '.git');
225
    }
226
227
    /**
228
     * Test for checkGitRevision packs folder
229
     *
230
     * @group git-revision
231
     */
232
    public function testCheckGitRevisionRefFile(): void
233
    {
234
        mkdir($this->testDir . '.git');
235
        file_put_contents($this->testDir . '.git/config', '');
236
237
        $commit = $this->object->checkGitRevision();
238
239
        $this->assertNull($commit);
240
        $this->assertFalse($this->object->hasGitInformation());
241
242
        file_put_contents($this->testDir . '.git/HEAD', 'ref: refs/remotes/origin/master');
243
        mkdir($this->testDir . '.git/refs/remotes/origin', 0777, true);
244
        file_put_contents(
245
            $this->testDir . '.git/refs/remotes/origin/master',
246
            'c1f2ff2eb0c3fda741f859913fd589379f4e4a8f',
247
        );
248
        mkdir($this->testDir . '.git/objects/pack', 0777, true);//default = 0777, recursive mode
249
        $commit = $this->object->checkGitRevision();
250
251
        $this->assertNull($commit);
252
        $this->assertFalse($this->object->hasGitInformation());
253
254
        unlink($this->testDir . '.git/refs/remotes/origin/master');
255
        rmdir($this->testDir . '.git/refs/remotes/origin');
256
        rmdir($this->testDir . '.git/refs/remotes');
257
        rmdir($this->testDir . '.git/refs');
258
        rmdir($this->testDir . '.git/objects/pack');
259
        rmdir($this->testDir . '.git/objects');
260
        unlink($this->testDir . '.git/HEAD');
261
        unlink($this->testDir . '.git/config');
262
        rmdir($this->testDir . '.git');
263
    }
264
265
    /**
266
     * Test for checkGitRevision with packs as file
267
     *
268
     * @group git-revision
269
     */
270
    public function testCheckGitRevisionPacksFile(): void
271
    {
272
        mkdir($this->testDir . '.git');
273
        file_put_contents($this->testDir . '.git/config', '');
274
275
        $commit = $this->object->checkGitRevision();
276
277
        $this->assertNull($commit);
278
        $this->assertFalse($this->object->hasGitInformation());
279
280
        file_put_contents($this->testDir . '.git/HEAD', 'ref: refs/remotes/origin/master');
281
282
        $commit = $this->object->checkGitRevision();
283
284
        $this->assertNull($commit);
285
286
        file_put_contents(
287
            $this->testDir . '.git/packed-refs',
288
            '# pack-refs with: peeled fully-peeled sorted' . "\n" .
289
            'c1f2ff2eb0c3fda741f859913fd589379f4e4a8f refs/tags/4.3.10' . "\n" .
290
            '^6f2e60343b0a324c65f2d1411bf4bd03e114fb98' . "\n" .
291
            '8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e refs/remotes/origin/master' . "\n",
292
        );
293
        mkdir($this->testDir . '.git/objects/info', 0777, true);
294
        file_put_contents(
295
            $this->testDir . '.git/objects/info/packs',
296
            'P pack-faea49765800da462c70bea555848cc8c7a1c28d.pack' . "\n" .
297
            '  pack-.pack' . "\n" .
298
            "\n" .
299
            'P pack-420568bae521465fd11863bff155a2b2831023.pack' . "\n" .
300
            "\n",
301
        );
302
303
        $commit = $this->object->checkGitRevision();
304
305
        if (
306
            $commit === null
307
            && ! isset($_SESSION['PMA_VERSION_REMOTECOMMIT_8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e'])
308
        ) {
309
            $this->markTestSkipped('Unable to get remote commit information.');
310
        }
311
312
        $this->assertIsArray($commit);
313
        $this->assertSame('8d660283c5c88a04bac7a2b3aa9ad9eaff0fd05e', $commit['hash']);
314
        $this->assertSame('master', $commit['branch']);
315
        $this->assertSame(
316
            'Update po files' . "\n\n" . '[ci skip]' . "\n\n" . 'Signed-off-by: phpMyAdmin bot <[email protected]>',
317
            $commit['message'],
318
        );
319
        $this->assertTrue($commit['is_remote_commit']);
320
        $this->assertTrue($commit['is_remote_branch']);
321
        $this->assertSame('phpMyAdmin bot', $commit['author']['name']);
322
        $this->assertSame('[email protected]', $commit['author']['email']);
323
        $this->assertSame('2023-05-14T00:19:49Z', $commit['author']['date']);
324
        $this->assertSame('phpMyAdmin bot', $commit['committer']['name']);
325
        $this->assertSame('[email protected]', $commit['committer']['email']);
326
        $this->assertSame('2023-05-14T00:19:49Z', $commit['committer']['date']);
327
328
        unlink($this->testDir . '.git/objects/info/packs');
329
        rmdir($this->testDir . '.git/objects/info');
330
        rmdir($this->testDir . '.git/objects');
331
        unlink($this->testDir . '.git/packed-refs');
332
        unlink($this->testDir . '.git/HEAD');
333
        unlink($this->testDir . '.git/config');
334
        rmdir($this->testDir . '.git');
335
    }
336
337
    /**
338
     * Test for checkGitRevision
339
     */
340
    public function testCheckGitRevisionSkipped(): void
341
    {
342
        $this->object = new Git(false);
343
        $commit = $this->object->checkGitRevision();
344
345
        $this->assertNull($commit);
346
347
        $this->assertFalse($this->object->hasGitInformation());
348
    }
349
350
    /**
351
     * Test for git infos in session
352
     */
353
    public function testSessionCacheGitFolder(): void
354
    {
355
        $_SESSION['git_location'] = 'customdir/.git';
356
        $_SESSION['is_git_revision'] = true;
357
        $gitFolder = '';
358
        $this->assertTrue($this->object->isGitRevision($gitFolder));
359
360
        $this->assertEquals($gitFolder, 'customdir/.git');
361
    }
362
363
    /**
364
     * Test that git folder is not looked up if cached value is false
365
     */
366
    public function testSessionCacheGitFolderNotRevisionNull(): void
367
    {
368
        $_SESSION['is_git_revision'] = false;
369
        $_SESSION['git_location'] = null;
370
        $gitFolder = 'defaultvaluebyref';
371
        $this->assertFalse($this->object->isGitRevision($gitFolder));
372
373
        // Assert that the value is replaced by cached one
374
        $this->assertEquals($gitFolder, null);
375
    }
376
377
    /**
378
     * Test that git folder is not looked up if cached value is false
379
     */
380
    public function testSessionCacheGitFolderNotRevisionString(): void
381
    {
382
        $_SESSION['is_git_revision'] = false;
383
        $_SESSION['git_location'] = 'randomdir/.git';
384
        $gitFolder = 'defaultvaluebyref';
385
        $this->assertFalse($this->object->isGitRevision($gitFolder));
386
387
        // Assert that the value is replaced by cached one
388
        $this->assertEquals($gitFolder, 'randomdir/.git');
389
    }
390
391
    /**
392
     * Test that we can extract values from Git objects
393
     */
394
    public function testExtractDataFormTextBody(): void
395
    {
396
        $extractedData = $this->callFunction(
397
            $this->object,
398
            Git::class,
399
            'extractDataFormTextBody',
400
            [
401
                [
402
                    'tree ed7fec263e1813887001855ddca9293479289180',
403
                    'parent 90543399991cdb294185f90e8ae1a45e059c31ab',
404
                    'author William Desportes <[email protected]> 1657717000 +0200',
405
                    'committer William Desportes <[email protected]> 1657717000 +0200',
406
                    'gpgsig -----BEGIN PGP SIGNATURE-----',
407
                    ' ',
408
                    ' iQIzBAABCgAdFiEExNkf3872tKPGU\/14kKDvG4JRqIkFAmLOwQgACgkQkKDvG4JR',
409
                    ' qIn8Kg\/+Os5e3bFLEtd3q\/w3e4IfvR64rdadA4IUugd4pJvGqJHleJNBQ8PNqwjR',
410
                    ' 9W0S9PQXAsul0XW5YtuLmBMGFFQDOab2ieix9CVA1w0D7quVQR8uLNb1Gln28NuS',
411
                    ' 6b24Q4cAQlp5uOoKT3ohRBUtGmu8SXF8Q\/5BwPY1AuL1LqY6w6EwSsInPXK1Yq3r',
412
                    ' RShxRXDhonKx3NqoCdRkWmAKkQrztWGGBI7mBG\/\/X0F4hSjsuwdpHBsl6yyri9p2',
413
                    ' bJbyAI+xQ+rBHb0iFIoLbxj6G1EkEmpISl+4980uef24SwMVk9ZOfH8cAgBZ62Mf',
414
                    ' xJ3f99ujhD9dvwCQivOwcEav+fPObiLC0EzfoqZgB7rTQdxUIu7WRpShZGwfuiEv',
415
                    ' sBmvQcnZptYHi0Kk78fdzISCQcPBgCw0gGcv+yLOE3HuQ24B+ncCusYdxyJQqMSc',
416
                    ' pm9vVHpwioufy5c7aBa05K7f2b1AhiZeVpT2t\/rboIYlIhQGY9uRNGX44Qtt6Oeb',
417
                    ' G6aU8O7gS5+Wsj00K+uSvUE\/znxx7Ad0zVuFQGUAhd3cDp9T09+FIr4TOE+3Z4Pk',
418
                    ' PlssVGVBdbaNaI0\/eV6fTa6B0hMH9mhmZhtHLXdsTw5xVySz7by5DZqZldydSFtk',
419
                    ' tVuUPxykK6F0qY79IPBH8Unx8egIlSzKWfP0JpRd+otemBnTKWg=',
420
                    ' =BVHc',
421
                    ' -----END PGP SIGNATURE-----',
422
                    '',
423
                    'Remove ignore config.inc.php for psalm because it fails the CI',
424
                    '',
425
                    'Signed-off-by: William Desportes <[email protected]>',
426
                    '',
427
                ],
428
            ],
429
        );
430
431
        $this->assertSame([
432
            ['name' => 'William Desportes', 'email' => '[email protected]', 'date' => '2022-07-13 14:56:40 +0200'],
433
            ['name' => 'William Desportes', 'email' => '[email protected]', 'date' => '2022-07-13 14:56:40 +0200'],
434
            'Remove ignore config.inc.php for psalm because '
435
                . 'it fails the CI  Signed-off-by: William Desportes <[email protected]>',
436
        ], $extractedData);
437
    }
438
}
439