MediaWiki  1.28.0
HTMLRestrictionsFieldTest.php
Go to the documentation of this file.
1 <?php
2 
3 class HTMLRestrictionsFieldTest extends PHPUnit_Framework_TestCase {
4  public function testConstruct() {
5  $field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions' ] );
6  $this->assertNotEmpty( $field->getLabel(), 'has a default label' );
7  $this->assertNotEmpty( $field->getHelpText(), 'has a default help text' );
8  $this->assertEquals( MWRestrictions::newDefault(), $field->getDefault(),
9  'defaults to the default MWRestrictions object' );
10 
11  $field = new HTMLRestrictionsField( [
12  'fieldname' => 'restrictions',
13  'label' => 'foo',
14  'help' => 'bar',
15  'default' => 'baz',
16  ] );
17  $this->assertEquals( 'foo', $field->getLabel(), 'label can be customized' );
18  $this->assertEquals( 'bar', $field->getHelpText(), 'help text can be customized' );
19  $this->assertEquals( 'baz', $field->getDefault(), 'default can be customized' );
20  }
21 
25  public function testForm( $text, $value ) {
26  $form = HTMLForm::factory( 'ooui', [
27  'restrictions' => [ 'class' => HTMLRestrictionsField::class ],
28  ] );
29  $request = new FauxRequest( [ 'wprestrictions' => $text ], true );
31  $context->setRequest( $request );
32  $form->setContext( $context );
33  $form->setTitle( Title::newFromText( 'Main Page' ) )->setSubmitCallback( function () {
34  return true;
35  } )->prepareForm();
36  $status = $form->trySubmit();
37 
38  if ( $status instanceof StatusValue ) {
39  $this->assertEquals( $value !== false, $status->isGood() );
40  } elseif ( $value === false ) {
41  $this->assertNotSame( true, $status );
42  } else {
43  $this->assertSame( true, $status );
44  }
45 
46  if ( $value !== false ) {
47  $restrictions = $form->mFieldData['restrictions'];
48  $this->assertInstanceOf( MWRestrictions::class, $restrictions );
49  $this->assertEquals( $value, $restrictions->toArray()['IPAddresses'] );
50  }
51 
52  // sanity
53  $form->getHTML( $status );
54  }
55 
56  public function provideValidate() {
57  return [
58  // submitted text, value of 'IPAddresses' key or false for validation error
59  [ null, [ '0.0.0.0/0', '::/0' ] ],
60  [ '', [] ],
61  [ "1.2.3.4\n::/0", [ '1.2.3.4', '::/0' ] ],
62  [ "1.2.3.4\n::/x", false ],
63  ];
64  }
65 }
$context
Definition: load.php:50
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
An IContextSource implementation which will inherit context from another source but allow individual ...
static factory($displayFormat)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:275
Class for updating an MWRestrictions value (which is, currently, basically just an IP address list)...
$value
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
static getMain()
Static methods.
testForm($text, $value)
provideValidate
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2573
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
static newDefault()