MediaWiki  1.33.0
TagHooksTest.php
Go to the documentation of this file.
1 <?php
2 
4 
35  public static function provideValidNames() {
36  return [
37  [ 'foo' ],
38  [ 'foo-bar' ],
39  [ 'foo_bar' ],
40  [ 'FOO-BAR' ],
41  [ 'foo bar' ]
42  ];
43  }
44 
45  public static function provideBadNames() {
46  return [ [ "foo<bar" ], [ "foo>bar" ], [ "foo\nbar" ], [ "foo\rbar" ] ];
47  }
48 
49  private function getParserOptions() {
51  MediaWikiServices::getInstance()->getContentLanguage() );
52  return $popt;
53  }
54 
58  public function testTagHooks( $tag ) {
59  $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
60 
61  $parser->setHook( $tag, [ $this, 'tagCallback' ] );
62  $parserOutput = $parser->parse(
63  "Foo<$tag>Bar</$tag>Baz",
64  Title::newFromText( 'Test' ),
65  $this->getParserOptions()
66  );
67  $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText( [ 'unwrap' => true ] ) );
68 
69  $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
70  }
71 
76  public function testBadTagHooks( $tag ) {
77  $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
78 
79  $parser->setHook( $tag, [ $this, 'tagCallback' ] );
80  $parser->parse(
81  "Foo<$tag>Bar</$tag>Baz",
82  Title::newFromText( 'Test' ),
83  $this->getParserOptions()
84  );
85  $this->fail( 'Exception not thrown.' );
86  }
87 
91  public function testFunctionTagHooks( $tag ) {
92  $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
93 
94  $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
95  $parserOutput = $parser->parse(
96  "Foo<$tag>Bar</$tag>Baz",
97  Title::newFromText( 'Test' ),
98  $this->getParserOptions()
99  );
100  $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText( [ 'unwrap' => true ] ) );
101 
102  $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
103  }
104 
109  public function testBadFunctionTagHooks( $tag ) {
110  $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
111 
112  $parser->setFunctionTagHook(
113  $tag,
114  [ $this, 'functionTagCallback' ],
116  );
117  $parser->parse(
118  "Foo<$tag>Bar</$tag>Baz",
119  Title::newFromText( 'Test' ),
120  $this->getParserOptions()
121  );
122  $this->fail( 'Exception not thrown.' );
123  }
124 
125  function tagCallback( $text, $params, $parser ) {
126  return str_rot13( $text );
127  }
128 
129  function functionTagCallback( &$parser, $frame, $code, $attribs ) {
130  return str_rot13( $code );
131  }
132 }
TagHooksTest\provideValidNames
static provideValidNames()
Definition: TagHooksTest.php:35
TagHooksTest\testBadFunctionTagHooks
testBadFunctionTagHooks( $tag)
provideBadNames MWException
Definition: TagHooksTest.php:109
TagHooksTest
Database Parser.
Definition: TagHooksTest.php:34
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
TagHooksTest\tagCallback
tagCallback( $text, $params, $parser)
Definition: TagHooksTest.php:125
SFH_OBJECT_ARGS
const SFH_OBJECT_ARGS
Definition: Defines.php:198
$params
$params
Definition: styleTest.css.php:44
TagHooksTest\testBadTagHooks
testBadTagHooks( $tag)
provideBadNames MWException
Definition: TagHooksTest.php:76
php
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
Preprocessor
Definition: Preprocessor.php:29
ParserOptions\newFromUserAndLang
static newFromUserAndLang(User $user, Language $lang)
Get a ParserOptions object from a given user and language.
Definition: ParserOptions.php:1031
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
$attribs
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 an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1985
TagHooksTest\getParserOptions
getParserOptions()
Definition: TagHooksTest.php:49
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$code
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 it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:780
$parser
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition: hooks.txt:1802
TagHooksTest\testFunctionTagHooks
testFunctionTagHooks( $tag)
provideValidNames
Definition: TagHooksTest.php:91
TagHooksTest\functionTagCallback
functionTagCallback(&$parser, $frame, $code, $attribs)
Definition: TagHooksTest.php:129
TagHooksTest\testTagHooks
testTagHooks( $tag)
provideValidNames
Definition: TagHooksTest.php:58
TagHooksTest\provideBadNames
static provideBadNames()
Definition: TagHooksTest.php:45
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48