summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-api/tests/integration/NamespaceGetterTest.php
blob: eea55f9fb09f5aa4a982d91e7f02fde5d4fe7a73 (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
<?php

namespace Mediawiki\Api\Test;

use Mediawiki\Api\MediawikiApi;
use Mediawiki\Api\Service\NamespaceGetter;
use Mediawiki\Api\SimpleRequest;
use Mediawiki\DataModel\NamespaceInfo;

class NamespaceGetterTest extends \PHPUnit_Framework_TestCase {
	public function testGetNamespaceByCanonicalNameReturnsNullIfNamespaceWasNotFound() {
		$nsGetter = new NamespaceGetter( $this->getApi() );
		$this->assertNull( $nsGetter->getNamespaceByCanonicalName( 'Dummy' ) );
	}

	public function testGetNamespaceByCanonicalNameReturnsNamespaceIfNamespaceWasFound() {
		$nsGetter = new NamespaceGetter( $this->getApi() );
		$expectedNamespace = new NamespaceInfo( 1, 'Talk', 'Diskussion', 'first-letter' );
		$this->assertEquals( $expectedNamespace, $nsGetter->getNamespaceByCanonicalName( 'Talk' ) );
	}

	public function testGetNamespaceByNameTriesAllNames() {
		$nsGetter = new NamespaceGetter( $this->getApi() );
		$expectedNamespace = new NamespaceInfo( 1, 'Talk', 'Diskussion', 'first-letter' );
		$this->assertEquals( $expectedNamespace, $nsGetter->getNamespaceByName( 'Talk' ) );
		$this->assertEquals( $expectedNamespace, $nsGetter->getNamespaceByName( 'Diskussion' ) );
	}

	public function testGetNamespaceByNameTriesAliases() {
		$nsGetter = new NamespaceGetter( $this->getApi() );
		$expectedNamespace = new NamespaceInfo(
			3,
			'User talk',
			'Benutzer Diskussion',
			'first-letter',
			null,
			[ 'BD', 'Benutzerin Diskussion' ]
		);
		$this->assertEquals( $expectedNamespace, $nsGetter->getNamespaceByName(
			'Benutzerin Diskussion'
		) );
		$this->assertEquals( $expectedNamespace, $nsGetter->getNamespaceByName( 'BD' ) );
	}

	public function testGetNamespacesReturnsAllNamespaces() {
		$nsGetter = new NamespaceGetter( $this->getApi() );
		$talkNamespace = new NamespaceInfo( 1, 'Talk', 'Diskussion', 'first-letter' );
		$gadgetNamespace = new NamespaceInfo(
			2302,
			'Gadget definition',
			'Gadget-Definition',
			'case-sensitive',
			'GadgetDefinition'
		);
		$namespaces = $nsGetter->getNamespaces();
		$this->assertCount( 27, $namespaces );
		$this->assertArrayHasKey( 1, $namespaces );
		$this->assertEquals( $talkNamespace, $namespaces[1] );
		$this->assertArrayHasKey( 2302, $namespaces );
		$this->assertEquals( $gadgetNamespace, $namespaces[2302] );
	}

	/**
	 * @return \PHPUnit_Framework_MockObject_MockObject|MediawikiApi
	 */
	private function getApi() {
		$api = $this->getMockBuilder( MediawikiApi::class )->disableOriginalConstructor()->getMock();
		$api->expects( $this->any() )
			->method( 'getRequest' )
			->with( $this->getRequest() )
			->willReturn( $this->getNamespaceFixture() );
		return $api;
	}

	private function getRequest() {
		return new SimpleRequest(
			'query', [
			'meta' => 'siteinfo',
			'siprop' => 'namespaces|namespacealiases'
		] );
	}

	private function getNamespaceFixture() {
		return json_decode( file_get_contents( __DIR__ . '/../fixtures/namespaces.json' ), true );
	}
}