summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/MediaWiki/Api/AskTest.php
blob: 05804efdf0cc80732512e92d996fa66dda1e3308 (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
<?php

namespace SMW\Tests\MediaWiki\Api;

use SMW\MediaWiki\Api\Ask;
use SMW\Tests\Utils\MwApiFactory;

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

	private $apiFactory;

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

		$this->apiFactory = new MwApiFactory();
	}

	public function testCanConstruct() {

		$instance = new Ask(
			$this->apiFactory->newApiMain( [ 'query' => 'Foo' ] ),
			'ask'
		);

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

	/**
	 * @dataProvider sampleQueryProvider
	 */
	public function testExecute( array $query, array $expected ) {

		$results = $this->apiFactory->doApiRequest( [
			'action' => 'ask',
			'query' => implode( '|', $query )
		] );

		$this->assertInternalType( 'array', $results );

		// If their is no printrequests array we expect an error array
		if ( isset( $results['query']['printrequests'] ) ) {
			return $this->assertEquals( $expected, $results['query']['printrequests'] );
		}

		$this->assertArrayHasKey( 'error', $results );
	}

	public function sampleQueryProvider() {

		// #0 Standard query
		$provider[] = [
			[
				'[[Modification date::+]]',
				'?Modification date',
				'limit=10'
			],
			[
				[
					'label'=> '',
					'typeid' => '_wpg',
					'mode' => 2,
					'format' => false,
					'key' => '',
					'redi' => ''
				],
				[
					'label'=> 'Modification date',
					'typeid' => '_dat',
					'mode' => 1,
					'format' => '',
					'key' => '_MDAT',
					'redi' => ''
				]
			]
		];

		$provider[] = [
			[
				'[[Modification date::+!]]',
				'limit=3'
			],
			[
				[
					'error'=> 'foo',
				]
			]
		];

		return $provider;
	}

}