Add block restriction classes
[lhc/web/wiklou.git] / tests / phpunit / includes / block / Restriction / RestrictionTestCase.php
1 <?php
2
3 namespace MediaWiki\Tests\Block\Restriction;
4
5 /**
6 * @group Blocking
7 */
8 abstract class RestrictionTestCase extends \MediaWikiTestCase {
9 public function testConstruct() {
10 $class = $this->getClass();
11 $restriction = new $class( 1, 2 );
12
13 $this->assertSame( $restriction->getBlockId(), 1 );
14 $this->assertSame( $restriction->getValue(), 2 );
15 }
16
17 public function testSetBlockId() {
18 $class = $this->getClass();
19 $restriction = new $class( 1, 2 );
20
21 $restriction->setBlockId( 10 );
22 $this->assertSame( $restriction->getBlockId(), 10 );
23 }
24
25 public function testEquals() {
26 $class = $this->getClass();
27
28 // Test two restrictions with the same data.
29 $restriction = new $class( 1, 2 );
30 $second = new $class( 1, 2 );
31 $this->assertTrue( $restriction->equals( $second ) );
32
33 // Test two restrictions that implement different classes.
34 $second = $this->createMock( $this->getClass() );
35 $this->assertFalse( $restriction->equals( $second ) );
36
37 // Not the same block id.
38 $second = new $class( 2, 2 );
39 $this->assertTrue( $restriction->equals( $second ) );
40
41 // Not the same value.
42 $second = new $class( 1, 3 );
43 $this->assertFalse( $restriction->equals( $second ) );
44 }
45
46 public function testNewFromRow() {
47 $class = $this->getClass();
48
49 $restriction = $class::newFromRow( (object)[
50 'ir_ipb_id' => 1,
51 'ir_value' => 2,
52 ] );
53
54 $this->assertSame( 1, $restriction->getBlockId() );
55 $this->assertSame( 2, $restriction->getValue() );
56 }
57
58 public function testToRow() {
59 $class = $this->getClass();
60
61 $restriction = new $class( 1, 2 );
62 $row = $restriction->toRow();
63
64 $this->assertSame( 1, $row['ir_ipb_id'] );
65 $this->assertSame( 2, $row['ir_value'] );
66 }
67
68 /**
69 * Get the class name of the class that is being tested.
70 *
71 * @return string
72 */
73 abstract protected function getClass();
74 }