Completed
Push — AUTOMATED_TESTING ( aecfa5...b401db )
by Gordon
12:37
created

testSingularMappableItemMarkerUJSExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4286
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
class MapUtilTest extends SapphireTest {
4
5
	/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
6
	Other tests:
7
	1) List, ArrayList, DataList, null for get_map
8
	2) Negative and zero map sizes
9
	3) Invalid map type
10
11
12
	public function setUpOnce() {
13
		$this->requiredExtensions = array(
14
			'Member' => array('MapExtension')
15
		);
16
		parent::setupOnce();
17
	}
18
*/
19
20
	public function test_set_api_key_string() {
21
		MapUtil::set_api_key('PRETENDAPIKEY');
22
		$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
		$this->fail('Where is this used?');
24
	}
25
26
	public function test_set_api_key_host_array() {
27
		$url = Director::absoluteBaseURL();
28
		// remove http and https
29
		$url = str_replace('http://', '', $url);
30
		$url = str_replace('https://', '', $url);
31
		$parts = explode('/', $url);
32
		$host = $parts[0];
33
		$key = array($host => 'PRETENDAPIKEY');
34
		MapUtil::set_api_key($key);
0 ignored issues
show
Documentation introduced by
$key is of type array<?,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
		$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
		$this->fail('Where is this used?');
37
	}
38
39
	public function test_get_set_map_already_rendered() {
40
		MapUtil::set_map_already_rendered(false);
41
		$this->assertFalse(MapUtil::get_map_already_rendered());
42
		MapUtil::set_map_already_rendered(true);
43
		$this->assertTrue(MapUtil::get_map_already_rendered());
44
	}
45
46
	public function test_set_map_size() {
47
		MapUtil::set_map_size('890px', '24em');
48
		$html = $this->htmlForMap();
49
		$this->assertContains(' style="width:890px; height: 24em;"', $html);
50
	}
51
52
	public function testSanitizeEmptyString() {
53
		$this->assertEquals(
54
			'',
55
			MapUtil::sanitize('')
56
		);
57
	}
58
59
	public function testSanitizeAlreadySanitized() {
60
		$this->assertEquals(
61
			'This is already sanitized',
62
			MapUtil::sanitize('This is already sanitized')
63
		);
64
	}
65
66
	public function testSanitizeSlashN() {
67
		$this->assertEquals(
68
			'String to be sanitized',
69
			MapUtil::sanitize("String\n to be sanitized")
70
		);
71
	}
72
73
	public function testSanitizeSlashT() {
74
		$this->assertEquals(
75
			'String to be sanitized',
76
			MapUtil::sanitize("String\t to be sanitized")
77
		);
78
	}
79
80
	public function testSanitizeSlashR() {
81
		$this->assertEquals(
82
			'String to be sanitized',
83
			MapUtil::sanitize("String\r to be sanitized")
84
		);
85
	}
86
87
	/**
88
	 * A single marker for the Member should appear in the UJS map data
89
	 */
90
	public function testSingularMappableItemMarkerUJSExists() {
91
		Member::add_extension('MapExtension');
92
		$member = new Member();
93
		$member->Lat = 12.847;
94
		$member->Lon = 29.24;
95
96
		// because we are not writing, set this manually
97
		$member->MapPinEdited = true;
98
		$list = new ArrayList();
99
		$list->push($member);
100
		$map = MapUtil::get_map($list, array());
101
		$html = $map->forTemplate();
102
		$markerExpected = 'data-mapmarkers=\'[{"latitude":12.847,"longitude":29.24,"html":"MEMBER: ","category":"default","icon":false}]\'';
103
		$this->assertContains($markerExpected, $html);
104
		Member::remove_extension('MapExtension');
105
	}
106
107
	private function htmlForMap() {
108
		$map = MapUtil::get_map(new ArrayList(), array());
109
		$html = $map->forTemplate();
110
		return $html;
111
	}
112
113
114
	// These appear to test code that's not used
115
	public function test_set_center() {
116
		MapUtil::set_center('Klong Tan, Bangkok, Thailand');
117
		$html = $this->htmlForMap();
118
		//coordinates of Klong Tan in Bangkok
119
		$expected = 'data-centre=\'{"lat":13.7243075,"lng":100.5718086}';
120
		$this->assertContains($expected, $html);
121
	}
122
123
	 public function test_set_map_type() {
124
		MapUtil::set_map_type('google.maps.MapTypeId.G_PHYSICAL_MAP');
125
		$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
126
		$this->fail('No evidence of map type changing');
127
	}
128
129
	 public function test_set_info_window_width() {
130
	 	MapUtil::set_info_window_width(420);
131
	 	echo $html;
0 ignored issues
show
Bug introduced by
The variable $html does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
132
	 	$this->fail('No evidence of set info width being used');
133
	}
134
135
	 public function test_set_icon_size() {
136
	 	MapUtil::set_icon_size(14, 37);
137
	 	$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
	 	$html = $this->htmlForMap();
139
	 	echo $html;
140
	}
141
142
}
143