MediaWiki REL1_28
HTMLRestrictionsFieldTest.php
Go to the documentation of this file.
1<?php
2
3class 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}
An IContextSource implementation which will inherit context from another source but allow individual ...
WebRequest clone which takes values from a provided array.
static factory( $displayFormat)
Construct a HTMLForm object for given display type.
Definition HTMLForm.php:275
testForm( $text, $value)
provideValidate
Class for updating an MWRestrictions value (which is, currently, basically just an IP address list).
static getMain()
Static methods.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
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:1049
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2685
processing should stop and the error should be shown to the user * false
Definition hooks.txt:189
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:37
$context
Definition load.php:50