CommentStore: Hard-deprecate newKey()
[lhc/web/wiklou.git] / tests / phpunit / includes / GitInfoTest.php
1 <?php
2 /**
3 * @covers GitInfo
4 */
5 class GitInfoTest extends MediaWikiTestCase {
6
7 private static $tempDir;
8
9 public static function setUpBeforeClass() {
10 self::$tempDir = wfTempDir() . '/mw-phpunit-' . wfRandomString( 8 );
11 if ( !mkdir( self::$tempDir ) ) {
12 self::$tempDir = null;
13 throw new Exception( 'Unable to create temporary directory' );
14 }
15 mkdir( self::$tempDir . '/gitrepo' );
16 mkdir( self::$tempDir . '/gitrepo/1' );
17 mkdir( self::$tempDir . '/gitrepo/2' );
18 mkdir( self::$tempDir . '/gitrepo/3' );
19 mkdir( self::$tempDir . '/gitrepo/1/.git' );
20 mkdir( self::$tempDir . '/gitrepo/1/.git/refs' );
21 mkdir( self::$tempDir . '/gitrepo/1/.git/refs/heads' );
22 file_put_contents( self::$tempDir . '/gitrepo/1/.git/HEAD',
23 "ref: refs/heads/master\n" );
24 file_put_contents( self::$tempDir . '/gitrepo/1/.git/refs/heads/master',
25 "0123456789012345678901234567890123abcdef\n" );
26 file_put_contents( self::$tempDir . '/gitrepo/1/.git/packed-refs',
27 "abcdef6789012345678901234567890123456789 refs/heads/master\n" );
28 file_put_contents( self::$tempDir . '/gitrepo/2/.git',
29 "gitdir: ../1/.git\n" );
30 file_put_contents( self::$tempDir . '/gitrepo/3/.git',
31 'gitdir: ' . self::$tempDir . "/gitrepo/1/.git\n" );
32 }
33
34 public static function tearDownAfterClass() {
35 if ( self::$tempDir ) {
36 wfRecursiveRemoveDir( self::$tempDir );
37 }
38 }
39
40 protected function setUp() {
41 parent::setUp();
42 $this->setMwGlobals( 'wgGitInfoCacheDirectory', __DIR__ . '/../data/gitinfo' );
43 }
44
45 protected function assertValidGitInfo( GitInfo $gitInfo ) {
46 $this->assertTrue( $gitInfo->cacheIsComplete() );
47 $this->assertEquals( 'refs/heads/master', $gitInfo->getHead() );
48 $this->assertEquals( '0123456789abcdef0123456789abcdef01234567',
49 $gitInfo->getHeadSHA1() );
50 $this->assertEquals( '1070884800', $gitInfo->getHeadCommitDate() );
51 $this->assertEquals( 'master', $gitInfo->getCurrentBranch() );
52 $this->assertContains( '0123456789abcdef0123456789abcdef01234567',
53 $gitInfo->getHeadViewUrl() );
54 }
55
56 public function testValidJsonData() {
57 global $IP;
58
59 $this->assertValidGitInfo( new GitInfo( "$IP/testValidJsonData" ) );
60 $this->assertValidGitInfo( new GitInfo( __DIR__ . "/../data/gitinfo/extension" ) );
61 }
62
63 public function testMissingJsonData() {
64 $dir = $GLOBALS['IP'] . '/testMissingJsonData';
65 $fixture = new GitInfo( $dir );
66
67 $this->assertFalse( $fixture->cacheIsComplete() );
68
69 $this->assertEquals( false, $fixture->getHead() );
70 $this->assertEquals( false, $fixture->getHeadSHA1() );
71 $this->assertEquals( false, $fixture->getHeadCommitDate() );
72 $this->assertEquals( false, $fixture->getCurrentBranch() );
73 $this->assertEquals( false, $fixture->getHeadViewUrl() );
74
75 // After calling all the outputs, the cache should be complete
76 $this->assertTrue( $fixture->cacheIsComplete() );
77 }
78
79 public function testReadingHead() {
80 $dir = self::$tempDir . '/gitrepo/1';
81 $fixture = new GitInfo( $dir );
82
83 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
84 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
85 }
86
87 public function testIndirection() {
88 $dir = self::$tempDir . '/gitrepo/2';
89 $fixture = new GitInfo( $dir );
90
91 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
92 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
93 }
94
95 public function testIndirection2() {
96 $dir = self::$tempDir . '/gitrepo/3';
97 $fixture = new GitInfo( $dir );
98
99 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
100 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
101 }
102
103 public function testReadingPackedRefs() {
104 $dir = self::$tempDir . '/gitrepo/1';
105 unlink( self::$tempDir . '/gitrepo/1/.git/refs/heads/master' );
106 $fixture = new GitInfo( $dir );
107
108 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
109 $this->assertEquals( 'abcdef6789012345678901234567890123456789', $fixture->getHeadSHA1() );
110 }
111 }