Merge "Remove use of BagOStuff TTL constants from unrelated code"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryImageInfoTest.php
blob56d07f711b4600251434ee45a4a420a564c44778
1 <?php
3 namespace MediaWiki\Tests\Api\Query;
5 use File;
6 use MediaWiki\Api\ApiQueryImageInfo;
7 use MediaWiki\Request\FauxRequest;
8 use MediaWiki\Tests\Api\ApiTestCase;
9 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
10 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
11 use MediaWiki\User\UserIdentity;
12 use MediaWiki\User\UserIdentityValue;
13 use MediaWiki\Utils\MWTimestamp;
15 /**
16 * @covers \MediaWiki\Api\ApiQueryImageInfo
17 * @group API
18 * @group medium
19 * @group Database
21 class ApiQueryImageInfoTest extends ApiTestCase {
22 use MockAuthorityTrait;
23 use TempUserTestTrait;
25 private const IMAGE_NAME = 'Random-11m.png';
27 private const OLD_IMAGE_TIMESTAMP = '20201105235241';
29 private const NEW_IMAGE_TIMESTAMP = '20201105235242';
31 private const OLD_IMAGE_SIZE = 12345;
33 private const NEW_IMAGE_SIZE = 54321;
35 private const NO_COMMENT_TIMESTAMP = '20201105235239';
37 private const IMAGE_2_NAME = 'Random-2.png';
38 private const IMAGE_2_TIMESTAMP = '20230101000000';
39 private const IMAGE_2_SIZE = 12345;
41 /** @var UserIdentity */
42 private $testUser = null;
43 /** @var User */
44 private $tempUser = null;
46 public function addDBData() {
47 parent::addDBData();
48 $this->testUser = new UserIdentityValue( 12364321, 'Dummy User' );
50 $actorId = $this->getServiceContainer()
51 ->getActorStore()
52 ->acquireActorId( $this->testUser, $this->getDb() );
53 $this->getDb()->newInsertQueryBuilder()
54 ->insertInto( 'image' )
55 ->row( [
56 'img_name' => 'Random-11m.png',
57 'img_size' => self::NEW_IMAGE_SIZE,
58 'img_width' => 1000,
59 'img_height' => 1800,
60 'img_metadata' => '',
61 'img_bits' => 16,
62 'img_media_type' => 'BITMAP',
63 'img_major_mime' => 'image',
64 'img_minor_mime' => 'png',
65 'img_description_id' => $this->getServiceContainer()
66 ->getCommentStore()
67 ->createComment( $this->getDb(), "'''comment'''" )->id,
68 'img_actor' => $actorId,
69 'img_timestamp' => $this->getDb()->timestamp( self::NEW_IMAGE_TIMESTAMP ),
70 'img_sha1' => 'sy02psim0bgdh0jt4vdltuzoh7j80ru',
71 ] )
72 ->caller( __METHOD__ )
73 ->execute();
74 $this->getDb()->newInsertQueryBuilder()
75 ->insertInto( 'oldimage' )
76 ->row( [
77 'oi_name' => 'Random-11m.png',
78 'oi_archive_name' => self::OLD_IMAGE_TIMESTAMP . 'Random-11m.png',
79 'oi_size' => self::OLD_IMAGE_SIZE,
80 'oi_width' => 1000,
81 'oi_height' => 1800,
82 'oi_metadata' => '',
83 'oi_bits' => 16,
84 'oi_media_type' => 'BITMAP',
85 'oi_major_mime' => 'image',
86 'oi_minor_mime' => 'png',
87 'oi_description_id' => $this->getServiceContainer()
88 ->getCommentStore()
89 ->createComment( $this->getDb(), 'deleted comment' )->id,
90 'oi_actor' => $actorId,
91 'oi_timestamp' => $this->getDb()->timestamp( self::OLD_IMAGE_TIMESTAMP ),
92 'oi_sha1' => 'sy02psim0bgdh0jt4vdltuzoh7j80ru',
93 'oi_deleted' => File::DELETED_FILE | File::DELETED_COMMENT | File::DELETED_USER,
94 ] )
95 ->row( [
96 'oi_name' => 'Random-11m.png',
97 'oi_archive_name' => self::NO_COMMENT_TIMESTAMP . 'Random-11m.png',
98 'oi_size' => self::OLD_IMAGE_SIZE,
99 'oi_width' => 1000,
100 'oi_height' => 1800,
101 'oi_metadata' => '',
102 'oi_bits' => 16,
103 'oi_media_type' => 'BITMAP',
104 'oi_major_mime' => 'image',
105 'oi_minor_mime' => 'png',
106 'oi_description_id' => $this->getServiceContainer()
107 ->getCommentStore()
108 ->createComment( $this->getDb(), '' )->id,
109 'oi_actor' => $actorId,
110 'oi_timestamp' => $this->getDb()->timestamp( self::NO_COMMENT_TIMESTAMP ),
111 'oi_sha1' => 'sy02psim0bgdh0jt4vdltuzoh7j80ru',
112 'oi_deleted' => 0,
114 ->caller( __METHOD__ )
115 ->execute();
117 // Set up temp user config
118 $this->enableAutoCreateTempUser();
119 $this->tempUser = $this->getServiceContainer()
120 ->getTempUserCreator()
121 ->create( null, new FauxRequest() )->getUser();
122 $tempActorId = $this->getServiceContainer()
123 ->getActorStore()
124 ->acquireActorId( $this->tempUser, $this->getDb() );
125 $this->getDb()->newInsertQueryBuilder()
126 ->insertInto( 'image' )
127 ->row( [
128 'img_name' => self::IMAGE_2_NAME,
129 'img_size' => self::IMAGE_2_SIZE,
130 'img_width' => 1000,
131 'img_height' => 1800,
132 'img_metadata' => '',
133 'img_bits' => 16,
134 'img_media_type' => 'BITMAP',
135 'img_major_mime' => 'image',
136 'img_minor_mime' => 'png',
137 'img_description_id' => $this->getServiceContainer()
138 ->getCommentStore()
139 ->createComment( $this->getDb(), "'''comment'''" )->id,
140 'img_actor' => $tempActorId,
141 'img_timestamp' => $this->getDb()->timestamp( self::IMAGE_2_TIMESTAMP ),
142 'img_sha1' => 'aaaaasim0bgdh0jt4vdltuzoh7',
144 ->caller( __METHOD__ )
145 ->execute();
148 private function getImageInfoFromResult( array $result ) {
149 $this->assertArrayHasKey( 'query', $result );
150 $this->assertArrayHasKey( 'pages', $result['query'] );
151 $this->assertArrayHasKey( '-1', $result['query']['pages'] );
152 $info = $result['query']['pages']['-1'];
153 $this->assertSame( NS_FILE, $info['ns'] );
154 $this->assertSame( 'File:' . self::IMAGE_NAME, $info['title'] );
155 $this->assertTrue( $info['missing'] );
156 $this->assertTrue( $info['known'] );
157 $this->assertSame( 'local', $info['imagerepository'] );
158 $this->assertFalse( $info['badfile'] );
159 $this->assertIsArray( $info['imageinfo'] );
160 return $info['imageinfo'][0];
163 public function testGetImageInfoLatestImage() {
164 [ $result, ] = $this->doApiRequest( [
165 'action' => 'query',
166 'prop' => 'imageinfo',
167 'titles' => 'File:' . self::IMAGE_NAME,
168 'iiprop' => implode( '|', ApiQueryImageInfo::getPropertyNames() ),
169 'iistart' => self::NEW_IMAGE_TIMESTAMP,
170 'iiend' => self::NEW_IMAGE_TIMESTAMP,
171 ] );
172 $image = $this->getImageInfoFromResult( $result );
173 $this->assertSame( MWTimestamp::convert( TS_ISO_8601, self::NEW_IMAGE_TIMESTAMP ), $image['timestamp'] );
174 $this->assertSame( "'''comment'''", $image['comment'] );
175 $this->assertSame( $this->testUser->getName(), $image['user'] );
176 $this->assertSame( $this->testUser->getId(), $image['userid'] );
177 $this->assertSame( self::NEW_IMAGE_SIZE, $image['size'] );
180 public function testGetImageCreatedByTempUser() {
181 [ $result, ] = $this->doApiRequest( [
182 'action' => 'query',
183 'prop' => 'imageinfo',
184 'titles' => 'File:' . self::IMAGE_2_NAME
185 ] );
186 $image = $result['query']['pages']['-1']['imageinfo'][0];
187 $this->assertArrayHasKey( 'temp', $image );
188 $this->assertTrue( $image['temp'] );
191 public function testGetImageEmptyComment() {
192 [ $result, ] = $this->doApiRequest( [
193 'action' => 'query',
194 'prop' => 'imageinfo',
195 'titles' => 'File:' . self::IMAGE_NAME,
196 'iiprop' => implode( '|', ApiQueryImageInfo::getPropertyNames() ),
197 'iistart' => self::NO_COMMENT_TIMESTAMP,
198 'iiend' => self::NO_COMMENT_TIMESTAMP,
199 ] );
200 $image = $this->getImageInfoFromResult( $result );
201 $this->assertSame( MWTimestamp::convert( TS_ISO_8601, self::NO_COMMENT_TIMESTAMP ), $image['timestamp'] );
202 $this->assertSame( '', $image['comment'] );
203 $this->assertArrayNotHasKey( 'commenthidden', $image );
206 public function testGetImageInfoOldRestrictedImage() {
207 [ $result, ] = $this->doApiRequest( [
208 'action' => 'query',
209 'prop' => 'imageinfo',
210 'titles' => 'File:' . self::IMAGE_NAME,
211 'iiprop' => implode( '|', ApiQueryImageInfo::getPropertyNames() ),
212 'iistart' => self::OLD_IMAGE_TIMESTAMP,
213 'iiend' => self::OLD_IMAGE_TIMESTAMP,
215 null,
216 false,
217 $this->getTestUser()->getAuthority()
219 $image = $this->getImageInfoFromResult( $result );
220 $this->assertSame( MWTimestamp::convert( TS_ISO_8601, self::OLD_IMAGE_TIMESTAMP ), $image['timestamp'] );
221 $this->assertTrue( $image['commenthidden'] );
222 $this->assertArrayNotHasKey( "comment", $image );
223 $this->assertTrue( $image['userhidden'] );
224 $this->assertArrayNotHasKey( 'user', $image );
225 $this->assertArrayNotHasKey( 'userid', $image );
226 $this->assertTrue( $image['filehidden'] );
227 $this->assertSame( self::OLD_IMAGE_SIZE, $image['size'] );
230 public function testGetImageInfoOldRestrictedImage_sysop() {
231 [ $result, ] = $this->doApiRequest( [
232 'action' => 'query',
233 'prop' => 'imageinfo',
234 'titles' => 'File:' . self::IMAGE_NAME,
235 'iiprop' => implode( '|', ApiQueryImageInfo::getPropertyNames() ),
236 'iistart' => self::OLD_IMAGE_TIMESTAMP,
237 'iiend' => self::OLD_IMAGE_TIMESTAMP,
239 null,
240 false,
241 $this->mockRegisteredUltimateAuthority()
243 $image = $this->getImageInfoFromResult( $result );
244 $this->assertSame( MWTimestamp::convert( TS_ISO_8601, self::OLD_IMAGE_TIMESTAMP ), $image['timestamp'] );
245 $this->assertTrue( $image['commenthidden'] );
246 $this->assertSame( 'deleted comment', $image['comment'] );
247 $this->assertTrue( $image['userhidden'] );
248 $this->assertSame( $this->testUser->getName(), $image['user'] );
249 $this->assertSame( $this->testUser->getId(), $image['userid'] );
250 $this->assertTrue( $image['filehidden'] );
251 $this->assertSame( self::OLD_IMAGE_SIZE, $image['size'] );