MediaWiki  master
TitleValueTest.php
Go to the documentation of this file.
1 <?php
28 
29  public function goodConstructorProvider() {
30  return [
31  [ NS_MAIN, '', 'fragment', '', true, false ],
32  [ NS_MAIN, '', '', 'interwiki', false, true ],
33  [ NS_MAIN, '', 'fragment', 'interwiki', true, true ],
34  [ NS_USER, 'TestThis', 'stuff', '', true, false ],
35  [ NS_USER, 'TestThis', '', 'baz', false, true ],
36  [ NS_MAIN, 'foo bar', '', '', false, false ],
37  [ NS_MAIN, 'foo_bar', '', '', false, false ],
38  ];
39  }
40 
44  public function testConstruction( $ns, $text, $fragment, $interwiki, $hasFragment,
45  $hasInterwiki
46  ) {
47  $title = new TitleValue( $ns, $text, $fragment, $interwiki );
48 
49  $this->assertEquals( $ns, $title->getNamespace() );
50  $this->assertTrue( $title->inNamespace( $ns ) );
51  $this->assertEquals( strtr( $text, ' ', '_' ), $title->getDbKey() );
52  $this->assertEquals( strtr( $text, '_', ' ' ), $title->getText() );
53  $this->assertEquals( $fragment, $title->getFragment() );
54  $this->assertEquals( $hasFragment, $title->hasFragment() );
55  $this->assertEquals( $interwiki, $title->getInterwiki() );
56  $this->assertEquals( $hasInterwiki, $title->isExternal() );
57  }
58 
59  public function badConstructorProvider() {
60  return [
61  [ 'foo', 'title', 'fragment', '' ],
62  [ null, 'title', 'fragment', '' ],
63  [ 2.3, 'title', 'fragment', '' ],
64 
65  [ NS_MAIN, 5, 'fragment', '' ],
66  [ NS_MAIN, null, 'fragment', '' ],
67  [ NS_USER, '', 'fragment', '' ],
68  [ NS_USER, '', '', 'interwiki' ],
69  [ NS_MAIN, 'bar_', '', '' ],
70  [ NS_MAIN, '_foo', '', '' ],
71  [ NS_MAIN, ' eek ', '', '' ],
72 
73  [ NS_MAIN, 'title', 5, '' ],
74  [ NS_MAIN, 'title', null, '' ],
75  [ NS_MAIN, 'title', [], '' ],
76 
77  [ NS_MAIN, 'title', '', 5 ],
78  [ NS_MAIN, 'title', null, 5 ],
79  [ NS_MAIN, 'title', [], 5 ],
80  ];
81  }
82 
86  public function testConstructionErrors( $ns, $text, $fragment, $interwiki ) {
87  $this->setExpectedException( InvalidArgumentException::class );
88  new TitleValue( $ns, $text, $fragment, $interwiki );
89  }
90 
91  public function fragmentTitleProvider() {
92  return [
93  [ new TitleValue( NS_MAIN, 'Test' ), 'foo' ],
94  [ new TitleValue( NS_TALK, 'Test', 'foo' ), '' ],
95  [ new TitleValue( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
96  ];
97  }
98 
102  public function testCreateFragmentTitle( TitleValue $title, $fragment ) {
103  $fragmentTitle = $title->createFragmentTarget( $fragment );
104 
105  $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
106  $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
107  $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
108  }
109 
110  public function getTextProvider() {
111  return [
112  [ 'Foo', 'Foo' ],
113  [ 'Foo_Bar', 'Foo Bar' ],
114  ];
115  }
116 
120  public function testGetText( $dbkey, $text ) {
121  $title = new TitleValue( NS_MAIN, $dbkey );
122 
123  $this->assertEquals( $text, $title->getText() );
124  }
125 
126  public function provideTestToString() {
127  yield [
128  new TitleValue( 0, 'Foo' ),
129  '0:Foo'
130  ];
131  yield [
132  new TitleValue( 1, 'Bar_Baz' ),
133  '1:Bar_Baz'
134  ];
135  yield [
136  new TitleValue( 9, 'JoJo', 'Frag' ),
137  '9:JoJo#Frag'
138  ];
139  yield [
140  new TitleValue( 200, 'tea', 'Fragment', 'wikicode' ),
141  'wikicode:200:tea#Fragment'
142  ];
143  }
144 
148  public function testToString( TitleValue $value, $expected ) {
149  $this->assertSame(
150  $expected,
151  $value->__toString()
152  );
153  }
154 }
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:174
const NS_MAIN
Definition: Defines.php:60
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:193
__toString()
Returns a string representation of the title, for logging.
Definition: TitleValue.php:223
$value
testCreateFragmentTitle(TitleValue $title, $fragment)
fragmentTitleProvider
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1983
TitleValue.
Base class for unit tests.
const NS_CATEGORY
Definition: Defines.php:74
testToString(TitleValue $value, $expected)
provideTestToString
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:773
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:918
testConstructionErrors( $ns, $text, $fragment, $interwiki)
badConstructorProvider
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
testGetText( $dbkey, $text)
getTextProvider
testConstruction( $ns, $text, $fragment, $interwiki, $hasFragment, $hasInterwiki)
goodConstructorProvider
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
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:186
const NS_TALK
Definition: Defines.php:61