| 1 |
<?php |
| 2 |
|
| 3 |
use Flow\Model\AbstractRevision; |
| 4 |
use Flow\Model\PostRevision; |
| 5 |
use Flow\Model\UUID; |
| 6 |
use Flow\Model\Workflow; |
| 7 |
use MediaWiki\Deferred\DeferredUpdates; |
| 8 |
use MediaWiki\Registration\ExtensionRegistry; |
| 9 |
use MediaWiki\Tests\Api\ApiTestCase; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MediaWiki\User\User; |
| 12 |
use MediaWiki\WikiMap\WikiMap; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class ApiFlowThankIntegrationTest extends ApiTestCase { |
| 28 |
|
| 29 |
|
| 30 |
public $topic; |
| 31 |
|
| 32 |
|
| 33 |
public $meUser; |
| 34 |
|
| 35 |
|
| 36 |
public $otherUser; |
| 37 |
|
| 38 |
|
| 39 |
public $postByOtherUser; |
| 40 |
|
| 41 |
|
| 42 |
public $postByMe; |
| 43 |
|
| 44 |
public function setUp(): void { |
| 45 |
parent::setUp(); |
| 46 |
|
| 47 |
if ( !ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) { |
| 48 |
$this->markTestSkipped( 'Flow is not installed' ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
$this->meUser = $this->getMutableTestUser()->getUser(); |
| 53 |
$this->otherUser = $this->getMutableTestUser()->getUser(); |
| 54 |
$this->topic = $this->generateObject(); |
| 55 |
$this->postByOtherUser = $this->generateObject( [ |
| 56 |
'tree_orig_user_id' => $this->otherUser->getId(), |
| 57 |
'tree_parent_id' => $this->topic->getPostId()->getBinary(), |
| 58 |
], [], 1 ); |
| 59 |
$this->postByMe = $this->generateObject( [ |
| 60 |
'tree_orig_user_id' => $this->meUser->getId(), |
| 61 |
'tree_parent_id' => $this->topic->getPostId()->getBinary(), |
| 62 |
], [], 1 ); |
| 63 |
|
| 64 |
|
| 65 |
$mockLoader = $this->createMock( \Flow\Repository\RootPostLoader::class ); |
| 66 |
$mockLoader->expects( $this->any() ) |
| 67 |
->method( 'getWithRoot' ) |
| 68 |
->willReturnCallback( |
| 69 |
function ( $postId ) { |
| 70 |
switch ( $postId ) { |
| 71 |
case $this->postByOtherUser->getPostId(): |
| 72 |
return [ |
| 73 |
'post' => $this->postByOtherUser, |
| 74 |
'root' => $this->topic |
| 75 |
]; |
| 76 |
|
| 77 |
case $this->postByMe->getPostId(): |
| 78 |
return [ |
| 79 |
'post' => $this->postByMe, |
| 80 |
'root' => $this->topic |
| 81 |
]; |
| 82 |
|
| 83 |
default: |
| 84 |
return [ 'post' => null ]; |
| 85 |
} |
| 86 |
} |
| 87 |
); |
| 88 |
|
| 89 |
$mockWorkflow = $this->createMock( Workflow::class ); |
| 90 |
$mockWorkflow->expects( $this->any() ) |
| 91 |
->method( 'getOwnerTitle' ) |
| 92 |
->willReturn( $this->createMock( Title::class ) ); |
| 93 |
$mockWorkflow->expects( $this->any() ) |
| 94 |
->method( 'getArticleTitle' ) |
| 95 |
->willReturn( $this->createMock( Title::class ) ); |
| 96 |
|
| 97 |
$mockStorage = $this->createMock( \Flow\Data\ManagerGroup::class ); |
| 98 |
|
| 99 |
$mockStorage->expects( $this->any() ) |
| 100 |
->method( 'get' ) |
| 101 |
->willReturn( $mockWorkflow ); |
| 102 |
|
| 103 |
$mockTemplating = $this->createMock( \Flow\Templating::class ); |
| 104 |
|
| 105 |
$mockTemplating->expects( $this->any() ) |
| 106 |
->method( 'getContent' ) |
| 107 |
->willReturn( 'test content' ); |
| 108 |
|
| 109 |
Flow\Container::reset(); |
| 110 |
$container = Flow\Container::getContainer(); |
| 111 |
$container[ 'loader.root_post' ] = $mockLoader; |
| 112 |
$container[ 'storage' ] = $mockStorage; |
| 113 |
$container[ 'templating' ] = $mockTemplating; |
| 114 |
|
| 115 |
DeferredUpdates::clearPendingUpdates(); |
| 116 |
} |
| 117 |
|
| 118 |
public function testRequestWithoutToken() { |
| 119 |
$this->expectApiErrorCode( 'missingparam' ); |
| 120 |
$this->doApiRequest( [ |
| 121 |
'action' => 'flowthank', |
| 122 |
'postid' => UUID::create( '42' )->getAlphadecimal(), |
| 123 |
] ); |
| 124 |
} |
| 125 |
|
| 126 |
public function testInvalidRequest() { |
| 127 |
$this->expectApiErrorCode( 'missingparam' ); |
| 128 |
$this->doApiRequestWithToken( [ 'action' => 'flowthank' ] ); |
| 129 |
} |
| 130 |
|
| 131 |
public function testValidRequest() { |
| 132 |
[ $result,, ] = $this->doApiRequestWithToken( [ |
| 133 |
'action' => 'flowthank', |
| 134 |
'postid' => $this->postByOtherUser->getPostId()->getAlphadecimal(), |
| 135 |
] ); |
| 136 |
$this->assertSuccess( $result ); |
| 137 |
} |
| 138 |
|
| 139 |
public function testRequestWithInvalidId() { |
| 140 |
$this->expectApiErrorCode( 'invalidpostid' ); |
| 141 |
$this->doApiRequestWithToken( [ |
| 142 |
'action' => 'flowthank', |
| 143 |
'postid' => UUID::create( '42' )->getAlphadecimal(), |
| 144 |
] ); |
| 145 |
} |
| 146 |
|
| 147 |
public function testRequestWithOwnId() { |
| 148 |
$this->expectApiErrorCode( 'invalidrecipient' ); |
| 149 |
$this->doApiRequestWithToken( [ |
| 150 |
'action' => 'flowthank', |
| 151 |
'postid' => $this->postByMe->getPostId()->getAlphadecimal(), |
| 152 |
], null, $this->meUser ); |
| 153 |
} |
| 154 |
|
| 155 |
protected function assertSuccess( $result ) { |
| 156 |
$this->assertSame( 1, $result[ 'result' ][ 'success' ] ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
protected function generateRow( array $row = [] ) { |
| 173 |
$uuidPost = UUID::create(); |
| 174 |
$uuidRevision = UUID::create(); |
| 175 |
|
| 176 |
$user = $this->meUser; |
| 177 |
$userId = $user->getId(); |
| 178 |
$userIp = null; |
| 179 |
|
| 180 |
return $row + [ |
| 181 |
|
| 182 |
'rev_id' => $uuidRevision->getBinary(), |
| 183 |
'rev_type' => 'post', |
| 184 |
'rev_user_id' => $userId, |
| 185 |
'rev_user_ip' => $userIp, |
| 186 |
'rev_user_wiki' => WikiMap::getCurrentWikiId(), |
| 187 |
'rev_parent_id' => null, |
| 188 |
'rev_flags' => 'html', |
| 189 |
'rev_content' => 'test content', |
| 190 |
'rev_change_type' => 'new-post', |
| 191 |
'rev_mod_state' => AbstractRevision::MODERATED_NONE, |
| 192 |
'rev_mod_user_id' => null, |
| 193 |
'rev_mod_user_ip' => null, |
| 194 |
'rev_mod_user_wiki' => null, |
| 195 |
'rev_mod_timestamp' => null, |
| 196 |
'rev_mod_reason' => null, |
| 197 |
'rev_last_edit_id' => null, |
| 198 |
'rev_edit_user_id' => null, |
| 199 |
'rev_edit_user_ip' => null, |
| 200 |
'rev_edit_user_wiki' => null, |
| 201 |
|
| 202 |
|
| 203 |
'tree_rev_descendant_id' => $uuidPost->getBinary(), |
| 204 |
'rev_type_id' => $uuidPost->getBinary(), |
| 205 |
'tree_rev_id' => $uuidRevision->getBinary(), |
| 206 |
'tree_orig_create_time' => wfTimestampNow(), |
| 207 |
'tree_orig_user_id' => $userId, |
| 208 |
'tree_orig_user_ip' => $userIp, |
| 209 |
'tree_orig_user_wiki' => WikiMap::getCurrentWikiId(), |
| 210 |
'tree_parent_id' => null, |
| 211 |
]; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
protected function generateObject( array $row = [], $children = [], $depth = 0 ) { |
| 229 |
$row = $this->generateRow( $row ); |
| 230 |
|
| 231 |
$revision = PostRevision::fromStorageRow( $row ); |
| 232 |
$revision->setChildren( $children ); |
| 233 |
$revision->setDepth( $depth ); |
| 234 |
|
| 235 |
return $revision; |
| 236 |
} |
| 237 |
|
| 238 |
} |