clean up temp files. Fixes r99546
[lhc/web/wiklou.git] / tests / phpunit / includes / filerepo / StoreBatchTest.php
1 <?php
2 /**
3 * @group Filerepo
4 */
5 class StoreBatchTest extends MediaWikiTestCase {
6
7 public function setUp() {
8 parent::setUp();
9
10 $this->repo = RepoGroup::singleton()->getLocalRepo();
11 $this->date = gmdate( "YmdHis" );
12 $this->createdFiles = array();
13 $this->users = array(
14 'sysop' => new ApiTestUser(
15 'Uploadstashtestsysop',
16 'Upload Stash Test Sysop',
17 'upload_stash_test_sysop@sample.com',
18 array( 'sysop' )
19 ),
20 'uploader' => new ApiTestUser(
21 'Uploadstashtestuser',
22 'Upload Stash Test User',
23 'upload_stash_test_user@sample.com',
24 array()
25 )
26 );
27 }
28
29 /**
30 * Store a file or virtual URL source into a media file name.
31 *
32 * @param $originalName string The title of the image
33 * @param $srcPath string The filepath or virtual URL
34 * @param $flags integer Flags to pass into repo::store().
35 */
36 private function storeit($originalName, $srcPath, $flags) {
37 $hashPath = $this->repo->getHashPath( $originalName );
38 $dstRel = "$hashPath{$this->date}!$originalName";
39 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
40
41 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
42 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
43 $this->createdFiles[] = $result->value;
44 return $result;
45 }
46
47
48 /**
49 * Test storing a file using different flags.
50 *
51 * @param $fn string The title of the image
52 * @param $infn string The name of the file (in the filesystem)
53 * @param $otherfn string The name of the different file (in the filesystem)
54 * @param $fromrepo logical 'true' if we want to copy from a virtual URL out of the Repo.
55 */
56 private function storecohort($fn, $infn, $otherfn, $fromrepo) {
57 $f = $this->storeit( $fn, $infn, 0 );
58 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
59 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
60 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
61 if ( $fromrepo ) {
62 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE);
63 $infn = $f->value;
64 }
65 // This should work because we're allowed to overwrite
66 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
67 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
68 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
69 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
70 // This should fail because we're overwriting.
71 $f = $this->storeit( $fn, $infn, 0 );
72 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
73 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
74 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
75 // This should succeed because we're overwriting the same content.
76 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
77 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
78 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
79 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
80 // This should fail because we're overwriting different content.
81 if ( $fromrepo ) {
82 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE);
83 $otherfn = $f->value;
84 }
85 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
86 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
87 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
88 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
89 }
90
91 public function teststore() {
92 global $IP;
93 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
94 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );
95 }
96
97 public function tearDown() {
98 $this->repo->cleanupBatch( $this->createdFile );
99 parent::tearDown();
100
101 }
102 }