1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps\Test; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Values\LatLongValue; |
6
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\StubGeocoder; |
7
|
|
|
use Maps\Elements\Location; |
8
|
|
|
use Maps\LocationParser; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers \Maps\LocationParser |
12
|
|
|
* @licence GNU GPL v2+ |
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
14
|
|
|
*/ |
15
|
|
|
class LocationParserTest extends \PHPUnit_Framework_TestCase { |
16
|
|
|
|
17
|
|
|
public function setUp() { |
18
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) { |
19
|
|
|
$this->markTestSkipped( 'MediaWiki is not available' ); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @dataProvider titleProvider |
25
|
|
|
*/ |
26
|
|
|
public function testGivenTitleThatIsNotLink_titleIsSetAndLinkIsNot( $title ) { |
27
|
|
|
$parser = LocationParser::newInstance( new StubGeocoder( new LatLongValue( 1, 2 ) ) ); |
28
|
|
|
$location = $parser->parse( '4,2~' . $title ); |
29
|
|
|
|
30
|
|
|
$this->assertTitleAndLinkAre( $location, $title, '' ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function assertTitleAndLinkAre( Location $location, $title, $link ) { |
34
|
|
|
$this->assertHasJsonKeyWithValue( $location, 'title', $title ); |
35
|
|
|
$this->assertHasJsonKeyWithValue( $location, 'link', $link ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function assertHasJsonKeyWithValue( Location $polygon, $key, $value ) { |
39
|
|
|
$json = $polygon->getJSONObject(); |
40
|
|
|
|
41
|
|
|
$this->assertArrayHasKey( $key, $json ); |
42
|
|
|
$this->assertEquals( $value, $json[$key] ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function titleProvider() { |
46
|
|
|
return [ |
47
|
|
|
[ '' ], |
48
|
|
|
[ 'Title' ], |
49
|
|
|
[ 'Some title' ], |
50
|
|
|
[ 'link' ], |
51
|
|
|
[ 'links:foo' ], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @dataProvider linkProvider |
57
|
|
|
*/ |
58
|
|
|
public function testGivenTitleThatIsLink_linkIsSetAndTitleIsNot( $link ) { |
59
|
|
|
$parser = LocationParser::newInstance( new StubGeocoder( new LatLongValue( 1, 2 ) ) ); |
60
|
|
|
$location = $parser->parse( '4,2~link:' . $link ); |
61
|
|
|
|
62
|
|
|
$this->assertTitleAndLinkAre( $location, '', $link ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function linkProvider() { |
66
|
|
|
return [ |
67
|
|
|
[ 'https://www.semantic-mediawiki.org' ], |
68
|
|
|
[ 'irc://freenode.net' ], |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// /** |
73
|
|
|
// * @dataProvider titleLinkProvider |
74
|
|
|
// */ |
75
|
|
|
// public function testGivenPageTitleAsLink_pageTitleIsTurnedIntoUrl( $link ) { |
76
|
|
|
// $parser = new LocationParser(); |
77
|
|
|
// $location = $parser->parse( '4,2~link:' . $link ); |
78
|
|
|
// |
79
|
|
|
// $linkUrl = Title::newFromText( $link )->getFullURL(); |
80
|
|
|
// $this->assertTitleAndLinkAre( $location, '', $linkUrl ); |
81
|
|
|
// } |
82
|
|
|
// |
83
|
|
|
// public function titleLinkProvider() { |
84
|
|
|
// return array( |
85
|
|
|
// array( 'Foo' ), |
86
|
|
|
// array( 'Some_Page' ), |
87
|
|
|
// ); |
88
|
|
|
// } |
89
|
|
|
|
90
|
|
|
public function testGivenAddressAndNoTitle_addressIsSetAsTitle() { |
91
|
|
|
$geocoder = new StubGeocoder( new LatLongValue( 4, 2 ) ); |
92
|
|
|
|
93
|
|
|
$parser = LocationParser::newInstance( $geocoder, true ); |
94
|
|
|
$location = $parser->parse( 'Tempelhofer Ufer 42' ); |
95
|
|
|
|
96
|
|
|
$this->assertSame( 'Tempelhofer Ufer 42', $location->getTitle() ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGivenAddressAndTitle_addressIsNotUsedAsTitle() { |
100
|
|
|
$geocoder = new StubGeocoder( new LatLongValue( 4, 2 ) ); |
101
|
|
|
|
102
|
|
|
$parser = LocationParser::newInstance( $geocoder, true ); |
103
|
|
|
$location = $parser->parse( 'Tempelhofer Ufer 42~Great title of doom' ); |
104
|
|
|
|
105
|
|
|
$this->assertSame( 'Great title of doom', $location->getTitle() ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGivenCoordinatesAndNoTitle_noTitleIsSet() { |
109
|
|
|
$parser = LocationParser::newInstance( |
110
|
|
|
new StubGeocoder( new LatLongValue( 1, 2 ) ), |
111
|
|
|
true |
112
|
|
|
); |
113
|
|
|
$location = $parser->parse( '4,2' ); |
114
|
|
|
|
115
|
|
|
$this->assertFalse( $location->getOptions()->hasOption( 'title' ) ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|