Пример #1
0
 /**
  * @covers ApiErrorFormatter_BackCompat
  */
 public function testErrorFormatterBC()
 {
     $mainpagePlain = wfMessage('mainpage')->useDatabase(false)->plain();
     $parensPlain = wfMessage('parentheses', 'foobar')->useDatabase(false)->plain();
     $result = new ApiResult(8388608);
     $formatter = new ApiErrorFormatter_BackCompat($result);
     $formatter->addWarning('string', 'mainpage');
     $formatter->addError('err', 'mainpage');
     $this->assertSame(array('error' => array('code' => 'mainpage', 'info' => $mainpagePlain), 'warnings' => array('string' => array('warnings' => $mainpagePlain, ApiResult::META_CONTENT => 'warnings')), ApiResult::META_TYPE => 'assoc'), $result->getResultData(), 'Simple test');
     $result->reset();
     $formatter->addWarning('foo', 'mainpage');
     $formatter->addWarning('foo', 'mainpage');
     $formatter->addWarning('foo', array('parentheses', 'foobar'));
     $msg1 = wfMessage('mainpage');
     $formatter->addWarning('message', $msg1);
     $msg2 = new ApiMessage('mainpage', 'overriddenCode', array('overriddenData' => true));
     $formatter->addWarning('messageWithData', $msg2);
     $formatter->addError('errWithData', $msg2);
     $this->assertSame(array('error' => array('code' => 'overriddenCode', 'info' => $mainpagePlain, 'overriddenData' => true), 'warnings' => array('messageWithData' => array('warnings' => $mainpagePlain, ApiResult::META_CONTENT => 'warnings'), 'message' => array('warnings' => $mainpagePlain, ApiResult::META_CONTENT => 'warnings'), 'foo' => array('warnings' => "{$mainpagePlain}\n{$parensPlain}", ApiResult::META_CONTENT => 'warnings')), ApiResult::META_TYPE => 'assoc'), $result->getResultData(), 'Complex test');
     $result->reset();
     $status = Status::newGood();
     $status->warning('mainpage');
     $status->warning('parentheses', 'foobar');
     $status->warning($msg1);
     $status->warning($msg2);
     $status->error('mainpage');
     $status->error('parentheses', 'foobar');
     $formatter->addMessagesFromStatus('status', $status);
     $this->assertSame(array('error' => array('code' => 'parentheses', 'info' => $parensPlain), 'warnings' => array('status' => array('warnings' => "{$mainpagePlain}\n{$parensPlain}", ApiResult::META_CONTENT => 'warnings')), ApiResult::META_TYPE => 'assoc'), $result->getResultData(), 'Status test');
     $I = ApiResult::META_INDEXED_TAG_NAME;
     $this->assertSame(array(array('type' => 'error', 'message' => 'mainpage', 'params' => array($I => 'param')), array('type' => 'error', 'message' => 'parentheses', 'params' => array('foobar', $I => 'param')), $I => 'error'), $formatter->arrayFromStatus($status, 'error'), 'arrayFromStatus test for error');
     $this->assertSame(array(array('type' => 'warning', 'message' => 'mainpage', 'params' => array($I => 'param')), array('type' => 'warning', 'message' => 'parentheses', 'params' => array('foobar', $I => 'param')), array('message' => 'mainpage', 'params' => array($I => 'param'), 'type' => 'warning'), array('message' => 'mainpage', 'params' => array($I => 'param'), 'type' => 'warning'), $I => 'warning'), $formatter->arrayFromStatus($status, 'warning'), 'arrayFromStatus test for warning');
 }
Пример #2
0
 /**
  * Actually add the warning or error to the result
  * @param string $tag 'warning' or 'error'
  * @param string $moduleName
  * @param ApiMessage|ApiRawMessage $msg
  */
 protected function addWarningOrError($tag, $moduleName, $msg)
 {
     $value = ['code' => $msg->getApiCode()];
     switch ($this->format) {
         case 'wikitext':
             $value += ['text' => $msg->text(), ApiResult::META_CONTENT => 'text'];
             break;
         case 'html':
             $value += ['html' => $msg->parse(), ApiResult::META_CONTENT => 'html'];
             break;
         case 'raw':
             $value += ['message' => $msg->getKey(), 'params' => $msg->getParams()];
             ApiResult::setIndexedTagName($value['params'], 'param');
             break;
         case 'none':
             break;
     }
     $value += $msg->getApiData();
     $path = [$tag . 's', $moduleName];
     $existing = $this->result->getResultData($path);
     if ($existing === null || !in_array($value, $existing)) {
         $flags = ApiResult::NO_SIZE_CHECK;
         if ($existing === null) {
             $flags |= ApiResult::ADD_ON_TOP;
         }
         $this->result->addValue($path, null, $value, $flags);
         $this->result->addIndexedTagName($path, $tag);
     }
 }
Пример #3
0
 /**
  * @covers ApiResult
  */
 public function testDeprecatedContinuation()
 {
     // Ignore ApiResult deprecation warnings during this test
     set_error_handler(function ($errno, $errstr) use(&$warnings) {
         if (preg_match('/Use of ApiResult::\\S+ was deprecated in MediaWiki \\d+.\\d+\\./', $errstr)) {
             return true;
         }
         return false;
     });
     $reset = new ScopedCallback('restore_error_handler');
     $allModules = array(new MockApiQueryBase('mock1'), new MockApiQueryBase('mock2'), new MockApiQueryBase('mocklist'));
     $generator = new MockApiQueryBase('generator');
     $main = new ApiMain(RequestContext::getMain());
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation(null, $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, $allModules), $ret);
     $result->setContinueParam($allModules[0], 'm1continue', array(1, 2));
     $result->setContinueParam($allModules[2], 'mlcontinue', 2);
     $result->setGeneratorContinueParam($generator, 'gcontinue', 3);
     $result->endContinuation('raw');
     $result->endContinuation('standard');
     $this->assertSame(array('mlcontinue' => 2, 'm1continue' => '1|2', 'continue' => '||mock2'), $result->getResultData('continue'));
     $this->assertSame(null, $result->getResultData('batchcomplete'));
     $this->assertSame(array('mock1' => array('m1continue' => '1|2'), 'mocklist' => array('mlcontinue' => 2), 'generator' => array('gcontinue' => 3)), $result->getResultData('query-continue'));
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation(null, $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, $allModules), $ret);
     $result->setContinueParam($allModules[0], 'm1continue', array(1, 2));
     $result->setGeneratorContinueParam($generator, 'gcontinue', array(3, 4));
     $result->endContinuation('raw');
     $result->endContinuation('standard');
     $this->assertSame(array('m1continue' => '1|2', 'continue' => '||mock2|mocklist'), $result->getResultData('continue'));
     $this->assertSame(null, $result->getResultData('batchcomplete'));
     $this->assertSame(array('mock1' => array('m1continue' => '1|2'), 'generator' => array('gcontinue' => '3|4')), $result->getResultData('query-continue'));
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation(null, $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, $allModules), $ret);
     $result->setContinueParam($allModules[2], 'mlcontinue', 2);
     $result->setGeneratorContinueParam($generator, 'gcontinue', 3);
     $result->endContinuation('raw');
     $result->endContinuation('standard');
     $this->assertSame(array('mlcontinue' => 2, 'gcontinue' => 3, 'continue' => 'gcontinue||'), $result->getResultData('continue'));
     $this->assertSame(true, $result->getResultData('batchcomplete'));
     $this->assertSame(array('mocklist' => array('mlcontinue' => 2), 'generator' => array('gcontinue' => 3)), $result->getResultData('query-continue'));
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation(null, $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, $allModules), $ret);
     $result->setGeneratorContinueParam($generator, 'gcontinue', 3);
     $result->endContinuation('raw');
     $result->endContinuation('standard');
     $this->assertSame(array('gcontinue' => 3, 'continue' => 'gcontinue||mocklist'), $result->getResultData('continue'));
     $this->assertSame(true, $result->getResultData('batchcomplete'));
     $this->assertSame(array('generator' => array('gcontinue' => 3)), $result->getResultData('query-continue'));
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation(null, $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, $allModules), $ret);
     $result->setContinueParam($allModules[0], 'm1continue', array(1, 2));
     $result->setContinueParam($allModules[2], 'mlcontinue', 2);
     $result->endContinuation('raw');
     $result->endContinuation('standard');
     $this->assertSame(array('mlcontinue' => 2, 'm1continue' => '1|2', 'continue' => '||mock2'), $result->getResultData('continue'));
     $this->assertSame(null, $result->getResultData('batchcomplete'));
     $this->assertSame(array('mock1' => array('m1continue' => '1|2'), 'mocklist' => array('mlcontinue' => 2)), $result->getResultData('query-continue'));
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation(null, $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, $allModules), $ret);
     $result->setContinueParam($allModules[0], 'm1continue', array(1, 2));
     $result->endContinuation('raw');
     $result->endContinuation('standard');
     $this->assertSame(array('m1continue' => '1|2', 'continue' => '||mock2|mocklist'), $result->getResultData('continue'));
     $this->assertSame(null, $result->getResultData('batchcomplete'));
     $this->assertSame(array('mock1' => array('m1continue' => '1|2')), $result->getResultData('query-continue'));
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation(null, $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, $allModules), $ret);
     $result->setContinueParam($allModules[2], 'mlcontinue', 2);
     $result->endContinuation('raw');
     $result->endContinuation('standard');
     $this->assertSame(array('mlcontinue' => 2, 'continue' => '-||mock1|mock2'), $result->getResultData('continue'));
     $this->assertSame(true, $result->getResultData('batchcomplete'));
     $this->assertSame(array('mocklist' => array('mlcontinue' => 2)), $result->getResultData('query-continue'));
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation(null, $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, $allModules), $ret);
     $result->endContinuation('raw');
     $result->endContinuation('standard');
     $this->assertSame(null, $result->getResultData('continue'));
     $this->assertSame(true, $result->getResultData('batchcomplete'));
     $this->assertSame(null, $result->getResultData('query-continue'));
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation('||mock2', $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(false, array_values(array_diff_key($allModules, array(1 => 1)))), $ret);
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $ret = $result->beginContinuation('-||', $allModules, array('mock1', 'mock2'));
     $this->assertSame(array(true, array_values(array_diff_key($allModules, array(0 => 0, 1 => 1)))), $ret);
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     try {
         $result->beginContinuation('foo', $allModules, array('mock1', 'mock2'));
         $this->fail('Expected exception not thrown');
     } catch (UsageException $ex) {
         $this->assertSame('Invalid continue param. You should pass the original value returned by the previous query', $ex->getMessage(), 'Expected exception');
     }
     $main->setContinuationManager(null);
     $result = new ApiResult(8388608);
     $result->setMainForContinuation($main);
     $result->beginContinuation('||mock2', array_slice($allModules, 0, 2), array('mock1', 'mock2'));
     try {
         $result->setContinueParam($allModules[1], 'm2continue', 1);
         $this->fail('Expected exception not thrown');
     } catch (UnexpectedValueException $ex) {
         $this->assertSame('Module \'mock2\' was not supposed to have been executed, but it was executed anyway', $ex->getMessage(), 'Expected exception');
     }
     try {
         $result->setContinueParam($allModules[2], 'mlcontinue', 1);
         $this->fail('Expected exception not thrown');
     } catch (UnexpectedValueException $ex) {
         $this->assertSame('Module \'mocklist\' called ApiContinuationManager::addContinueParam ' . 'but was not passed to ApiContinuationManager::__construct', $ex->getMessage(), 'Expected exception');
     }
     $main->setContinuationManager(null);
 }
Пример #4
0
 /**
  * @covers ApiResult
  * @dataProvider provideTransformations
  * @param string $label
  * @param array $input
  * @param array $transforms
  * @param array|Exception $expect
  */
 public function testTransformations($label, $input, $transforms, $expect)
 {
     $result = new ApiResult(false);
     $result->addValue(null, 'test', $input);
     if ($expect instanceof Exception) {
         try {
             $output = $result->getResultData('test', $transforms);
             $this->fail('Expected exception not thrown', $label);
         } catch (Exception $ex) {
             $this->assertEquals($ex, $expect, $label);
         }
     } else {
         $output = $result->getResultData('test', $transforms);
         $this->assertEquals($expect, $output, $label);
     }
 }