MediaWiki  1.34.0
ApiQueryExtractsTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace TextExtracts\Test;
4 
5 use MediaWikiCoversValidator;
7 use Wikimedia\TestingAccessWrapper;
8 
15 class ApiQueryExtractsTest extends \MediaWikiTestCase {
16  use MediaWikiCoversValidator;
17  use \PHPUnit4And6Compat;
18 
19  private function newInstance() {
20  $config = new \HashConfig( [
21  'ParserCacheExpireTime' => \IExpiringStore::TTL_INDEFINITE,
22  ] );
23 
24  $context = $this->createMock( \IContextSource::class );
25  $context->method( 'getConfig' )
26  ->willReturn( $config );
27 
28  $main = $this->createMock( \ApiMain::class );
29  $main->expects( $this->once() )
30  ->method( 'getContext' )
31  ->willReturn( $context );
32 
33  $query = $this->createMock( \ApiQuery::class );
34  $query->expects( $this->once() )
35  ->method( 'getMain' )
36  ->willReturn( $main );
37 
38  return new ApiQueryExtracts( $query, '', $config );
39  }
40 
41  public function testMemCacheHelpers() {
42  $this->setMwGlobals( 'wgMemc', new \HashBagOStuff() );
43 
44  $title = $this->createMock( \Title::class );
45  $title->method( 'getPageLanguage' )
46  ->willReturn( $this->createMock( \Language::class ) );
47 
48  $page = $this->createMock( \WikiPage::class );
49  $page->method( 'getTitle' )
50  ->willReturn( $title );
51 
52  $text = 'Text to cache';
53 
55  $instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
56  $this->assertFalse( $instance->getFromCache( $page, false ), 'is not cached yet' );
57  $instance->setCache( $page, $text );
58  $this->assertSame( $text, $instance->getFromCache( $page, false ) );
59  }
60 
61  public function testSelfDocumentation() {
63  $instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
64 
65  $this->assertInternalType( 'string', $instance->getCacheMode( [] ) );
66  $this->assertNotEmpty( $instance->getExamplesMessages() );
67  $this->assertInternalType( 'string', $instance->getHelpUrls() );
68 
69  $params = $instance->getAllowedParams();
70  $this->assertInternalType( 'array', $params );
71 
72  $this->assertSame( $params['chars'][\ApiBase::PARAM_MIN], 1 );
73  $this->assertSame( $params['chars'][\ApiBase::PARAM_MAX], 1200 );
74 
75  $this->assertSame( $params['limit'][\ApiBase::PARAM_DFLT], 20 );
76  $this->assertSame( $params['limit'][\ApiBase::PARAM_TYPE], 'limit' );
77  $this->assertSame( $params['limit'][\ApiBase::PARAM_MIN], 1 );
78  $this->assertSame( $params['limit'][\ApiBase::PARAM_MAX], 20 );
79  $this->assertSame( $params['limit'][\ApiBase::PARAM_MAX2], 20 );
80  }
81 
85  public function testGetFirstSection( $text, $isPlainText, $expected ) {
87  $instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
88 
89  $this->assertSame( $expected, $instance->getFirstSection( $text, $isPlainText ) );
90  }
91 
92  public function provideFirstSectionsToExtract() {
93  return [
94  'Plain text match' => [
95  "First\nsection \1\2... \1\2...",
96  true,
97  "First\nsection ",
98  ],
99  'Plain text without a match' => [
100  'Example\1\2...',
101  true,
102  'Example\1\2...',
103  ],
104 
105  'HTML match' => [
106  "First\nsection <h1>...<h2>...",
107  false,
108  "First\nsection ",
109  ],
110  'HTML without a match' => [
111  'Example <h11>...',
112  false,
113  'Example <h11>...',
114  ],
115  ];
116  }
117 
121  public function testDoSections( $text, $format, $expected ) {
123  $instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
124  $instance->params = [ 'sectionformat' => $format ];
125 
126  $this->assertSame( $expected, $instance->doSections( $text ) );
127  }
128 
129  public function provideSectionsToFormat() {
130  $level = 3;
131  $marker = "\1\2$level\2\1";
132 
133  return [
134  'Raw' => [
135  "$marker Headline\t\nNext line",
136  'raw',
137  "$marker Headline\t\nNext line",
138  ],
139  'Wiki text' => [
140  "$marker Headline\t\nNext line",
141  'wiki',
142  "\n=== Headline ===\nNext line",
143  ],
144  'Plain text' => [
145  "$marker Headline\t\nNext line",
146  'plain',
147  "\nHeadline\nNext line",
148  ],
149 
150  'Multiple matches' => [
151  "${marker}First\n${marker}Second",
152  'wiki',
153  "\n=== First ===\n\n=== Second ===",
154  ],
155  ];
156  }
157 }
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
TextExtracts\Test\ApiQueryExtractsTest\newInstance
newInstance()
Definition: ApiQueryExtractsTest.php:19
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
TextExtracts\Test
Definition: ApiQueryExtractsTest.php:3
TextExtracts\Test\ApiQueryExtractsTest\provideSectionsToFormat
provideSectionsToFormat()
Definition: ApiQueryExtractsTest.php:129
TextExtracts\Test\ApiQueryExtractsTest\testSelfDocumentation
testSelfDocumentation()
Definition: ApiQueryExtractsTest.php:61
ApiBase\PARAM_MIN
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:106
TextExtracts\ApiQueryExtracts
Definition: ApiQueryExtracts.php:24
IExpiringStore\TTL_INDEFINITE
const TTL_INDEFINITE
Definition: IExpiringStore.php:44
ApiBase\PARAM_MAX
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:97
$title
$title
Definition: testCompression.php:34
TextExtracts\Test\ApiQueryExtractsTest\testGetFirstSection
testGetFirstSection( $text, $isPlainText, $expected)
@dataProvider provideFirstSectionsToExtract
Definition: ApiQueryExtractsTest.php:85
TextExtracts\Test\ApiQueryExtractsTest\provideFirstSectionsToExtract
provideFirstSectionsToExtract()
Definition: ApiQueryExtractsTest.php:92
TextExtracts\Test\ApiQueryExtractsTest\testMemCacheHelpers
testMemCacheHelpers()
Definition: ApiQueryExtractsTest.php:41
$context
$context
Definition: load.php:45
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiBase\PARAM_MAX2
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition: ApiBase.php:103
TextExtracts\Test\ApiQueryExtractsTest\testDoSections
testDoSections( $text, $format, $expected)
@dataProvider provideSectionsToFormat
Definition: ApiQueryExtractsTest.php:121
TextExtracts\Test\ApiQueryExtractsTest
@covers \TextExtracts\ApiQueryExtracts @group TextExtracts
Definition: ApiQueryExtractsTest.php:15