b33c1bbb398620b502dcf4e6af6dbea38fae17df
[lhc/web/wiklou.git] / tests / phpunit / includes / filerepo / StoreBatchTest.php
1 <?php
2
3 /**
4 * @group FileRepo
5 * @group medium
6 */
7 class StoreBatchTest extends MediaWikiTestCase {
8
9 protected $createdFiles;
10 protected $date;
11 /** @var FileRepo */
12 protected $repo;
13
14 protected function setUp() {
15 global $wgFileBackends;
16 parent::setUp();
17
18 # Forge a FSRepo object to not have to rely on local wiki settings
19 $tmpPrefix = wfTempDir() . '/storebatch-test-' . time() . '-' . mt_rand();
20 if ( $this->getCliArg( 'use-filebackend=' ) ) {
21 $name = $this->getCliArg( 'use-filebackend=' );
22 $useConfig = array();
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
25 $useConfig = $conf;
26 }
27 }
28 $useConfig['name'] = 'local-testing'; // swap name
29 $class = $useConfig['class'];
30 $backend = new $class( $useConfig );
31 } else {
32 $backend = new FSFileBackend( array(
33 'name' => 'local-testing',
34 'lockManager' => 'nullLockManager',
35 'containerPaths' => array(
36 'unittests-public' => "{$tmpPrefix}-public",
37 'unittests-thumb' => "{$tmpPrefix}-thumb",
38 'unittests-temp' => "{$tmpPrefix}-temp",
39 'unittests-deleted' => "{$tmpPrefix}-deleted",
40 )
41 ) );
42 }
43 $this->repo = new FileRepo( array(
44 'name' => 'unittests',
45 'backend' => $backend
46 ) );
47
48 $this->date = gmdate( "YmdHis" );
49 $this->createdFiles = array();
50 }
51
52 protected function tearDown() {
53 $this->repo->cleanupBatch( $this->createdFiles ); // delete files
54 foreach ( $this->createdFiles as $tmp ) { // delete dirs
55 $tmp = $this->repo->resolveVirtualUrl( $tmp );
56 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
57 $this->repo->getBackend()->clean( array( 'dir' => $tmp ) );
58 }
59 }
60 parent::tearDown();
61 }
62
63 /**
64 * Store a file or virtual URL source into a media file name.
65 *
66 * @param $originalName string The title of the image
67 * @param $srcPath string The filepath or virtual URL
68 * @param $flags integer Flags to pass into repo::store().
69 * @return FileRepoStatus
70 */
71 private function storeit( $originalName, $srcPath, $flags ) {
72 $hashPath = $this->repo->getHashPath( $originalName );
73 $dstRel = "$hashPath{$this->date}!$originalName";
74 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
75
76 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
77 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
78 $this->createdFiles[] = $result->value;
79
80 return $result;
81 }
82
83 /**
84 * Test storing a file using different flags.
85 *
86 * @param $fn string The title of the image
87 * @param $infn string The name of the file (in the filesystem)
88 * @param $otherfn string The name of the different file (in the filesystem)
89 * @param $fromrepo bool 'true' if we want to copy from a virtual URL out of the Repo.
90 */
91 private function storecohort( $fn, $infn, $otherfn, $fromrepo ) {
92 $f = $this->storeit( $fn, $infn, 0 );
93 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
94 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
95 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
96 if ( $fromrepo ) {
97 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE );
98 $infn = $f->value;
99 }
100 // This should work because we're allowed to overwrite
101 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
102 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
103 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
104 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
105 // This should fail because we're overwriting.
106 $f = $this->storeit( $fn, $infn, 0 );
107 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
108 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
109 $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
110 // This should succeed because we're overwriting the same content.
111 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
112 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
113 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
114 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
115 // This should fail because we're overwriting different content.
116 if ( $fromrepo ) {
117 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE );
118 $otherfn = $f->value;
119 }
120 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
121 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
122 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
123 $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
124 }
125
126 /**
127 * @covers FileRepo::store
128 */
129 public function teststore() {
130 global $IP;
131 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
132 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );
133 }
134 }