1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
<?php
use Flow\Model\AbstractRevision;
use Flow\Model\PostRevision;
use Flow\Model\UUID;
/**
* Integration tests for the Thanks Flow api module
*
* @covers ApiFlowThank
*
* @group Thanks
* @gropu Database
* @group medium
* @group API
* @group Flow
*
* @author Benjamin Chen
*/
class ApiFlowThankTest extends ApiTestCase {
/**
* @var PostRevision
*/
public $topic,
$meUser,
$otherUser,
$postByOtherUser,
$postByMe;
public function setUp() {
parent::setUp();
if ( !class_exists( 'FlowHooks' ) ) {
$this->markTestSkipped( 'Flow is not installed' );
}
// mock topic and post
if ( method_exists( $this, 'getTestUser' ) ) {
$this->meUser = $this->getMutableTestUser()->getUser();
$this->otherUser = $this->getMutableTestUser()->getUser();
} else {
$this->meUser = self::$users[ 'sysop' ]->getUser();
$this->otherUser = self::$users[ 'uploader' ]->getUser();
}
$this->topic = $this->generateObject();
$this->postByOtherUser = $this->generateObject( [
'tree_orig_user_id' => $this->otherUser->getId(),
'tree_parent_id' => $this->topic->getPostId()->getBinary(),
], [], 1 );
$this->postByMe = $this->generateObject( [
'tree_orig_user_id' => $this->meUser->getId(),
'tree_parent_id' => $this->topic->getPostId()->getBinary(),
], [], 1 );
// Set up mock classes in Container.
$mockLoader = $this->getMockBuilder( '\Flow\Repository\RootPostLoader' )
->disableOriginalConstructor()
->getMock();
$that = $this;
$mockLoader->expects( $this->any() )
->method( 'getWithRoot' )
->will( $this->returnCallback(
// Hard to work with class variables or callbacks,
// using anonymous function instead.
function ( $postId ) use ( $that ) {
switch ( $postId ) {
case $that->postByOtherUser->getPostId():
return [
'post' => $that->postByOtherUser,
'root' => $that->topic
];
case $that->postByMe->getPostId():
return [
'post' => $that->postByMe,
'root' => $that->topic
];
default:
return [ 'post' => null ];
}
}
) );
$mockWorkflow = $this->getMock( '\Flow\Model\Workflow' );
$mockWorkflow->expects( $this->any() )
->method( 'getOwnerTitle' )
->will( $this->returnValue( new Title() ) );
$mockWorkflow->expects( $this->any() )
->method( 'getArticleTitle' )
->will( $this->returnValue( new Title() ) );
$mockStorage = $this->getMockBuilder( '\Flow\Data\ManagerGroup' )
->disableOriginalConstructor()
->getMock();
$mockStorage->expects( $this->any() )
->method( 'get' )
->will( $this->returnValue( $mockWorkflow ) );
$mockTemplating = $this->getMockBuilder( 'Flow\Templating' )
->disableOriginalConstructor()
->getMock();
$mockTemplating->expects( $this->any() )
->method( 'getContent' )
->will( $this->returnValue( 'test content' ) );
Flow\Container::reset();
$container = Flow\Container::getContainer();
$container[ 'loader.root_post' ] = $mockLoader;
$container[ 'storage' ] = $mockStorage;
$container[ 'templating' ] = $mockTemplating;
$this->doLogin( 'sysop' );
\DeferredUpdates::clearPendingUpdates();
}
public function testRequestWithoutToken() {
$this->setExpectedException( 'ApiUsageException', 'The "token" parameter must be set.' );
$this->doApiRequest( [
'action' => 'flowthank',
'postid' => UUID::create( '42' )->getAlphadecimal(),
] );
}
public function testInvalidRequest() {
$this->setExpectedException( 'ApiUsageException', 'The "postid" parameter must be set.' );
$this->doApiRequestWithToken( [ 'action' => 'flowthank' ] );
}
public function testValidRequest() {
list( $result,, ) = $this->doApiRequestWithToken( [
'action' => 'flowthank',
'postid' => $this->postByOtherUser->getPostId()->getAlphadecimal(),
] );
$this->assertSuccess( $result );
}
public function testRequestWithInvalidId() {
$this->setExpectedException( 'ApiUsageException', 'Post ID is not valid' );
list( $result,, ) = $this->doApiRequestWithToken( [
'action' => 'flowthank',
'postid' => UUID::create( '42' )->getAlphadecimal(),
] );
}
public function testRequestWithOwnId() {
$this->setExpectedException( 'ApiUsageException', 'You cannot thank yourself' );
list( $result,, ) = $this->doApiRequestWithToken( [
'action' => 'flowthank',
'postid' => $this->postByMe->getPostId()->getAlphadecimal(),
], null, $this->meUser );
}
protected function assertSuccess( $result ) {
$this->assertEquals( 1, $result[ 'result' ][ 'success' ] );
}
/**
* This method is obtained from Flow/tests/PostRevisionTestCase.php
*
* Returns an array, representing flow_revision & flow_tree_revision db
* columns.
*
* You can pass in arguments to override default data.
* With no arguments tossed in, default data (resembling a newly-created
* topic title) will be returned.
*
* @param array[optional] $row DB row data (only specify override columns)
* @return array
*/
protected function generateRow( array $row = [] ) {
$uuidPost = UUID::create();
$uuidRevision = UUID::create();
$user = $this->meUser;
$userId = $user->getId();
$userIp = null;
return $row + [
// flow_revision
'rev_id' => $uuidRevision->getBinary(),
'rev_type' => 'post',
'rev_user_id' => $userId,
'rev_user_ip' => $userIp,
'rev_user_wiki' => wfWikiId(),
'rev_parent_id' => null,
'rev_flags' => 'html',
'rev_content' => 'test content',
'rev_change_type' => 'new-post',
'rev_mod_state' => AbstractRevision::MODERATED_NONE,
'rev_mod_user_id' => null,
'rev_mod_user_ip' => null,
'rev_mod_user_wiki' => null,
'rev_mod_timestamp' => null,
'rev_mod_reason' => null,
'rev_last_edit_id' => null,
'rev_edit_user_id' => null,
'rev_edit_user_ip' => null,
'rev_edit_user_wiki' => null,
// flow_tree_revision
'tree_rev_descendant_id' => $uuidPost->getBinary(),
'rev_type_id' => $uuidPost->getBinary(),
'tree_rev_id' => $uuidRevision->getBinary(),
'tree_orig_create_time' => wfTimestampNow(),
'tree_orig_user_id' => $userId,
'tree_orig_user_ip' => $userIp,
'tree_orig_user_wiki' => wfWikiId(),
'tree_parent_id' => null,
];
}
/**
* This method is obtained from Flow/tests/PostRevisionTestCase.php
*
* Returns a PostRevision object.
*
* You can pass in arguments to override default data.
* With no arguments tossed in, a default revision (resembling a newly-
* created topic title) will be returned.
*
* @param array[optional] $row DB row data (only specify override columns)
* @param array[optional] $children Array of child PostRevision objects
* @param int[optional] $depth Depth of the PostRevision object
* @return PostRevision
*/
protected function generateObject( array $row = [], $children = [], $depth = 0 ) {
$row = $this->generateRow( $row );
$revision = PostRevision::fromStorageRow( $row );
$revision->setChildren( $children );
$revision->setDepth( $depth );
return $revision;
}
}
|