Improve coverage for mostly untested ApiQueryExtracts

Instead of trying to come up with a (probably hypercomplex) integration
test for the entire API module, I start to test a few more critical
details. Yes, this is now testing private implementation details. This
is meant as a migration path. See, it's hard to test code like this
without rewriting it, and hard to rewrite it without tests. This patch
aims to give us a little more confidence for future rewrites.

Change-Id: I0405e13eba31124fcb5af83159d7438fe0ad9879
This commit is contained in:
Thiemo Kreuz 2019-02-11 17:32:20 +01:00
parent cc8cd45901
commit ca83bbfd3b

View file

@ -1,8 +1,10 @@
<?php
namespace TextExtracts\Test;
use MediaWikiCoversValidator;
use TextExtracts\ApiQueryExtracts;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \TextExtracts\ApiQueryExtracts
@ -37,8 +39,14 @@ class ApiQueryExtractsTest extends \PHPUnit\Framework\TestCase {
return new ApiQueryExtracts( $query, '', $config );
}
public function testGetAllowedParams() {
$instance = $this->newInstance();
public function testSelfDocumentation() {
/** @var ApiQueryExtracts $instance */
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
$this->assertInternalType( 'string', $instance->getCacheMode( [] ) );
$this->assertNotEmpty( $instance->getExamplesMessages() );
$this->assertInternalType( 'string', $instance->getHelpUrls() );
$params = $instance->getAllowedParams();
$this->assertInternalType( 'array', $params );
$this->assertArrayHasKey( 'chars', $params );
@ -51,4 +59,80 @@ class ApiQueryExtractsTest extends \PHPUnit\Framework\TestCase {
$this->assertEquals( $params['limit'][\ApiBase::PARAM_MAX], 20 );
$this->assertEquals( $params['limit'][\ApiBase::PARAM_MAX2], 20 );
}
/**
* @dataProvider provideFirstSectionsToExtract
*/
public function testGetFirstSection( $text, $isPlainText, $expected ) {
/** @var ApiQueryExtracts $instance */
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
$this->assertSame( $expected, $instance->getFirstSection( $text, $isPlainText ) );
}
public function provideFirstSectionsToExtract() {
return [
'Plain text match' => [
"First\nsection \1\2... \1\2...",
true,
"First\nsection ",
],
'Plain text without a match' => [
'Example\1\2...',
true,
'Example\1\2...',
],
'HTML match' => [
"First\nsection <h1>...<h2>...",
false,
"First\nsection ",
],
'HTML without a match' => [
'Example <h11>...',
false,
'Example <h11>...',
],
];
}
/**
* @dataProvider provideSectionsToFormat
*/
public function testDoSections( $text, $format, $expected ) {
/** @var ApiQueryExtracts $instance */
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
$instance->params = [ 'sectionformat' => $format ];
$this->assertSame( $expected, $instance->doSections( $text ) );
}
public function provideSectionsToFormat() {
$level = 3;
$marker = "\1\2$level\2\1";
return [
'Raw' => [
"$marker Headline\t\nNext line",
'raw',
"$marker Headline\t\nNext line",
],
'Wiki text' => [
"$marker Headline\t\nNext line",
'wiki',
"\n=== Headline ===\nNext line",
],
'Plain text' => [
"$marker Headline\t\nNext line",
'plain',
"\nHeadline\nNext line",
],
'Multiple matches' => [
"${marker}First\n${marker}Second",
'wiki',
"\n=== First ===\n\n=== Second ===",
],
];
}
}