Clean and repair many phpunit tests (+ fix implied configuration)
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiBlockTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 */
7 class ApiBlockTest extends ApiTestCase {
8
9 protected function setUp() {
10 parent::setUp();
11 $this->doLogin();
12 }
13
14 function getTokens() {
15 return $this->getTokenList( self::$users['sysop'] );
16 }
17
18 function addDBData() {
19 $user = User::newFromName( 'UTApiBlockee' );
20
21 if ( $user->getId() == 0 ) {
22 $user->addToDatabase();
23 $user->setPassword( 'UTApiBlockeePassword' );
24
25 $user->saveSettings();
26 }
27 }
28
29 /**
30 * This test has probably always been broken and use an invalid token
31 * Bug tracking brokenness is https://bugzilla.wikimedia.org/35646
32 *
33 * Root cause is https://gerrit.wikimedia.org/r/3434
34 * Which made the Block/Unblock API to actually verify the token
35 * previously always considered valid (bug 34212).
36 */
37 function testMakeNormalBlock() {
38
39 $data = $this->getTokens();
40
41 $user = User::newFromName( 'UTApiBlockee' );
42
43 if ( !$user->getId() ) {
44 $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
45 }
46
47 if( !isset( $data[0]['query']['pages'] ) ) {
48 $this->markTestIncomplete( "No block token found" );
49 }
50
51 $keys = array_keys( $data[0]['query']['pages'] );
52 $key = array_pop( $keys );
53 $pageinfo = $data[0]['query']['pages'][$key];
54
55 $data = $this->doApiRequest( array(
56 'action' => 'block',
57 'user' => 'UTApiBlockee',
58 'reason' => 'Some reason',
59 'token' => $pageinfo['blocktoken'] ), null, false, self::$users['sysop']->user );
60
61 $block = Block::newFromTarget('UTApiBlockee');
62
63 $this->assertTrue( !is_null( $block ), 'Block is valid' );
64
65 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
66 $this->assertEquals( 'Some reason', $block->mReason );
67 $this->assertEquals( 'infinity', $block->mExpiry );
68
69 }
70
71 /**
72 * @dataProvider provideBlockUnblockAction
73 */
74 function testGetTokenUsingABlockingAction( $action ) {
75 $data = $this->doApiRequest(
76 array(
77 'action' => $action,
78 'user' => 'UTApiBlockee',
79 'gettoken' => '' ),
80 null,
81 false,
82 self::$users['sysop']->user
83 );
84 $this->assertEquals( 34, strlen( $data[0][$action]["{$action}token"] ) );
85 }
86
87 /**
88 * Attempting to block without a token should give a UsageException with
89 * error message:
90 * "The token parameter must be set"
91 *
92 * @dataProvider provideBlockUnblockAction
93 * @expectedException UsageException
94 */
95 function testBlockingActionWithNoToken( $action ) {
96 $this->doApiRequest(
97 array(
98 'action' => $action,
99 'user' => 'UTApiBlockee',
100 'reason' => 'Some reason',
101 ),
102 null,
103 false,
104 self::$users['sysop']->user
105 );
106 }
107
108 /**
109 * Just provide the 'block' and 'unblock' action to test both API calls
110 */
111 function provideBlockUnblockAction() {
112 return array(
113 array( 'block' ),
114 array( 'unblock' ),
115 );
116 }
117 }