Merge "(bug 56849) Deprecate dangerous edittime-based content update functions"
[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['lockManager'] = LockManagerGroup::singleton()->get( $useConfig['lockManager'] );
29 unset( $useConfig['fileJournal'] );
30 $useConfig['name'] = 'local-testing'; // swap name
31 $class = $useConfig['class'];
32 $backend = new $class( $useConfig );
33 } else {
34 $backend = new FSFileBackend( array(
35 'name' => 'local-testing',
36 'wikiId' => wfWikiID(),
37 'containerPaths' => array(
38 'unittests-public' => "{$tmpPrefix}-public",
39 'unittests-thumb' => "{$tmpPrefix}-thumb",
40 'unittests-temp' => "{$tmpPrefix}-temp",
41 'unittests-deleted' => "{$tmpPrefix}-deleted",
42 )
43 ) );
44 }
45 $this->repo = new FileRepo( array(
46 'name' => 'unittests',
47 'backend' => $backend
48 ) );
49
50 $this->date = gmdate( "YmdHis" );
51 $this->createdFiles = array();
52 }
53
54 protected function tearDown() {
55 $this->repo->cleanupBatch( $this->createdFiles ); // delete files
56 foreach ( $this->createdFiles as $tmp ) { // delete dirs
57 $tmp = $this->repo->resolveVirtualUrl( $tmp );
58 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
59 $this->repo->getBackend()->clean( array( 'dir' => $tmp ) );
60 }
61 }
62 parent::tearDown();
63 }
64
65 /**
66 * Store a file or virtual URL source into a media file name.
67 *
68 * @param string $originalName The title of the image
69 * @param string $srcPath The filepath or virtual URL
70 * @param int $flags Flags to pass into repo::store().
71 * @return FileRepoStatus
72 */
73 private function storeit( $originalName, $srcPath, $flags ) {
74 $hashPath = $this->repo->getHashPath( $originalName );
75 $dstRel = "$hashPath{$this->date}!$originalName";
76 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
77
78 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
79 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
80 $this->createdFiles[] = $result->value;
81
82 return $result;
83 }
84
85 /**
86 * Test storing a file using different flags.
87 *
88 * @param string $fn The title of the image
89 * @param string $infn The name of the file (in the filesystem)
90 * @param string $otherfn The name of the different file (in the filesystem)
91 * @param bool $fromrepo 'true' if we want to copy from a virtual URL out of the Repo.
92 */
93 private function storecohort( $fn, $infn, $otherfn, $fromrepo ) {
94 $f = $this->storeit( $fn, $infn, 0 );
95 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
96 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
97 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
98 if ( $fromrepo ) {
99 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE );
100 $infn = $f->value;
101 }
102 // This should work because we're allowed to overwrite
103 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
104 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
105 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
106 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
107 // This should fail because we're overwriting.
108 $f = $this->storeit( $fn, $infn, 0 );
109 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
110 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
111 $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
112 // This should succeed because we're overwriting the same content.
113 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
114 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
115 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
116 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
117 // This should fail because we're overwriting different content.
118 if ( $fromrepo ) {
119 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE );
120 $otherfn = $f->value;
121 }
122 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
123 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
124 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
125 $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
126 }
127
128 /**
129 * @covers FileRepo::store
130 */
131 public function teststore() {
132 global $IP;
133 $this->storecohort(
134 "Test1.png",
135 "$IP/tests/phpunit/data/filerepo/wiki.png",
136 "$IP/tests/phpunit/data/filerepo/video.png",
137 false
138 );
139 $this->storecohort(
140 "Test2.png",
141 "$IP/tests/phpunit/data/filerepo/wiki.png",
142 "$IP/tests/phpunit/data/filerepo/video.png",
143 true
144 );
145 }
146 }