Completed
Push — master ( ed4fe9...fde0a3 )
by
unknown
07:35
created

EntityParserOutputGeneratorTestBase::getMetaTags()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare( strict_types = 1 );
4
namespace Wikibase\Repo\Tests\ParserOutput;
5
6
use DataValues\StringValue;
7
use MediaWikiIntegrationTestCase;
8
use Title;
9
use Wikibase\DataModel\Entity\EntityId;
10
use Wikibase\DataModel\Entity\Item;
11
use Wikibase\DataModel\Entity\ItemId;
12
use Wikibase\DataModel\Entity\PropertyId;
13
use Wikibase\DataModel\Services\EntityId\SuffixEntityIdParser;
14
use Wikibase\DataModel\Services\Lookup\InMemoryDataTypeLookup;
15
use Wikibase\DataModel\Snak\PropertyValueSnak;
16
use Wikibase\Lib\Store\EntityTitleLookup;
17
use Wikibase\Lib\TermLanguageFallbackChain;
18
use Wikibase\Repo\EntityReferenceExtractors\EntityReferenceExtractorCollection;
19
use Wikibase\Repo\EntityReferenceExtractors\EntityReferenceExtractorDelegator;
20
use Wikibase\Repo\EntityReferenceExtractors\SiteLinkBadgeItemReferenceExtractor;
21
use Wikibase\Repo\EntityReferenceExtractors\StatementEntityReferenceExtractor;
22
use Wikibase\Repo\ParserOutput\DispatchingEntityMetaTagsCreatorFactory;
23
use Wikibase\Repo\ParserOutput\DispatchingEntityViewFactory;
24
use Wikibase\Repo\ParserOutput\ParserOutputJsConfigBuilder;
25
use Wikibase\View\EntityDocumentView;
26
use Wikibase\View\EntityMetaTagsCreator;
27
use Wikibase\View\ViewContent;
28
29
/**
30
 * BaseClass with helper methods for required
31
 * services for mocking EntityParserOutputGenerator
32
 *
33
 * @group Wikibase
34
 * @group Database
35
 *
36
 * @license GPL-2.0-or-later
37
 */
38
class EntityParserOutputGeneratorTestBase extends MediaWikiIntegrationTestCase {
39
40
	/**
41
	 * @var DispatchingEntityViewFactory
42
	 */
43
	protected $entityViewFactory;
44
45
	/**
46
	 * @return TermLanguageFallbackChain
47
	 */
48
	protected function newLanguageFallbackChain() {
49
		$fallbackChain = $this->getMockBuilder( TermLanguageFallbackChain::class )
50
			->disableOriginalConstructor()
51
			->getMock();
52
53
		$fallbackChain->expects( $this->any() )
54
			->method( 'extractPreferredValue' )
55
			->will( $this->returnCallback( function( $labels ) {
56
				if ( array_key_exists( 'en', $labels ) ) {
57
					return [
58
						'value' => $labels['en'],
59
						'language' => 'en',
60
						'source' => 'en'
61
					];
62
				}
63
64
				return null;
65
			} ) );
66
67
		$fallbackChain->method( 'getFetchLanguageCodes' )
68
			->willReturn( [ 'en' ] );
69
70
		return $fallbackChain;
71
	}
72
73
	protected function newItem() {
74
		$item = new Item( new ItemId( 'Q7799929' ) );
75
76
		$item->setLabel( 'en', 'kitten item' );
77
78
		$statements = $item->getStatements();
79
80
		$statements->addNewStatement( new PropertyValueSnak( 42, new StringValue( 'http://an.url.com' ) ) );
81
		$statements->addNewStatement( new PropertyValueSnak( 42, new StringValue( 'https://another.url.org' ) ) );
82
83
		$statements->addNewStatement( new PropertyValueSnak( 10, new StringValue( 'File:This is a file.pdf' ) ) );
84
		$statements->addNewStatement( new PropertyValueSnak( 10, new StringValue( 'File:Selfie.jpg' ) ) );
85
86
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'kitten', [ new ItemId( 'Q42' ) ] );
87
		$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'meow', [ new ItemId( 'Q42' ), new ItemId( 'Q35' ) ] );
88
89
		return $item;
90
	}
91
92
	/**
93
	 * @param bool $createView
94
	 *
95
	 * @return DispatchingEntityViewFactory
96
	 */
97
	public function mockEntityViewFactory( $createView ) {
98
		$entityViewFactory = $this->getMockBuilder( DispatchingEntityViewFactory::class )
99
			->disableOriginalConstructor()
100
			->getMock();
101
102
		$entityViewFactory->expects( $createView ? $this->once() : $this->never() )
103
			->method( 'newEntityView' )
104
			->will( $this->returnValue( $this->getEntityView() ) );
105
106
		return $entityViewFactory;
107
	}
108
109
	/**
110
	 * @return EntityDocumentView
111
	 */
112
	protected function getEntityView() {
113
		$entityView = $this->getMockBuilder( EntityDocumentView::class )
114
			->setMethods( [
115
				'getTitleHtml',
116
				'getContent'
117
			] )
118
			->disableOriginalConstructor()
119
			->getMockForAbstractClass();
120
121
		$entityView->expects( $this->any() )
122
			->method( 'getTitleHtml' )
123
			->will( $this->returnValue( '<TITLE>' ) );
124
125
		$viewContent = new ViewContent(
126
			'<HTML>',
127
			[]
128
		);
129
130
		$entityView->expects( $this->any() )
131
			->method( 'getContent' )
132
			->will( $this->returnValue( $viewContent ) );
133
134
		return $entityView;
135
	}
136
137
	/**
138
	 * @return DispatchingEntityMetaTagsCreatorFactory
139
	 */
140
	protected function getEntityMetaTagsFactory( $title = null, $description = null ) {
141
		$entityMetaTagsCreatorFactory = $this->createMock( DispatchingEntityMetaTagsCreatorFactory::class );
142
143
		$entityMetaTagsCreatorFactory
144
			->method( 'newEntityMetaTags' )
145
			->will( $this->returnValue( $this->getMetaTags( $title, $description ) ) );
146
147
		return $entityMetaTagsCreatorFactory;
148
	}
149
150
	/**
151
	 * @return EntityMetaTags
152
	 */
153
	protected function getMetaTags( $title, $description ) {
154
		$entityMetaTagsCreator = $this->getMockBuilder( EntityMetaTagsCreator::class )
155
			->setMethods( [
156
				'getMetaTags',
157
			] )
158
			->disableOriginalConstructor()
159
			->getMockForAbstractClass();
160
161
		$tags = [];
162
163
		$tags[ 'title' ] = $title;
164
165
		if ( $description !== null ) {
166
			$tags[ 'description' ] = $description;
167
		}
168
169
		$entityMetaTagsCreator->expects( $this->any() )
170
			->method( 'getMetaTags' )
171
			->will( $this->returnValue( $tags ) );
172
173
		return $entityMetaTagsCreator;
174
	}
175
176
	/**
177
	 * @return ParserOutputJsConfigBuilder
178
	 */
179
	protected function getConfigBuilderMock() {
180
		$configBuilder = $this->getMockBuilder( ParserOutputJsConfigBuilder::class )
181
			->disableOriginalConstructor()
182
			->getMock();
183
184
		$configBuilder->expects( $this->any() )
185
			->method( 'build' )
186
			->will( $this->returnValue( [ '<JS>' ] ) );
187
188
		return $configBuilder;
189
	}
190
191
	/**
192
	 * @return EntityTitleLookup
193
	 */
194
	protected function getEntityTitleLookupMock() {
195
		$entityTitleLookup = $this->createMock( EntityTitleLookup::class );
196
197
		$entityTitleLookup->expects( $this->any() )
198
			->method( 'getTitleForId' )
199
			->will( $this->returnCallback( function( EntityId $id ) {
200
				return Title::makeTitle(
201
					NS_MAIN,
202
					$id->getEntityType() . ':' . $id->getSerialization()
203
				);
204
			} ) );
205
206
		return $entityTitleLookup;
207
	}
208
209
	protected function getPropertyDataTypeLookup() {
210
		$dataTypeLookup = new InMemoryDataTypeLookup();
211
212
		$dataTypeLookup->setDataTypeForProperty( new PropertyId( 'P42' ), 'url' );
213
		$dataTypeLookup->setDataTypeForProperty( new PropertyId( 'P10' ), 'commonsMedia' );
214
215
		return $dataTypeLookup;
216
	}
217
218
	protected function newEntityReferenceExtractor() {
219
		return new EntityReferenceExtractorDelegator( [
220
			'item' => function() {
221
				return new EntityReferenceExtractorCollection( [
222
					new SiteLinkBadgeItemReferenceExtractor(),
223
					new StatementEntityReferenceExtractor(
224
						$this->getMockBuilder( SuffixEntityIdParser::class )
225
							->disableOriginalConstructor()
226
							->getMock()
227
					)
228
				] );
229
			}
230
		], $this->getMockBuilder( StatementEntityReferenceExtractor::class )
231
			->disableOriginalConstructor()
232
			->getMock() );
233
	}
234
235
}
236