Merge "Remove patrol config check in User::isAllowed()"
[lhc/web/wiklou.git] / tests / phpunit / includes / session / TestUtils.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 /**
6 * Utility functions for Session unit tests
7 */
8 class TestUtils {
9
10 /**
11 * Override the singleton for unit testing
12 * @param SessionManager|null $manager
13 * @return \\ScopedCallback|null
14 */
15 public static function setSessionManagerSingleton( SessionManager $manager = null ) {
16 session_write_close();
17
18 $rInstance = new \ReflectionProperty(
19 'MediaWiki\\Session\\SessionManager', 'instance'
20 );
21 $rInstance->setAccessible( true );
22 $rGlobalSession = new \ReflectionProperty(
23 'MediaWiki\\Session\\SessionManager', 'globalSession'
24 );
25 $rGlobalSession->setAccessible( true );
26 $rGlobalSessionRequest = new \ReflectionProperty(
27 'MediaWiki\\Session\\SessionManager', 'globalSessionRequest'
28 );
29 $rGlobalSessionRequest->setAccessible( true );
30
31 $oldInstance = $rInstance->getValue();
32
33 $reset = array(
34 array( $rInstance, $oldInstance ),
35 array( $rGlobalSession, $rGlobalSession->getValue() ),
36 array( $rGlobalSessionRequest, $rGlobalSessionRequest->getValue() ),
37 );
38
39 $rInstance->setValue( $manager );
40 $rGlobalSession->setValue( null );
41 $rGlobalSessionRequest->setValue( null );
42 if ( $manager && PHPSessionHandler::isInstalled() ) {
43 PHPSessionHandler::install( $manager );
44 }
45
46 return new \ScopedCallback( function () use ( &$reset, $oldInstance ) {
47 foreach ( $reset as &$arr ) {
48 $arr[0]->setValue( $arr[1] );
49 }
50 if ( $oldInstance && PHPSessionHandler::isInstalled() ) {
51 PHPSessionHandler::install( $oldInstance );
52 }
53 } );
54 }
55
56 /**
57 * If you need a SessionBackend for testing but don't want to create a real
58 * one, use this.
59 * @return SessionBackend Unconfigured! Use reflection to set any private
60 * fields necessary.
61 */
62 public static function getDummySessionBackend() {
63 $rc = new \ReflectionClass( 'MediaWiki\\Session\\SessionBackend' );
64 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
65 \PHPUnit_Framework_Assert::markTestSkipped(
66 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
67 );
68 }
69
70 return $rc->newInstanceWithoutConstructor();
71 }
72
73 /**
74 * If you need a Session for testing but don't want to create a backend to
75 * construct one, use this.
76 * @param object $backend Object to serve as the SessionBackend
77 * @param int $index Index
78 * @return Session
79 */
80 public static function getDummySession( $backend = null, $index = -1 ) {
81 $rc = new \ReflectionClass( 'MediaWiki\\Session\\Session' );
82 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
83 \PHPUnit_Framework_Assert::markTestSkipped(
84 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
85 );
86 }
87
88 if ( $backend === null ) {
89 $backend = new DummySessionBackend;
90 }
91
92 $session = $rc->newInstanceWithoutConstructor();
93 $priv = \TestingAccessWrapper::newFromObject( $session );
94 $priv->backend = $backend;
95 $priv->index = $index;
96 return $session;
97 }
98
99 }