Merge "SECURITY: Do not allow users to undelete a page they can't edit or create"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / connectionmanager / ConnectionManagerTest.php
1 <?php
2
3 namespace Wikimedia\Tests\Rdbms;
4
5 use IDatabase;
6 use Wikimedia\Rdbms\LoadBalancer;
7 use PHPUnit_Framework_MockObject_MockObject;
8 use Wikimedia\Rdbms\ConnectionManager;
9
10 /**
11 * @covers Wikimedia\Rdbms\ConnectionManager
12 *
13 * @license GPL-2.0+
14 * @author Daniel Kinzler
15 */
16 class ConnectionManagerTest extends \PHPUnit_Framework_TestCase {
17
18 /**
19 * @return IDatabase|PHPUnit_Framework_MockObject_MockObject
20 */
21 private function getIDatabaseMock() {
22 return $this->getMock( IDatabase::class );
23 }
24
25 /**
26 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
27 */
28 private function getLoadBalancerMock() {
29 $lb = $this->getMockBuilder( LoadBalancer::class )
30 ->disableOriginalConstructor()
31 ->getMock();
32
33 return $lb;
34 }
35
36 public function testGetReadConnection_nullGroups() {
37 $database = $this->getIDatabaseMock();
38 $lb = $this->getLoadBalancerMock();
39
40 $lb->expects( $this->once() )
41 ->method( 'getConnection' )
42 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
43 ->will( $this->returnValue( $database ) );
44
45 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
46 $actual = $manager->getReadConnection();
47
48 $this->assertSame( $database, $actual );
49 }
50
51 public function testGetReadConnection_withGroups() {
52 $database = $this->getIDatabaseMock();
53 $lb = $this->getLoadBalancerMock();
54
55 $lb->expects( $this->once() )
56 ->method( 'getConnection' )
57 ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
58 ->will( $this->returnValue( $database ) );
59
60 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
61 $actual = $manager->getReadConnection( [ 'group2' ] );
62
63 $this->assertSame( $database, $actual );
64 }
65
66 public function testGetWriteConnection() {
67 $database = $this->getIDatabaseMock();
68 $lb = $this->getLoadBalancerMock();
69
70 $lb->expects( $this->once() )
71 ->method( 'getConnection' )
72 ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
73 ->will( $this->returnValue( $database ) );
74
75 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
76 $actual = $manager->getWriteConnection();
77
78 $this->assertSame( $database, $actual );
79 }
80
81 public function testReleaseConnection() {
82 $database = $this->getIDatabaseMock();
83 $lb = $this->getLoadBalancerMock();
84
85 $lb->expects( $this->once() )
86 ->method( 'reuseConnection' )
87 ->with( $database )
88 ->will( $this->returnValue( null ) );
89
90 $manager = new ConnectionManager( $lb );
91 $manager->releaseConnection( $database );
92 }
93
94 public function testGetReadConnectionRef_nullGroups() {
95 $database = $this->getIDatabaseMock();
96 $lb = $this->getLoadBalancerMock();
97
98 $lb->expects( $this->once() )
99 ->method( 'getConnectionRef' )
100 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
101 ->will( $this->returnValue( $database ) );
102
103 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
104 $actual = $manager->getReadConnectionRef();
105
106 $this->assertSame( $database, $actual );
107 }
108
109 public function testGetReadConnectionRef_withGroups() {
110 $database = $this->getIDatabaseMock();
111 $lb = $this->getLoadBalancerMock();
112
113 $lb->expects( $this->once() )
114 ->method( 'getConnectionRef' )
115 ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
116 ->will( $this->returnValue( $database ) );
117
118 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
119 $actual = $manager->getReadConnectionRef( [ 'group2' ] );
120
121 $this->assertSame( $database, $actual );
122 }
123
124 public function testGetWriteConnectionRef() {
125 $database = $this->getIDatabaseMock();
126 $lb = $this->getLoadBalancerMock();
127
128 $lb->expects( $this->once() )
129 ->method( 'getConnectionRef' )
130 ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
131 ->will( $this->returnValue( $database ) );
132
133 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
134 $actual = $manager->getWriteConnectionRef();
135
136 $this->assertSame( $database, $actual );
137 }
138
139 }