FileBackendTest.php 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643
  1. <?php
  2. use Wikimedia\TestingAccessWrapper;
  3. /**
  4. * @group FileRepo
  5. * @group FileBackend
  6. * @group medium
  7. *
  8. * @covers FileBackend
  9. *
  10. * @covers CopyFileOp
  11. * @covers CreateFileOp
  12. * @covers DeleteFileOp
  13. * @covers DescribeFileOp
  14. * @covers FSFile
  15. * @covers FSFileBackend
  16. * @covers FSFileBackendDirList
  17. * @covers FSFileBackendFileList
  18. * @covers FSFileBackendList
  19. * @covers FSFileOpHandle
  20. * @covers FileBackendDBRepoWrapper
  21. * @covers FileBackendError
  22. * @covers FileBackendGroup
  23. * @covers FileBackendMultiWrite
  24. * @covers FileBackendStore
  25. * @covers FileBackendStoreOpHandle
  26. * @covers FileBackendStoreShardDirIterator
  27. * @covers FileBackendStoreShardFileIterator
  28. * @covers FileBackendStoreShardListIterator
  29. * @covers FileJournal
  30. * @covers FileOp
  31. * @covers FileOpBatch
  32. * @covers HTTPFileStreamer
  33. * @covers LockManagerGroup
  34. * @covers MemoryFileBackend
  35. * @covers MoveFileOp
  36. * @covers MySqlLockManager
  37. * @covers NullFileJournal
  38. * @covers NullFileOp
  39. * @covers StoreFileOp
  40. * @covers TempFSFile
  41. *
  42. * @covers FSLockManager
  43. * @covers LockManager
  44. * @covers NullLockManager
  45. */
  46. class FileBackendTest extends MediaWikiTestCase {
  47. /** @var FileBackend */
  48. private $backend;
  49. /** @var FileBackendMultiWrite */
  50. private $multiBackend;
  51. /** @var FSFileBackend */
  52. public $singleBackend;
  53. private static $backendToUse;
  54. protected function setUp() {
  55. global $wgFileBackends;
  56. parent::setUp();
  57. $tmpDir = $this->getNewTempDirectory();
  58. if ( $this->getCliArg( 'use-filebackend' ) ) {
  59. if ( self::$backendToUse ) {
  60. $this->singleBackend = self::$backendToUse;
  61. } else {
  62. $name = $this->getCliArg( 'use-filebackend' );
  63. $useConfig = [];
  64. foreach ( $wgFileBackends as $conf ) {
  65. if ( $conf['name'] == $name ) {
  66. $useConfig = $conf;
  67. break;
  68. }
  69. }
  70. $useConfig['name'] = 'localtesting'; // swap name
  71. $useConfig['shardViaHashLevels'] = [ // test sharding
  72. 'unittest-cont1' => [ 'levels' => 1, 'base' => 16, 'repeat' => 1 ]
  73. ];
  74. if ( isset( $useConfig['fileJournal'] ) ) {
  75. $useConfig['fileJournal'] = FileJournal::factory( $useConfig['fileJournal'], $name );
  76. }
  77. $useConfig['lockManager'] = LockManagerGroup::singleton()->get( $useConfig['lockManager'] );
  78. $class = $useConfig['class'];
  79. self::$backendToUse = new $class( $useConfig );
  80. $this->singleBackend = self::$backendToUse;
  81. }
  82. } else {
  83. $this->singleBackend = new FSFileBackend( [
  84. 'name' => 'localtesting',
  85. 'lockManager' => LockManagerGroup::singleton()->get( 'fsLockManager' ),
  86. 'wikiId' => wfWikiID(),
  87. 'containerPaths' => [
  88. 'unittest-cont1' => "{$tmpDir}/localtesting-cont1",
  89. 'unittest-cont2' => "{$tmpDir}/localtesting-cont2" ]
  90. ] );
  91. }
  92. $this->multiBackend = new FileBackendMultiWrite( [
  93. 'name' => 'localtesting',
  94. 'lockManager' => LockManagerGroup::singleton()->get( 'fsLockManager' ),
  95. 'parallelize' => 'implicit',
  96. 'wikiId' => wfWikiID() . wfRandomString(),
  97. 'backends' => [
  98. [
  99. 'name' => 'localmultitesting1',
  100. 'class' => FSFileBackend::class,
  101. 'containerPaths' => [
  102. 'unittest-cont1' => "{$tmpDir}/localtestingmulti1-cont1",
  103. 'unittest-cont2' => "{$tmpDir}/localtestingmulti1-cont2" ],
  104. 'isMultiMaster' => false
  105. ],
  106. [
  107. 'name' => 'localmultitesting2',
  108. 'class' => FSFileBackend::class,
  109. 'containerPaths' => [
  110. 'unittest-cont1' => "{$tmpDir}/localtestingmulti2-cont1",
  111. 'unittest-cont2' => "{$tmpDir}/localtestingmulti2-cont2" ],
  112. 'isMultiMaster' => true
  113. ]
  114. ]
  115. ] );
  116. }
  117. private static function baseStorePath() {
  118. return 'mwstore://localtesting';
  119. }
  120. private function backendClass() {
  121. return get_class( $this->backend );
  122. }
  123. /**
  124. * @dataProvider provider_testIsStoragePath
  125. */
  126. public function testIsStoragePath( $path, $isStorePath ) {
  127. $this->assertEquals( $isStorePath, FileBackend::isStoragePath( $path ),
  128. "FileBackend::isStoragePath on path '$path'" );
  129. }
  130. public static function provider_testIsStoragePath() {
  131. return [
  132. [ 'mwstore://', true ],
  133. [ 'mwstore://backend', true ],
  134. [ 'mwstore://backend/container', true ],
  135. [ 'mwstore://backend/container/', true ],
  136. [ 'mwstore://backend/container/path', true ],
  137. [ 'mwstore://backend//container/', true ],
  138. [ 'mwstore://backend//container//', true ],
  139. [ 'mwstore://backend//container//path', true ],
  140. [ 'mwstore:///', true ],
  141. [ 'mwstore:/', false ],
  142. [ 'mwstore:', false ],
  143. ];
  144. }
  145. /**
  146. * @dataProvider provider_testSplitStoragePath
  147. */
  148. public function testSplitStoragePath( $path, $res ) {
  149. $this->assertEquals( $res, FileBackend::splitStoragePath( $path ),
  150. "FileBackend::splitStoragePath on path '$path'" );
  151. }
  152. public static function provider_testSplitStoragePath() {
  153. return [
  154. [ 'mwstore://backend/container', [ 'backend', 'container', '' ] ],
  155. [ 'mwstore://backend/container/', [ 'backend', 'container', '' ] ],
  156. [ 'mwstore://backend/container/path', [ 'backend', 'container', 'path' ] ],
  157. [ 'mwstore://backend/container//path', [ 'backend', 'container', '/path' ] ],
  158. [ 'mwstore://backend//container/path', [ null, null, null ] ],
  159. [ 'mwstore://backend//container//path', [ null, null, null ] ],
  160. [ 'mwstore://', [ null, null, null ] ],
  161. [ 'mwstore://backend', [ null, null, null ] ],
  162. [ 'mwstore:///', [ null, null, null ] ],
  163. [ 'mwstore:/', [ null, null, null ] ],
  164. [ 'mwstore:', [ null, null, null ] ]
  165. ];
  166. }
  167. /**
  168. * @dataProvider provider_normalizeStoragePath
  169. */
  170. public function testNormalizeStoragePath( $path, $res ) {
  171. $this->assertEquals( $res, FileBackend::normalizeStoragePath( $path ),
  172. "FileBackend::normalizeStoragePath on path '$path'" );
  173. }
  174. public static function provider_normalizeStoragePath() {
  175. return [
  176. [ 'mwstore://backend/container', 'mwstore://backend/container' ],
  177. [ 'mwstore://backend/container/', 'mwstore://backend/container' ],
  178. [ 'mwstore://backend/container/path', 'mwstore://backend/container/path' ],
  179. [ 'mwstore://backend/container//path', 'mwstore://backend/container/path' ],
  180. [ 'mwstore://backend/container///path', 'mwstore://backend/container/path' ],
  181. [
  182. 'mwstore://backend/container///path//to///obj',
  183. 'mwstore://backend/container/path/to/obj'
  184. ],
  185. [ 'mwstore://', null ],
  186. [ 'mwstore://backend', null ],
  187. [ 'mwstore://backend//container/path', null ],
  188. [ 'mwstore://backend//container//path', null ],
  189. [ 'mwstore:///', null ],
  190. [ 'mwstore:/', null ],
  191. [ 'mwstore:', null ],
  192. ];
  193. }
  194. /**
  195. * @dataProvider provider_testParentStoragePath
  196. */
  197. public function testParentStoragePath( $path, $res ) {
  198. $this->assertEquals( $res, FileBackend::parentStoragePath( $path ),
  199. "FileBackend::parentStoragePath on path '$path'" );
  200. }
  201. public static function provider_testParentStoragePath() {
  202. return [
  203. [ 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ],
  204. [ 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ],
  205. [ 'mwstore://backend/container/path', 'mwstore://backend/container' ],
  206. [ 'mwstore://backend/container', null ],
  207. [ 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ],
  208. [ 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ],
  209. [ 'mwstore://backend/container/path/', 'mwstore://backend/container' ],
  210. [ 'mwstore://backend/container/', null ],
  211. ];
  212. }
  213. /**
  214. * @dataProvider provider_testExtensionFromPath
  215. */
  216. public function testExtensionFromPath( $path, $res ) {
  217. $this->assertEquals( $res, FileBackend::extensionFromPath( $path ),
  218. "FileBackend::extensionFromPath on path '$path'" );
  219. }
  220. public static function provider_testExtensionFromPath() {
  221. return [
  222. [ 'mwstore://backend/container/path.txt', 'txt' ],
  223. [ 'mwstore://backend/container/path.svg.png', 'png' ],
  224. [ 'mwstore://backend/container/path', '' ],
  225. [ 'mwstore://backend/container/path.', '' ],
  226. ];
  227. }
  228. /**
  229. * @dataProvider provider_testStore
  230. */
  231. public function testStore( $op ) {
  232. $this->addTmpFiles( $op['src'] );
  233. $this->backend = $this->singleBackend;
  234. $this->tearDownFiles();
  235. $this->doTestStore( $op );
  236. $this->tearDownFiles();
  237. $this->backend = $this->multiBackend;
  238. $this->tearDownFiles();
  239. $this->doTestStore( $op );
  240. $this->tearDownFiles();
  241. }
  242. private function doTestStore( $op ) {
  243. $backendName = $this->backendClass();
  244. $source = $op['src'];
  245. $dest = $op['dst'];
  246. $this->prepare( [ 'dir' => dirname( $dest ) ] );
  247. file_put_contents( $source, "Unit test file" );
  248. if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
  249. $this->backend->store( $op );
  250. }
  251. $status = $this->backend->doOperation( $op );
  252. $this->assertGoodStatus( $status,
  253. "Store from $source to $dest succeeded without warnings ($backendName)." );
  254. $this->assertEquals( true, $status->isOK(),
  255. "Store from $source to $dest succeeded ($backendName)." );
  256. $this->assertEquals( [ 0 => true ], $status->success,
  257. "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
  258. $this->assertEquals( true, file_exists( $source ),
  259. "Source file $source still exists ($backendName)." );
  260. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $dest ] ),
  261. "Destination file $dest exists ($backendName)." );
  262. $this->assertEquals( filesize( $source ),
  263. $this->backend->getFileSize( [ 'src' => $dest ] ),
  264. "Destination file $dest has correct size ($backendName)." );
  265. $props1 = FSFile::getPropsFromPath( $source );
  266. $props2 = $this->backend->getFileProps( [ 'src' => $dest ] );
  267. $this->assertEquals( $props1, $props2,
  268. "Source and destination have the same props ($backendName)." );
  269. $this->assertBackendPathsConsistent( [ $dest ] );
  270. }
  271. public static function provider_testStore() {
  272. $cases = [];
  273. $tmpName = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
  274. $toPath = self::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
  275. $op = [ 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath ];
  276. $cases[] = [ $op ];
  277. $op2 = $op;
  278. $op2['overwrite'] = true;
  279. $cases[] = [ $op2 ];
  280. $op3 = $op;
  281. $op3['overwriteSame'] = true;
  282. $cases[] = [ $op3 ];
  283. return $cases;
  284. }
  285. /**
  286. * @dataProvider provider_testCopy
  287. */
  288. public function testCopy( $op ) {
  289. $this->backend = $this->singleBackend;
  290. $this->tearDownFiles();
  291. $this->doTestCopy( $op );
  292. $this->tearDownFiles();
  293. $this->backend = $this->multiBackend;
  294. $this->tearDownFiles();
  295. $this->doTestCopy( $op );
  296. $this->tearDownFiles();
  297. }
  298. private function doTestCopy( $op ) {
  299. $backendName = $this->backendClass();
  300. $source = $op['src'];
  301. $dest = $op['dst'];
  302. $this->prepare( [ 'dir' => dirname( $source ) ] );
  303. $this->prepare( [ 'dir' => dirname( $dest ) ] );
  304. if ( isset( $op['ignoreMissingSource'] ) ) {
  305. $status = $this->backend->doOperation( $op );
  306. $this->assertGoodStatus( $status,
  307. "Move from $source to $dest succeeded without warnings ($backendName)." );
  308. $this->assertEquals( [ 0 => true ], $status->success,
  309. "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
  310. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $source ] ),
  311. "Source file $source does not exist ($backendName)." );
  312. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $dest ] ),
  313. "Destination file $dest does not exist ($backendName)." );
  314. return; // done
  315. }
  316. $status = $this->backend->doOperation(
  317. [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
  318. $this->assertGoodStatus( $status,
  319. "Creation of file at $source succeeded ($backendName)." );
  320. if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
  321. $this->backend->copy( $op );
  322. }
  323. $status = $this->backend->doOperation( $op );
  324. $this->assertGoodStatus( $status,
  325. "Copy from $source to $dest succeeded without warnings ($backendName)." );
  326. $this->assertEquals( true, $status->isOK(),
  327. "Copy from $source to $dest succeeded ($backendName)." );
  328. $this->assertEquals( [ 0 => true ], $status->success,
  329. "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
  330. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $source ] ),
  331. "Source file $source still exists ($backendName)." );
  332. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $dest ] ),
  333. "Destination file $dest exists after copy ($backendName)." );
  334. $this->assertEquals(
  335. $this->backend->getFileSize( [ 'src' => $source ] ),
  336. $this->backend->getFileSize( [ 'src' => $dest ] ),
  337. "Destination file $dest has correct size ($backendName)." );
  338. $props1 = $this->backend->getFileProps( [ 'src' => $source ] );
  339. $props2 = $this->backend->getFileProps( [ 'src' => $dest ] );
  340. $this->assertEquals( $props1, $props2,
  341. "Source and destination have the same props ($backendName)." );
  342. $this->assertBackendPathsConsistent( [ $source, $dest ] );
  343. }
  344. public static function provider_testCopy() {
  345. $cases = [];
  346. $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
  347. $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
  348. $op = [ 'op' => 'copy', 'src' => $source, 'dst' => $dest ];
  349. $cases[] = [
  350. $op, // operation
  351. $source, // source
  352. $dest, // dest
  353. ];
  354. $op2 = $op;
  355. $op2['overwrite'] = true;
  356. $cases[] = [
  357. $op2, // operation
  358. $source, // source
  359. $dest, // dest
  360. ];
  361. $op2 = $op;
  362. $op2['overwriteSame'] = true;
  363. $cases[] = [
  364. $op2, // operation
  365. $source, // source
  366. $dest, // dest
  367. ];
  368. $op2 = $op;
  369. $op2['ignoreMissingSource'] = true;
  370. $cases[] = [
  371. $op2, // operation
  372. $source, // source
  373. $dest, // dest
  374. ];
  375. $op2 = $op;
  376. $op2['ignoreMissingSource'] = true;
  377. $cases[] = [
  378. $op2, // operation
  379. self::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
  380. $dest, // dest
  381. ];
  382. return $cases;
  383. }
  384. /**
  385. * @dataProvider provider_testMove
  386. */
  387. public function testMove( $op ) {
  388. $this->backend = $this->singleBackend;
  389. $this->tearDownFiles();
  390. $this->doTestMove( $op );
  391. $this->tearDownFiles();
  392. $this->backend = $this->multiBackend;
  393. $this->tearDownFiles();
  394. $this->doTestMove( $op );
  395. $this->tearDownFiles();
  396. }
  397. private function doTestMove( $op ) {
  398. $backendName = $this->backendClass();
  399. $source = $op['src'];
  400. $dest = $op['dst'];
  401. $this->prepare( [ 'dir' => dirname( $source ) ] );
  402. $this->prepare( [ 'dir' => dirname( $dest ) ] );
  403. if ( isset( $op['ignoreMissingSource'] ) ) {
  404. $status = $this->backend->doOperation( $op );
  405. $this->assertGoodStatus( $status,
  406. "Move from $source to $dest succeeded without warnings ($backendName)." );
  407. $this->assertEquals( [ 0 => true ], $status->success,
  408. "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
  409. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $source ] ),
  410. "Source file $source does not exist ($backendName)." );
  411. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $dest ] ),
  412. "Destination file $dest does not exist ($backendName)." );
  413. return; // done
  414. }
  415. $status = $this->backend->doOperation(
  416. [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
  417. $this->assertGoodStatus( $status,
  418. "Creation of file at $source succeeded ($backendName)." );
  419. if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
  420. $this->backend->copy( $op );
  421. }
  422. $status = $this->backend->doOperation( $op );
  423. $this->assertGoodStatus( $status,
  424. "Move from $source to $dest succeeded without warnings ($backendName)." );
  425. $this->assertEquals( true, $status->isOK(),
  426. "Move from $source to $dest succeeded ($backendName)." );
  427. $this->assertEquals( [ 0 => true ], $status->success,
  428. "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
  429. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $source ] ),
  430. "Source file $source does not still exists ($backendName)." );
  431. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $dest ] ),
  432. "Destination file $dest exists after move ($backendName)." );
  433. $this->assertNotEquals(
  434. $this->backend->getFileSize( [ 'src' => $source ] ),
  435. $this->backend->getFileSize( [ 'src' => $dest ] ),
  436. "Destination file $dest has correct size ($backendName)." );
  437. $props1 = $this->backend->getFileProps( [ 'src' => $source ] );
  438. $props2 = $this->backend->getFileProps( [ 'src' => $dest ] );
  439. $this->assertEquals( false, $props1['fileExists'],
  440. "Source file does not exist accourding to props ($backendName)." );
  441. $this->assertEquals( true, $props2['fileExists'],
  442. "Destination file exists accourding to props ($backendName)." );
  443. $this->assertBackendPathsConsistent( [ $source, $dest ] );
  444. }
  445. public static function provider_testMove() {
  446. $cases = [];
  447. $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
  448. $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
  449. $op = [ 'op' => 'move', 'src' => $source, 'dst' => $dest ];
  450. $cases[] = [
  451. $op, // operation
  452. $source, // source
  453. $dest, // dest
  454. ];
  455. $op2 = $op;
  456. $op2['overwrite'] = true;
  457. $cases[] = [
  458. $op2, // operation
  459. $source, // source
  460. $dest, // dest
  461. ];
  462. $op2 = $op;
  463. $op2['overwriteSame'] = true;
  464. $cases[] = [
  465. $op2, // operation
  466. $source, // source
  467. $dest, // dest
  468. ];
  469. $op2 = $op;
  470. $op2['ignoreMissingSource'] = true;
  471. $cases[] = [
  472. $op2, // operation
  473. $source, // source
  474. $dest, // dest
  475. ];
  476. $op2 = $op;
  477. $op2['ignoreMissingSource'] = true;
  478. $cases[] = [
  479. $op2, // operation
  480. self::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
  481. $dest, // dest
  482. ];
  483. return $cases;
  484. }
  485. /**
  486. * @dataProvider provider_testDelete
  487. */
  488. public function testDelete( $op, $withSource, $okStatus ) {
  489. $this->backend = $this->singleBackend;
  490. $this->tearDownFiles();
  491. $this->doTestDelete( $op, $withSource, $okStatus );
  492. $this->tearDownFiles();
  493. $this->backend = $this->multiBackend;
  494. $this->tearDownFiles();
  495. $this->doTestDelete( $op, $withSource, $okStatus );
  496. $this->tearDownFiles();
  497. }
  498. private function doTestDelete( $op, $withSource, $okStatus ) {
  499. $backendName = $this->backendClass();
  500. $source = $op['src'];
  501. $this->prepare( [ 'dir' => dirname( $source ) ] );
  502. if ( $withSource ) {
  503. $status = $this->backend->doOperation(
  504. [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
  505. $this->assertGoodStatus( $status,
  506. "Creation of file at $source succeeded ($backendName)." );
  507. }
  508. $status = $this->backend->doOperation( $op );
  509. if ( $okStatus ) {
  510. $this->assertGoodStatus( $status,
  511. "Deletion of file at $source succeeded without warnings ($backendName)." );
  512. $this->assertEquals( true, $status->isOK(),
  513. "Deletion of file at $source succeeded ($backendName)." );
  514. $this->assertEquals( [ 0 => true ], $status->success,
  515. "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
  516. } else {
  517. $this->assertEquals( false, $status->isOK(),
  518. "Deletion of file at $source failed ($backendName)." );
  519. }
  520. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $source ] ),
  521. "Source file $source does not exist after move ($backendName)." );
  522. $this->assertFalse(
  523. $this->backend->getFileSize( [ 'src' => $source ] ),
  524. "Source file $source has correct size (false) ($backendName)." );
  525. $props1 = $this->backend->getFileProps( [ 'src' => $source ] );
  526. $this->assertFalse( $props1['fileExists'],
  527. "Source file $source does not exist according to props ($backendName)." );
  528. $this->assertBackendPathsConsistent( [ $source ] );
  529. }
  530. public static function provider_testDelete() {
  531. $cases = [];
  532. $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
  533. $op = [ 'op' => 'delete', 'src' => $source ];
  534. $cases[] = [
  535. $op, // operation
  536. true, // with source
  537. true // succeeds
  538. ];
  539. $cases[] = [
  540. $op, // operation
  541. false, // without source
  542. false // fails
  543. ];
  544. $op['ignoreMissingSource'] = true;
  545. $cases[] = [
  546. $op, // operation
  547. false, // without source
  548. true // succeeds
  549. ];
  550. $op['ignoreMissingSource'] = true;
  551. $op['src'] = self::baseStorePath() . '/unittest-cont-bad/e/file.txt';
  552. $cases[] = [
  553. $op, // operation
  554. false, // without source
  555. true // succeeds
  556. ];
  557. return $cases;
  558. }
  559. /**
  560. * @dataProvider provider_testDescribe
  561. */
  562. public function testDescribe( $op, $withSource, $okStatus ) {
  563. $this->backend = $this->singleBackend;
  564. $this->tearDownFiles();
  565. $this->doTestDescribe( $op, $withSource, $okStatus );
  566. $this->tearDownFiles();
  567. $this->backend = $this->multiBackend;
  568. $this->tearDownFiles();
  569. $this->doTestDescribe( $op, $withSource, $okStatus );
  570. $this->tearDownFiles();
  571. }
  572. private function doTestDescribe( $op, $withSource, $okStatus ) {
  573. $backendName = $this->backendClass();
  574. $source = $op['src'];
  575. $this->prepare( [ 'dir' => dirname( $source ) ] );
  576. if ( $withSource ) {
  577. $status = $this->backend->doOperation(
  578. [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source,
  579. 'headers' => [ 'Content-Disposition' => 'xxx' ] ] );
  580. $this->assertGoodStatus( $status,
  581. "Creation of file at $source succeeded ($backendName)." );
  582. if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
  583. $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
  584. $this->assertHasHeaders( [ 'Content-Disposition' => 'xxx' ], $attr );
  585. }
  586. $status = $this->backend->describe( [ 'src' => $source,
  587. 'headers' => [ 'Content-Disposition' => '' ] ] ); // remove
  588. $this->assertGoodStatus( $status,
  589. "Removal of header for $source succeeded ($backendName)." );
  590. if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
  591. $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
  592. $this->assertFalse( isset( $attr['headers']['content-disposition'] ),
  593. "File 'Content-Disposition' header removed." );
  594. }
  595. }
  596. $status = $this->backend->doOperation( $op );
  597. if ( $okStatus ) {
  598. $this->assertGoodStatus( $status,
  599. "Describe of file at $source succeeded without warnings ($backendName)." );
  600. $this->assertEquals( true, $status->isOK(),
  601. "Describe of file at $source succeeded ($backendName)." );
  602. $this->assertEquals( [ 0 => true ], $status->success,
  603. "Describe of file at $source has proper 'success' field in Status ($backendName)." );
  604. if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
  605. $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
  606. $this->assertHasHeaders( $op['headers'], $attr );
  607. }
  608. } else {
  609. $this->assertEquals( false, $status->isOK(),
  610. "Describe of file at $source failed ($backendName)." );
  611. }
  612. $this->assertBackendPathsConsistent( [ $source ] );
  613. }
  614. private function assertHasHeaders( array $headers, array $attr ) {
  615. foreach ( $headers as $n => $v ) {
  616. if ( $n !== '' ) {
  617. $this->assertTrue( isset( $attr['headers'][strtolower( $n )] ),
  618. "File has '$n' header." );
  619. $this->assertEquals( $v, $attr['headers'][strtolower( $n )],
  620. "File has '$n' header value." );
  621. } else {
  622. $this->assertFalse( isset( $attr['headers'][strtolower( $n )] ),
  623. "File does not have '$n' header." );
  624. }
  625. }
  626. }
  627. public static function provider_testDescribe() {
  628. $cases = [];
  629. $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
  630. $op = [ 'op' => 'describe', 'src' => $source,
  631. 'headers' => [ 'Content-Disposition' => 'inline' ], ];
  632. $cases[] = [
  633. $op, // operation
  634. true, // with source
  635. true // succeeds
  636. ];
  637. $cases[] = [
  638. $op, // operation
  639. false, // without source
  640. false // fails
  641. ];
  642. return $cases;
  643. }
  644. /**
  645. * @dataProvider provider_testCreate
  646. */
  647. public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
  648. $this->backend = $this->singleBackend;
  649. $this->tearDownFiles();
  650. $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
  651. $this->tearDownFiles();
  652. $this->backend = $this->multiBackend;
  653. $this->tearDownFiles();
  654. $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
  655. $this->tearDownFiles();
  656. }
  657. private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
  658. $backendName = $this->backendClass();
  659. $dest = $op['dst'];
  660. $this->prepare( [ 'dir' => dirname( $dest ) ] );
  661. $oldText = 'blah...blah...waahwaah';
  662. if ( $alreadyExists ) {
  663. $status = $this->backend->doOperation(
  664. [ 'op' => 'create', 'content' => $oldText, 'dst' => $dest ] );
  665. $this->assertGoodStatus( $status,
  666. "Creation of file at $dest succeeded ($backendName)." );
  667. }
  668. $status = $this->backend->doOperation( $op );
  669. if ( $okStatus ) {
  670. $this->assertGoodStatus( $status,
  671. "Creation of file at $dest succeeded without warnings ($backendName)." );
  672. $this->assertEquals( true, $status->isOK(),
  673. "Creation of file at $dest succeeded ($backendName)." );
  674. $this->assertEquals( [ 0 => true ], $status->success,
  675. "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
  676. } else {
  677. $this->assertEquals( false, $status->isOK(),
  678. "Creation of file at $dest failed ($backendName)." );
  679. }
  680. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $dest ] ),
  681. "Destination file $dest exists after creation ($backendName)." );
  682. $props1 = $this->backend->getFileProps( [ 'src' => $dest ] );
  683. $this->assertEquals( true, $props1['fileExists'],
  684. "Destination file $dest exists according to props ($backendName)." );
  685. if ( $okStatus ) { // file content is what we saved
  686. $this->assertEquals( $newSize, $props1['size'],
  687. "Destination file $dest has expected size according to props ($backendName)." );
  688. $this->assertEquals( $newSize,
  689. $this->backend->getFileSize( [ 'src' => $dest ] ),
  690. "Destination file $dest has correct size ($backendName)." );
  691. } else { // file content is some other previous text
  692. $this->assertEquals( strlen( $oldText ), $props1['size'],
  693. "Destination file $dest has original size according to props ($backendName)." );
  694. $this->assertEquals( strlen( $oldText ),
  695. $this->backend->getFileSize( [ 'src' => $dest ] ),
  696. "Destination file $dest has original size according to props ($backendName)." );
  697. }
  698. $this->assertBackendPathsConsistent( [ $dest ] );
  699. }
  700. /**
  701. * @dataProvider provider_testCreate
  702. */
  703. public static function provider_testCreate() {
  704. $cases = [];
  705. $dest = self::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
  706. $op = [ 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest ];
  707. $cases[] = [
  708. $op, // operation
  709. false, // no dest already exists
  710. true, // succeeds
  711. strlen( $op['content'] )
  712. ];
  713. $op2 = $op;
  714. $op2['content'] = "\n";
  715. $cases[] = [
  716. $op2, // operation
  717. false, // no dest already exists
  718. true, // succeeds
  719. strlen( $op2['content'] )
  720. ];
  721. $op2 = $op;
  722. $op2['content'] = "fsf\n waf 3kt";
  723. $cases[] = [
  724. $op2, // operation
  725. true, // dest already exists
  726. false, // fails
  727. strlen( $op2['content'] )
  728. ];
  729. $op2 = $op;
  730. $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
  731. $op2['overwrite'] = true;
  732. $cases[] = [
  733. $op2, // operation
  734. true, // dest already exists
  735. true, // succeeds
  736. strlen( $op2['content'] )
  737. ];
  738. $op2 = $op;
  739. $op2['content'] = "39qjmg3-qg";
  740. $op2['overwriteSame'] = true;
  741. $cases[] = [
  742. $op2, // operation
  743. true, // dest already exists
  744. false, // succeeds
  745. strlen( $op2['content'] )
  746. ];
  747. return $cases;
  748. }
  749. public function testDoQuickOperations() {
  750. $this->backend = $this->singleBackend;
  751. $this->doTestDoQuickOperations();
  752. $this->tearDownFiles();
  753. $this->backend = $this->multiBackend;
  754. $this->doTestDoQuickOperations();
  755. $this->tearDownFiles();
  756. }
  757. private function doTestDoQuickOperations() {
  758. $backendName = $this->backendClass();
  759. $base = self::baseStorePath();
  760. $files = [
  761. "$base/unittest-cont1/e/fileA.a",
  762. "$base/unittest-cont1/e/fileB.a",
  763. "$base/unittest-cont1/e/fileC.a"
  764. ];
  765. $createOps = [];
  766. $purgeOps = [];
  767. foreach ( $files as $path ) {
  768. $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
  769. $this->assertGoodStatus( $status,
  770. "Preparing $path succeeded without warnings ($backendName)." );
  771. $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) ];
  772. $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
  773. $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
  774. $purgeOps[] = [ 'op' => 'delete', 'src' => $path ];
  775. $purgeOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
  776. }
  777. $purgeOps[] = [ 'op' => 'null' ];
  778. $this->assertGoodStatus(
  779. $this->backend->doQuickOperations( $createOps ),
  780. "Creation of source files succeeded ($backendName)." );
  781. foreach ( $files as $file ) {
  782. $this->assertTrue( $this->backend->fileExists( [ 'src' => $file ] ),
  783. "File $file exists." );
  784. }
  785. $this->assertGoodStatus(
  786. $this->backend->doQuickOperations( $copyOps ),
  787. "Quick copy of source files succeeded ($backendName)." );
  788. foreach ( $files as $file ) {
  789. $this->assertTrue( $this->backend->fileExists( [ 'src' => "$file-2" ] ),
  790. "File $file-2 exists." );
  791. }
  792. $this->assertGoodStatus(
  793. $this->backend->doQuickOperations( $moveOps ),
  794. "Quick move of source files succeeded ($backendName)." );
  795. foreach ( $files as $file ) {
  796. $this->assertTrue( $this->backend->fileExists( [ 'src' => "$file-3" ] ),
  797. "File $file-3 move in." );
  798. $this->assertFalse( $this->backend->fileExists( [ 'src' => "$file-2" ] ),
  799. "File $file-2 moved away." );
  800. }
  801. $this->assertGoodStatus(
  802. $this->backend->quickCopy( [ 'src' => $files[0], 'dst' => $files[0] ] ),
  803. "Copy of file {$files[0]} over itself succeeded ($backendName)." );
  804. $this->assertTrue( $this->backend->fileExists( [ 'src' => $files[0] ] ),
  805. "File {$files[0]} still exists." );
  806. $this->assertGoodStatus(
  807. $this->backend->quickMove( [ 'src' => $files[0], 'dst' => $files[0] ] ),
  808. "Move of file {$files[0]} over itself succeeded ($backendName)." );
  809. $this->assertTrue( $this->backend->fileExists( [ 'src' => $files[0] ] ),
  810. "File {$files[0]} still exists." );
  811. $this->assertGoodStatus(
  812. $this->backend->doQuickOperations( $purgeOps ),
  813. "Quick deletion of source files succeeded ($backendName)." );
  814. foreach ( $files as $file ) {
  815. $this->assertFalse( $this->backend->fileExists( [ 'src' => $file ] ),
  816. "File $file purged." );
  817. $this->assertFalse( $this->backend->fileExists( [ 'src' => "$file-3" ] ),
  818. "File $file-3 purged." );
  819. }
  820. }
  821. /**
  822. * @dataProvider provider_testConcatenate
  823. */
  824. public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
  825. $this->backend = $this->singleBackend;
  826. $this->tearDownFiles();
  827. $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
  828. $this->tearDownFiles();
  829. $this->backend = $this->multiBackend;
  830. $this->tearDownFiles();
  831. $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
  832. $this->tearDownFiles();
  833. }
  834. private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
  835. $backendName = $this->backendClass();
  836. $expContent = '';
  837. // Create sources
  838. $ops = [];
  839. foreach ( $srcs as $i => $source ) {
  840. $this->prepare( [ 'dir' => dirname( $source ) ] );
  841. $ops[] = [
  842. 'op' => 'create', // operation
  843. 'dst' => $source, // source
  844. 'content' => $srcsContent[$i]
  845. ];
  846. $expContent .= $srcsContent[$i];
  847. }
  848. $status = $this->backend->doOperations( $ops );
  849. $this->assertGoodStatus( $status,
  850. "Creation of source files succeeded ($backendName)." );
  851. $dest = $params['dst'] = $this->getNewTempFile();
  852. if ( $alreadyExists ) {
  853. $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
  854. $this->assertEquals( true, $ok,
  855. "Creation of file at $dest succeeded ($backendName)." );
  856. } else {
  857. $ok = file_put_contents( $dest, '' ) !== false;
  858. $this->assertEquals( true, $ok,
  859. "Creation of 0-byte file at $dest succeeded ($backendName)." );
  860. }
  861. // Combine the files into one
  862. $status = $this->backend->concatenate( $params );
  863. if ( $okStatus ) {
  864. $this->assertGoodStatus( $status,
  865. "Creation of concat file at $dest succeeded without warnings ($backendName)." );
  866. $this->assertEquals( true, $status->isOK(),
  867. "Creation of concat file at $dest succeeded ($backendName)." );
  868. } else {
  869. $this->assertEquals( false, $status->isOK(),
  870. "Creation of concat file at $dest failed ($backendName)." );
  871. }
  872. if ( $okStatus ) {
  873. $this->assertEquals( true, is_file( $dest ),
  874. "Dest concat file $dest exists after creation ($backendName)." );
  875. } else {
  876. $this->assertEquals( true, is_file( $dest ),
  877. "Dest concat file $dest exists after failed creation ($backendName)." );
  878. }
  879. $contents = file_get_contents( $dest );
  880. $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
  881. if ( $okStatus ) {
  882. $this->assertEquals( $expContent, $contents,
  883. "Concat file at $dest has correct contents ($backendName)." );
  884. } else {
  885. $this->assertNotEquals( $expContent, $contents,
  886. "Concat file at $dest has correct contents ($backendName)." );
  887. }
  888. }
  889. public static function provider_testConcatenate() {
  890. $cases = [];
  891. $srcs = [
  892. self::baseStorePath() . '/unittest-cont1/e/file1.txt',
  893. self::baseStorePath() . '/unittest-cont1/e/file2.txt',
  894. self::baseStorePath() . '/unittest-cont1/e/file3.txt',
  895. self::baseStorePath() . '/unittest-cont1/e/file4.txt',
  896. self::baseStorePath() . '/unittest-cont1/e/file5.txt',
  897. self::baseStorePath() . '/unittest-cont1/e/file6.txt',
  898. self::baseStorePath() . '/unittest-cont1/e/file7.txt',
  899. self::baseStorePath() . '/unittest-cont1/e/file8.txt',
  900. self::baseStorePath() . '/unittest-cont1/e/file9.txt',
  901. self::baseStorePath() . '/unittest-cont1/e/file10.txt'
  902. ];
  903. $content = [
  904. 'egfage',
  905. 'ageageag',
  906. 'rhokohlr',
  907. 'shgmslkg',
  908. 'kenga',
  909. 'owagmal',
  910. 'kgmae',
  911. 'g eak;g',
  912. 'lkaem;a',
  913. 'legma'
  914. ];
  915. $params = [ 'srcs' => $srcs ];
  916. $cases[] = [
  917. $params, // operation
  918. $srcs, // sources
  919. $content, // content for each source
  920. false, // no dest already exists
  921. true, // succeeds
  922. ];
  923. $cases[] = [
  924. $params, // operation
  925. $srcs, // sources
  926. $content, // content for each source
  927. true, // dest already exists
  928. false, // succeeds
  929. ];
  930. return $cases;
  931. }
  932. /**
  933. * @dataProvider provider_testGetFileStat
  934. */
  935. public function testGetFileStat( $path, $content, $alreadyExists ) {
  936. $this->backend = $this->singleBackend;
  937. $this->tearDownFiles();
  938. $this->doTestGetFileStat( $path, $content, $alreadyExists );
  939. $this->tearDownFiles();
  940. $this->backend = $this->multiBackend;
  941. $this->tearDownFiles();
  942. $this->doTestGetFileStat( $path, $content, $alreadyExists );
  943. $this->tearDownFiles();
  944. }
  945. private function doTestGetFileStat( $path, $content, $alreadyExists ) {
  946. $backendName = $this->backendClass();
  947. if ( $alreadyExists ) {
  948. $this->prepare( [ 'dir' => dirname( $path ) ] );
  949. $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
  950. $this->assertGoodStatus( $status,
  951. "Creation of file at $path succeeded ($backendName)." );
  952. $size = $this->backend->getFileSize( [ 'src' => $path ] );
  953. $time = $this->backend->getFileTimestamp( [ 'src' => $path ] );
  954. $stat = $this->backend->getFileStat( [ 'src' => $path ] );
  955. $this->assertEquals( strlen( $content ), $size,
  956. "Correct file size of '$path'" );
  957. $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
  958. "Correct file timestamp of '$path'" );
  959. $size = $stat['size'];
  960. $time = $stat['mtime'];
  961. $this->assertEquals( strlen( $content ), $size,
  962. "Correct file size of '$path'" );
  963. $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
  964. "Correct file timestamp of '$path'" );
  965. $this->backend->clearCache( [ $path ] );
  966. $size = $this->backend->getFileSize( [ 'src' => $path ] );
  967. $this->assertEquals( strlen( $content ), $size,
  968. "Correct file size of '$path'" );
  969. $this->backend->preloadCache( [ $path ] );
  970. $size = $this->backend->getFileSize( [ 'src' => $path ] );
  971. $this->assertEquals( strlen( $content ), $size,
  972. "Correct file size of '$path'" );
  973. } else {
  974. $size = $this->backend->getFileSize( [ 'src' => $path ] );
  975. $time = $this->backend->getFileTimestamp( [ 'src' => $path ] );
  976. $stat = $this->backend->getFileStat( [ 'src' => $path ] );
  977. $this->assertFalse( $size, "Correct file size of '$path'" );
  978. $this->assertFalse( $time, "Correct file timestamp of '$path'" );
  979. $this->assertFalse( $stat, "Correct file stat of '$path'" );
  980. }
  981. }
  982. public static function provider_testGetFileStat() {
  983. $cases = [];
  984. $base = self::baseStorePath();
  985. $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
  986. $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
  987. $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
  988. return $cases;
  989. }
  990. /**
  991. * @dataProvider provider_testGetFileStat
  992. */
  993. public function testStreamFile( $path, $content, $alreadyExists ) {
  994. $this->backend = $this->singleBackend;
  995. $this->tearDownFiles();
  996. $this->doTestStreamFile( $path, $content, $alreadyExists );
  997. $this->tearDownFiles();
  998. $this->backend = $this->multiBackend;
  999. $this->tearDownFiles();
  1000. $this->doTestStreamFile( $path, $content, $alreadyExists );
  1001. $this->tearDownFiles();
  1002. }
  1003. private function doTestStreamFile( $path, $content ) {
  1004. $backendName = $this->backendClass();
  1005. if ( $content !== null ) {
  1006. $this->prepare( [ 'dir' => dirname( $path ) ] );
  1007. $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
  1008. $this->assertGoodStatus( $status,
  1009. "Creation of file at $path succeeded ($backendName)." );
  1010. ob_start();
  1011. $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
  1012. $data = ob_get_contents();
  1013. ob_end_clean();
  1014. $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
  1015. } else { // 404 case
  1016. ob_start();
  1017. $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
  1018. $data = ob_get_contents();
  1019. ob_end_clean();
  1020. $this->assertRegExp( '#<h1>File not found</h1>#', $data,
  1021. "Correct content streamed from '$path' ($backendName)" );
  1022. }
  1023. }
  1024. public static function provider_testStreamFile() {
  1025. $cases = [];
  1026. $base = self::baseStorePath();
  1027. $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
  1028. $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
  1029. return $cases;
  1030. }
  1031. public function testStreamFileRange() {
  1032. $this->backend = $this->singleBackend;
  1033. $this->tearDownFiles();
  1034. $this->doTestStreamFileRange();
  1035. $this->tearDownFiles();
  1036. $this->backend = $this->multiBackend;
  1037. $this->tearDownFiles();
  1038. $this->doTestStreamFileRange();
  1039. $this->tearDownFiles();
  1040. }
  1041. private function doTestStreamFileRange() {
  1042. $backendName = $this->backendClass();
  1043. $base = self::baseStorePath();
  1044. $path = "$base/unittest-cont1/e/b/z/range_file.txt";
  1045. $content = "0123456789ABCDEF";
  1046. $this->prepare( [ 'dir' => dirname( $path ) ] );
  1047. $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
  1048. $this->assertGoodStatus( $status,
  1049. "Creation of file at $path succeeded ($backendName)." );
  1050. static $ranges = [
  1051. 'bytes=0-0' => '0',
  1052. 'bytes=0-3' => '0123',
  1053. 'bytes=4-8' => '45678',
  1054. 'bytes=15-15' => 'F',
  1055. 'bytes=14-15' => 'EF',
  1056. 'bytes=-5' => 'BCDEF',
  1057. 'bytes=-1' => 'F',
  1058. 'bytes=10-16' => 'ABCDEF',
  1059. 'bytes=10-99' => 'ABCDEF',
  1060. ];
  1061. foreach ( $ranges as $range => $chunk ) {
  1062. ob_start();
  1063. $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
  1064. 'options' => [ 'range' => $range ] ] );
  1065. $data = ob_get_contents();
  1066. ob_end_clean();
  1067. $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
  1068. }
  1069. }
  1070. /**
  1071. * @dataProvider provider_testGetFileContents
  1072. */
  1073. public function testGetFileContents( $source, $content ) {
  1074. $this->backend = $this->singleBackend;
  1075. $this->tearDownFiles();
  1076. $this->doTestGetFileContents( $source, $content );
  1077. $this->tearDownFiles();
  1078. $this->backend = $this->multiBackend;
  1079. $this->tearDownFiles();
  1080. $this->doTestGetFileContents( $source, $content );
  1081. $this->tearDownFiles();
  1082. }
  1083. private function doTestGetFileContents( $source, $content ) {
  1084. $backendName = $this->backendClass();
  1085. $srcs = (array)$source;
  1086. $content = (array)$content;
  1087. foreach ( $srcs as $i => $src ) {
  1088. $this->prepare( [ 'dir' => dirname( $src ) ] );
  1089. $status = $this->backend->doOperation(
  1090. [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
  1091. $this->assertGoodStatus( $status,
  1092. "Creation of file at $src succeeded ($backendName)." );
  1093. }
  1094. if ( is_array( $source ) ) {
  1095. $contents = $this->backend->getFileContentsMulti( [ 'srcs' => $source ] );
  1096. foreach ( $contents as $path => $data ) {
  1097. $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
  1098. $this->assertEquals(
  1099. current( $content ),
  1100. $data,
  1101. "Contents of $path is correct ($backendName)."
  1102. );
  1103. next( $content );
  1104. }
  1105. $this->assertEquals(
  1106. $source,
  1107. array_keys( $contents ),
  1108. "Contents in right order ($backendName)."
  1109. );
  1110. $this->assertEquals(
  1111. count( $source ),
  1112. count( $contents ),
  1113. "Contents array size correct ($backendName)."
  1114. );
  1115. } else {
  1116. $data = $this->backend->getFileContents( [ 'src' => $source ] );
  1117. $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
  1118. $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
  1119. }
  1120. }
  1121. public static function provider_testGetFileContents() {
  1122. $cases = [];
  1123. $base = self::baseStorePath();
  1124. $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
  1125. $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
  1126. $cases[] = [
  1127. [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
  1128. "$base/unittest-cont1/e/a/z.txt" ],
  1129. [ "contents xx", "contents xy", "contents xz" ]
  1130. ];
  1131. return $cases;
  1132. }
  1133. /**
  1134. * @dataProvider provider_testGetLocalCopy
  1135. */
  1136. public function testGetLocalCopy( $source, $content ) {
  1137. $this->backend = $this->singleBackend;
  1138. $this->tearDownFiles();
  1139. $this->doTestGetLocalCopy( $source, $content );
  1140. $this->tearDownFiles();
  1141. $this->backend = $this->multiBackend;
  1142. $this->tearDownFiles();
  1143. $this->doTestGetLocalCopy( $source, $content );
  1144. $this->tearDownFiles();
  1145. }
  1146. private function doTestGetLocalCopy( $source, $content ) {
  1147. $backendName = $this->backendClass();
  1148. $srcs = (array)$source;
  1149. $content = (array)$content;
  1150. foreach ( $srcs as $i => $src ) {
  1151. $this->prepare( [ 'dir' => dirname( $src ) ] );
  1152. $status = $this->backend->doOperation(
  1153. [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
  1154. $this->assertGoodStatus( $status,
  1155. "Creation of file at $src succeeded ($backendName)." );
  1156. }
  1157. if ( is_array( $source ) ) {
  1158. $tmpFiles = $this->backend->getLocalCopyMulti( [ 'srcs' => $source ] );
  1159. foreach ( $tmpFiles as $path => $tmpFile ) {
  1160. $this->assertNotNull( $tmpFile,
  1161. "Creation of local copy of $path succeeded ($backendName)." );
  1162. $contents = file_get_contents( $tmpFile->getPath() );
  1163. $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
  1164. $this->assertEquals(
  1165. current( $content ),
  1166. $contents,
  1167. "Local copy of $path is correct ($backendName)."
  1168. );
  1169. next( $content );
  1170. }
  1171. $this->assertEquals(
  1172. $source,
  1173. array_keys( $tmpFiles ),
  1174. "Local copies in right order ($backendName)."
  1175. );
  1176. $this->assertEquals(
  1177. count( $source ),
  1178. count( $tmpFiles ),
  1179. "Local copies array size correct ($backendName)."
  1180. );
  1181. } else {
  1182. $tmpFile = $this->backend->getLocalCopy( [ 'src' => $source ] );
  1183. $this->assertNotNull( $tmpFile,
  1184. "Creation of local copy of $source succeeded ($backendName)." );
  1185. $contents = file_get_contents( $tmpFile->getPath() );
  1186. $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
  1187. $this->assertEquals(
  1188. $content[0],
  1189. $contents,
  1190. "Local copy of $source is correct ($backendName)."
  1191. );
  1192. }
  1193. $obj = new stdClass();
  1194. $tmpFile->bind( $obj );
  1195. }
  1196. public static function provider_testGetLocalCopy() {
  1197. $cases = [];
  1198. $base = self::baseStorePath();
  1199. $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
  1200. $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
  1201. $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
  1202. $cases[] = [
  1203. [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
  1204. "$base/unittest-cont1/e/a/z.txt" ],
  1205. [ "contents xx $", "contents xy 111", "contents xz" ]
  1206. ];
  1207. return $cases;
  1208. }
  1209. /**
  1210. * @dataProvider provider_testGetLocalReference
  1211. */
  1212. public function testGetLocalReference( $source, $content ) {
  1213. $this->backend = $this->singleBackend;
  1214. $this->tearDownFiles();
  1215. $this->doTestGetLocalReference( $source, $content );
  1216. $this->tearDownFiles();
  1217. $this->backend = $this->multiBackend;
  1218. $this->tearDownFiles();
  1219. $this->doTestGetLocalReference( $source, $content );
  1220. $this->tearDownFiles();
  1221. }
  1222. private function doTestGetLocalReference( $source, $content ) {
  1223. $backendName = $this->backendClass();
  1224. $srcs = (array)$source;
  1225. $content = (array)$content;
  1226. foreach ( $srcs as $i => $src ) {
  1227. $this->prepare( [ 'dir' => dirname( $src ) ] );
  1228. $status = $this->backend->doOperation(
  1229. [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
  1230. $this->assertGoodStatus( $status,
  1231. "Creation of file at $src succeeded ($backendName)." );
  1232. }
  1233. if ( is_array( $source ) ) {
  1234. $tmpFiles = $this->backend->getLocalReferenceMulti( [ 'srcs' => $source ] );
  1235. foreach ( $tmpFiles as $path => $tmpFile ) {
  1236. $this->assertNotNull( $tmpFile,
  1237. "Creation of local copy of $path succeeded ($backendName)." );
  1238. $contents = file_get_contents( $tmpFile->getPath() );
  1239. $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
  1240. $this->assertEquals(
  1241. current( $content ),
  1242. $contents,
  1243. "Local ref of $path is correct ($backendName)."
  1244. );
  1245. next( $content );
  1246. }
  1247. $this->assertEquals(
  1248. $source,
  1249. array_keys( $tmpFiles ),
  1250. "Local refs in right order ($backendName)."
  1251. );
  1252. $this->assertEquals(
  1253. count( $source ),
  1254. count( $tmpFiles ),
  1255. "Local refs array size correct ($backendName)."
  1256. );
  1257. } else {
  1258. $tmpFile = $this->backend->getLocalReference( [ 'src' => $source ] );
  1259. $this->assertNotNull( $tmpFile,
  1260. "Creation of local copy of $source succeeded ($backendName)." );
  1261. $contents = file_get_contents( $tmpFile->getPath() );
  1262. $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
  1263. $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
  1264. }
  1265. }
  1266. public static function provider_testGetLocalReference() {
  1267. $cases = [];
  1268. $base = self::baseStorePath();
  1269. $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
  1270. $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
  1271. $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
  1272. $cases[] = [
  1273. [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
  1274. "$base/unittest-cont1/e/a/z.txt" ],
  1275. [ "contents xx 1111", "contents xy %", "contents xz $" ]
  1276. ];
  1277. return $cases;
  1278. }
  1279. public function testGetLocalCopyAndReference404() {
  1280. $this->backend = $this->singleBackend;
  1281. $this->tearDownFiles();
  1282. $this->doTestGetLocalCopyAndReference404();
  1283. $this->tearDownFiles();
  1284. $this->backend = $this->multiBackend;
  1285. $this->tearDownFiles();
  1286. $this->doTestGetLocalCopyAndReference404();
  1287. $this->tearDownFiles();
  1288. }
  1289. public function doTestGetLocalCopyAndReference404() {
  1290. $backendName = $this->backendClass();
  1291. $base = self::baseStorePath();
  1292. $tmpFile = $this->backend->getLocalCopy( [
  1293. 'src' => "$base/unittest-cont1/not-there" ] );
  1294. $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
  1295. $tmpFile = $this->backend->getLocalReference( [
  1296. 'src' => "$base/unittest-cont1/not-there" ] );
  1297. $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
  1298. }
  1299. /**
  1300. * @dataProvider provider_testGetFileHttpUrl
  1301. */
  1302. public function testGetFileHttpUrl( $source, $content ) {
  1303. $this->backend = $this->singleBackend;
  1304. $this->tearDownFiles();
  1305. $this->doTestGetFileHttpUrl( $source, $content );
  1306. $this->tearDownFiles();
  1307. $this->backend = $this->multiBackend;
  1308. $this->tearDownFiles();
  1309. $this->doTestGetFileHttpUrl( $source, $content );
  1310. $this->tearDownFiles();
  1311. }
  1312. private function doTestGetFileHttpUrl( $source, $content ) {
  1313. $backendName = $this->backendClass();
  1314. $this->prepare( [ 'dir' => dirname( $source ) ] );
  1315. $status = $this->backend->doOperation(
  1316. [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
  1317. $this->assertGoodStatus( $status,
  1318. "Creation of file at $source succeeded ($backendName)." );
  1319. $url = $this->backend->getFileHttpUrl( [ 'src' => $source ] );
  1320. if ( $url !== null ) { // supported
  1321. $data = Http::request( "GET", $url, [], __METHOD__ );
  1322. $this->assertEquals( $content, $data,
  1323. "HTTP GET of URL has right contents ($backendName)." );
  1324. }
  1325. }
  1326. public static function provider_testGetFileHttpUrl() {
  1327. $cases = [];
  1328. $base = self::baseStorePath();
  1329. $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
  1330. $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
  1331. $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
  1332. return $cases;
  1333. }
  1334. /**
  1335. * @dataProvider provider_testPrepareAndClean
  1336. */
  1337. public function testPrepareAndClean( $path, $isOK ) {
  1338. $this->backend = $this->singleBackend;
  1339. $this->doTestPrepareAndClean( $path, $isOK );
  1340. $this->tearDownFiles();
  1341. $this->backend = $this->multiBackend;
  1342. $this->doTestPrepareAndClean( $path, $isOK );
  1343. $this->tearDownFiles();
  1344. }
  1345. public static function provider_testPrepareAndClean() {
  1346. $base = self::baseStorePath();
  1347. return [
  1348. [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
  1349. [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
  1350. # Specific to FS backend with no basePath field set
  1351. # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
  1352. ];
  1353. }
  1354. private function doTestPrepareAndClean( $path, $isOK ) {
  1355. $backendName = $this->backendClass();
  1356. $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
  1357. if ( $isOK ) {
  1358. $this->assertGoodStatus( $status,
  1359. "Preparing dir $path succeeded without warnings ($backendName)." );
  1360. $this->assertEquals( true, $status->isOK(),
  1361. "Preparing dir $path succeeded ($backendName)." );
  1362. } else {
  1363. $this->assertEquals( false, $status->isOK(),
  1364. "Preparing dir $path failed ($backendName)." );
  1365. }
  1366. $status = $this->backend->secure( [ 'dir' => dirname( $path ) ] );
  1367. if ( $isOK ) {
  1368. $this->assertGoodStatus( $status,
  1369. "Securing dir $path succeeded without warnings ($backendName)." );
  1370. $this->assertEquals( true, $status->isOK(),
  1371. "Securing dir $path succeeded ($backendName)." );
  1372. } else {
  1373. $this->assertEquals( false, $status->isOK(),
  1374. "Securing dir $path failed ($backendName)." );
  1375. }
  1376. $status = $this->backend->publish( [ 'dir' => dirname( $path ) ] );
  1377. if ( $isOK ) {
  1378. $this->assertGoodStatus( $status,
  1379. "Publishing dir $path succeeded without warnings ($backendName)." );
  1380. $this->assertEquals( true, $status->isOK(),
  1381. "Publishing dir $path succeeded ($backendName)." );
  1382. } else {
  1383. $this->assertEquals( false, $status->isOK(),
  1384. "Publishing dir $path failed ($backendName)." );
  1385. }
  1386. $status = $this->backend->clean( [ 'dir' => dirname( $path ) ] );
  1387. if ( $isOK ) {
  1388. $this->assertGoodStatus( $status,
  1389. "Cleaning dir $path succeeded without warnings ($backendName)." );
  1390. $this->assertEquals( true, $status->isOK(),
  1391. "Cleaning dir $path succeeded ($backendName)." );
  1392. } else {
  1393. $this->assertEquals( false, $status->isOK(),
  1394. "Cleaning dir $path failed ($backendName)." );
  1395. }
  1396. }
  1397. public function testRecursiveClean() {
  1398. $this->backend = $this->singleBackend;
  1399. $this->doTestRecursiveClean();
  1400. $this->tearDownFiles();
  1401. $this->backend = $this->multiBackend;
  1402. $this->doTestRecursiveClean();
  1403. $this->tearDownFiles();
  1404. }
  1405. private function doTestRecursiveClean() {
  1406. $backendName = $this->backendClass();
  1407. $base = self::baseStorePath();
  1408. $dirs = [
  1409. "$base/unittest-cont1",
  1410. "$base/unittest-cont1/e",
  1411. "$base/unittest-cont1/e/a",
  1412. "$base/unittest-cont1/e/a/b",
  1413. "$base/unittest-cont1/e/a/b/c",
  1414. "$base/unittest-cont1/e/a/b/c/d0",
  1415. "$base/unittest-cont1/e/a/b/c/d1",
  1416. "$base/unittest-cont1/e/a/b/c/d2",
  1417. "$base/unittest-cont1/e/a/b/c/d0/1",
  1418. "$base/unittest-cont1/e/a/b/c/d0/2",
  1419. "$base/unittest-cont1/e/a/b/c/d1/3",
  1420. "$base/unittest-cont1/e/a/b/c/d1/4",
  1421. "$base/unittest-cont1/e/a/b/c/d2/5",
  1422. "$base/unittest-cont1/e/a/b/c/d2/6"
  1423. ];
  1424. foreach ( $dirs as $dir ) {
  1425. $status = $this->prepare( [ 'dir' => $dir ] );
  1426. $this->assertGoodStatus( $status,
  1427. "Preparing dir $dir succeeded without warnings ($backendName)." );
  1428. }
  1429. if ( $this->backend instanceof FSFileBackend ) {
  1430. foreach ( $dirs as $dir ) {
  1431. $this->assertEquals( true, $this->backend->directoryExists( [ 'dir' => $dir ] ),
  1432. "Dir $dir exists ($backendName)." );
  1433. }
  1434. }
  1435. $status = $this->backend->clean(
  1436. [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
  1437. $this->assertGoodStatus( $status,
  1438. "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
  1439. foreach ( $dirs as $dir ) {
  1440. $this->assertEquals( false, $this->backend->directoryExists( [ 'dir' => $dir ] ),
  1441. "Dir $dir no longer exists ($backendName)." );
  1442. }
  1443. }
  1444. public function testDoOperations() {
  1445. $this->backend = $this->singleBackend;
  1446. $this->tearDownFiles();
  1447. $this->doTestDoOperations();
  1448. $this->tearDownFiles();
  1449. $this->backend = $this->multiBackend;
  1450. $this->tearDownFiles();
  1451. $this->doTestDoOperations();
  1452. $this->tearDownFiles();
  1453. }
  1454. private function doTestDoOperations() {
  1455. $base = self::baseStorePath();
  1456. $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
  1457. $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
  1458. $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
  1459. $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
  1460. $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
  1461. $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
  1462. $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
  1463. $this->prepare( [ 'dir' => dirname( $fileA ) ] );
  1464. $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
  1465. $this->prepare( [ 'dir' => dirname( $fileB ) ] );
  1466. $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
  1467. $this->prepare( [ 'dir' => dirname( $fileC ) ] );
  1468. $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
  1469. $this->prepare( [ 'dir' => dirname( $fileD ) ] );
  1470. $status = $this->backend->doOperations( [
  1471. [ 'op' => 'describe', 'src' => $fileA,
  1472. 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
  1473. [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
  1474. // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
  1475. [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
  1476. // Now: A:<A>, B:<B>, C:<A>, D:<empty>
  1477. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
  1478. // Now: A:<A>, B:<B>, C:<empty>, D:<A>
  1479. [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
  1480. // Now: A:<A>, B:<empty>, C:<B>, D:<A>
  1481. [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
  1482. // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
  1483. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
  1484. // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
  1485. [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
  1486. // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
  1487. [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
  1488. // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
  1489. [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
  1490. // Does nothing
  1491. [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
  1492. // Does nothing
  1493. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
  1494. // Does nothing
  1495. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
  1496. // Does nothing
  1497. [ 'op' => 'null' ],
  1498. // Does nothing
  1499. ] );
  1500. $this->assertGoodStatus( $status, "Operation batch succeeded" );
  1501. $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
  1502. $this->assertEquals( 14, count( $status->success ),
  1503. "Operation batch has correct success array" );
  1504. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileA ] ),
  1505. "File does not exist at $fileA" );
  1506. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
  1507. "File does not exist at $fileB" );
  1508. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
  1509. "File does not exist at $fileD" );
  1510. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
  1511. "File exists at $fileC" );
  1512. $this->assertEquals( $fileBContents,
  1513. $this->backend->getFileContents( [ 'src' => $fileC ] ),
  1514. "Correct file contents of $fileC" );
  1515. $this->assertEquals( strlen( $fileBContents ),
  1516. $this->backend->getFileSize( [ 'src' => $fileC ] ),
  1517. "Correct file size of $fileC" );
  1518. $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
  1519. $this->backend->getFileSha1Base36( [ 'src' => $fileC ] ),
  1520. "Correct file SHA-1 of $fileC" );
  1521. }
  1522. public function testDoOperationsPipeline() {
  1523. $this->backend = $this->singleBackend;
  1524. $this->tearDownFiles();
  1525. $this->doTestDoOperationsPipeline();
  1526. $this->tearDownFiles();
  1527. $this->backend = $this->multiBackend;
  1528. $this->tearDownFiles();
  1529. $this->doTestDoOperationsPipeline();
  1530. $this->tearDownFiles();
  1531. }
  1532. // concurrency orientated
  1533. private function doTestDoOperationsPipeline() {
  1534. $base = self::baseStorePath();
  1535. $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
  1536. $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
  1537. $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
  1538. $tmpNameA = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
  1539. $tmpNameB = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
  1540. $tmpNameC = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
  1541. $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
  1542. file_put_contents( $tmpNameA, $fileAContents );
  1543. file_put_contents( $tmpNameB, $fileBContents );
  1544. file_put_contents( $tmpNameC, $fileCContents );
  1545. $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
  1546. $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
  1547. $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
  1548. $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
  1549. $this->prepare( [ 'dir' => dirname( $fileA ) ] );
  1550. $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
  1551. $this->prepare( [ 'dir' => dirname( $fileB ) ] );
  1552. $this->prepare( [ 'dir' => dirname( $fileC ) ] );
  1553. $this->prepare( [ 'dir' => dirname( $fileD ) ] );
  1554. $status = $this->backend->doOperations( [
  1555. [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
  1556. [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
  1557. [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
  1558. [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
  1559. // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
  1560. [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
  1561. // Now: A:<A>, B:<B>, C:<A>, D:<empty>
  1562. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
  1563. // Now: A:<A>, B:<B>, C:<empty>, D:<A>
  1564. [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
  1565. // Now: A:<A>, B:<empty>, C:<B>, D:<A>
  1566. [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
  1567. // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
  1568. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
  1569. // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
  1570. [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
  1571. // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
  1572. [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
  1573. // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
  1574. [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
  1575. // Does nothing
  1576. [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
  1577. // Does nothing
  1578. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
  1579. // Does nothing
  1580. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
  1581. // Does nothing
  1582. [ 'op' => 'null' ],
  1583. // Does nothing
  1584. ] );
  1585. $this->assertGoodStatus( $status, "Operation batch succeeded" );
  1586. $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
  1587. $this->assertEquals( 16, count( $status->success ),
  1588. "Operation batch has correct success array" );
  1589. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileA ] ),
  1590. "File does not exist at $fileA" );
  1591. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
  1592. "File does not exist at $fileB" );
  1593. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
  1594. "File does not exist at $fileD" );
  1595. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
  1596. "File exists at $fileC" );
  1597. $this->assertEquals( $fileBContents,
  1598. $this->backend->getFileContents( [ 'src' => $fileC ] ),
  1599. "Correct file contents of $fileC" );
  1600. $this->assertEquals( strlen( $fileBContents ),
  1601. $this->backend->getFileSize( [ 'src' => $fileC ] ),
  1602. "Correct file size of $fileC" );
  1603. $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
  1604. $this->backend->getFileSha1Base36( [ 'src' => $fileC ] ),
  1605. "Correct file SHA-1 of $fileC" );
  1606. }
  1607. public function testDoOperationsFailing() {
  1608. $this->backend = $this->singleBackend;
  1609. $this->tearDownFiles();
  1610. $this->doTestDoOperationsFailing();
  1611. $this->tearDownFiles();
  1612. $this->backend = $this->multiBackend;
  1613. $this->tearDownFiles();
  1614. $this->doTestDoOperationsFailing();
  1615. $this->tearDownFiles();
  1616. }
  1617. private function doTestDoOperationsFailing() {
  1618. $base = self::baseStorePath();
  1619. $fileA = "$base/unittest-cont2/a/b/fileA.txt";
  1620. $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
  1621. $fileB = "$base/unittest-cont2/a/b/fileB.txt";
  1622. $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
  1623. $fileC = "$base/unittest-cont2/a/b/fileC.txt";
  1624. $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
  1625. $fileD = "$base/unittest-cont2/a/b/fileD.txt";
  1626. $this->prepare( [ 'dir' => dirname( $fileA ) ] );
  1627. $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
  1628. $this->prepare( [ 'dir' => dirname( $fileB ) ] );
  1629. $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
  1630. $this->prepare( [ 'dir' => dirname( $fileC ) ] );
  1631. $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
  1632. $status = $this->backend->doOperations( [
  1633. [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
  1634. // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
  1635. [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
  1636. // Now: A:<A>, B:<B>, C:<A>, D:<empty>
  1637. [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
  1638. // Now: A:<A>, B:<B>, C:<A>, D:<B>
  1639. [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
  1640. // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
  1641. [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
  1642. // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
  1643. [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
  1644. // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
  1645. [ 'op' => 'delete', 'src' => $fileD ],
  1646. // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
  1647. [ 'op' => 'null' ],
  1648. // Does nothing
  1649. ], [ 'force' => 1 ] );
  1650. $this->assertNotEquals( [], $status->getErrors(), "Operation had warnings" );
  1651. $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
  1652. $this->assertEquals( 8, count( $status->success ),
  1653. "Operation batch has correct success array" );
  1654. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
  1655. "File does not exist at $fileB" );
  1656. $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
  1657. "File does not exist at $fileD" );
  1658. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileA ] ),
  1659. "File does not exist at $fileA" );
  1660. $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
  1661. "File exists at $fileC" );
  1662. $this->assertEquals( $fileBContents,
  1663. $this->backend->getFileContents( [ 'src' => $fileA ] ),
  1664. "Correct file contents of $fileA" );
  1665. $this->assertEquals( strlen( $fileBContents ),
  1666. $this->backend->getFileSize( [ 'src' => $fileA ] ),
  1667. "Correct file size of $fileA" );
  1668. $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
  1669. $this->backend->getFileSha1Base36( [ 'src' => $fileA ] ),
  1670. "Correct file SHA-1 of $fileA" );
  1671. }
  1672. public function testGetFileList() {
  1673. $this->backend = $this->singleBackend;
  1674. $this->tearDownFiles();
  1675. $this->doTestGetFileList();
  1676. $this->tearDownFiles();
  1677. $this->backend = $this->multiBackend;
  1678. $this->tearDownFiles();
  1679. $this->doTestGetFileList();
  1680. $this->tearDownFiles();
  1681. }
  1682. private function doTestGetFileList() {
  1683. $backendName = $this->backendClass();
  1684. $base = self::baseStorePath();
  1685. // Should have no errors
  1686. $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
  1687. $files = [
  1688. "$base/unittest-cont1/e/test1.txt",
  1689. "$base/unittest-cont1/e/test2.txt",
  1690. "$base/unittest-cont1/e/test3.txt",
  1691. "$base/unittest-cont1/e/subdir1/test1.txt",
  1692. "$base/unittest-cont1/e/subdir1/test2.txt",
  1693. "$base/unittest-cont1/e/subdir2/test3.txt",
  1694. "$base/unittest-cont1/e/subdir2/test4.txt",
  1695. "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
  1696. "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
  1697. "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
  1698. "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
  1699. "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
  1700. "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
  1701. "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
  1702. ];
  1703. // Add the files
  1704. $ops = [];
  1705. foreach ( $files as $file ) {
  1706. $this->prepare( [ 'dir' => dirname( $file ) ] );
  1707. $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
  1708. }
  1709. $status = $this->backend->doQuickOperations( $ops );
  1710. $this->assertGoodStatus( $status,
  1711. "Creation of files succeeded ($backendName)." );
  1712. $this->assertEquals( true, $status->isOK(),
  1713. "Creation of files succeeded with OK status ($backendName)." );
  1714. // Expected listing at root
  1715. $expected = [
  1716. "e/test1.txt",
  1717. "e/test2.txt",
  1718. "e/test3.txt",
  1719. "e/subdir1/test1.txt",
  1720. "e/subdir1/test2.txt",
  1721. "e/subdir2/test3.txt",
  1722. "e/subdir2/test4.txt",
  1723. "e/subdir2/subdir/test1.txt",
  1724. "e/subdir2/subdir/test2.txt",
  1725. "e/subdir2/subdir/test3.txt",
  1726. "e/subdir2/subdir/test4.txt",
  1727. "e/subdir2/subdir/test5.txt",
  1728. "e/subdir2/subdir/sub/test0.txt",
  1729. "e/subdir2/subdir/sub/120-px-file.txt",
  1730. ];
  1731. sort( $expected );
  1732. // Actual listing (no trailing slash) at root
  1733. $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
  1734. $list = $this->listToArray( $iter );
  1735. sort( $list );
  1736. $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
  1737. // Actual listing (no trailing slash) at root with advise
  1738. $iter = $this->backend->getFileList( [
  1739. 'dir' => "$base/unittest-cont1",
  1740. 'adviseStat' => 1
  1741. ] );
  1742. $list = $this->listToArray( $iter );
  1743. sort( $list );
  1744. $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
  1745. // Actual listing (with trailing slash) at root
  1746. $list = [];
  1747. $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
  1748. foreach ( $iter as $file ) {
  1749. $list[] = $file;
  1750. }
  1751. sort( $list );
  1752. $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
  1753. // Expected listing at subdir
  1754. $expected = [
  1755. "test1.txt",
  1756. "test2.txt",
  1757. "test3.txt",
  1758. "test4.txt",
  1759. "test5.txt",
  1760. "sub/test0.txt",
  1761. "sub/120-px-file.txt",
  1762. ];
  1763. sort( $expected );
  1764. // Actual listing (no trailing slash) at subdir
  1765. $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
  1766. $list = $this->listToArray( $iter );
  1767. sort( $list );
  1768. $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
  1769. // Actual listing (no trailing slash) at subdir with advise
  1770. $iter = $this->backend->getFileList( [
  1771. 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
  1772. 'adviseStat' => 1
  1773. ] );
  1774. $list = $this->listToArray( $iter );
  1775. sort( $list );
  1776. $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
  1777. // Actual listing (with trailing slash) at subdir
  1778. $list = [];
  1779. $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
  1780. foreach ( $iter as $file ) {
  1781. $list[] = $file;
  1782. }
  1783. sort( $list );
  1784. $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
  1785. // Actual listing (using iterator second time)
  1786. $list = $this->listToArray( $iter );
  1787. sort( $list );
  1788. $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
  1789. // Actual listing (top files only) at root
  1790. $iter = $this->backend->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
  1791. $list = $this->listToArray( $iter );
  1792. sort( $list );
  1793. $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
  1794. // Expected listing (top files only) at subdir
  1795. $expected = [
  1796. "test1.txt",
  1797. "test2.txt",
  1798. "test3.txt",
  1799. "test4.txt",
  1800. "test5.txt"
  1801. ];
  1802. sort( $expected );
  1803. // Actual listing (top files only) at subdir
  1804. $iter = $this->backend->getTopFileList(
  1805. [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
  1806. );
  1807. $list = $this->listToArray( $iter );
  1808. sort( $list );
  1809. $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
  1810. // Actual listing (top files only) at subdir with advise
  1811. $iter = $this->backend->getTopFileList( [
  1812. 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
  1813. 'adviseStat' => 1
  1814. ] );
  1815. $list = $this->listToArray( $iter );
  1816. sort( $list );
  1817. $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
  1818. foreach ( $files as $file ) { // clean up
  1819. $this->backend->doOperation( [ 'op' => 'delete', 'src' => $file ] );
  1820. }
  1821. $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
  1822. foreach ( $iter as $iter ) {
  1823. // no errors
  1824. }
  1825. }
  1826. public function testGetDirectoryList() {
  1827. $this->backend = $this->singleBackend;
  1828. $this->tearDownFiles();
  1829. $this->doTestGetDirectoryList();
  1830. $this->tearDownFiles();
  1831. $this->backend = $this->multiBackend;
  1832. $this->tearDownFiles();
  1833. $this->doTestGetDirectoryList();
  1834. $this->tearDownFiles();
  1835. }
  1836. private function doTestGetDirectoryList() {
  1837. $backendName = $this->backendClass();
  1838. $base = self::baseStorePath();
  1839. $files = [
  1840. "$base/unittest-cont1/e/test1.txt",
  1841. "$base/unittest-cont1/e/test2.txt",
  1842. "$base/unittest-cont1/e/test3.txt",
  1843. "$base/unittest-cont1/e/subdir1/test1.txt",
  1844. "$base/unittest-cont1/e/subdir1/test2.txt",
  1845. "$base/unittest-cont1/e/subdir2/test3.txt",
  1846. "$base/unittest-cont1/e/subdir2/test4.txt",
  1847. "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
  1848. "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
  1849. "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
  1850. "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
  1851. "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
  1852. "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
  1853. "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
  1854. ];
  1855. // Add the files
  1856. $ops = [];
  1857. foreach ( $files as $file ) {
  1858. $this->prepare( [ 'dir' => dirname( $file ) ] );
  1859. $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
  1860. }
  1861. $status = $this->backend->doQuickOperations( $ops );
  1862. $this->assertGoodStatus( $status,
  1863. "Creation of files succeeded ($backendName)." );
  1864. $this->assertEquals( true, $status->isOK(),
  1865. "Creation of files succeeded with OK status ($backendName)." );
  1866. $this->assertEquals( true,
  1867. $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
  1868. "Directory exists in ($backendName)." );
  1869. $this->assertEquals( true,
  1870. $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
  1871. "Directory exists in ($backendName)." );
  1872. $this->assertEquals( false,
  1873. $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
  1874. "Directory does not exists in ($backendName)." );
  1875. // Expected listing
  1876. $expected = [
  1877. "e",
  1878. ];
  1879. sort( $expected );
  1880. // Actual listing (no trailing slash)
  1881. $list = [];
  1882. $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
  1883. foreach ( $iter as $file ) {
  1884. $list[] = $file;
  1885. }
  1886. sort( $list );
  1887. $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
  1888. // Expected listing
  1889. $expected = [
  1890. "subdir1",
  1891. "subdir2",
  1892. "subdir3",
  1893. "subdir4",
  1894. ];
  1895. sort( $expected );
  1896. // Actual listing (no trailing slash)
  1897. $list = [];
  1898. $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
  1899. foreach ( $iter as $file ) {
  1900. $list[] = $file;
  1901. }
  1902. sort( $list );
  1903. $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
  1904. // Actual listing (with trailing slash)
  1905. $list = [];
  1906. $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
  1907. foreach ( $iter as $file ) {
  1908. $list[] = $file;
  1909. }
  1910. sort( $list );
  1911. $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
  1912. // Expected listing
  1913. $expected = [
  1914. "subdir",
  1915. ];
  1916. sort( $expected );
  1917. // Actual listing (no trailing slash)
  1918. $list = [];
  1919. $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
  1920. foreach ( $iter as $file ) {
  1921. $list[] = $file;
  1922. }
  1923. sort( $list );
  1924. $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
  1925. // Actual listing (with trailing slash)
  1926. $list = [];
  1927. $iter = $this->backend->getTopDirectoryList(
  1928. [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
  1929. );
  1930. foreach ( $iter as $file ) {
  1931. $list[] = $file;
  1932. }
  1933. sort( $list );
  1934. $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
  1935. // Actual listing (using iterator second time)
  1936. $list = [];
  1937. foreach ( $iter as $file ) {
  1938. $list[] = $file;
  1939. }
  1940. sort( $list );
  1941. $this->assertEquals(
  1942. $expected,
  1943. $list,
  1944. "Correct top dir listing ($backendName), second iteration."
  1945. );
  1946. // Expected listing (recursive)
  1947. $expected = [
  1948. "e",
  1949. "e/subdir1",
  1950. "e/subdir2",
  1951. "e/subdir3",
  1952. "e/subdir4",
  1953. "e/subdir2/subdir",
  1954. "e/subdir3/subdir",
  1955. "e/subdir4/subdir",
  1956. "e/subdir4/subdir/sub",
  1957. ];
  1958. sort( $expected );
  1959. // Actual listing (recursive)
  1960. $list = [];
  1961. $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
  1962. foreach ( $iter as $file ) {
  1963. $list[] = $file;
  1964. }
  1965. sort( $list );
  1966. $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
  1967. // Expected listing (recursive)
  1968. $expected = [
  1969. "subdir",
  1970. "subdir/sub",
  1971. ];
  1972. sort( $expected );
  1973. // Actual listing (recursive)
  1974. $list = [];
  1975. $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
  1976. foreach ( $iter as $file ) {
  1977. $list[] = $file;
  1978. }
  1979. sort( $list );
  1980. $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
  1981. // Actual listing (recursive, second time)
  1982. $list = [];
  1983. foreach ( $iter as $file ) {
  1984. $list[] = $file;
  1985. }
  1986. sort( $list );
  1987. $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
  1988. $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
  1989. $items = $this->listToArray( $iter );
  1990. $this->assertEquals( [], $items, "Directory listing is empty." );
  1991. foreach ( $files as $file ) { // clean up
  1992. $this->backend->doOperation( [ 'op' => 'delete', 'src' => $file ] );
  1993. }
  1994. $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
  1995. foreach ( $iter as $file ) {
  1996. // no errors
  1997. }
  1998. $items = $this->listToArray( $iter );
  1999. $this->assertEquals( [], $items, "Directory listing is empty." );
  2000. $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
  2001. $items = $this->listToArray( $iter );
  2002. $this->assertEquals( [], $items, "Directory listing is empty." );
  2003. }
  2004. public function testLockCalls() {
  2005. $this->backend = $this->singleBackend;
  2006. $this->doTestLockCalls();
  2007. }
  2008. private function doTestLockCalls() {
  2009. $backendName = $this->backendClass();
  2010. $base = $this->backend->getContainerStoragePath( 'test' );
  2011. $paths = [
  2012. "$base/test1.txt",
  2013. "$base/test2.txt",
  2014. "$base/test3.txt",
  2015. "$base/subdir1",
  2016. "$base/subdir1", // duplicate
  2017. "$base/subdir1/test1.txt",
  2018. "$base/subdir1/test2.txt",
  2019. "$base/subdir2",
  2020. "$base/subdir2", // duplicate
  2021. "$base/subdir2/test3.txt",
  2022. "$base/subdir2/test4.txt",
  2023. "$base/subdir2/subdir",
  2024. "$base/subdir2/subdir/test1.txt",
  2025. "$base/subdir2/subdir/test2.txt",
  2026. "$base/subdir2/subdir/test3.txt",
  2027. "$base/subdir2/subdir/test4.txt",
  2028. "$base/subdir2/subdir/test5.txt",
  2029. "$base/subdir2/subdir/sub",
  2030. "$base/subdir2/subdir/sub/test0.txt",
  2031. "$base/subdir2/subdir/sub/120-px-file.txt",
  2032. ];
  2033. for ( $i = 0; $i < 25; $i++ ) {
  2034. $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
  2035. $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
  2036. "Locking of files succeeded ($backendName) ($i)." );
  2037. $this->assertEquals( true, $status->isOK(),
  2038. "Locking of files succeeded with OK status ($backendName) ($i)." );
  2039. $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
  2040. $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
  2041. "Locking of files succeeded ($backendName) ($i)." );
  2042. $this->assertEquals( true, $status->isOK(),
  2043. "Locking of files succeeded with OK status ($backendName) ($i)." );
  2044. $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
  2045. $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
  2046. "Locking of files succeeded ($backendName) ($i)." );
  2047. $this->assertEquals( true, $status->isOK(),
  2048. "Locking of files succeeded with OK status ($backendName) ($i)." );
  2049. $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
  2050. $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
  2051. "Locking of files succeeded ($backendName). ($i)" );
  2052. $this->assertEquals( true, $status->isOK(),
  2053. "Locking of files succeeded with OK status ($backendName) ($i)." );
  2054. # # Flip the acquire/release ordering around ##
  2055. $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
  2056. $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
  2057. "Locking of files succeeded ($backendName) ($i)." );
  2058. $this->assertEquals( true, $status->isOK(),
  2059. "Locking of files succeeded with OK status ($backendName) ($i)." );
  2060. $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
  2061. $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
  2062. "Locking of files succeeded ($backendName) ($i)." );
  2063. $this->assertEquals( true, $status->isOK(),
  2064. "Locking of files succeeded with OK status ($backendName) ($i)." );
  2065. $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
  2066. $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
  2067. "Locking of files succeeded ($backendName). ($i)" );
  2068. $this->assertEquals( true, $status->isOK(),
  2069. "Locking of files succeeded with OK status ($backendName) ($i)." );
  2070. $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
  2071. $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
  2072. "Locking of files succeeded ($backendName) ($i)." );
  2073. $this->assertEquals( true, $status->isOK(),
  2074. "Locking of files succeeded with OK status ($backendName) ($i)." );
  2075. }
  2076. $status = Status::newGood();
  2077. $sl = $this->backend->getScopedFileLocks( $paths, LockManager::LOCK_EX, $status );
  2078. $this->assertInstanceOf( ScopedLock::class, $sl,
  2079. "Scoped locking of files succeeded ($backendName)." );
  2080. $this->assertEquals( [], $status->getErrors(),
  2081. "Scoped locking of files succeeded ($backendName)." );
  2082. $this->assertEquals( true, $status->isOK(),
  2083. "Scoped locking of files succeeded with OK status ($backendName)." );
  2084. ScopedLock::release( $sl );
  2085. $this->assertEquals( null, $sl,
  2086. "Scoped unlocking of files succeeded ($backendName)." );
  2087. $this->assertEquals( [], $status->getErrors(),
  2088. "Scoped unlocking of files succeeded ($backendName)." );
  2089. $this->assertEquals( true, $status->isOK(),
  2090. "Scoped unlocking of files succeeded with OK status ($backendName)." );
  2091. }
  2092. /**
  2093. * @dataProvider provider_testGetContentType
  2094. */
  2095. public function testGetContentType( $mimeCallback, $mimeFromString ) {
  2096. global $IP;
  2097. $be = TestingAccessWrapper::newFromObject( new MemoryFileBackend(
  2098. [
  2099. 'name' => 'testing',
  2100. 'class' => MemoryFileBackend::class,
  2101. 'wikiId' => 'meow',
  2102. 'mimeCallback' => $mimeCallback
  2103. ]
  2104. ) );
  2105. $dst = 'mwstore://testing/container/path/to/file_no_ext';
  2106. $src = "$IP/tests/phpunit/data/media/srgb.jpg";
  2107. $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) );
  2108. $this->assertEquals(
  2109. $mimeFromString ? 'image/jpeg' : 'unknown/unknown',
  2110. $be->getContentType( $dst, file_get_contents( $src ), null ) );
  2111. $src = "$IP/tests/phpunit/data/media/Png-native-test.png";
  2112. $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) );
  2113. $this->assertEquals(
  2114. $mimeFromString ? 'image/png' : 'unknown/unknown',
  2115. $be->getContentType( $dst, file_get_contents( $src ), null ) );
  2116. }
  2117. public static function provider_testGetContentType() {
  2118. return [
  2119. [ null, false ],
  2120. [ [ FileBackendGroup::singleton(), 'guessMimeInternal' ], true ]
  2121. ];
  2122. }
  2123. public function testReadAffinity() {
  2124. $be = TestingAccessWrapper::newFromObject(
  2125. new FileBackendMultiWrite( [
  2126. 'name' => 'localtesting',
  2127. 'wikiId' => wfWikiID() . mt_rand(),
  2128. 'backends' => [
  2129. [ // backend 0
  2130. 'name' => 'multitesting0',
  2131. 'class' => MemoryFileBackend::class,
  2132. 'isMultiMaster' => false,
  2133. 'readAffinity' => true
  2134. ],
  2135. [ // backend 1
  2136. 'name' => 'multitesting1',
  2137. 'class' => MemoryFileBackend::class,
  2138. 'isMultiMaster' => true
  2139. ]
  2140. ]
  2141. ] )
  2142. );
  2143. $this->assertEquals(
  2144. 1,
  2145. $be->getReadIndexFromParams( [ 'latest' => 1 ] ),
  2146. 'Reads with "latest" flag use backend 1'
  2147. );
  2148. $this->assertEquals(
  2149. 0,
  2150. $be->getReadIndexFromParams( [ 'latest' => 0 ] ),
  2151. 'Reads without "latest" flag use backend 0'
  2152. );
  2153. $p = 'container/test-cont/file.txt';
  2154. $be->backends[0]->quickCreate( [
  2155. 'dst' => "mwstore://multitesting0/$p", 'content' => 'cattitude' ] );
  2156. $be->backends[1]->quickCreate( [
  2157. 'dst' => "mwstore://multitesting1/$p", 'content' => 'princess of power' ] );
  2158. $this->assertEquals(
  2159. 'cattitude',
  2160. $be->getFileContents( [ 'src' => "mwstore://localtesting/$p" ] ),
  2161. "Non-latest read came from backend 0"
  2162. );
  2163. $this->assertEquals(
  2164. 'princess of power',
  2165. $be->getFileContents( [ 'src' => "mwstore://localtesting/$p", 'latest' => 1 ] ),
  2166. "Latest read came from backend1"
  2167. );
  2168. }
  2169. public function testAsyncWrites() {
  2170. $be = TestingAccessWrapper::newFromObject(
  2171. new FileBackendMultiWrite( [
  2172. 'name' => 'localtesting',
  2173. 'wikiId' => wfWikiID() . mt_rand(),
  2174. 'backends' => [
  2175. [ // backend 0
  2176. 'name' => 'multitesting0',
  2177. 'class' => MemoryFileBackend::class,
  2178. 'isMultiMaster' => false
  2179. ],
  2180. [ // backend 1
  2181. 'name' => 'multitesting1',
  2182. 'class' => MemoryFileBackend::class,
  2183. 'isMultiMaster' => true
  2184. ]
  2185. ],
  2186. 'replication' => 'async'
  2187. ] )
  2188. );
  2189. $this->setMwGlobals( 'wgCommandLineMode', false );
  2190. $p = 'container/test-cont/file.txt';
  2191. $be->quickCreate( [
  2192. 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ] );
  2193. $this->assertEquals(
  2194. false,
  2195. $be->backends[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
  2196. "File not yet written to backend 0"
  2197. );
  2198. $this->assertEquals(
  2199. 'cattitude',
  2200. $be->backends[1]->getFileContents( [ 'src' => "mwstore://multitesting1/$p" ] ),
  2201. "File already written to backend 1"
  2202. );
  2203. DeferredUpdates::doUpdates();
  2204. $this->assertEquals(
  2205. 'cattitude',
  2206. $be->backends[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
  2207. "File now written to backend 0"
  2208. );
  2209. }
  2210. public function testSanitizeOpHeaders() {
  2211. $be = TestingAccessWrapper::newFromObject( new MemoryFileBackend( [
  2212. 'name' => 'localtesting',
  2213. 'wikiId' => wfWikiID()
  2214. ] ) );
  2215. $name = wfRandomString( 300 );
  2216. $input = [
  2217. 'headers' => [
  2218. 'content-Disposition' => FileBackend::makeContentDisposition( 'inline', $name ),
  2219. 'Content-dUration' => 25.6,
  2220. 'X-LONG-VALUE' => str_pad( '0', 300 ),
  2221. 'CONTENT-LENGTH' => 855055,
  2222. ]
  2223. ];
  2224. $expected = [
  2225. 'headers' => [
  2226. 'content-disposition' => FileBackend::makeContentDisposition( 'inline', $name ),
  2227. 'content-duration' => 25.6,
  2228. 'content-length' => 855055
  2229. ]
  2230. ];
  2231. Wikimedia\suppressWarnings();
  2232. $actual = $be->sanitizeOpHeaders( $input );
  2233. Wikimedia\restoreWarnings();
  2234. $this->assertEquals( $expected, $actual, "Header sanitized properly" );
  2235. }
  2236. // helper function
  2237. private function listToArray( $iter ) {
  2238. return is_array( $iter ) ? $iter : iterator_to_array( $iter );
  2239. }
  2240. // test helper wrapper for backend prepare() function
  2241. private function prepare( array $params ) {
  2242. return $this->backend->prepare( $params );
  2243. }
  2244. // test helper wrapper for backend prepare() function
  2245. private function create( array $params ) {
  2246. $params['op'] = 'create';
  2247. return $this->backend->doQuickOperations( [ $params ] );
  2248. }
  2249. function tearDownFiles() {
  2250. $containers = [ 'unittest-cont1', 'unittest-cont2', 'unittest-cont-bad' ];
  2251. foreach ( $containers as $container ) {
  2252. $this->deleteFiles( $container );
  2253. }
  2254. }
  2255. private function deleteFiles( $container ) {
  2256. $base = self::baseStorePath();
  2257. $iter = $this->backend->getFileList( [ 'dir' => "$base/$container" ] );
  2258. if ( $iter ) {
  2259. foreach ( $iter as $file ) {
  2260. $this->backend->quickDelete( [ 'src' => "$base/$container/$file" ] );
  2261. }
  2262. // free the directory, to avoid Permission denied under windows on rmdir
  2263. unset( $iter );
  2264. }
  2265. $this->backend->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
  2266. }
  2267. function assertBackendPathsConsistent( array $paths ) {
  2268. if ( $this->backend instanceof FileBackendMultiWrite ) {
  2269. $status = $this->backend->consistencyCheck( $paths );
  2270. $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
  2271. }
  2272. }
  2273. function assertGoodStatus( StatusValue $status, $msg ) {
  2274. $this->assertEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );
  2275. }
  2276. }