3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
10 use function file_put_contents
;
12 use function is_string
;
14 use function mt_getrandmax
;
15 use function random_int
;
17 use function sys_get_temp_dir
;
20 use const DIRECTORY_SEPARATOR
;
24 * @covers \PhpMyAdmin\Git
27 class GitTest
extends AbstractTestCase
39 * Sets up the fixture, for example, opens a network connection.
40 * This method is called before a test is executed.
42 protected function setUp(): void
45 parent
::setProxySettings();
46 $this->object = new Git(true);
47 $this->testDir
= sys_get_temp_dir() . DIRECTORY_SEPARATOR
. 'gittempdir_' . random_int(0, mt_getrandmax());
49 unset($_SESSION['git_location']);
50 unset($_SESSION['is_git_revision']);
51 $this->cwd
= is_string(getcwd()) ?
getcwd() : './';
52 mkdir($this->testDir
);
53 chdir((string) $this->testDir
);
57 * Tears down the fixture, for example, closes a network connection.
58 * This method is called after a test is executed.
60 protected function tearDown(): void
62 chdir((string) $this->cwd
);
63 rmdir($this->testDir
);
69 * Test for isGitRevision
71 public function testIsGitRevision(): void
73 $_SESSION['git_location'] = '.cachedgitlocation';
74 $_SESSION['is_git_revision'] = true;
78 $this->assertTrue($this->object->isGitRevision($git_location));
80 $this->assertFalse($this->object->hasGitInformation());
82 $this->assertEquals('.cachedgitlocation', $git_location);
86 * Test for isGitRevision
88 public function testIsGitRevisionSkipped(): void
90 $this->object = new Git(false);
92 $this->object->isGitRevision($git_location)
97 * Test for isGitRevision
101 public function testIsGitRevisionLocalGitDir(): void
104 $this->object->isGitRevision()
107 $this->assertFalse($this->object->hasGitInformation());
109 unset($_SESSION['git_location']);
110 unset($_SESSION['is_git_revision']);
115 $this->object->isGitRevision()
118 $this->assertFalse($this->object->hasGitInformation());
120 unset($_SESSION['git_location']);
121 unset($_SESSION['is_git_revision']);
123 file_put_contents('.git/config', '');
125 $this->assertTrue($this->object->isGitRevision());
127 $this->assertFalse($this->object->hasGitInformation());
129 unlink('.git/config');
134 * Test for isGitRevision
136 * @group git-revision
138 public function testIsGitRevisionExternalGitDir(): void
140 file_put_contents('.git', 'gitdir: ./.customgitdir');
142 $this->object->isGitRevision()
145 $this->assertFalse($this->object->hasGitInformation());
147 unset($_SESSION['git_location']);
148 unset($_SESSION['is_git_revision']);
150 mkdir('.customgitdir');
152 $this->assertTrue($this->object->isGitRevision());
154 $this->assertFalse($this->object->hasGitInformation());
156 unset($_SESSION['git_location']);
157 unset($_SESSION['is_git_revision']);
159 file_put_contents('.git', 'random data here');
162 $this->object->isGitRevision()
165 $this->assertFalse($this->object->hasGitInformation());
168 rmdir('.customgitdir');
172 * Test for checkGitRevision packs folder
174 * @group git-revision
176 public function testCheckGitRevisionPacksFolder(): void
179 file_put_contents('.git/config', '');
181 $commit = $this->object->checkGitRevision();
183 $this->assertNull($commit);
184 $this->assertFalse($this->object->hasGitInformation());
186 file_put_contents('.git/HEAD', 'ref: refs/remotes/origin/master');
188 $commit = $this->object->checkGitRevision();
190 $this->assertNull($commit);
194 '# pack-refs with: peeled fully-peeled sorted' . PHP_EOL
.
195 'c1f2ff2eb0c3fda741f859913fd589379f4e4a8f refs/tags/4.3.10' . PHP_EOL
.
196 '^6f2e60343b0a324c65f2d1411bf4bd03e114fb98' . PHP_EOL
.
197 '17bf8b7309919f8ac593d7c563b31472780ee83b refs/remotes/origin/master' . PHP_EOL
199 mkdir('.git/objects/pack', 0777, true);//default = 0777, recursive mode
201 $commit = $this->object->checkGitRevision();
203 $this->assertIsArray($commit);
204 $this->assertArrayHasKey('hash', $commit);
205 $this->assertEquals('17bf8b7309919f8ac593d7c563b31472780ee83b', $commit['hash']);
207 $this->assertArrayHasKey('branch', $commit);
208 $this->assertEquals('master', $commit['branch']);
210 $this->assertArrayHasKey('message', $commit);
211 $this->assertIsString($commit['message']);
213 $this->assertArrayHasKey('is_remote_commit', $commit);
214 $this->assertIsBool($commit['is_remote_commit']);
216 $this->assertArrayHasKey('is_remote_branch', $commit);
217 $this->assertIsBool($commit['is_remote_branch']);
219 $this->assertArrayHasKey('author', $commit);
220 $this->assertIsArray($commit['author']);
221 $this->assertArrayHasKey('name', $commit['author']);
222 $this->assertArrayHasKey('email', $commit['author']);
223 $this->assertArrayHasKey('date', $commit['author']);
224 $this->assertIsString($commit['author']['name']);
225 $this->assertIsString($commit['author']['email']);
226 $this->assertIsString($commit['author']['date']);
228 $this->assertArrayHasKey('committer', $commit);
229 $this->assertIsArray($commit['committer']);
230 $this->assertArrayHasKey('name', $commit['committer']);
231 $this->assertArrayHasKey('email', $commit['committer']);
232 $this->assertArrayHasKey('date', $commit['committer']);
233 $this->assertIsString($commit['committer']['name']);
234 $this->assertIsString($commit['committer']['email']);
235 $this->assertIsString($commit['committer']['date']);
237 rmdir('.git/objects/pack');
238 rmdir('.git/objects');
239 unlink('.git/packed-refs');
241 unlink('.git/config');
246 * Test for checkGitRevision packs folder
248 * @group git-revision
250 public function testCheckGitRevisionRefFile(): void
253 file_put_contents('.git/config', '');
255 $commit = $this->object->checkGitRevision();
257 $this->assertNull($commit);
258 $this->assertFalse($this->object->hasGitInformation());
260 file_put_contents('.git/HEAD', 'ref: refs/remotes/origin/master');
261 mkdir('.git/refs/remotes/origin', 0777, true);
262 file_put_contents('.git/refs/remotes/origin/master', 'c1f2ff2eb0c3fda741f859913fd589379f4e4a8f');
263 mkdir('.git/objects/pack', 0777, true);//default = 0777, recursive mode
264 $commit = $this->object->checkGitRevision();
266 $this->assertNull($commit);
267 $this->assertFalse($this->object->hasGitInformation());
269 unlink('.git/refs/remotes/origin/master');
270 rmdir('.git/refs/remotes/origin');
271 rmdir('.git/refs/remotes');
273 rmdir('.git/objects/pack');
274 rmdir('.git/objects');
276 unlink('.git/config');
281 * Test for checkGitRevision with packs as file
283 * @group git-revision
285 public function testCheckGitRevisionPacksFile(): void
288 file_put_contents('.git/config', '');
290 $commit = $this->object->checkGitRevision();
292 $this->assertNull($commit);
293 $this->assertFalse($this->object->hasGitInformation());
295 file_put_contents('.git/HEAD', 'ref: refs/remotes/origin/master');
297 $commit = $this->object->checkGitRevision();
299 $this->assertNull($commit);
303 '# pack-refs with: peeled fully-peeled sorted' . PHP_EOL
.
304 'c1f2ff2eb0c3fda741f859913fd589379f4e4a8f refs/tags/4.3.10' . PHP_EOL
.
305 '^6f2e60343b0a324c65f2d1411bf4bd03e114fb98' . PHP_EOL
.
306 '17bf8b7309919f8ac593d7c563b31472780ee83b refs/remotes/origin/master' . PHP_EOL
308 mkdir('.git/objects/info', 0777, true);
310 '.git/objects/info/packs',
311 'P pack-faea49765800da462c70bea555848cc8c7a1c28d.pack' . PHP_EOL
.
312 ' pack-.pack' . PHP_EOL
.
314 'P pack-420568bae521465fd11863bff155a2b2831023.pack' . PHP_EOL
.
318 $commit = $this->object->checkGitRevision();
320 $this->assertIsArray($commit);
321 $this->assertArrayHasKey('hash', $commit);
322 $this->assertEquals('17bf8b7309919f8ac593d7c563b31472780ee83b', $commit['hash']);
324 $this->assertArrayHasKey('branch', $commit);
325 $this->assertEquals('master', $commit['branch']);
327 $this->assertArrayHasKey('message', $commit);
328 $this->assertIsString($commit['message']);
330 $this->assertArrayHasKey('is_remote_commit', $commit);
331 $this->assertIsBool($commit['is_remote_commit']);
333 $this->assertArrayHasKey('is_remote_branch', $commit);
334 $this->assertIsBool($commit['is_remote_branch']);
336 $this->assertArrayHasKey('author', $commit);
337 $this->assertIsArray($commit['author']);
338 $this->assertArrayHasKey('name', $commit['author']);
339 $this->assertArrayHasKey('email', $commit['author']);
340 $this->assertArrayHasKey('date', $commit['author']);
341 $this->assertIsString($commit['author']['name']);
342 $this->assertIsString($commit['author']['email']);
343 $this->assertIsString($commit['author']['date']);
345 $this->assertArrayHasKey('committer', $commit);
346 $this->assertIsArray($commit['committer']);
347 $this->assertArrayHasKey('name', $commit['committer']);
348 $this->assertArrayHasKey('email', $commit['committer']);
349 $this->assertArrayHasKey('date', $commit['committer']);
350 $this->assertIsString($commit['committer']['name']);
351 $this->assertIsString($commit['committer']['email']);
352 $this->assertIsString($commit['committer']['date']);
354 unlink('.git/objects/info/packs');
355 rmdir('.git/objects/info');
356 rmdir('.git/objects');
357 unlink('.git/packed-refs');
359 unlink('.git/config');
364 * Test for checkGitRevision
366 public function testCheckGitRevisionSkipped(): void
368 $this->object = new Git(false);
369 $commit = $this->object->checkGitRevision();
371 $this->assertNull($commit);
373 $this->assertFalse($this->object->hasGitInformation());
377 * Test for git infos in session
379 public function testSessionCacheGitFolder(): void
381 $_SESSION['git_location'] = 'customdir/.git';
382 $_SESSION['is_git_revision'] = true;
384 $this->assertTrue($this->object->isGitRevision($gitFolder));
386 $this->assertEquals($gitFolder, 'customdir/.git');
390 * Test that git folder is not looked up if cached value is false
392 public function testSessionCacheGitFolderNotRevisionNull(): void
394 $_SESSION['is_git_revision'] = false;
395 $_SESSION['git_location'] = null;
396 $gitFolder = 'defaultvaluebyref';
397 $this->assertFalse($this->object->isGitRevision($gitFolder));
399 // Assert that the value is replaced by cached one
400 $this->assertEquals($gitFolder, null);
404 * Test that git folder is not looked up if cached value is false
406 public function testSessionCacheGitFolderNotRevisionString(): void
408 $_SESSION['is_git_revision'] = false;
409 $_SESSION['git_location'] = 'randomdir/.git';
410 $gitFolder = 'defaultvaluebyref';
411 $this->assertFalse($this->object->isGitRevision($gitFolder));
413 // Assert that the value is replaced by cached one
414 $this->assertEquals($gitFolder, 'randomdir/.git');