BlockTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * @group Database
  4. * @group Blocking
  5. */
  6. class BlockTest extends MediaWikiLangTestCase {
  7. /**
  8. * @return User
  9. */
  10. private function getUserForBlocking() {
  11. $testUser = $this->getMutableTestUser();
  12. $user = $testUser->getUser();
  13. $user->addToDatabase();
  14. TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
  15. $user->saveSettings();
  16. return $user;
  17. }
  18. /**
  19. * @param User $user
  20. *
  21. * @return Block
  22. * @throws MWException
  23. */
  24. private function addBlockForUser( User $user ) {
  25. // Delete the last round's block if it's still there
  26. $oldBlock = Block::newFromTarget( $user->getName() );
  27. if ( $oldBlock ) {
  28. // An old block will prevent our new one from saving.
  29. $oldBlock->delete();
  30. }
  31. $blockOptions = [
  32. 'address' => $user->getName(),
  33. 'user' => $user->getId(),
  34. 'by' => $this->getTestSysop()->getUser()->getId(),
  35. 'reason' => 'Parce que',
  36. 'expiry' => time() + 100500,
  37. ];
  38. $block = new Block( $blockOptions );
  39. $block->insert();
  40. // save up ID for use in assertion. Since ID is an autoincrement,
  41. // its value might change depending on the order the tests are run.
  42. // ApiBlockTest insert its own blocks!
  43. if ( !$block->getId() ) {
  44. throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
  45. }
  46. $this->addXffBlocks();
  47. return $block;
  48. }
  49. /**
  50. * @covers Block::newFromTarget
  51. */
  52. public function testINewFromTargetReturnsCorrectBlock() {
  53. $user = $this->getUserForBlocking();
  54. $block = $this->addBlockForUser( $user );
  55. $this->assertTrue(
  56. $block->equals( Block::newFromTarget( $user->getName() ) ),
  57. "newFromTarget() returns the same block as the one that was made"
  58. );
  59. }
  60. /**
  61. * @covers Block::newFromID
  62. */
  63. public function testINewFromIDReturnsCorrectBlock() {
  64. $user = $this->getUserForBlocking();
  65. $block = $this->addBlockForUser( $user );
  66. $this->assertTrue(
  67. $block->equals( Block::newFromID( $block->getId() ) ),
  68. "newFromID() returns the same block as the one that was made"
  69. );
  70. }
  71. /**
  72. * per T28425
  73. * @covers Block::__construct
  74. */
  75. public function testT28425BlockTimestampDefaultsToTime() {
  76. $user = $this->getUserForBlocking();
  77. $block = $this->addBlockForUser( $user );
  78. $madeAt = wfTimestamp( TS_MW );
  79. // delta to stop one-off errors when things happen to go over a second mark.
  80. $delta = abs( $madeAt - $block->mTimestamp );
  81. $this->assertLessThan(
  82. 2,
  83. $delta,
  84. "If no timestamp is specified, the block is recorded as time()"
  85. );
  86. }
  87. /**
  88. * CheckUser since being changed to use Block::newFromTarget started failing
  89. * because the new function didn't accept empty strings like Block::load()
  90. * had. Regression T31116.
  91. *
  92. * @dataProvider provideT31116Data
  93. * @covers Block::newFromTarget
  94. */
  95. public function testT31116NewFromTargetWithEmptyIp( $vagueTarget ) {
  96. $user = $this->getUserForBlocking();
  97. $initialBlock = $this->addBlockForUser( $user );
  98. $block = Block::newFromTarget( $user->getName(), $vagueTarget );
  99. $this->assertTrue(
  100. $initialBlock->equals( $block ),
  101. "newFromTarget() returns the same block as the one that was made when "
  102. . "given empty vagueTarget param " . var_export( $vagueTarget, true )
  103. );
  104. }
  105. public static function provideT31116Data() {
  106. return [
  107. [ null ],
  108. [ '' ],
  109. [ false ]
  110. ];
  111. }
  112. /**
  113. * @covers Block::prevents
  114. */
  115. public function testBlockedUserCanNotCreateAccount() {
  116. $username = 'BlockedUserToCreateAccountWith';
  117. $u = User::newFromName( $username );
  118. $u->addToDatabase();
  119. $userId = $u->getId();
  120. $this->assertNotEquals( 0, $userId, 'sanity' );
  121. TestUser::setPasswordForUser( $u, 'NotRandomPass' );
  122. unset( $u );
  123. // Sanity check
  124. $this->assertNull(
  125. Block::newFromTarget( $username ),
  126. "$username should not be blocked"
  127. );
  128. // Reload user
  129. $u = User::newFromName( $username );
  130. $this->assertFalse(
  131. $u->isBlockedFromCreateAccount(),
  132. "Our sandbox user should be able to create account before being blocked"
  133. );
  134. // Foreign perspective (blockee not on current wiki)...
  135. $blockOptions = [
  136. 'address' => $username,
  137. 'user' => $userId,
  138. 'reason' => 'crosswiki block...',
  139. 'timestamp' => wfTimestampNow(),
  140. 'expiry' => $this->db->getInfinity(),
  141. 'createAccount' => true,
  142. 'enableAutoblock' => true,
  143. 'hideName' => true,
  144. 'blockEmail' => true,
  145. 'byText' => 'm>MetaWikiUser',
  146. ];
  147. $block = new Block( $blockOptions );
  148. $block->insert();
  149. // Reload block from DB
  150. $userBlock = Block::newFromTarget( $username );
  151. $this->assertTrue(
  152. (bool)$block->prevents( 'createaccount' ),
  153. "Block object in DB should prevents 'createaccount'"
  154. );
  155. $this->assertInstanceOf(
  156. Block::class,
  157. $userBlock,
  158. "'$username' block block object should be existent"
  159. );
  160. // Reload user
  161. $u = User::newFromName( $username );
  162. $this->assertTrue(
  163. (bool)$u->isBlockedFromCreateAccount(),
  164. "Our sandbox user '$username' should NOT be able to create account"
  165. );
  166. }
  167. /**
  168. * @covers Block::insert
  169. */
  170. public function testCrappyCrossWikiBlocks() {
  171. // Delete the last round's block if it's still there
  172. $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
  173. if ( $oldBlock ) {
  174. // An old block will prevent our new one from saving.
  175. $oldBlock->delete();
  176. }
  177. // Local perspective (blockee on current wiki)...
  178. $user = User::newFromName( 'UserOnForeignWiki' );
  179. $user->addToDatabase();
  180. $userId = $user->getId();
  181. $this->assertNotEquals( 0, $userId, 'sanity' );
  182. // Foreign perspective (blockee not on current wiki)...
  183. $blockOptions = [
  184. 'address' => 'UserOnForeignWiki',
  185. 'user' => $user->getId(),
  186. 'reason' => 'crosswiki block...',
  187. 'timestamp' => wfTimestampNow(),
  188. 'expiry' => $this->db->getInfinity(),
  189. 'createAccount' => true,
  190. 'enableAutoblock' => true,
  191. 'hideName' => true,
  192. 'blockEmail' => true,
  193. 'byText' => 'Meta>MetaWikiUser',
  194. ];
  195. $block = new Block( $blockOptions );
  196. $res = $block->insert( $this->db );
  197. $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
  198. $user = null; // clear
  199. $block = Block::newFromID( $res['id'] );
  200. $this->assertEquals(
  201. 'UserOnForeignWiki',
  202. $block->getTarget()->getName(),
  203. 'Correct blockee name'
  204. );
  205. $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
  206. $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
  207. 'Correct blocker name' );
  208. $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
  209. $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
  210. }
  211. protected function addXffBlocks() {
  212. static $inited = false;
  213. if ( $inited ) {
  214. return;
  215. }
  216. $inited = true;
  217. $blockList = [
  218. [ 'target' => '70.2.0.0/16',
  219. 'type' => Block::TYPE_RANGE,
  220. 'desc' => 'Range Hardblock',
  221. 'ACDisable' => false,
  222. 'isHardblock' => true,
  223. 'isAutoBlocking' => false,
  224. ],
  225. [ 'target' => '2001:4860:4001::/48',
  226. 'type' => Block::TYPE_RANGE,
  227. 'desc' => 'Range6 Hardblock',
  228. 'ACDisable' => false,
  229. 'isHardblock' => true,
  230. 'isAutoBlocking' => false,
  231. ],
  232. [ 'target' => '60.2.0.0/16',
  233. 'type' => Block::TYPE_RANGE,
  234. 'desc' => 'Range Softblock with AC Disabled',
  235. 'ACDisable' => true,
  236. 'isHardblock' => false,
  237. 'isAutoBlocking' => false,
  238. ],
  239. [ 'target' => '50.2.0.0/16',
  240. 'type' => Block::TYPE_RANGE,
  241. 'desc' => 'Range Softblock',
  242. 'ACDisable' => false,
  243. 'isHardblock' => false,
  244. 'isAutoBlocking' => false,
  245. ],
  246. [ 'target' => '50.1.1.1',
  247. 'type' => Block::TYPE_IP,
  248. 'desc' => 'Exact Softblock',
  249. 'ACDisable' => false,
  250. 'isHardblock' => false,
  251. 'isAutoBlocking' => false,
  252. ],
  253. ];
  254. $blocker = $this->getTestUser()->getUser();
  255. foreach ( $blockList as $insBlock ) {
  256. $target = $insBlock['target'];
  257. if ( $insBlock['type'] === Block::TYPE_IP ) {
  258. $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
  259. } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
  260. $target = IP::sanitizeRange( $target );
  261. }
  262. $block = new Block();
  263. $block->setTarget( $target );
  264. $block->setBlocker( $blocker );
  265. $block->mReason = $insBlock['desc'];
  266. $block->mExpiry = 'infinity';
  267. $block->prevents( 'createaccount', $insBlock['ACDisable'] );
  268. $block->isHardblock( $insBlock['isHardblock'] );
  269. $block->isAutoblocking( $insBlock['isAutoBlocking'] );
  270. $block->insert();
  271. }
  272. }
  273. public static function providerXff() {
  274. return [
  275. [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
  276. 'count' => 2,
  277. 'result' => 'Range Hardblock'
  278. ],
  279. [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
  280. 'count' => 2,
  281. 'result' => 'Range Softblock with AC Disabled'
  282. ],
  283. [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
  284. 'count' => 2,
  285. 'result' => 'Exact Softblock'
  286. ],
  287. [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
  288. 'count' => 3,
  289. 'result' => 'Exact Softblock'
  290. ],
  291. [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
  292. 'count' => 2,
  293. 'result' => 'Range Hardblock'
  294. ],
  295. [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
  296. 'count' => 2,
  297. 'result' => 'Range Hardblock'
  298. ],
  299. [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
  300. 'count' => 2,
  301. 'result' => 'Range Softblock with AC Disabled'
  302. ],
  303. [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
  304. 'count' => 2,
  305. 'result' => 'Exact Softblock'
  306. ],
  307. [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
  308. 'count' => 1,
  309. 'result' => 'Range Softblock with AC Disabled'
  310. ],
  311. [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
  312. 'count' => 2,
  313. 'result' => 'Range6 Hardblock'
  314. ],
  315. ];
  316. }
  317. /**
  318. * @dataProvider providerXff
  319. * @covers Block::getBlocksForIPList
  320. * @covers Block::chooseBlock
  321. */
  322. public function testBlocksOnXff( $xff, $exCount, $exResult ) {
  323. $user = $this->getUserForBlocking();
  324. $this->addBlockForUser( $user );
  325. $list = array_map( 'trim', explode( ',', $xff ) );
  326. $xffblocks = Block::getBlocksForIPList( $list, true );
  327. $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
  328. $block = Block::chooseBlock( $xffblocks, $list );
  329. $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
  330. }
  331. /**
  332. * @covers Block::__construct
  333. */
  334. public function testDeprecatedConstructor() {
  335. $this->hideDeprecated( 'Block::__construct with multiple arguments' );
  336. $username = 'UnthinkablySecretRandomUsername';
  337. $reason = 'being irrational';
  338. # Set up the target
  339. $u = User::newFromName( $username );
  340. if ( $u->getId() == 0 ) {
  341. $u->addToDatabase();
  342. TestUser::setPasswordForUser( $u, 'TotallyObvious' );
  343. }
  344. unset( $u );
  345. # Make sure the user isn't blocked
  346. $this->assertNull(
  347. Block::newFromTarget( $username ),
  348. "$username should not be blocked"
  349. );
  350. # Perform the block
  351. $block = new Block(
  352. /* address */ $username,
  353. /* user */ 0,
  354. /* by */ $this->getTestSysop()->getUser()->getId(),
  355. /* reason */ $reason,
  356. /* timestamp */ 0,
  357. /* auto */ false,
  358. /* expiry */ 0
  359. );
  360. $block->insert();
  361. # Check target
  362. $this->assertEquals(
  363. $block->getTarget()->getName(),
  364. $username,
  365. "Target should be set properly"
  366. );
  367. # Check supplied parameter
  368. $this->assertEquals(
  369. $block->mReason,
  370. $reason,
  371. "Reason should be non-default"
  372. );
  373. # Check default parameter
  374. $this->assertFalse(
  375. (bool)$block->prevents( 'createaccount' ),
  376. "Account creation should not be blocked by default"
  377. );
  378. }
  379. /**
  380. * @covers Block::getSystemBlockType
  381. * @covers Block::insert
  382. * @covers Block::doAutoblock
  383. */
  384. public function testSystemBlocks() {
  385. $user = $this->getUserForBlocking();
  386. $this->addBlockForUser( $user );
  387. $blockOptions = [
  388. 'address' => $user->getName(),
  389. 'reason' => 'test system block',
  390. 'timestamp' => wfTimestampNow(),
  391. 'expiry' => $this->db->getInfinity(),
  392. 'byText' => 'MediaWiki default',
  393. 'systemBlock' => 'test',
  394. 'enableAutoblock' => true,
  395. ];
  396. $block = new Block( $blockOptions );
  397. $this->assertSame( 'test', $block->getSystemBlockType() );
  398. try {
  399. $block->insert();
  400. $this->fail( 'Expected exception not thrown' );
  401. } catch ( MWException $ex ) {
  402. $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
  403. }
  404. try {
  405. $block->doAutoblock( '192.0.2.2' );
  406. $this->fail( 'Expected exception not thrown' );
  407. } catch ( MWException $ex ) {
  408. $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
  409. }
  410. }
  411. }