MediaWiki REL1_34
ApiQueryExtractsTest.php
Go to the documentation of this file.
1<?php
2
4
5use MediaWikiCoversValidator;
7use Wikimedia\TestingAccessWrapper;
8
15class 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
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}
Simple store for keeping values in an associative array for the current process.
@covers \TextExtracts\ApiQueryExtracts @group TextExtracts
testDoSections( $text, $format, $expected)
@dataProvider provideSectionsToFormat
testGetFirstSection( $text, $isPlainText, $expected)
@dataProvider provideFirstSectionsToExtract
$context
Definition load.php:45