| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Integration\HTMLForm\Field; |
| 4 |
|
| 5 |
use EmptyIterator; |
| 6 |
use MediaWiki\Context\DerivativeContext; |
| 7 |
use MediaWiki\Context\RequestContext; |
| 8 |
use MediaWiki\HTMLForm\Field\HTMLRestrictionsField; |
| 9 |
use MediaWiki\HTMLForm\HTMLForm; |
| 10 |
use MediaWiki\Language\Language; |
| 11 |
use MediaWiki\Page\PageSelectQueryBuilder; |
| 12 |
use MediaWiki\Page\PageStore; |
| 13 |
use MediaWiki\Request\FauxRequest; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use MediaWikiCoversValidator; |
| 16 |
use MediaWikiIntegrationTestCase; |
| 17 |
use MWRestrictions; |
| 18 |
use StatusValue; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class HTMLRestrictionsFieldTest extends MediaWikiIntegrationTestCase { |
| 24 |
|
| 25 |
use MediaWikiCoversValidator; |
| 26 |
|
| 27 |
public function testConstruct() { |
| 28 |
$htmlForm = $this->createMock( HTMLForm::class ); |
| 29 |
$htmlForm->method( 'msg' )->willReturnCallback( 'wfMessage' ); |
| 30 |
$languageMock = $this->createMock( Language::class ); |
| 31 |
$languageMock->method( 'getCode' )->willReturn( 'en' ); |
| 32 |
$titleMock = $this->createMock( Title::class ); |
| 33 |
|
| 34 |
$htmlForm->method( 'getLanguage' )->willReturn( $languageMock ); |
| 35 |
$htmlForm->method( 'getTitle' )->willReturn( $titleMock ); |
| 36 |
|
| 37 |
$field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions', 'parent' => $htmlForm ] ); |
| 38 |
$this->assertEquals( MWRestrictions::newDefault(), $field->getDefault(), |
| 39 |
'defaults to the default MWRestrictions object' ); |
| 40 |
|
| 41 |
$field = new HTMLRestrictionsField( [ |
| 42 |
'fieldname' => 'restrictions', |
| 43 |
'label' => 'foo', |
| 44 |
'help' => 'bar', |
| 45 |
'default' => 'baz', |
| 46 |
'parent' => $htmlForm, |
| 47 |
] ); |
| 48 |
$this->assertEquals( 'foo', $field->getLabel(), 'label can be customized' ); |
| 49 |
$this->assertEquals( 'bar', $field->getHelpText(), 'help text can be customized' ); |
| 50 |
$this->assertEquals( 'baz', $field->getDefault(), 'default can be customized' ); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function testForm( $ipText, $value ) { |
| 57 |
$request = new FauxRequest( [ 'wprestrictions-ip' => $ipText ], true ); |
| 58 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 59 |
$context->setRequest( $request ); |
| 60 |
$form = HTMLForm::factory( 'ooui', [ |
| 61 |
'restrictions' => [ 'class' => HTMLRestrictionsField::class ], |
| 62 |
], $context ); |
| 63 |
|
| 64 |
$pageStore = $this->createMock( PageStore::class ); |
| 65 |
$this->setService( 'PageStore', $pageStore ); |
| 66 |
$queryBuilderMock = $this->createMock( PageSelectQueryBuilder::class ); |
| 67 |
$queryBuilderMock->method( 'fetchPageRecords' )->willReturn( new EmptyIterator() ); |
| 68 |
$queryBuilderMock->method( 'wherePageIds' )->willReturnSelf(); |
| 69 |
$queryBuilderMock->method( 'caller' )->willReturnSelf(); |
| 70 |
$pageStore->method( 'newSelectQueryBuilder' )->willReturn( $queryBuilderMock ); |
| 71 |
|
| 72 |
$form->setTitle( Title::makeTitle( NS_MAIN, 'Main Page' ) )->setSubmitCallback( static function () { |
| 73 |
return true; |
| 74 |
} )->prepareForm(); |
| 75 |
$status = $form->trySubmit(); |
| 76 |
|
| 77 |
if ( $status instanceof StatusValue ) { |
| 78 |
$this->assertEquals( $value !== false, $status->isGood() ); |
| 79 |
} elseif ( $value === false ) { |
| 80 |
$this->assertFalse( $status ); |
| 81 |
} else { |
| 82 |
$this->assertTrue( $status ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( $value !== false ) { |
| 86 |
$restrictions = $form->mFieldData['restrictions']; |
| 87 |
$this->assertInstanceOf( MWRestrictions::class, $restrictions ); |
| 88 |
$this->assertEquals( $value, $restrictions->toArray()['IPAddresses'] ); |
| 89 |
} |
| 90 |
|
| 91 |
$form->getHTML( $status ); |
| 92 |
} |
| 93 |
|
| 94 |
public static function provideValidate() { |
| 95 |
return [ |
| 96 |
|
| 97 |
[ null, [ '0.0.0.0/0', '::/0' ] ], |
| 98 |
[ '', [] ], |
| 99 |
[ "1.2.3.4\n::0", [ '1.2.3.4', '::0' ] ], |
| 100 |
[ "1.2.3.4\n::/x", false ], |
| 101 |
]; |
| 102 |
} |
| 103 |
} |