| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Integration\Context; |
| 4 |
|
| 5 |
use MediaWiki\Actions\ActionFactory; |
| 6 |
use MediaWiki\Config\HashConfig; |
| 7 |
use MediaWiki\Context\DerivativeContext; |
| 8 |
use MediaWiki\Context\IContextSource; |
| 9 |
use MediaWiki\Context\RequestContext; |
| 10 |
use MediaWiki\Language\Language; |
| 11 |
use MediaWiki\Output\OutputPage; |
| 12 |
use MediaWiki\Permissions\Authority; |
| 13 |
use MediaWiki\Request\FauxRequest; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use MediaWiki\User\User; |
| 16 |
use MediaWikiIntegrationTestCase; |
| 17 |
use WikiPage; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class DerivativeContextTest extends MediaWikiIntegrationTestCase { |
| 23 |
|
| 24 |
public function provideGetterSetter() { |
| 25 |
$initialContext = new RequestContext(); |
| 26 |
yield 'get/set Context' => [ |
| 27 |
'initialContext' => $initialContext, |
| 28 |
'initialValue' => $initialContext, |
| 29 |
'newValue' => new RequestContext(), |
| 30 |
'getter' => 'getContext', |
| 31 |
'setter' => 'setContext' |
| 32 |
]; |
| 33 |
yield 'get/set Config' => [ |
| 34 |
'initialContext' => $initialContext, |
| 35 |
'initialValue' => new HashConfig(), |
| 36 |
'newValue' => new HashConfig(), |
| 37 |
'getter' => 'getConfig', |
| 38 |
'setter' => 'setConfig' |
| 39 |
]; |
| 40 |
yield 'get/set OutputPage' => [ |
| 41 |
'initialContext' => $initialContext, |
| 42 |
'initialValue' => $this->createNoOpMock( OutputPage::class ), |
| 43 |
'newValue' => $this->createNoOpMock( OutputPage::class ), |
| 44 |
'getter' => 'getOutput', |
| 45 |
'setter' => 'setOutput' |
| 46 |
]; |
| 47 |
yield 'get/set User' => [ |
| 48 |
'initialContext' => $initialContext, |
| 49 |
'initialValue' => $this->createNoOpMock( User::class ), |
| 50 |
'newValue' => $this->createNoOpMock( User::class ), |
| 51 |
'getter' => 'getUser', |
| 52 |
'setter' => 'setUser' |
| 53 |
]; |
| 54 |
yield 'get/set Authority' => [ |
| 55 |
'initialContext' => $initialContext, |
| 56 |
'initialValue' => $this->createNoOpMock( Authority::class ), |
| 57 |
'newValue' => $this->createNoOpMock( Authority::class ), |
| 58 |
'getter' => 'getAuthority', |
| 59 |
'setter' => 'setAuthority' |
| 60 |
]; |
| 61 |
yield 'get/set Language' => [ |
| 62 |
'initialContext' => $initialContext, |
| 63 |
'initialValue' => $this->createNoOpMock( Language::class ), |
| 64 |
'newValue' => $this->createNoOpMock( Language::class ), |
| 65 |
'getter' => 'getLanguage', |
| 66 |
'setter' => 'setLanguage' |
| 67 |
]; |
| 68 |
yield 'get/set WebRequest' => [ |
| 69 |
'initialContext' => $initialContext, |
| 70 |
'initialValue' => new FauxRequest(), |
| 71 |
'newValue' => new FauxRequest(), |
| 72 |
'getter' => 'getRequest', |
| 73 |
'setter' => 'setRequest' |
| 74 |
]; |
| 75 |
$initialTitle = $this->createMock( Title::class ); |
| 76 |
$initialTitle->expects( $this->any() )->method( 'equals' ); |
| 77 |
yield 'get/set Title' => [ |
| 78 |
'initialContext' => $initialContext, |
| 79 |
'initialValue' => $initialTitle, |
| 80 |
'newValue' => $this->createNoOpMock( Title::class ), |
| 81 |
'getter' => 'getTitle', |
| 82 |
'setter' => 'setTitle', |
| 83 |
]; |
| 84 |
$initialWikiPage = $this->createMock( WikiPage::class ); |
| 85 |
$initialWikiPage->expects( $this->any() )->method( 'getTitle' )->willReturn( $initialTitle ); |
| 86 |
$newWikiPage = $this->createMock( WikiPage::class ); |
| 87 |
$newWikiPage->expects( $this->any() )->method( 'getTitle' ); |
| 88 |
yield 'get/set WikiPage' => [ |
| 89 |
'initialContext' => $initialContext, |
| 90 |
'initialValue' => $initialWikiPage, |
| 91 |
'newValue' => $newWikiPage, |
| 92 |
'getter' => 'getWikiPage', |
| 93 |
'setter' => 'setWikiPage', |
| 94 |
]; |
| 95 |
yield 'get/set ActionName' => [ |
| 96 |
'initialContext' => $initialContext, |
| 97 |
'initialValue' => 'initActionName', |
| 98 |
'newValue' => 'newActionName', |
| 99 |
'getter' => 'getActionName', |
| 100 |
'setter' => 'setActionName', |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testGetterSetter( |
| 108 |
IContextSource $initialContext, |
| 109 |
$initialValue, |
| 110 |
$newValue, |
| 111 |
string $getter, |
| 112 |
string $setter |
| 113 |
) { |
| 114 |
if ( $setter !== 'setContext' ) { |
| 115 |
$initialContext->$setter( $initialValue ); |
| 116 |
} |
| 117 |
|
| 118 |
$derivativeContext = new DerivativeContext( $initialContext ); |
| 119 |
$this->assertSame( $initialValue, $derivativeContext->$getter(), 'Get inital value' ); |
| 120 |
$derivativeContext->$setter( $newValue ); |
| 121 |
$this->assertSame( $newValue, $derivativeContext->$getter(), 'Get new value' ); |
| 122 |
} |
| 123 |
|
| 124 |
public function testOverideActionName() { |
| 125 |
$parent = new RequestContext(); |
| 126 |
$parent->setActionName( 'view' ); |
| 127 |
|
| 128 |
$factory = $this->createMock( ActionFactory::class ); |
| 129 |
$factory |
| 130 |
->method( 'getActionName' ) |
| 131 |
->willReturnOnConsecutiveCalls( 'foo', 'bar', 'baz' ); |
| 132 |
$this->setService( 'ActionFactory', $factory ); |
| 133 |
|
| 134 |
$derivative = new DerivativeContext( $parent ); |
| 135 |
$this->assertSame( 'view', $derivative->getActionName(), 'default to parent cache' ); |
| 136 |
|
| 137 |
$derivative->setTitle( $this->createMock( Title::class ) ); |
| 138 |
$this->assertSame( 'foo', $derivative->getActionName(), 'recompute after change' ); |
| 139 |
$this->assertSame( 'foo', $derivative->getActionName(), 'local cache' ); |
| 140 |
|
| 141 |
$derivative->setWikiPage( $this->createMock( WikiPage::class ) ); |
| 142 |
$this->assertSame( 'bar', $derivative->getActionName(), 'recompute after change' ); |
| 143 |
$this->assertSame( 'bar', $derivative->getActionName(), 'local cache' ); |
| 144 |
|
| 145 |
$derivative->setActionName( 'custom' ); |
| 146 |
$this->assertSame( 'custom', $derivative->getActionName(), 'override' ); |
| 147 |
} |
| 148 |
} |