575efd6df1ba2802068c3098bdd31eb96680ab75
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiBlockTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 *
8 * @covers ApiBlock
9 */
10 class ApiBlockTest extends ApiTestCase {
11 protected function setUp() {
12 parent::setUp();
13 $this->doLogin();
14 }
15
16 protected function getTokens() {
17 return $this->getTokenList( self::$users['sysop'] );
18 }
19
20 function addDBData() {
21 $user = User::newFromName( 'UTApiBlockee' );
22
23 if ( $user->getId() == 0 ) {
24 $user->addToDatabase();
25 $user->setPassword( 'UTApiBlockeePassword' );
26
27 $user->saveSettings();
28 }
29 }
30
31 /**
32 * This test has probably always been broken and use an invalid token
33 * Bug tracking brokenness is https://bugzilla.wikimedia.org/35646
34 *
35 * Root cause is https://gerrit.wikimedia.org/r/3434
36 * Which made the Block/Unblock API to actually verify the token
37 * previously always considered valid (bug 34212).
38 */
39 public function testMakeNormalBlock() {
40 $tokens = $this->getTokens();
41
42 $user = User::newFromName( 'UTApiBlockee' );
43
44 if ( !$user->getId() ) {
45 $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
46 }
47
48 if ( !array_key_exists( 'blocktoken', $tokens ) ) {
49 $this->markTestIncomplete( "No block token found" );
50 }
51
52 $this->doApiRequest( array(
53 'action' => 'block',
54 'user' => 'UTApiBlockee',
55 'reason' => 'Some reason',
56 'token' => $tokens['blocktoken'] ), null, false, self::$users['sysop']->getUser() );
57
58 $block = Block::newFromTarget( 'UTApiBlockee' );
59
60 $this->assertTrue( !is_null( $block ), 'Block is valid' );
61
62 $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
63 $this->assertEquals( 'Some reason', $block->mReason );
64 $this->assertEquals( 'infinity', $block->mExpiry );
65 }
66
67 /**
68 * @expectedException UsageException
69 * @expectedExceptionMessage The token parameter must be set
70 */
71 public function testBlockingActionWithNoToken() {
72 $this->doApiRequest(
73 array(
74 'action' => 'block',
75 'user' => 'UTApiBlockee',
76 'reason' => 'Some reason',
77 ),
78 null,
79 false,
80 self::$users['sysop']->getUser()
81 );
82 }
83 }