ParserOptionsTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. use Wikimedia\TestingAccessWrapper;
  3. use Wikimedia\ScopedCallback;
  4. /**
  5. * @covers ParserOptions
  6. */
  7. class ParserOptionsTest extends MediaWikiTestCase {
  8. private static function clearCache() {
  9. $wrap = TestingAccessWrapper::newFromClass( ParserOptions::class );
  10. $wrap->defaults = null;
  11. $wrap->lazyOptions = [
  12. 'dateformat' => [ ParserOptions::class, 'initDateFormat' ],
  13. 'speculativeRevId' => [ ParserOptions::class, 'initSpeculativeRevId' ],
  14. ];
  15. $wrap->inCacheKey = [
  16. 'dateformat' => true,
  17. 'numberheadings' => true,
  18. 'thumbsize' => true,
  19. 'stubthreshold' => true,
  20. 'printable' => true,
  21. 'userlang' => true,
  22. ];
  23. }
  24. protected function setUp() {
  25. parent::setUp();
  26. self::clearCache();
  27. $this->setMwGlobals( [
  28. 'wgRenderHashAppend' => '',
  29. ] );
  30. // This is crazy, but registering false, null, or other falsey values
  31. // as a hook callback "works".
  32. $this->setTemporaryHook( 'PageRenderingHash', null );
  33. }
  34. protected function tearDown() {
  35. self::clearCache();
  36. parent::tearDown();
  37. }
  38. public function testNewCanonical() {
  39. $wgUser = $this->getMutableTestUser()->getUser();
  40. $wgLang = Language::factory( 'fr' );
  41. $contLang = Language::factory( 'qqx' );
  42. $this->setContentLang( $contLang );
  43. $this->setMwGlobals( [
  44. 'wgUser' => $wgUser,
  45. 'wgLang' => $wgLang,
  46. ] );
  47. $user = $this->getMutableTestUser()->getUser();
  48. $lang = Language::factory( 'de' );
  49. $lang2 = Language::factory( 'bug' );
  50. $context = new DerivativeContext( RequestContext::getMain() );
  51. $context->setUser( $user );
  52. $context->setLanguage( $lang );
  53. // No parameters picks up $wgUser and $wgLang
  54. $popt = ParserOptions::newCanonical();
  55. $this->assertSame( $wgUser, $popt->getUser() );
  56. $this->assertSame( $wgLang, $popt->getUserLangObj() );
  57. // Just a user uses $wgLang
  58. $popt = ParserOptions::newCanonical( $user );
  59. $this->assertSame( $user, $popt->getUser() );
  60. $this->assertSame( $wgLang, $popt->getUserLangObj() );
  61. // Just a language uses $wgUser
  62. $popt = ParserOptions::newCanonical( null, $lang );
  63. $this->assertSame( $wgUser, $popt->getUser() );
  64. $this->assertSame( $lang, $popt->getUserLangObj() );
  65. // Passing both works
  66. $popt = ParserOptions::newCanonical( $user, $lang );
  67. $this->assertSame( $user, $popt->getUser() );
  68. $this->assertSame( $lang, $popt->getUserLangObj() );
  69. // Passing 'canonical' uses an anon and $contLang, and ignores any passed $userLang
  70. $popt = ParserOptions::newCanonical( 'canonical' );
  71. $this->assertTrue( $popt->getUser()->isAnon() );
  72. $this->assertSame( $contLang, $popt->getUserLangObj() );
  73. $popt = ParserOptions::newCanonical( 'canonical', $lang2 );
  74. $this->assertSame( $contLang, $popt->getUserLangObj() );
  75. // Passing an IContextSource uses the user and lang from it, and ignores
  76. // any passed $userLang
  77. $popt = ParserOptions::newCanonical( $context );
  78. $this->assertSame( $user, $popt->getUser() );
  79. $this->assertSame( $lang, $popt->getUserLangObj() );
  80. $popt = ParserOptions::newCanonical( $context, $lang2 );
  81. $this->assertSame( $lang, $popt->getUserLangObj() );
  82. // Passing something else raises an exception
  83. try {
  84. $popt = ParserOptions::newCanonical( 'bogus' );
  85. $this->fail( 'Excpected exception not thrown' );
  86. } catch ( InvalidArgumentException $ex ) {
  87. }
  88. }
  89. /**
  90. * @dataProvider provideIsSafeToCache
  91. * @param bool $expect Expected value
  92. * @param array $options Options to set
  93. */
  94. public function testIsSafeToCache( $expect, $options ) {
  95. $popt = ParserOptions::newCanonical();
  96. foreach ( $options as $name => $value ) {
  97. $popt->setOption( $name, $value );
  98. }
  99. $this->assertSame( $expect, $popt->isSafeToCache() );
  100. }
  101. public static function provideIsSafeToCache() {
  102. return [
  103. 'No overrides' => [ true, [] ],
  104. 'In-key options are ok' => [ true, [
  105. 'thumbsize' => 1e100,
  106. 'printable' => false,
  107. ] ],
  108. 'Non-in-key options are not ok' => [ false, [
  109. 'removeComments' => false,
  110. ] ],
  111. 'Non-in-key options are not ok (2)' => [ false, [
  112. 'wrapclass' => 'foobar',
  113. ] ],
  114. 'Canonical override, not default (1)' => [ true, [
  115. 'tidy' => true,
  116. ] ],
  117. 'Canonical override, not default (2)' => [ false, [
  118. 'tidy' => false,
  119. ] ],
  120. ];
  121. }
  122. /**
  123. * @dataProvider provideOptionsHash
  124. * @param array $usedOptions Used options
  125. * @param string $expect Expected value
  126. * @param array $options Options to set
  127. * @param array $globals Globals to set
  128. * @param callable|null $hookFunc PageRenderingHash hook function
  129. */
  130. public function testOptionsHash(
  131. $usedOptions, $expect, $options, $globals = [], $hookFunc = null
  132. ) {
  133. $this->setMwGlobals( $globals );
  134. $this->setTemporaryHook( 'PageRenderingHash', $hookFunc );
  135. $popt = ParserOptions::newCanonical();
  136. foreach ( $options as $name => $value ) {
  137. $popt->setOption( $name, $value );
  138. }
  139. $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
  140. }
  141. public static function provideOptionsHash() {
  142. $used = [ 'thumbsize', 'printable' ];
  143. $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
  144. $classWrapper->getDefaults();
  145. $allUsableOptions = array_diff(
  146. array_keys( $classWrapper->inCacheKey ),
  147. array_keys( $classWrapper->lazyOptions )
  148. );
  149. return [
  150. 'Canonical options, nothing used' => [ [], 'canonical', [] ],
  151. 'Canonical options, used some options' => [ $used, 'canonical', [] ],
  152. 'Used some options, non-default values' => [
  153. $used,
  154. 'printable=1!thumbsize=200',
  155. [
  156. 'thumbsize' => 200,
  157. 'printable' => true,
  158. ]
  159. ],
  160. 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
  161. 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
  162. [],
  163. 'canonical!wgRenderHashAppend!onPageRenderingHash',
  164. [],
  165. [ 'wgRenderHashAppend' => '!wgRenderHashAppend' ],
  166. [ __CLASS__ . '::onPageRenderingHash' ],
  167. ],
  168. ];
  169. }
  170. public function testUsedLazyOptionsInHash() {
  171. $this->setTemporaryHook( 'ParserOptionsRegister',
  172. function ( &$defaults, &$inCacheKey, &$lazyOptions ) {
  173. $lazyFuncs = $this->getMockBuilder( stdClass::class )
  174. ->setMethods( [ 'neverCalled', 'calledOnce' ] )
  175. ->getMock();
  176. $lazyFuncs->expects( $this->never() )->method( 'neverCalled' );
  177. $lazyFuncs->expects( $this->once() )->method( 'calledOnce' )->willReturn( 'value' );
  178. $defaults += [
  179. 'opt1' => null,
  180. 'opt2' => null,
  181. 'opt3' => null,
  182. ];
  183. $inCacheKey += [
  184. 'opt1' => true,
  185. 'opt2' => true,
  186. ];
  187. $lazyOptions += [
  188. 'opt1' => [ $lazyFuncs, 'calledOnce' ],
  189. 'opt2' => [ $lazyFuncs, 'neverCalled' ],
  190. 'opt3' => [ $lazyFuncs, 'neverCalled' ],
  191. ];
  192. }
  193. );
  194. self::clearCache();
  195. $popt = ParserOptions::newCanonical();
  196. $popt->registerWatcher( function () {
  197. $this->fail( 'Watcher should not have been called' );
  198. } );
  199. $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
  200. // Second call to see that opt1 isn't resolved a second time
  201. $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
  202. }
  203. public static function onPageRenderingHash( &$confstr ) {
  204. $confstr .= '!onPageRenderingHash';
  205. }
  206. /**
  207. * @expectedException InvalidArgumentException
  208. * @expectedExceptionMessage Unknown parser option bogus
  209. */
  210. public function testGetInvalidOption() {
  211. $popt = ParserOptions::newCanonical();
  212. $popt->getOption( 'bogus' );
  213. }
  214. /**
  215. * @expectedException InvalidArgumentException
  216. * @expectedExceptionMessage Unknown parser option bogus
  217. */
  218. public function testSetInvalidOption() {
  219. $popt = ParserOptions::newCanonical();
  220. $popt->setOption( 'bogus', true );
  221. }
  222. public function testMatches() {
  223. $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
  224. $oldDefaults = $classWrapper->defaults;
  225. $oldLazy = $classWrapper->lazyOptions;
  226. $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
  227. $classWrapper->defaults = $oldDefaults;
  228. $classWrapper->lazyOptions = $oldLazy;
  229. } );
  230. $popt1 = ParserOptions::newCanonical();
  231. $popt2 = ParserOptions::newCanonical();
  232. $this->assertTrue( $popt1->matches( $popt2 ) );
  233. $popt1->enableLimitReport( true );
  234. $popt2->enableLimitReport( false );
  235. $this->assertTrue( $popt1->matches( $popt2 ) );
  236. $popt2->setTidy( !$popt2->getTidy() );
  237. $this->assertFalse( $popt1->matches( $popt2 ) );
  238. $ctr = 0;
  239. $classWrapper->defaults += [ __METHOD__ => null ];
  240. $classWrapper->lazyOptions += [ __METHOD__ => function () use ( &$ctr ) {
  241. return ++$ctr;
  242. } ];
  243. $popt1 = ParserOptions::newCanonical();
  244. $popt2 = ParserOptions::newCanonical();
  245. $this->assertFalse( $popt1->matches( $popt2 ) );
  246. ScopedCallback::consume( $reset );
  247. }
  248. public function testAllCacheVaryingOptions() {
  249. $this->setTemporaryHook( 'ParserOptionsRegister', null );
  250. $this->assertSame( [
  251. 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
  252. 'thumbsize', 'userlang'
  253. ], ParserOptions::allCacheVaryingOptions() );
  254. self::clearCache();
  255. $this->setTemporaryHook( 'ParserOptionsRegister', function ( &$defaults, &$inCacheKey ) {
  256. $defaults += [
  257. 'foo' => 'foo',
  258. 'bar' => 'bar',
  259. 'baz' => 'baz',
  260. ];
  261. $inCacheKey += [
  262. 'foo' => true,
  263. 'bar' => false,
  264. ];
  265. } );
  266. $this->assertSame( [
  267. 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
  268. 'thumbsize', 'userlang'
  269. ], ParserOptions::allCacheVaryingOptions() );
  270. }
  271. public function testGetSpeculativeRevid() {
  272. $options = new ParserOptions();
  273. $this->assertFalse( $options->getSpeculativeRevId() );
  274. $counter = 0;
  275. $options->setSpeculativeRevIdCallback( function () use( &$counter ) {
  276. return ++$counter;
  277. } );
  278. // make sure the same value is re-used once it is determined
  279. $this->assertSame( 1, $options->getSpeculativeRevId() );
  280. $this->assertSame( 1, $options->getSpeculativeRevId() );
  281. }
  282. }