MediaWiki  REL1_31
ParserOptionsTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 use Wikimedia\ScopedCallback;
5 
10 
11  private static function clearCache() {
12  $wrap = TestingAccessWrapper::newFromClass( ParserOptions::class );
13  $wrap->defaults = null;
14  $wrap->lazyOptions = [
15  'dateformat' => [ ParserOptions::class, 'initDateFormat' ],
16  ];
17  $wrap->inCacheKey = [
18  'dateformat' => true,
19  'numberheadings' => true,
20  'thumbsize' => true,
21  'stubthreshold' => true,
22  'printable' => true,
23  'userlang' => true,
24  ];
25  }
26 
27  protected function setUp() {
29 
30  parent::setUp();
32 
33  $this->setMwGlobals( [
34  'wgRenderHashAppend' => '',
35  'wgHooks' => [
36  'PageRenderingHash' => [],
37  ] + $wgHooks,
38  ] );
39  }
40 
41  protected function tearDown() {
43  parent::tearDown();
44  }
45 
51  public function testIsSafeToCache( $expect, $options ) {
53  foreach ( $options as $name => $value ) {
54  $popt->setOption( $name, $value );
55  }
56  $this->assertSame( $expect, $popt->isSafeToCache() );
57  }
58 
59  public static function provideIsSafeToCache() {
60  return [
61  'No overrides' => [ true, [] ],
62  'In-key options are ok' => [ true, [
63  'thumbsize' => 1e100,
64  'printable' => false,
65  ] ],
66  'Non-in-key options are not ok' => [ false, [
67  'removeComments' => false,
68  ] ],
69  'Non-in-key options are not ok (2)' => [ false, [
70  'wrapclass' => 'foobar',
71  ] ],
72  'Canonical override, not default (1)' => [ true, [
73  'tidy' => true,
74  ] ],
75  'Canonical override, not default (2)' => [ false, [
76  'tidy' => false,
77  ] ],
78  ];
79  }
80 
88  public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
90 
91  $globals += [
92  'wgHooks' => [],
93  ];
94  $globals['wgHooks'] += [
95  'PageRenderingHash' => [],
96  ] + $wgHooks;
97  $this->setMwGlobals( $globals );
98 
100  foreach ( $options as $name => $value ) {
101  $popt->setOption( $name, $value );
102  }
103  $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
104  }
105 
106  public static function provideOptionsHash() {
107  $used = [ 'thumbsize', 'printable' ];
108 
109  $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
110  $classWrapper->getDefaults();
111  $allUsableOptions = array_diff(
112  array_keys( $classWrapper->inCacheKey ),
113  array_keys( $classWrapper->lazyOptions )
114  );
115 
116  return [
117  'Canonical options, nothing used' => [ [], 'canonical', [] ],
118  'Canonical options, used some options' => [ $used, 'canonical', [] ],
119  'Used some options, non-default values' => [
120  $used,
121  'printable=1!thumbsize=200',
122  [
123  'thumbsize' => 200,
124  'printable' => true,
125  ]
126  ],
127  'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
128  'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
129  [],
130  'canonical!wgRenderHashAppend!onPageRenderingHash',
131  [],
132  [
133  'wgRenderHashAppend' => '!wgRenderHashAppend',
134  'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__ . '::onPageRenderingHash' ] ] ],
135  ]
136  ],
137  ];
138  }
139 
140  public static function onPageRenderingHash( &$confstr ) {
141  $confstr .= '!onPageRenderingHash';
142  }
143 
148  public function testGetInvalidOption() {
149  $popt = ParserOptions::newCanonical();
150  $popt->getOption( 'bogus' );
151  }
152 
157  public function testSetInvalidOption() {
158  $popt = ParserOptions::newCanonical();
159  $popt->setOption( 'bogus', true );
160  }
161 
162  public function testMatches() {
163  $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
164  $oldDefaults = $classWrapper->defaults;
165  $oldLazy = $classWrapper->lazyOptions;
166  $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
167  $classWrapper->defaults = $oldDefaults;
168  $classWrapper->lazyOptions = $oldLazy;
169  } );
170 
171  $popt1 = ParserOptions::newCanonical();
172  $popt2 = ParserOptions::newCanonical();
173  $this->assertTrue( $popt1->matches( $popt2 ) );
174 
175  $popt1->enableLimitReport( true );
176  $popt2->enableLimitReport( false );
177  $this->assertTrue( $popt1->matches( $popt2 ) );
178 
179  $popt2->setTidy( !$popt2->getTidy() );
180  $this->assertFalse( $popt1->matches( $popt2 ) );
181 
182  $ctr = 0;
183  $classWrapper->defaults += [ __METHOD__ => null ];
184  $classWrapper->lazyOptions += [ __METHOD__ => function () use ( &$ctr ) {
185  return ++$ctr;
186  } ];
187  $popt1 = ParserOptions::newCanonical();
188  $popt2 = ParserOptions::newCanonical();
189  $this->assertFalse( $popt1->matches( $popt2 ) );
190 
191  ScopedCallback::consume( $reset );
192  }
193 
194  public function testAllCacheVaryingOptions() {
196 
197  // $wgHooks is already saved in self::setUp(), so we can modify it freely here
198  $wgHooks['ParserOptionsRegister'] = [];
199  $this->assertSame( [
200  'dateformat', 'numberheadings', 'printable', 'stubthreshold',
201  'thumbsize', 'userlang'
203 
205 
206  $wgHooks['ParserOptionsRegister'][] = function ( &$defaults, &$inCacheKey ) {
207  $defaults += [
208  'foo' => 'foo',
209  'bar' => 'bar',
210  'baz' => 'baz',
211  ];
212  $inCacheKey += [
213  'foo' => true,
214  'bar' => false,
215  ];
216  };
217  $this->assertSame( [
218  'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
219  'thumbsize', 'userlang'
221  }
222 
223 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
ParserOptionsTest\testIsSafeToCache
testIsSafeToCache( $expect, $options)
provideIsSafeToCache
Definition: ParserOptionsTest.php:51
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:37
ParserOptions\newCanonical
static newCanonical(User $user=null, $lang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1020
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
ParserOptionsTest\provideOptionsHash
static provideOptionsHash()
Definition: ParserOptionsTest.php:106
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
ParserOptionsTest\testAllCacheVaryingOptions
testAllCacheVaryingOptions()
Definition: ParserOptionsTest.php:194
ParserOptionsTest\setUp
setUp()
Definition: ParserOptionsTest.php:27
ParserOptionsTest\testOptionsHash
testOptionsHash( $usedOptions, $expect, $options, $globals=[])
provideOptionsHash
Definition: ParserOptionsTest.php:88
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
$wgHooks
$wgHooks['ArticleShow'][]
Definition: hooks.txt:108
$options
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 & $options
Definition: hooks.txt:2001
ParserOptionsTest\clearCache
static clearCache()
Definition: ParserOptionsTest.php:11
$value
$value
Definition: styleTest.css.php:45
ParserOptionsTest\onPageRenderingHash
static onPageRenderingHash(&$confstr)
Definition: ParserOptionsTest.php:140
ParserOptionsTest\tearDown
tearDown()
Definition: ParserOptionsTest.php:41
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ParserOptionsTest\testSetInvalidOption
testSetInvalidOption()
InvalidArgumentException Unknown parser option bogus.
Definition: ParserOptionsTest.php:157
ParserOptions\allCacheVaryingOptions
static allCacheVaryingOptions()
Return all option keys that vary the options hash.
Definition: ParserOptions.php:1233
ParserOptionsTest\testMatches
testMatches()
Definition: ParserOptionsTest.php:162
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
ParserOptionsTest\testGetInvalidOption
testGetInvalidOption()
InvalidArgumentException Unknown parser option bogus.
Definition: ParserOptionsTest.php:148
ParserOptionsTest
ParserOptions.
Definition: ParserOptionsTest.php:9
class
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:56
ParserOptionsTest\provideIsSafeToCache
static provideIsSafeToCache()
Definition: ParserOptionsTest.php:59