Translated using Weblate (Lithuanian)
[phpmyadmin.git] / test / classes / GitTest.php
blob2789dad1aade9ba39d83b90200becfc6a2f0f100
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Git;
9 use function chdir;
10 use function file_put_contents;
11 use function getcwd;
12 use function is_string;
13 use function mkdir;
14 use function mt_getrandmax;
15 use function random_int;
16 use function rmdir;
17 use function sys_get_temp_dir;
18 use function unlink;
20 use const DIRECTORY_SEPARATOR;
21 use const PHP_EOL;
23 /**
24 * @covers \PhpMyAdmin\Git
25 * @group git-revision
27 class GitTest extends AbstractTestCase
29 /** @var Git */
30 protected $object;
32 /** @var string */
33 protected $testDir;
35 /** @var string */
36 protected $cwd;
38 /**
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
44 parent::setUp();
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);
56 /**
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);
64 parent::tearDown();
65 unset($this->object);
68 /**
69 * Test for isGitRevision
71 public function testIsGitRevision(): void
73 $_SESSION['git_location'] = '.cachedgitlocation';
74 $_SESSION['is_git_revision'] = true;
76 $git_location = '';
78 $this->assertTrue($this->object->isGitRevision($git_location));
80 $this->assertFalse($this->object->hasGitInformation());
82 $this->assertEquals('.cachedgitlocation', $git_location);
85 /**
86 * Test for isGitRevision
88 public function testIsGitRevisionSkipped(): void
90 $this->object = new Git(false);
91 $this->assertFalse(
92 $this->object->isGitRevision($git_location)
96 /**
97 * Test for isGitRevision
99 * @group git-revision
101 public function testIsGitRevisionLocalGitDir(): void
103 $this->assertFalse(
104 $this->object->isGitRevision()
107 $this->assertFalse($this->object->hasGitInformation());
109 unset($_SESSION['git_location']);
110 unset($_SESSION['is_git_revision']);
112 mkdir('.git');
114 $this->assertFalse(
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');
130 rmdir('.git');
134 * Test for isGitRevision
136 * @group git-revision
138 public function testIsGitRevisionExternalGitDir(): void
140 file_put_contents('.git', 'gitdir: ./.customgitdir');
141 $this->assertFalse(
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');
161 $this->assertFalse(
162 $this->object->isGitRevision()
165 $this->assertFalse($this->object->hasGitInformation());
167 unlink('.git');
168 rmdir('.customgitdir');
172 * Test for checkGitRevision packs folder
174 * @group git-revision
176 public function testCheckGitRevisionPacksFolder(): void
178 mkdir('.git');
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);
192 file_put_contents(
193 '.git/packed-refs',
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');
240 unlink('.git/HEAD');
241 unlink('.git/config');
242 rmdir('.git');
246 * Test for checkGitRevision packs folder
248 * @group git-revision
250 public function testCheckGitRevisionRefFile(): void
252 mkdir('.git');
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');
272 rmdir('.git/refs');
273 rmdir('.git/objects/pack');
274 rmdir('.git/objects');
275 unlink('.git/HEAD');
276 unlink('.git/config');
277 rmdir('.git');
281 * Test for checkGitRevision with packs as file
283 * @group git-revision
285 public function testCheckGitRevisionPacksFile(): void
287 mkdir('.git');
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);
301 file_put_contents(
302 '.git/packed-refs',
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);
309 file_put_contents(
310 '.git/objects/info/packs',
311 'P pack-faea49765800da462c70bea555848cc8c7a1c28d.pack' . PHP_EOL .
312 ' pack-.pack' . PHP_EOL .
313 PHP_EOL .
314 'P pack-420568bae521465fd11863bff155a2b2831023.pack' . PHP_EOL .
315 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');
358 unlink('.git/HEAD');
359 unlink('.git/config');
360 rmdir('.git');
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;
383 $gitFolder = '';
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');