| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError; |
| 6 |
use MediaWiki\Extension\Scribunto\ScribuntoException; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class LuaCommonTest extends LuaEngineTestBase { |
| 20 |
|
| 21 |
protected static $moduleName = 'CommonTests'; |
| 22 |
|
| 23 |
|
| 24 |
private static $allowedGlobals = [ |
| 25 |
|
| 26 |
'assert', |
| 27 |
'error', |
| 28 |
'getfenv', |
| 29 |
'getmetatable', |
| 30 |
'ipairs', |
| 31 |
'next', |
| 32 |
'pairs', |
| 33 |
'pcall', |
| 34 |
'rawequal', |
| 35 |
'rawget', |
| 36 |
'rawset', |
| 37 |
'require', |
| 38 |
'select', |
| 39 |
'setfenv', |
| 40 |
'setmetatable', |
| 41 |
'tonumber', |
| 42 |
'tostring', |
| 43 |
'type', |
| 44 |
'unpack', |
| 45 |
'xpcall', |
| 46 |
|
| 47 |
|
| 48 |
'_G', |
| 49 |
'debug', |
| 50 |
'math', |
| 51 |
'mw', |
| 52 |
'os', |
| 53 |
'package', |
| 54 |
'string', |
| 55 |
'table', |
| 56 |
|
| 57 |
|
| 58 |
'_VERSION', |
| 59 |
]; |
| 60 |
|
| 61 |
protected function setUp(): void { |
| 62 |
parent::setUp(); |
| 63 |
|
| 64 |
|
| 65 |
$this->setTemporaryHook( |
| 66 |
'ScribuntoExternalLibraries', |
| 67 |
static function ( $engine, &$libs ) { |
| 68 |
$libs += [ |
| 69 |
'CommonTestsLib' => [ |
| 70 |
'class' => LuaCommonTestsLibrary::class, |
| 71 |
'deferLoad' => true, |
| 72 |
], |
| 73 |
'CommonTestsFailLib' => [ |
| 74 |
'class' => LuaCommonTestsFailLibrary::class, |
| 75 |
'deferLoad' => true, |
| 76 |
], |
| 77 |
]; |
| 78 |
} |
| 79 |
); |
| 80 |
|
| 81 |
$status = $this->editPage( |
| 82 |
Title::makeTitle( NS_MODULE, 'CommonTests-data.json' ), |
| 83 |
file_get_contents( __DIR__ . '/CommonTests-data.json' ) |
| 84 |
); |
| 85 |
if ( !$status->isOK() ) { |
| 86 |
throw new \Exception( "Failed to create Module:CommonTests-data.json: " . $status->getWikitext() ); |
| 87 |
} |
| 88 |
|
| 89 |
$status = $this->editPage( |
| 90 |
Title::makeTitle( NS_HELP, 'Foo' ), |
| 91 |
'help' |
| 92 |
); |
| 93 |
if ( !$status->isOK() ) { |
| 94 |
throw new \Exception( "Failed to create Help:Foo: " . $status->getWikitext() ); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
$this->getEngine()->getParser()->getOptions()->setExpensiveParserFunctionLimit( 10 ); |
| 99 |
|
| 100 |
|
| 101 |
$interpreter = $this->getEngine()->getInterpreter(); |
| 102 |
$interpreter->callFunction( $interpreter->loadString( |
| 103 |
'mw.makeProtectedEnvFuncsForTest = mw.makeProtectedEnvFuncs', 'fortest' |
| 104 |
) ); |
| 105 |
} |
| 106 |
|
| 107 |
protected function getTestModules() { |
| 108 |
return parent::getTestModules() + [ |
| 109 |
'CommonTests' => __DIR__ . '/CommonTests.lua', |
| 110 |
'CommonTests-data' => __DIR__ . '/CommonTests-data.lua', |
| 111 |
'CommonTests-data-fail1' => __DIR__ . '/CommonTests-data-fail1.lua', |
| 112 |
'CommonTests-data-fail2' => __DIR__ . '/CommonTests-data-fail2.lua', |
| 113 |
'CommonTests-data-fail3' => __DIR__ . '/CommonTests-data-fail3.lua', |
| 114 |
'CommonTests-data-fail4' => __DIR__ . '/CommonTests-data-fail4.lua', |
| 115 |
'CommonTests-data-fail5' => __DIR__ . '/CommonTests-data-fail5.lua', |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
public function testNoLeakedGlobals() { |
| 120 |
$interpreter = $this->getEngine()->getInterpreter(); |
| 121 |
|
| 122 |
[ $actualGlobals ] = $interpreter->callFunction( |
| 123 |
$interpreter->loadString( |
| 124 |
'local t = {} for k in pairs( _G ) do t[#t+1] = k end return t', |
| 125 |
'getglobals' |
| 126 |
) |
| 127 |
); |
| 128 |
|
| 129 |
$leakedGlobals = array_diff( $actualGlobals, self::$allowedGlobals ); |
| 130 |
$this->assertCount( 0, $leakedGlobals, |
| 131 |
'The following globals are leaked: ' . implode( ' ', $leakedGlobals ) |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
public function testPHPLibrary() { |
| 136 |
$engine = $this->getEngine(); |
| 137 |
$frame = $engine->getParser()->getPreprocessor()->newFrame(); |
| 138 |
|
| 139 |
$title = Title::makeTitle( NS_MODULE, 'TestInfoPassViaPHPLibrary' ); |
| 140 |
$this->extraModules[$title->getFullText()] = ' |
| 141 |
local p = {} |
| 142 |
|
| 143 |
function p.test() |
| 144 |
local lib = require( "CommonTestsLib" ) |
| 145 |
return table.concat( { lib.test() }, "; " ) |
| 146 |
end |
| 147 |
|
| 148 |
function p.setVal( frame ) |
| 149 |
local lib = require( "CommonTestsLib" ) |
| 150 |
lib.val = frame.args[1] |
| 151 |
lib.foobar.val = frame.args[1] |
| 152 |
end |
| 153 |
|
| 154 |
function p.getVal() |
| 155 |
local lib = require( "CommonTestsLib" ) |
| 156 |
return tostring( lib.val ), tostring( lib.foobar.val ) |
| 157 |
end |
| 158 |
|
| 159 |
function p.getSetVal( frame ) |
| 160 |
p.setVal( frame ) |
| 161 |
return p.getVal() |
| 162 |
end |
| 163 |
|
| 164 |
function p.checkPackage() |
| 165 |
local ret = {} |
| 166 |
ret[1] = package.loaded["CommonTestsLib"] == nil |
| 167 |
require( "CommonTestsLib" ) |
| 168 |
ret[2] = package.loaded["CommonTestsLib"] ~= nil |
| 169 |
return ret[1], ret[2] |
| 170 |
end |
| 171 |
|
| 172 |
function p.libSetVal( frame ) |
| 173 |
local lib = require( "CommonTestsLib" ) |
| 174 |
return lib.setVal( frame ) |
| 175 |
end |
| 176 |
|
| 177 |
function p.libGetVal() |
| 178 |
local lib = require( "CommonTestsLib" ) |
| 179 |
return lib.getVal() |
| 180 |
end |
| 181 |
|
| 182 |
return p |
| 183 |
'; |
| 184 |
|
| 185 |
|
| 186 |
$module = $engine->fetchModuleFromParser( $title ); |
| 187 |
$ret = $module->invoke( 'test', $frame->newChild() ); |
| 188 |
$this->assertSame( 'Test option; Test function', $ret, |
| 189 |
'Library can be loaded and called' ); |
| 190 |
|
| 191 |
|
| 192 |
$module = $engine->fetchModuleFromParser( $title ); |
| 193 |
$ret = $module->invoke( 'checkPackage', $frame->newChild() ); |
| 194 |
$this->assertSame( 'truetrue', $ret, |
| 195 |
'package.loaded is right on the first call' ); |
| 196 |
$ret = $module->invoke( 'checkPackage', $frame->newChild() ); |
| 197 |
$this->assertSame( 'truetrue', $ret, |
| 198 |
'package.loaded is right on the second call' ); |
| 199 |
|
| 200 |
|
| 201 |
$args = $engine->getParser()->getPreprocessor()->newPartNodeArray( [ 1 => 'cached' ] ); |
| 202 |
$ret = $module->invoke( 'getSetVal', $frame->newChild( $args ) ); |
| 203 |
$this->assertSame( 'cachedcached', $ret, |
| 204 |
'same loaded table is returned by multiple require calls' ); |
| 205 |
|
| 206 |
|
| 207 |
$module = $engine->fetchModuleFromParser( $title ); |
| 208 |
$args = $engine->getParser()->getPreprocessor()->newPartNodeArray( [ 1 => 'fail' ] ); |
| 209 |
$module->invoke( 'setVal', $frame->newChild( $args ) ); |
| 210 |
$ret = $module->invoke( 'getVal', $frame->newChild() ); |
| 211 |
$this->assertSame( 'nilnope', $ret, |
| 212 |
'same loaded table is not shared between invokes' ); |
| 213 |
|
| 214 |
|
| 215 |
$module = $engine->fetchModuleFromParser( $title ); |
| 216 |
$ret = $module->invoke( 'libGetVal', $frame->newChild() ); |
| 217 |
$this->assertSame( 'nil', $ret, 'sanity check' ); |
| 218 |
$args = $engine->getParser()->getPreprocessor()->newPartNodeArray( [ 1 => 'ok' ] ); |
| 219 |
$module->invoke( 'libSetVal', $frame->newChild( $args ) ); |
| 220 |
|
| 221 |
$module = $engine->fetchModuleFromParser( $title ); |
| 222 |
$ret = $module->invoke( 'libGetVal', $frame->newChild() ); |
| 223 |
$this->assertSame( 'ok', $ret, |
| 224 |
'library is not recreated between invokes' ); |
| 225 |
} |
| 226 |
|
| 227 |
public function testModuleStringExtend() { |
| 228 |
$engine = $this->getEngine(); |
| 229 |
$interpreter = $engine->getInterpreter(); |
| 230 |
|
| 231 |
$interpreter->callFunction( |
| 232 |
$interpreter->loadString( 'string.testModuleStringExtend = "ok"', 'extendstring' ) |
| 233 |
); |
| 234 |
$ret = $interpreter->callFunction( |
| 235 |
$interpreter->loadString( 'return ("").testModuleStringExtend', 'teststring1' ) |
| 236 |
); |
| 237 |
$this->assertSame( [ 'ok' ], $ret, 'string can be extended' ); |
| 238 |
|
| 239 |
$this->extraModules['Module:testModuleStringExtend'] = ' |
| 240 |
return { |
| 241 |
test = function() return ("").testModuleStringExtend end |
| 242 |
} |
| 243 |
'; |
| 244 |
$module = $engine->fetchModuleFromParser( |
| 245 |
Title::makeTitle( NS_MODULE, 'testModuleStringExtend' ) |
| 246 |
); |
| 247 |
$ret = $interpreter->callFunction( |
| 248 |
$engine->executeModule( $module->getInitChunk(), 'test', null ) |
| 249 |
); |
| 250 |
$this->assertSame( [ 'ok' ], $ret, 'string extension can be used from module' ); |
| 251 |
|
| 252 |
$this->extraModules['Module:testModuleStringExtend2'] = ' |
| 253 |
return { |
| 254 |
test = function() |
| 255 |
string.testModuleStringExtend = "fail" |
| 256 |
return ("").testModuleStringExtend |
| 257 |
end |
| 258 |
} |
| 259 |
'; |
| 260 |
$module = $engine->fetchModuleFromParser( |
| 261 |
Title::makeTitle( NS_MODULE, 'testModuleStringExtend2' ) |
| 262 |
); |
| 263 |
$ret = $interpreter->callFunction( |
| 264 |
$engine->executeModule( $module->getInitChunk(), 'test', null ) |
| 265 |
); |
| 266 |
$this->assertSame( [ 'ok' ], $ret, 'string extension cannot be modified from module' ); |
| 267 |
$ret = $interpreter->callFunction( |
| 268 |
$interpreter->loadString( 'return string.testModuleStringExtend', 'teststring2' ) |
| 269 |
); |
| 270 |
$this->assertSame( [ 'ok' ], $ret, 'string extension cannot be modified from module' ); |
| 271 |
|
| 272 |
$ret = $engine->runConsole( [ |
| 273 |
'prevQuestions' => [], |
| 274 |
'question' => '=("").testModuleStringExtend', |
| 275 |
'content' => 'return {}', |
| 276 |
'title' => Title::makeTitle( NS_MODULE, 'dummy' ), |
| 277 |
] ); |
| 278 |
$this->assertSame( 'ok', $ret['return'], 'string extension can be used from console' ); |
| 279 |
|
| 280 |
$ret = $engine->runConsole( [ |
| 281 |
'prevQuestions' => [ 'string.fail = "fail"' ], |
| 282 |
'question' => '=("").fail', |
| 283 |
'content' => 'return {}', |
| 284 |
'title' => Title::makeTitle( NS_MODULE, 'dummy' ), |
| 285 |
] ); |
| 286 |
$this->assertSame( 'nil', $ret['return'], 'string cannot be extended from console' ); |
| 287 |
|
| 288 |
$ret = $engine->runConsole( [ |
| 289 |
'prevQuestions' => [ 'string.testModuleStringExtend = "fail"' ], |
| 290 |
'question' => '=("").testModuleStringExtend', |
| 291 |
'content' => 'return {}', |
| 292 |
'title' => Title::makeTitle( NS_MODULE, 'dummy' ), |
| 293 |
] ); |
| 294 |
$this->assertSame( 'ok', $ret['return'], 'string extension cannot be modified from console' ); |
| 295 |
$ret = $interpreter->callFunction( |
| 296 |
$interpreter->loadString( 'return string.testModuleStringExtend', 'teststring3' ) |
| 297 |
); |
| 298 |
$this->assertSame( [ 'ok' ], $ret, 'string extension cannot be modified from console' ); |
| 299 |
|
| 300 |
$interpreter->callFunction( |
| 301 |
$interpreter->loadString( 'string.testModuleStringExtend = nil', 'unextendstring' ) |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
public function testLoadDataLoadedOnce() { |
| 306 |
$engine = $this->getEngine(); |
| 307 |
$interpreter = $engine->getInterpreter(); |
| 308 |
$frame = $engine->getParser()->getPreprocessor()->newFrame(); |
| 309 |
|
| 310 |
$loadcount = 0; |
| 311 |
$interpreter->callFunction( |
| 312 |
$interpreter->loadString( 'mw.markLoaded = ...', 'fortest' ), |
| 313 |
$interpreter->wrapPHPFunction( static function () use ( &$loadcount ) { |
| 314 |
$loadcount++; |
| 315 |
} ) |
| 316 |
); |
| 317 |
$this->extraModules['Module:TestLoadDataLoadedOnce-data'] = ' |
| 318 |
mw.markLoaded() |
| 319 |
return {} |
| 320 |
'; |
| 321 |
$this->extraModules['Module:TestLoadDataLoadedOnce'] = ' |
| 322 |
local data = mw.loadData( "Module:TestLoadDataLoadedOnce-data" ) |
| 323 |
return { |
| 324 |
foo = function() end, |
| 325 |
bar = function() |
| 326 |
return tostring( package.loaded["Module:TestLoadDataLoadedOnce-data"] ) |
| 327 |
end, |
| 328 |
} |
| 329 |
'; |
| 330 |
|
| 331 |
|
| 332 |
$title = Title::makeTitle( NS_MODULE, 'TestLoadDataLoadedOnce' ); |
| 333 |
for ( $i = 0; $i < 10; $i++ ) { |
| 334 |
$module = $engine->fetchModuleFromParser( $title ); |
| 335 |
$module->invoke( 'foo', $frame->newChild() ); |
| 336 |
} |
| 337 |
$this->assertSame( 1, $loadcount, 'data module was loaded more than once' ); |
| 338 |
|
| 339 |
|
| 340 |
$this->assertSame( 'nil', $module->invoke( 'bar', $frame ), |
| 341 |
'data module was stored in module\'s package.loaded' |
| 342 |
); |
| 343 |
$this->assertSame( [ 'nil' ], |
| 344 |
$interpreter->callFunction( $interpreter->loadString( |
| 345 |
'return tostring( package.loaded["Module:TestLoadDataLoadedOnce-data"] )', 'getLoaded' |
| 346 |
) ), |
| 347 |
'data module was stored in top level\'s package.loaded' |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
public function testLoadJsonDataLoadedOnce() { |
| 352 |
$this->extraModules['Module:TestLoadDataLoadedOnce'] = ' |
| 353 |
local data = mw.loadJsonData( "Module:CommonTests-data.json" ) |
| 354 |
return { |
| 355 |
foo = function() end, |
| 356 |
} |
| 357 |
'; |
| 358 |
|
| 359 |
$engine = $this->getEngine(); |
| 360 |
$interpreter = $engine->getInterpreter(); |
| 361 |
$frame = $engine->getParser()->getPreprocessor()->newFrame(); |
| 362 |
|
| 363 |
|
| 364 |
$title = Title::makeTitle( NS_MODULE, 'TestLoadDataLoadedOnce' ); |
| 365 |
for ( $i = 0; $i < 10; $i++ ) { |
| 366 |
$module = $engine->fetchModuleFromParser( $title ); |
| 367 |
$module->invoke( 'foo', $frame->newChild() ); |
| 368 |
} |
| 369 |
|
| 370 |
$this->assertSame( |
| 371 |
1, $this->templateLoadCounts['Module:CommonTests-data.json'], |
| 372 |
'JSON data was loaded more than once' |
| 373 |
); |
| 374 |
} |
| 375 |
|
| 376 |
public function testFrames() { |
| 377 |
$engine = $this->getEngine(); |
| 378 |
|
| 379 |
$ret = $engine->runConsole( [ |
| 380 |
'prevQuestions' => [], |
| 381 |
'question' => '=mw.getCurrentFrame()', |
| 382 |
'content' => 'return {}', |
| 383 |
'title' => Title::makeTitle( NS_MODULE, 'dummy' ), |
| 384 |
] ); |
| 385 |
$this->assertSame( 'table', $ret['return'], 'frames can be used in the console' ); |
| 386 |
|
| 387 |
$ret = $engine->runConsole( [ |
| 388 |
'prevQuestions' => [], |
| 389 |
'question' => '=mw.getCurrentFrame():newChild{}', |
| 390 |
'content' => 'return {}', |
| 391 |
'title' => Title::makeTitle( NS_MODULE, 'dummy' ), |
| 392 |
] ); |
| 393 |
$this->assertSame( 'table', $ret['return'], 'child frames can be created' ); |
| 394 |
|
| 395 |
$ret = $engine->runConsole( [ |
| 396 |
'prevQuestions' => [ |
| 397 |
'f = mw.getCurrentFrame():newChild{ args = { "ok" } }', |
| 398 |
'f2 = f:newChild{ args = {} }' |
| 399 |
], |
| 400 |
'question' => '=f2:getParent().args[1], f2:getParent():getParent()', |
| 401 |
'content' => 'return {}', |
| 402 |
'title' => Title::makeTitle( NS_MODULE, 'dummy' ), |
| 403 |
] ); |
| 404 |
$this->assertSame( "ok\ttable", $ret['return'], 'child frames have correct parents' ); |
| 405 |
} |
| 406 |
|
| 407 |
public function testCallParserFunction() { |
| 408 |
$engine = $this->getEngine(); |
| 409 |
$parser = $engine->getParser(); |
| 410 |
|
| 411 |
$args = [ |
| 412 |
'prevQuestions' => [], |
| 413 |
'content' => 'return {}', |
| 414 |
'title' => Title::makeTitle( NS_MODULE, 'dummy' ), |
| 415 |
]; |
| 416 |
|
| 417 |
|
| 418 |
$ret = $engine->runConsole( [ |
| 419 |
'question' => '=mw.getCurrentFrame():callParserFunction{ |
| 420 |
name = "urlencode", args = { "x x", "wiki" } |
| 421 |
}', |
| 422 |
] + $args ); |
| 423 |
$this->assertSame( "x_x", $ret['return'], |
| 424 |
'callParserFunction works for {{urlencode:x x|wiki}} (named args w/table)' |
| 425 |
); |
| 426 |
|
| 427 |
$ret = $engine->runConsole( [ |
| 428 |
'question' => '=mw.getCurrentFrame():callParserFunction{ |
| 429 |
name = "urlencode", args = "x x" |
| 430 |
}', |
| 431 |
] + $args ); |
| 432 |
$this->assertSame( "x+x", $ret['return'], |
| 433 |
'callParserFunction works for {{urlencode:x x}} (named args w/scalar)' |
| 434 |
); |
| 435 |
|
| 436 |
$ret = $engine->runConsole( [ |
| 437 |
'question' => '=mw.getCurrentFrame():callParserFunction( "urlencode", { "x x", "wiki" } )', |
| 438 |
] + $args ); |
| 439 |
$this->assertSame( "x_x", $ret['return'], |
| 440 |
'callParserFunction works for {{urlencode:x x|wiki}} (positional args w/table)' |
| 441 |
); |
| 442 |
|
| 443 |
$ret = $engine->runConsole( [ |
| 444 |
'question' => '=mw.getCurrentFrame():callParserFunction( "urlencode", "x x", "wiki" )', |
| 445 |
] + $args ); |
| 446 |
$this->assertSame( "x_x", $ret['return'], |
| 447 |
'callParserFunction works for {{urlencode:x x|wiki}} (positional args w/scalars)' |
| 448 |
); |
| 449 |
|
| 450 |
$ret = $engine->runConsole( [ |
| 451 |
'question' => '=mw.getCurrentFrame():callParserFunction{ |
| 452 |
name = "urlencode:x x", args = { "wiki" } |
| 453 |
}', |
| 454 |
] + $args ); |
| 455 |
$this->assertSame( "x_x", $ret['return'], |
| 456 |
'callParserFunction works for {{urlencode:x x|wiki}} (colon in name, named args w/table)' |
| 457 |
); |
| 458 |
|
| 459 |
$ret = $engine->runConsole( [ |
| 460 |
'question' => '=mw.getCurrentFrame():callParserFunction{ |
| 461 |
name = "urlencode:x x", args = "wiki" |
| 462 |
}', |
| 463 |
] + $args ); |
| 464 |
$this->assertSame( "x_x", $ret['return'], |
| 465 |
'callParserFunction works for {{urlencode:x x|wiki}} (colon in name, named args w/scalar)' |
| 466 |
); |
| 467 |
|
| 468 |
$ret = $engine->runConsole( [ |
| 469 |
'question' => '=mw.getCurrentFrame():callParserFunction( "urlencode:x x", { "wiki" } )', |
| 470 |
] + $args ); |
| 471 |
$this->assertSame( "x_x", $ret['return'], |
| 472 |
'callParserFunction works for {{urlencode:x x|wiki}} (colon in name, positional args w/table)' |
| 473 |
); |
| 474 |
|
| 475 |
$ret = $engine->runConsole( [ |
| 476 |
'question' => '=mw.getCurrentFrame():callParserFunction( "urlencode:x x", "wiki" )', |
| 477 |
] + $args ); |
| 478 |
$this->assertSame( "x_x", $ret['return'], |
| 479 |
'callParserFunction works for {{urlencode:x x|wiki}} (colon in name, positional args w/scalars)' |
| 480 |
); |
| 481 |
|
| 482 |
|
| 483 |
$ret = $engine->runConsole( [ |
| 484 |
'question' => '=mw.getCurrentFrame():callParserFunction( "#tag:pre", |
| 485 |
{ "foo", style = "margin-left: 1.6em" } |
| 486 |
)', |
| 487 |
] + $args ); |
| 488 |
$this->assertSame( |
| 489 |
'<pre style="margin-left: 1.6em">foo</pre>', |
| 490 |
$parser->getStripState()->unstripBoth( $ret['return'] ), |
| 491 |
'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' |
| 492 |
); |
| 493 |
|
| 494 |
|
| 495 |
$ret = $engine->runConsole( [ |
| 496 |
'question' => '=mw.getCurrentFrame():extensionTag( "pre", "foo", |
| 497 |
{ style = "margin-left: 1.6em" } |
| 498 |
)', |
| 499 |
] + $args ); |
| 500 |
$this->assertSame( |
| 501 |
'<pre style="margin-left: 1.6em">foo</pre>', |
| 502 |
$parser->getStripState()->unstripBoth( $ret['return'] ), |
| 503 |
'extensionTag works for {{#tag:pre|foo|style=margin-left: 1.6em}}' |
| 504 |
); |
| 505 |
|
| 506 |
$ret = $engine->runConsole( [ |
| 507 |
'question' => '=mw.getCurrentFrame():extensionTag{ name = "pre", content = "foo", |
| 508 |
args = { style = "margin-left: 1.6em" } |
| 509 |
}', |
| 510 |
] + $args ); |
| 511 |
$this->assertSame( |
| 512 |
'<pre style="margin-left: 1.6em">foo</pre>', |
| 513 |
$parser->getStripState()->unstripBoth( $ret['return'] ), |
| 514 |
'extensionTag works for {{#tag:pre|foo|style=margin-left: 1.6em}}' |
| 515 |
); |
| 516 |
|
| 517 |
|
| 518 |
try { |
| 519 |
$engine->runConsole( [ |
| 520 |
'question' => '=mw.getCurrentFrame():callParserFunction{ |
| 521 |
name = "thisDoesNotExist", args = { "" } |
| 522 |
}', |
| 523 |
] + $args ); |
| 524 |
$this->fail( "Expected LuaError not thrown for nonexistent parser function" ); |
| 525 |
} catch ( LuaError $err ) { |
| 526 |
$this->assertSame( |
| 527 |
'Lua error: callParserFunction: function "thisDoesNotExist" was not found.', |
| 528 |
$err->getMessage(), |
| 529 |
'callParserFunction correctly errors for nonexistent function' |
| 530 |
); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
public function testBug62291() { |
| 535 |
$engine = $this->getEngine(); |
| 536 |
$frame = $engine->getParser()->getPreprocessor()->newFrame(); |
| 537 |
|
| 538 |
$this->extraModules['Module:Bug62291'] = ' |
| 539 |
local p = {} |
| 540 |
function p.foo() |
| 541 |
return table.concat( { |
| 542 |
math.random(), math.random(), math.random(), math.random(), math.random() |
| 543 |
}, ", " ) |
| 544 |
end |
| 545 |
function p.bar() |
| 546 |
local t = {} |
| 547 |
t[1] = p.foo() |
| 548 |
t[2] = mw.getCurrentFrame():preprocess( "{{#invoke:Bug62291|bar2}}" ) |
| 549 |
t[3] = p.foo() |
| 550 |
return table.concat( t, "; " ) |
| 551 |
end |
| 552 |
function p.bar2() |
| 553 |
return "bar2 called" |
| 554 |
end |
| 555 |
return p |
| 556 |
'; |
| 557 |
|
| 558 |
$title = Title::makeTitle( NS_MODULE, 'Bug62291' ); |
| 559 |
$module = $engine->fetchModuleFromParser( $title ); |
| 560 |
|
| 561 |
|
| 562 |
$r1 = $module->invoke( 'foo', $frame->newChild() ); |
| 563 |
$r2 = $module->invoke( 'foo', $frame->newChild() ); |
| 564 |
$this->assertSame( $r1, $r2, 'Multiple invokes returned different sets of random numbers' ); |
| 565 |
|
| 566 |
|
| 567 |
$r1 = $module->invoke( 'bar', $frame->newChild() ); |
| 568 |
$r = explode( '; ', $r1 ); |
| 569 |
$this->assertNotSame( $r[0], $r[2], 'Recursive invoke reset PRNG' ); |
| 570 |
$this->assertSame( 'bar2 called', $r[1], 'Sanity check failed' ); |
| 571 |
|
| 572 |
|
| 573 |
$r2 = $module->invoke( 'bar', $frame->newChild() ); |
| 574 |
$this->assertSame( $r1, $r2, |
| 575 |
'Multiple invokes with recursive invoke returned different sets of random numbers' ); |
| 576 |
} |
| 577 |
|
| 578 |
public function testOsDateTimeTTLs() { |
| 579 |
$engine = $this->getEngine(); |
| 580 |
$pp = $engine->getParser()->getPreprocessor(); |
| 581 |
|
| 582 |
$this->extraModules['Module:DateTime'] = ' |
| 583 |
local p = {} |
| 584 |
function p.day() |
| 585 |
return os.date( "%d" ) |
| 586 |
end |
| 587 |
function p.AMPM() |
| 588 |
return os.date( "%p" ) |
| 589 |
end |
| 590 |
function p.hour() |
| 591 |
return os.date( "%H" ) |
| 592 |
end |
| 593 |
function p.minute() |
| 594 |
return os.date( "%M" ) |
| 595 |
end |
| 596 |
function p.second() |
| 597 |
return os.date( "%S" ) |
| 598 |
end |
| 599 |
function p.table() |
| 600 |
return os.date( "*t" ) |
| 601 |
end |
| 602 |
function p.tablesec() |
| 603 |
return os.date( "*t" ).sec |
| 604 |
end |
| 605 |
function p.time() |
| 606 |
return os.time() |
| 607 |
end |
| 608 |
function p.specificDateAndTime() |
| 609 |
return os.date("%S", os.time{year = 2013, month = 1, day = 1}) |
| 610 |
end |
| 611 |
return p |
| 612 |
'; |
| 613 |
|
| 614 |
$title = Title::makeTitle( NS_MODULE, 'DateTime' ); |
| 615 |
$module = $engine->fetchModuleFromParser( $title ); |
| 616 |
|
| 617 |
$frame = $pp->newFrame(); |
| 618 |
$module->invoke( 'day', $frame ); |
| 619 |
$this->assertNotNull( $frame->getTTL(), 'TTL must be set when day is requested' ); |
| 620 |
$this->assertLessThanOrEqual( 86400, $frame->getTTL(), |
| 621 |
'TTL must not exceed 1 day when day is requested' ); |
| 622 |
|
| 623 |
$frame = $pp->newFrame(); |
| 624 |
$module->invoke( 'AMPM', $frame ); |
| 625 |
$this->assertNotNull( $frame->getTTL(), 'TTL must be set when AM/PM is requested' ); |
| 626 |
$this->assertLessThanOrEqual( 43200, $frame->getTTL(), |
| 627 |
'TTL must not exceed 12 hours when AM/PM is requested' ); |
| 628 |
|
| 629 |
$frame = $pp->newFrame(); |
| 630 |
$module->invoke( 'hour', $frame ); |
| 631 |
$this->assertNotNull( $frame->getTTL(), 'TTL must be set when hour is requested' ); |
| 632 |
$this->assertLessThanOrEqual( 3600, $frame->getTTL(), |
| 633 |
'TTL must not exceed 1 hour when hours are requested' ); |
| 634 |
|
| 635 |
$frame = $pp->newFrame(); |
| 636 |
$module->invoke( 'minute', $frame ); |
| 637 |
$this->assertNotNull( $frame->getTTL(), 'TTL must be set when minutes are requested' ); |
| 638 |
$this->assertLessThanOrEqual( 60, $frame->getTTL(), |
| 639 |
'TTL must not exceed 1 minute when minutes are requested' ); |
| 640 |
|
| 641 |
$frame = $pp->newFrame(); |
| 642 |
$module->invoke( 'second', $frame ); |
| 643 |
$this->assertSame( 1, $frame->getTTL(), |
| 644 |
'TTL must be equal to 1 second when seconds are requested' ); |
| 645 |
|
| 646 |
$frame = $pp->newFrame(); |
| 647 |
$module->invoke( 'table', $frame ); |
| 648 |
$this->assertNull( $frame->getTTL(), |
| 649 |
'TTL must not be set when os.date( "*t" ) is called but no values are looked at' ); |
| 650 |
|
| 651 |
$frame = $pp->newFrame(); |
| 652 |
$module->invoke( 'tablesec', $frame ); |
| 653 |
$this->assertSame( 1, $frame->getTTL(), |
| 654 |
'TTL must be equal to 1 second when seconds are requested from a table' ); |
| 655 |
|
| 656 |
$frame = $pp->newFrame(); |
| 657 |
$module->invoke( 'time', $frame ); |
| 658 |
$this->assertSame( 1, $frame->getTTL(), |
| 659 |
'TTL must be equal to 1 second when os.time() is called' ); |
| 660 |
|
| 661 |
$frame = $pp->newFrame(); |
| 662 |
$module->invoke( 'specificDateAndTime', $frame ); |
| 663 |
$this->assertNull( $frame->getTTL(), |
| 664 |
'TTL must not be set when os.date() or os.time() are called with a specific time' ); |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
public function testVolatileCaching( $func ) { |
| 671 |
$engine = $this->getEngine(); |
| 672 |
$parser = $engine->getParser(); |
| 673 |
$pp = $parser->getPreprocessor(); |
| 674 |
|
| 675 |
$count = 0; |
| 676 |
$parser->setHook( 'scribuntocount', static function ( $str, $argv, $parser, $frame ) use ( &$count ) { |
| 677 |
$frame->setVolatile(); |
| 678 |
return ++$count; |
| 679 |
} ); |
| 680 |
|
| 681 |
$this->extraModules['Template:ScribuntoTestVolatileCaching'] = '<scribuntocount/>'; |
| 682 |
$this->extraModules['Module:TestVolatileCaching'] = ' |
| 683 |
return { |
| 684 |
preprocess = function ( frame ) |
| 685 |
return frame:preprocess( "<scribuntocount/>" ) |
| 686 |
end, |
| 687 |
extensionTag = function ( frame ) |
| 688 |
return frame:extensionTag( "scribuntocount" ) |
| 689 |
end, |
| 690 |
expandTemplate = function ( frame ) |
| 691 |
return frame:expandTemplate{ title = "ScribuntoTestVolatileCaching" } |
| 692 |
end, |
| 693 |
} |
| 694 |
'; |
| 695 |
|
| 696 |
$frame = $pp->newFrame(); |
| 697 |
$count = 0; |
| 698 |
$wikitext = "{{#invoke:TestVolatileCaching|$func}}"; |
| 699 |
$text = $frame->expand( $pp->preprocessToObj( "$wikitext $wikitext" ) ); |
| 700 |
$text = $parser->getStripState()->unstripBoth( $text ); |
| 701 |
$this->assertTrue( $frame->isVolatile(), "Frame is marked volatile" ); |
| 702 |
$this->assertEquals( '1 2', $text, "Volatile wikitext was not cached" ); |
| 703 |
} |
| 704 |
|
| 705 |
public static function provideVolatileCaching() { |
| 706 |
return [ |
| 707 |
[ 'preprocess' ], |
| 708 |
[ 'extensionTag' ], |
| 709 |
[ 'expandTemplate' ], |
| 710 |
]; |
| 711 |
} |
| 712 |
|
| 713 |
public function testGetCurrentFrameAndMWLoadData() { |
| 714 |
$engine = $this->getEngine(); |
| 715 |
$parser = $engine->getParser(); |
| 716 |
$pp = $parser->getPreprocessor(); |
| 717 |
|
| 718 |
$this->extraModules['Module:Bug65687'] = ' |
| 719 |
return { |
| 720 |
test = function ( frame ) |
| 721 |
return mw.loadData( "Module:Bug65687-LD" )[1] |
| 722 |
end |
| 723 |
} |
| 724 |
'; |
| 725 |
$this->extraModules['Module:Bug65687-LD'] = 'return { mw.getCurrentFrame().args[1] or "ok" }'; |
| 726 |
|
| 727 |
$frame = $pp->newFrame(); |
| 728 |
$text = $frame->expand( $pp->preprocessToObj( "{{#invoke:Bug65687|test|foo}}" ) ); |
| 729 |
$text = $parser->getStripState()->unstripBoth( $text ); |
| 730 |
$this->assertEquals( 'ok', $text, 'mw.loadData allowed access to frame args' ); |
| 731 |
} |
| 732 |
|
| 733 |
public function testGetCurrentFrameAtModuleScope() { |
| 734 |
$engine = $this->getEngine(); |
| 735 |
$parser = $engine->getParser(); |
| 736 |
$pp = $parser->getPreprocessor(); |
| 737 |
|
| 738 |
$this->extraModules['Module:Bug67498-directly'] = ' |
| 739 |
local f = mw.getCurrentFrame() |
| 740 |
local f2 = f and f.args[1] or "<none>" |
| 741 |
|
| 742 |
return { |
| 743 |
test = function ( frame ) |
| 744 |
return ( f and f.args[1] or "<none>" ) .. " " .. f2 |
| 745 |
end |
| 746 |
} |
| 747 |
'; |
| 748 |
$this->extraModules['Module:Bug67498-statically'] = ' |
| 749 |
local M = require( "Module:Bug67498-directly" ) |
| 750 |
return { |
| 751 |
test = function ( frame ) |
| 752 |
return M.test( frame ) |
| 753 |
end |
| 754 |
} |
| 755 |
'; |
| 756 |
$this->extraModules['Module:Bug67498-dynamically'] = ' |
| 757 |
return { |
| 758 |
test = function ( frame ) |
| 759 |
local M = require( "Module:Bug67498-directly" ) |
| 760 |
return M.test( frame ) |
| 761 |
end |
| 762 |
} |
| 763 |
'; |
| 764 |
|
| 765 |
foreach ( [ 'directly', 'statically', 'dynamically' ] as $how ) { |
| 766 |
$frame = $pp->newFrame(); |
| 767 |
$text = $frame->expand( $pp->preprocessToObj( |
| 768 |
"{{#invoke:Bug67498-$how|test|foo}} -- {{#invoke:Bug67498-$how|test|bar}}" |
| 769 |
) ); |
| 770 |
$text = $parser->getStripState()->unstripBoth( $text ); |
| 771 |
$text = explode( ' -- ', $text ); |
| 772 |
$this->assertEquals( 'foo foo', $text[0], |
| 773 |
"mw.getCurrentFrame() failed from a module loaded $how" |
| 774 |
); |
| 775 |
$this->assertEquals( 'bar bar', $text[1], |
| 776 |
"mw.getCurrentFrame() cached the frame from a module loaded $how" |
| 777 |
); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
public function testGetCurrentFrameAtModuleScopeT234368() { |
| 782 |
$engine = $this->getEngine(); |
| 783 |
$parser = $engine->getParser(); |
| 784 |
$pp = $parser->getPreprocessor(); |
| 785 |
|
| 786 |
$this->extraModules['Module:Outer'] = ' |
| 787 |
local p = {} |
| 788 |
|
| 789 |
function p.echo( frame ) |
| 790 |
return "(Outer: 1=" .. frame.args[1] .. ", 2=" .. frame.args[2] .. ")" |
| 791 |
end |
| 792 |
|
| 793 |
return p |
| 794 |
'; |
| 795 |
$this->extraModules['Module:Inner'] = ' |
| 796 |
local p = {} |
| 797 |
|
| 798 |
local f = mw.getCurrentFrame() |
| 799 |
local name = f:getTitle() |
| 800 |
local arg1 = f.args[1] |
| 801 |
|
| 802 |
function p.test( frame ) |
| 803 |
local f2 = mw.getCurrentFrame() |
| 804 |
return "(Inner: mod_name=" .. name .. ", mod_1=" .. arg1 .. ", name=" .. f2:getTitle() .. |
| 805 |
", 1=".. f2.args[1] .. ")" |
| 806 |
end |
| 807 |
|
| 808 |
return p |
| 809 |
'; |
| 810 |
|
| 811 |
$frame = $pp->newFrame(); |
| 812 |
$text = $frame->expand( $pp->preprocessToObj( |
| 813 |
"{{#invoke:Outer|echo|oarg|{{#invoke:Inner|test|iarg}}}}" |
| 814 |
) ); |
| 815 |
$text = $parser->getStripState()->unstripBoth( $text ); |
| 816 |
$this->assertSame( |
| 817 |
'(Outer: 1=oarg, 2=(Inner: mod_name=Module:Inner, mod_1=iarg, name=Module:Inner, 1=iarg))', |
| 818 |
$text |
| 819 |
); |
| 820 |
} |
| 821 |
|
| 822 |
public function testNonUtf8Errors() { |
| 823 |
$engine = $this->getEngine(); |
| 824 |
$parser = $engine->getParser(); |
| 825 |
|
| 826 |
$this->extraModules['Module:T208689'] = ' |
| 827 |
local p = {} |
| 828 |
|
| 829 |
p["foo\255bar"] = function () |
| 830 |
error( "error\255bar" ) |
| 831 |
end |
| 832 |
|
| 833 |
p.foo = function () |
| 834 |
p["foo\255bar"]() |
| 835 |
end |
| 836 |
|
| 837 |
return p |
| 838 |
'; |
| 839 |
|
| 840 |
|
| 841 |
try { |
| 842 |
$engine->runConsole( [ |
| 843 |
'prevQuestions' => [], |
| 844 |
'question' => 'p.foo()', |
| 845 |
'title' => Title::newFromText( 'Module:T208689' ), |
| 846 |
'content' => $this->extraModules['Module:T208689'], |
| 847 |
] ); |
| 848 |
$this->fail( 'Expected exception not thrown' ); |
| 849 |
} catch ( ScribuntoException $e ) { |
| 850 |
$this->assertTrue( mb_check_encoding( $e->getMessage(), 'UTF-8' ), 'Message is UTF-8' ); |
| 851 |
$this->assertTrue( mb_check_encoding( $e->getScriptTraceHtml(), 'UTF-8' ), 'Message is UTF-8' ); |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
$text = $parser->recursiveTagParseFully( '{{#invoke:T208689|foo}}' ); |
| 856 |
$this->assertTrue( mb_check_encoding( $text, 'UTF-8' ), 'Parser output is UTF-8' ); |
| 857 |
$vars = $parser->getOutput()->getJsConfigVars(); |
| 858 |
$this->assertArrayHasKey( 'ScribuntoErrors', $vars ); |
| 859 |
foreach ( $vars['ScribuntoErrors'] as $err ) { |
| 860 |
$this->assertTrue( mb_check_encoding( $err, 'UTF-8' ), 'JS config vars are UTF-8' ); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
public function testT236092() { |
| 865 |
$engine = $this->getEngine(); |
| 866 |
$parser = $engine->getParser(); |
| 867 |
$pp = $parser->getPreprocessor(); |
| 868 |
|
| 869 |
$this->extraModules['Module:T236092'] = ' |
| 870 |
local p = {} |
| 871 |
p.foo = mw.isSubsting |
| 872 |
return p |
| 873 |
'; |
| 874 |
|
| 875 |
$frame = $pp->newFrame(); |
| 876 |
$text = $frame->expand( $pp->preprocessToObj( ">{{#invoke:T236092|foo}}<" ) ); |
| 877 |
$text = $parser->getStripState()->unstripBoth( $text ); |
| 878 |
$this->assertSame( '>false<', $text ); |
| 879 |
} |
| 880 |
|
| 881 |
public function testAddWarning() { |
| 882 |
$engine = $this->getEngine(); |
| 883 |
$parser = $engine->getParser(); |
| 884 |
$pp = $parser->getPreprocessor(); |
| 885 |
|
| 886 |
$this->extraModules['Module:TestAddWarning'] = ' |
| 887 |
local p = {} |
| 888 |
|
| 889 |
p.foo = function () |
| 890 |
mw.addWarning( "Don\'t panic!" ) |
| 891 |
return "ok" |
| 892 |
end |
| 893 |
|
| 894 |
return p |
| 895 |
'; |
| 896 |
|
| 897 |
$frame = $pp->newFrame(); |
| 898 |
$text = $frame->expand( $pp->preprocessToObj( ">{{#invoke:TestAddWarning|foo}}<" ) ); |
| 899 |
$text = $parser->getStripState()->unstripBoth( $text ); |
| 900 |
$this->assertSame( '>ok<', $text ); |
| 901 |
$this->assertSame( [ 'Script warning: Don\'t panic!' ], $parser->getOutput()->getWarnings() ); |
| 902 |
} |
| 903 |
} |