UploadStashTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @group Database
  4. *
  5. * @covers UploadStash
  6. */
  7. class UploadStashTest extends MediaWikiTestCase {
  8. /**
  9. * @var TestUser[] Array of UploadStashTestUser
  10. */
  11. public static $users;
  12. /**
  13. * @var string
  14. */
  15. private $tmpFile;
  16. protected function setUp() {
  17. parent::setUp();
  18. $this->tmpFile = $this->getNewTempFile();
  19. file_put_contents( $this->tmpFile, "\x00" );
  20. self::$users = [
  21. 'sysop' => new TestUser(
  22. 'Uploadstashtestsysop',
  23. 'Upload Stash Test Sysop',
  24. 'upload_stash_test_sysop@example.com',
  25. [ 'sysop' ]
  26. ),
  27. 'uploader' => new TestUser(
  28. 'Uploadstashtestuser',
  29. 'Upload Stash Test User',
  30. 'upload_stash_test_user@example.com',
  31. []
  32. )
  33. ];
  34. }
  35. /**
  36. * @todo give this test a real name explaining what is being tested here
  37. */
  38. public function testT31408() {
  39. $this->setMwGlobals( 'wgUser', self::$users['uploader']->getUser() );
  40. $repo = RepoGroup::singleton()->getLocalRepo();
  41. $stash = new UploadStash( $repo );
  42. // Throws exception caught by PHPUnit on failure
  43. $file = $stash->stashFile( $this->tmpFile );
  44. // We'll never reach this point if we hit T31408
  45. $this->assertTrue( true, 'Unrecognized file without extension' );
  46. $stash->removeFile( $file->getFileKey() );
  47. }
  48. public static function provideInvalidRequests() {
  49. return [
  50. 'Check failure on bad wpFileKey' =>
  51. [ new FauxRequest( [ 'wpFileKey' => 'foo' ] ) ],
  52. 'Check failure on bad wpSessionKey' =>
  53. [ new FauxRequest( [ 'wpSessionKey' => 'foo' ] ) ],
  54. ];
  55. }
  56. /**
  57. * @dataProvider provideInvalidRequests
  58. */
  59. public function testValidRequestWithInvalidRequests( $request ) {
  60. $this->assertFalse( UploadFromStash::isValidRequest( $request ) );
  61. }
  62. public static function provideValidRequests() {
  63. return [
  64. 'Check good wpFileKey' =>
  65. [ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
  66. 'Check good wpSessionKey' =>
  67. [ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
  68. 'Check key precedence' =>
  69. [ new FauxRequest( [
  70. 'wpFileKey' => 'testkey-test.test',
  71. 'wpSessionKey' => 'foo'
  72. ] ) ],
  73. ];
  74. }
  75. /**
  76. * @dataProvider provideValidRequests
  77. */
  78. public function testValidRequestWithValidRequests( $request ) {
  79. $this->assertTrue( UploadFromStash::isValidRequest( $request ) );
  80. }
  81. public function testExceptionWhenStoreTempFails() {
  82. $mockRepoStoreStatusResult = Status::newFatal( 'TEST_ERROR' );
  83. $mockRepo = $this->getMockBuilder( FileRepo::class )
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $mockRepo->expects( $this->once() )
  87. ->method( 'storeTemp' )
  88. ->willReturn( $mockRepoStoreStatusResult );
  89. $stash = new UploadStash( $mockRepo );
  90. try {
  91. $stash->stashFile( $this->tmpFile );
  92. $this->fail( 'Expected UploadStashFileException not thrown' );
  93. } catch ( UploadStashFileException $e ) {
  94. $this->assertInstanceOf( ILocalizedException::class, $e );
  95. } catch ( Exception $e ) {
  96. $this->fail( 'Unexpected exception class ' . get_class( $e ) );
  97. }
  98. }
  99. }