summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/tests/phpunit/api/ApiQueryMessageGroupsTest.php
blob: e4a747717a592fd81e6766be60e9c8ffcb81b336 (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
<?php
/**
 * Unit tests for api module.
 *
 * @file
 * @author Harry Burt
 * @copyright Copyright © 2012-2013, Harry Burt
 * @license GPL-2.0+
 */

/**
 * @group medium
 */
class ApiQueryMessageGroupsTest extends ApiTestCase {

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

		global $wgHooks;
		$this->setMwGlobals( array(
			'wgHooks' => $wgHooks,
			'wgTranslateTranslationServices' => array(),
		) );
		$wgHooks['TranslatePostInitGroups'] = array( array( $this, 'getTestGroups' ) );

		$mg = MessageGroups::singleton();
		$mg->setCache( wfGetCache( 'hash' ) );
		$mg->recache();
	}

	public function getTestGroups( &$list ) {
		$exampleMessageGroup = new WikiMessageGroup( 'theid', 'thesource' );
		$exampleMessageGroup->setLabel( 'thelabel' ); // Example
		$exampleMessageGroup->setNamespace( 5 ); // Example
		$list['theid'] = $exampleMessageGroup;

		$anotherExampleMessageGroup = new WikiMessageGroup( 'anotherid', 'thesource' );
		$anotherExampleMessageGroup->setLabel( 'thelabel' ); // Example
		$anotherExampleMessageGroup->setNamespace( 5 ); // Example
		$list['anotherid'] = $anotherExampleMessageGroup;

		return false;
	}

	public function testAPIAccuracy() {
		list( $data ) = $this->doApiRequest(
			array(
				'action' => 'query',
				'meta' => 'messagegroups',
				'mgprop' => 'id|label|class|namespace|exists',
				// @see https://gerrit.wikimedia.org/r/#/c/160222/
				'continue' => ''
			)
		);

		// Check structure
		$this->assertArrayNotHasKey( 'warnings', $data );
		$this->assertArrayHasKey( 'query', $data );
		$this->assertCount( 1, $data['query'] );
		$this->assertArrayHasKey( 'messagegroups', $data['query'] );

		// Basic content checks
		$items = $data['query']['messagegroups'];

		// Ignore dynamic groups
		foreach ( $items as $index => $group ) {
			if ( $group['id'][0] === '!' ) {
				unset( $items[$index] );
			}
		}

		// Renumber keys
		$items = array_values( $items );

		$this->assertCount( 2, $items, 'Only the two groups specified are in the api' );
		$this->assertStringEndsWith( 'id', $items[0]['id'] );
		$this->assertStringEndsWith( 'id', $items[1]['id'] );
		$this->assertSame( $items[0]['label'], 'thelabel' );
		$this->assertSame( $items[1]['label'], 'thelabel' );
		$this->assertSame( $items[0]['exists'], true );
		$this->assertSame( $items[1]['exists'], true );
		$this->assertSame( $items[0]['namespace'], 5 );
		$this->assertSame( $items[1]['namespace'], 5 );
		$this->assertSame( $items[0]['class'], 'WikiMessageGroup' );
		$this->assertSame( $items[1]['class'], 'WikiMessageGroup' );
	}

	public function testAPIFilterAccuracy() {
		$ids = array( 'MadeUpGroup' );
		$ids += array_keys( MessageGroups::getAllGroups() );

		foreach ( $ids as $id ) {
			list( $data ) = $this->doApiRequest(
				array(
					'action' => 'query',
					'meta' => 'messagegroups',
					'mgprop' => 'id|label|class|namespace|exists',
					'mgfilter' => $id,
					// @see https://gerrit.wikimedia.org/r/#/c/160222/
					'continue' => ''
				)
			);

			if ( $id === 'MadeUpGroup' ) {
				// Check structure (shouldn't find anything)
				$this->assertArrayNotHasKey( 'warnings', $data );
				$this->assertArrayHasKey( 'query', $data );
				$this->assertCount( 1, $data['query'] );
				$this->assertArrayHasKey( 'messagegroups', $data['query'] );
				$this->assertCount( 0, $data['query']['messagegroups'] );
				continue;
			}

			// Check structure (filter is unique given these names)
			$this->assertArrayNotHasKey( 'warnings', $data );
			$this->assertArrayHasKey( 'query', $data );
			$this->assertCount( 1, $data['query'] );
			$this->assertArrayHasKey( 'messagegroups', $data['query'] );
			$this->assertCount( 1, $data['query']['messagegroups'] );

			// Check content
			$item = $data['query']['messagegroups'][0];
			$this->assertCount( 5, $item );

			$this->assertSame( $item['id'], $id );
			$this->assertSame( $item['label'], 'thelabel' );
			$this->assertSame( $item['exists'], true );
			$this->assertStringEndsWith( 'id', $item['id'] ); // theid, anotherid
			$this->assertSame( $item['namespace'], 5 );
			$this->assertSame( $item['class'], 'WikiMessageGroup' );
		}
	}

	public function testBadProperty() {
		list( $data ) = $this->doApiRequest(
			array(
				'action' => 'query',
				'meta' => 'messagegroups',
				'mgprop' => 'madeupproperty',
				// @see https://gerrit.wikimedia.org/r/#/c/160222/
				'continue' => ''
			)
		);

		$this->assertArrayHasKey( 'query', $data );
		$this->assertCount( 1, $data['query'] );
		$this->assertArrayHasKey( 'messagegroups', $data['query'] );
		// This doesn't work. invalid properties are only warnings,
		// so we ged empty groups listed
		// $this->assertCount( 0, $data['query']['messagegroups'] );

		$this->assertArrayHasKey( 'warnings', $data );
		$this->assertCount( 1, $data['warnings'] );
		$this->assertArrayHasKey( 'messagegroups', $data['warnings'] );
		$this->assertCount( 1, $data['warnings']['messagegroups'] );
		$this->assertArrayHasKey( 'warnings', $data['warnings']['messagegroups'] );
	}
}