summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/MediaWiki/Api/InfoTest.php
blob: 2210571382e07928533e442ac556fbadf2bf408c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php

namespace SMW\Tests\MediaWiki\Api;

use SMW\MediaWiki\Api\Info;
use SMW\Tests\TestEnvironment;

/**
 * @covers \SMW\MediaWiki\Api\Info
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class InfoTest extends \PHPUnit_Framework_TestCase {

	private $apiFactory;
	private $jobQueue;

	protected function setUp() {
		parent::setUp();

		$this->testEnvironment = new TestEnvironment();
		$this->apiFactory = $this->testEnvironment->getUtilityFactory()->newMwApiFactory();

		$this->jobQueue = $this->getMockBuilder( '\SMW\MediaWiki\JobQueue' )
			->disableOriginalConstructor()
			->getMock();

		$this->testEnvironment->registerObject( 'JobQueue', $this->jobQueue );
	}

	protected function tearDown() {
		$this->testEnvironment->tearDown();
		parent::tearDown();
	}

	public function testCanConstruct() {

		$instance = new Info(
			$this->apiFactory->newApiMain( [] ),
			'smwinfo'
		);

		$this->assertInstanceOf(
			'SMW\MediaWiki\Api\Info',
			$instance
		);
	}

	/**
	 * @dataProvider typeDataProvider
	 */
	public function testExecuteOnStore( $queryParameters, $expectedType ) {

		$result = $this->apiFactory->doApiRequest( [
				'action' => 'smwinfo',
				'info' => $queryParameters
		] );

		if ( $expectedType === 'integer' ) {
			return $this->assertGreaterThanOrEqual( 0, $result['info'][$queryParameters] );
		}

		$this->assertInternalType(
			'array',
			$result['info'][$queryParameters]
		);
	}

	/**
	 * @dataProvider countDataProvider
	 */
	public function testExecuteOnMockStore( $statistics, $type, $expected ) {

		$store = $this->getMockBuilder( '\SMW\Store' )
			->disableOriginalConstructor()
			->getMockForAbstractClass();

		$store->expects( $this->atLeastOnce() )
			->method( 'getStatistics' )
			->will( $this->returnValue( $statistics ) );

		$this->testEnvironment->registerObject( 'Store', $store );

		$instance = new Info(
			$this->apiFactory->newApiMain( [ 'info' => $type ] ),
			'smwinfo'
		);

		$instance->execute();

		// MW 1.25
		$result = method_exists( $instance->getResult(), 'getResultData' ) ? $instance->getResult()->getResultData() : $instance->getResultData();

		// This came with 1.25, no idea what this suppose to be
		unset( $result['_type'] );

		$this->assertEquals(
			$expected,
			$result['info'][$type]
		);
	}

	/**
	 * Test unknown query parameter
	 *
	 * Only valid parameters will yield an info array while an unknown parameter
	 * will produce a "warnings" array.
	 *
	 * @since 1.9
	 */
	public function testUnknownQueryParameter() {

		$data = $this->apiFactory->doApiRequest( [
				'action' => 'smwinfo',
				'info' => 'Foo'
		] );

		$this->assertInternalType(
			'array',
			$data['warnings']
		);
	}

	public function testJobCount() {

		$this->jobQueue->expects( $this->any() )
			->method( 'getQueueSize' )
			->will( $this->returnValue( 1 ) );

		$result = $this->apiFactory->doApiRequest(
			[
				'action' => 'smwinfo',
				'info' => 'jobcount'
			]
		);

		$this->assertArrayHasKey(
			'smw.update',
			$result['info']['jobcount']
		);
	}

	public function countDataProvider() {
		return [
			[ [ 'QUERYFORMATS' => [ 'table' => 3 ] ], 'formatcount', [ 'table' => 3 ] ],
			[ [ 'PROPUSES'     => 34 ], 'propcount',         34 ],
			[ [ 'ERRORUSES'    => 42 ], 'errorcount',        42 ],
			[ [ 'USEDPROPS'    => 51 ], 'usedpropcount',     51 ],
			[ [ 'TOTALPROPS'   => 52 ], 'totalpropcount',    52 ],
			[ [ 'DECLPROPS'    => 67 ], 'declaredpropcount', 67 ],
			[ [ 'OWNPAGE'      => 99 ], 'proppagecount',     99 ],
			[ [ 'QUERY'        => 11 ], 'querycount',        11 ],
			[ [ 'QUERYSIZE'    => 24 ], 'querysize',         24 ],
			[ [ 'CONCEPTS'     => 17 ], 'conceptcount',      17 ],
			[ [ 'SUBOBJECTS'   => 88 ], 'subobjectcount',    88 ],
		];
	}

	public function typeDataProvider() {
		return [
			[ 'proppagecount',     'integer' ],
			[ 'propcount',         'integer' ],
			[ 'errorcount',        'integer' ],
			[ 'querycount',        'integer' ],
			[ 'usedpropcount',     'integer' ],
			[ 'totalpropcount',    'integer' ],
			[ 'declaredpropcount', 'integer' ],
			[ 'conceptcount',      'integer' ],
			[ 'querysize',         'integer' ],
			[ 'subobjectcount',    'integer' ],
			[ 'formatcount',       'array'   ],
			[ 'jobcount',          'array'   ]
		];
	}

}