Example #1
0
 /**
  * @covers ApiResult
  */
 public function testDeprecatedFunctions()
 {
     // 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;
         }
         if (preg_match('/Use of ApiMain to ApiResult::__construct ' . 'was deprecated in MediaWiki \\d+.\\d+\\./', $errstr)) {
             return true;
         }
         return false;
     });
     $reset = new ScopedCallback('restore_error_handler');
     $context = new DerivativeContext(RequestContext::getMain());
     $context->setConfig(new HashConfig(array('APIModules' => array(), 'APIFormatModules' => array(), 'APIMaxResultSize' => 42)));
     $main = new ApiMain($context);
     $result = TestingAccessWrapper::newFromObject(new ApiResult($main));
     $this->assertSame(42, $result->maxSize);
     $this->assertSame($main->getErrorFormatter(), $result->errorFormatter);
     $this->assertSame($main, $result->mainForContinuation);
     $result = new ApiResult(8388608);
     $result->addContentValue(null, 'test', 'content');
     $result->addContentValue(array('foo', 'bar'), 'test', 'content');
     $result->addIndexedTagName(null, 'itn');
     $result->addSubelementsList(null, array('sub'));
     $this->assertSame(array('foo' => array('bar' => array('*' => 'content')), '*' => 'content'), $result->getData());
     $result->setRawMode();
     $this->assertSame(array('foo' => array('bar' => array('*' => 'content')), '*' => 'content', '_element' => 'itn', '_subelements' => array('sub')), $result->getData());
     $arr = array();
     ApiResult::setContent($arr, 'value');
     ApiResult::setContent($arr, 'value2', 'foobar');
     $this->assertSame(array(ApiResult::META_CONTENT => 'content', 'content' => 'value', 'foobar' => array(ApiResult::META_CONTENT => 'content', 'content' => 'value2')), $arr);
     $result = new ApiResult(3);
     $formatter = new ApiErrorFormatter_BackCompat($result);
     $result->setErrorFormatter($formatter);
     $result->disableSizeCheck();
     $this->assertTrue($result->addValue(null, 'foo', '1234567890'));
     $result->enableSizeCheck();
     $this->assertSame(0, $result->getSize());
     $this->assertFalse($result->addValue(null, 'foo', '1234567890'));
     $arr = array('foo' => array('bar' => 1));
     $result->setIndexedTagName_recursive($arr, 'itn');
     $this->assertSame(array('foo' => array('bar' => 1, ApiResult::META_INDEXED_TAG_NAME => 'itn')), $arr);
     $status = Status::newGood();
     $status->fatal('parentheses', '1');
     $status->fatal('parentheses', '2');
     $status->warning('parentheses', '3');
     $status->warning('parentheses', '4');
     $this->assertSame(array(array('type' => 'error', 'message' => 'parentheses', 'params' => array(0 => '1', ApiResult::META_INDEXED_TAG_NAME => 'param')), array('type' => 'error', 'message' => 'parentheses', 'params' => array(0 => '2', ApiResult::META_INDEXED_TAG_NAME => 'param')), ApiResult::META_INDEXED_TAG_NAME => 'error'), $result->convertStatusToArray($status, 'error'));
     $this->assertSame(array(array('type' => 'warning', 'message' => 'parentheses', 'params' => array(0 => '3', ApiResult::META_INDEXED_TAG_NAME => 'param')), array('type' => 'warning', 'message' => 'parentheses', 'params' => array(0 => '4', ApiResult::META_INDEXED_TAG_NAME => 'param')), ApiResult::META_INDEXED_TAG_NAME => 'warning'), $result->convertStatusToArray($status, 'warning'));
 }
Example #2
0
 /**
  * @covers ApiResult
  */
 public function testInstanceDataMethods()
 {
     $result = new ApiResult(8388608);
     $result->addValue(null, 'setValue', '1');
     $result->addValue(null, null, 'unnamed 1');
     $result->addValue(null, null, 'unnamed 2');
     $result->addValue(null, 'deleteValue', '2');
     $result->removeValue(null, 'deleteValue');
     $result->addValue(['a', 'b'], 'deleteValue', '3');
     $result->removeValue(['a', 'b', 'deleteValue'], null, '3');
     $result->addContentValue(null, 'setContentValue', '3');
     $this->assertSame(['setValue' => '1', 'unnamed 1', 'unnamed 2', 'a' => ['b' => []], 'setContentValue' => '3', ApiResult::META_TYPE => 'assoc', ApiResult::META_CONTENT => 'setContentValue'], $result->getResultData());
     $this->assertSame(20, $result->getSize());
     try {
         $result->addValue(null, 'setValue', '99');
         $this->fail('Expected exception not thrown');
     } catch (RuntimeException $ex) {
         $this->assertSame('Attempting to add element setValue=99, existing value is 1', $ex->getMessage(), 'Expected exception');
     }
     try {
         $result->addContentValue(null, 'setContentValue2', '99');
         $this->fail('Expected exception not thrown');
     } catch (RuntimeException $ex) {
         $this->assertSame('Attempting to set content element as setContentValue2 when setContentValue ' . 'is already set as the content element', $ex->getMessage(), 'Expected exception');
     }
     $result->addValue(null, 'setValue', '99', ApiResult::OVERRIDE);
     $this->assertSame('99', $result->getResultData(['setValue']));
     $result->addContentValue(null, 'setContentValue2', '99', ApiResult::OVERRIDE);
     $this->assertSame('setContentValue2', $result->getResultData([ApiResult::META_CONTENT]));
     $result->reset();
     $this->assertSame([ApiResult::META_TYPE => 'assoc'], $result->getResultData());
     $this->assertSame(0, $result->getSize());
     $result->addValue(null, 'foo', 1);
     $result->addValue(null, 'bar', 1);
     $result->addValue(null, 'top', '2', ApiResult::ADD_ON_TOP);
     $result->addValue(null, null, '2', ApiResult::ADD_ON_TOP);
     $result->addValue(null, 'bottom', '2');
     $result->addValue(null, 'foo', '2', ApiResult::OVERRIDE);
     $result->addValue(null, 'bar', '2', ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP);
     $this->assertSame([0, 'top', 'foo', 'bar', 'bottom', ApiResult::META_TYPE], array_keys($result->getResultData()));
     $result->reset();
     $result->addValue(null, 'foo', ['bar' => 1]);
     $result->addValue(['foo', 'top'], 'x', 2, ApiResult::ADD_ON_TOP);
     $result->addValue(['foo', 'bottom'], 'x', 2);
     $this->assertSame(['top', 'bar', 'bottom'], array_keys($result->getResultData(['foo'])));
     $result->reset();
     $result->addValue(null, 'sub', ['foo' => 1]);
     $result->addValue(null, 'sub', ['bar' => 1]);
     $this->assertSame(['sub' => ['foo' => 1, 'bar' => 1], ApiResult::META_TYPE => 'assoc'], $result->getResultData());
     try {
         $result->addValue(null, 'sub', ['foo' => 2, 'baz' => 2]);
         $this->fail('Expected exception not thrown');
     } catch (RuntimeException $ex) {
         $this->assertSame('Conflicting keys (foo) when attempting to merge element sub', $ex->getMessage(), 'Expected exception');
     }
     $result->reset();
     $title = Title::newFromText("MediaWiki:Foobar");
     $obj = new stdClass();
     $obj->foo = 1;
     $obj->bar = 2;
     $result->addValue(null, 'title', $title);
     $result->addValue(null, 'obj', $obj);
     $this->assertSame(['title' => (string) $title, 'obj' => ['foo' => 1, 'bar' => 2, ApiResult::META_TYPE => 'assoc'], ApiResult::META_TYPE => 'assoc'], $result->getResultData());
     $fh = tmpfile();
     try {
         $result->addValue(null, 'file', $fh);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add resource(stream) to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         $result->addValue(null, null, $fh);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add resource(stream) to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         $obj->file = $fh;
         $result->addValue(null, 'sub', $obj);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add resource(stream) to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         $obj->file = $fh;
         $result->addValue(null, null, $obj);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add resource(stream) to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     fclose($fh);
     try {
         $result->addValue(null, 'inf', INF);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         $result->addValue(null, null, INF);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         $result->addValue(null, 'nan', NAN);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         $result->addValue(null, null, NAN);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     $result->addValue(null, null, NAN, ApiResult::NO_VALIDATE);
     try {
         $result->addValue(null, null, NAN, ApiResult::NO_SIZE_CHECK);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     $result->reset();
     $result->addParsedLimit('foo', 12);
     $this->assertSame(['limits' => ['foo' => 12], ApiResult::META_TYPE => 'assoc'], $result->getResultData());
     $result->addParsedLimit('foo', 13);
     $this->assertSame(['limits' => ['foo' => 13], ApiResult::META_TYPE => 'assoc'], $result->getResultData());
     $this->assertSame(null, $result->getResultData(['foo', 'bar', 'baz']));
     $this->assertSame(13, $result->getResultData(['limits', 'foo']));
     try {
         $result->getResultData(['limits', 'foo', 'bar']);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Path limits.foo is not an array', $ex->getMessage(), 'Expected exception');
     }
     // Add two values and some metadata, but ensure metadata is not counted
     $result = new ApiResult(100);
     $obj = ['attr' => '12345'];
     ApiResult::setContentValue($obj, 'content', '1234567890');
     $this->assertTrue($result->addValue(null, 'foo', $obj));
     $this->assertSame(15, $result->getSize());
     $result = new ApiResult(10);
     $formatter = new ApiErrorFormatter($result, Language::factory('en'), 'none', false);
     $result->setErrorFormatter($formatter);
     $this->assertFalse($result->addValue(null, 'foo', '12345678901'));
     $this->assertTrue($result->addValue(null, 'foo', '12345678901', ApiResult::NO_SIZE_CHECK));
     $this->assertSame(0, $result->getSize());
     $result->reset();
     $this->assertTrue($result->addValue(null, 'foo', '1234567890'));
     $this->assertFalse($result->addValue(null, 'foo', '1'));
     $result->removeValue(null, 'foo');
     $this->assertTrue($result->addValue(null, 'foo', '1'));
     $result = new ApiResult(10);
     $obj = new ApiResultTestSerializableObject('ok');
     $obj->foobar = 'foobaz';
     $this->assertTrue($result->addValue(null, 'foo', $obj));
     $this->assertSame(2, $result->getSize());
     $result = new ApiResult(8388608);
     $result2 = new ApiResult(8388608);
     $result2->addValue(null, 'foo', 'bar');
     $result->addValue(null, 'baz', $result2);
     $this->assertSame(['baz' => ['foo' => 'bar', ApiResult::META_TYPE => 'assoc'], ApiResult::META_TYPE => 'assoc'], $result->getResultData());
     $result = new ApiResult(8388608);
     $result->addValue(null, 'foo', "foo�bar");
     $result->addValue(null, 'bar', "á");
     $result->addValue(null, 'baz', 74);
     $result->addValue(null, null, "foo�bar");
     $result->addValue(null, null, "á");
     $this->assertSame(['foo' => "foo�bar", 'bar' => "á", 'baz' => 74, 0 => "foo�bar", 1 => "á", ApiResult::META_TYPE => 'assoc'], $result->getResultData());
     $result = new ApiResult(8388608);
     $obj = new stdClass();
     $obj->{'1'} = 'one';
     $arr = [];
     $result->addValue($arr, 'foo', $obj);
     $this->assertSame(['foo' => [1 => 'one', ApiResult::META_TYPE => 'assoc'], ApiResult::META_TYPE => 'assoc'], $result->getResultData());
 }