| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\SpecialPage; |
| 4 |
|
| 5 |
use MediaWiki\Context\DerivativeContext; |
| 6 |
use MediaWiki\Context\RequestContext; |
| 7 |
use MediaWiki\HookContainer\HookContainer; |
| 8 |
use MediaWiki\Html\FormOptions; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Output\OutputPage; |
| 11 |
use MediaWiki\SpecialPage\ChangesListSpecialPage; |
| 12 |
use MediaWikiIntegrationTestCase; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
abstract class AbstractChangesListSpecialPageTestCase extends MediaWikiIntegrationTestCase { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected $changesListSpecialPage; |
| 26 |
|
| 27 |
protected function setUp(): void { |
| 28 |
parent::setUp(); |
| 29 |
$this->overrideConfigValues( [ |
| 30 |
MainConfigNames::RCWatchCategoryMembership => true, |
| 31 |
MainConfigNames::UseRCPatrol => true, |
| 32 |
] ); |
| 33 |
|
| 34 |
$this->setGroupPermissions( 'patrollers', 'patrol', true ); |
| 35 |
|
| 36 |
|
| 37 |
$this->changesListSpecialPage = $this->getPageAccessWrapper(); |
| 38 |
$context = $this->changesListSpecialPage->getContext(); |
| 39 |
$context = new DerivativeContext( $context ); |
| 40 |
$context->setUser( $this->getTestUser( [ 'patrollers' ] )->getUser() ); |
| 41 |
$this->changesListSpecialPage->setContext( $context ); |
| 42 |
$this->changesListSpecialPage->registerFilters(); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
abstract protected function getPageAccessWrapper(); |
| 49 |
|
| 50 |
abstract public function provideParseParameters(); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function testParseParameters( $params, $expected ) { |
| 56 |
$opts = new FormOptions(); |
| 57 |
foreach ( $expected as $key => $value ) { |
| 58 |
|
| 59 |
$opts->add( |
| 60 |
$key, |
| 61 |
null, |
| 62 |
FormOptions::guessType( $expected ) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
$this->changesListSpecialPage->parseParameters( |
| 67 |
$params, |
| 68 |
$opts |
| 69 |
); |
| 70 |
|
| 71 |
$this->assertArrayEquals( |
| 72 |
$expected, |
| 73 |
$opts->getAllValues(), |
| 74 |
false, |
| 75 |
true |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function testValidateOptions( |
| 83 |
$optionsToSet, |
| 84 |
$expectedRedirect, |
| 85 |
$expectedRedirectOptions, |
| 86 |
$rcfilters |
| 87 |
) { |
| 88 |
$redirectQuery = []; |
| 89 |
$redirected = false; |
| 90 |
$output = $this->getMockBuilder( OutputPage::class ) |
| 91 |
->disableProxyingToOriginalMethods() |
| 92 |
->disableOriginalConstructor() |
| 93 |
->getMock(); |
| 94 |
$output->method( 'redirect' )->willReturnCallback( |
| 95 |
static function ( $url ) use ( &$redirectQuery, &$redirected ) { |
| 96 |
$query = parse_url( $url, PHP_URL_QUERY ) ?? ''; |
| 97 |
parse_str( $query, $redirectQuery ); |
| 98 |
$redirected = true; |
| 99 |
} |
| 100 |
); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
$this->setTemporaryHook( |
| 105 |
'ChangesListSpecialPageStructuredFilters', |
| 106 |
HookContainer::NOOP |
| 107 |
); |
| 108 |
|
| 109 |
|
| 110 |
$user = $this->getTestSysop()->getUser(); |
| 111 |
$this->getServiceContainer()->getUserOptionsManager()->setOption( |
| 112 |
$user, |
| 113 |
'rcenhancedfilters-disable', |
| 114 |
$rcfilters ? 0 : 1 |
| 115 |
); |
| 116 |
$ctx = new RequestContext(); |
| 117 |
$ctx->setUser( $user ); |
| 118 |
|
| 119 |
$ctx->setOutput( $output ); |
| 120 |
$clsp = $this->changesListSpecialPage; |
| 121 |
$clsp->setContext( $ctx ); |
| 122 |
$opts = $clsp->getDefaultOptions(); |
| 123 |
|
| 124 |
foreach ( $optionsToSet as $option => $value ) { |
| 125 |
$opts->setValue( $option, $value ); |
| 126 |
} |
| 127 |
|
| 128 |
$clsp->validateOptions( $opts ); |
| 129 |
|
| 130 |
$this->assertEquals( $expectedRedirect, $redirected, 'redirection - ' . print_r( $optionsToSet, true ) ); |
| 131 |
|
| 132 |
if ( $expectedRedirect ) { |
| 133 |
if ( count( $expectedRedirectOptions ) > 0 ) { |
| 134 |
$expectedRedirectOptions += [ |
| 135 |
'title' => $clsp->getPageTitle()->getPrefixedText(), |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
$this->assertArrayEquals( |
| 140 |
$expectedRedirectOptions, |
| 141 |
$redirectQuery, |
| 142 |
false, |
| 143 |
true, |
| 144 |
'redirection query' |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
abstract public function validateOptionsProvider(); |
| 150 |
} |