GoogleMapV3HelperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. App::uses('GoogleMapV3Helper', 'Tools.View/Helper');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. App::uses('View', 'View');
  5. class GoogleMapV3HelperTest extends MyCakeTestCase {
  6. public function setUp() {
  7. parent::setUp();
  8. Configure::delete('Google');
  9. $this->GoogleMapV3 = new GoogleMapV3Helper(new View(null));
  10. }
  11. public function testObject() {
  12. $this->assertInstanceOf('GoogleMapV3Helper', $this->GoogleMapV3);
  13. }
  14. /**
  15. * GoogleMapV3HelperTest::testConfigMerge()
  16. *
  17. * @return void
  18. */
  19. public function testConfigMerge() {
  20. $config = [
  21. 'map' => [
  22. 'type' => 'foo',
  23. ]
  24. ];
  25. Configure::write('Google.zoom', 8);
  26. $this->GoogleMapV3 = new GoogleMapV3Helper(new View(null), $config);
  27. $result = $this->GoogleMapV3->settings;
  28. $this->assertEquals('foo', $result['map']['type']);
  29. $this->assertEquals(8, $result['map']['zoom']);
  30. }
  31. /**
  32. * GoogleMapV3HelperTest::testMapUrl()
  33. *
  34. * @return void
  35. */
  36. public function testMapUrl() {
  37. $url = $this->GoogleMapV3->mapUrl(['to' => 'Munich, Germany']);
  38. $this->assertEquals('http://maps.google.com/maps?daddr=Munich%2C+Germany', $url);
  39. $url = $this->GoogleMapV3->mapUrl(['to' => '<München>, Germany']);
  40. $this->assertEquals('http://maps.google.com/maps?daddr=%3CM%C3%BCnchen%3E%2C+Germany', $url);
  41. }
  42. /**
  43. * GoogleMapV3HelperTest::testMapLink()
  44. *
  45. * @return void
  46. */
  47. public function testMapLink() {
  48. $result = $this->GoogleMapV3->mapLink('<To Munich>!', ['to' => '<Munich>, Germany']);
  49. $expected = '<a href="http://maps.google.com/maps?daddr=%3CMunich%3E%2C+Germany">&lt;To Munich&gt;!</a>';
  50. //echo $result;
  51. $this->assertEquals($expected, $result);
  52. }
  53. /**
  54. * GoogleMapV3HelperTest::testLinkWithMapUrl()
  55. *
  56. * @return void
  57. */
  58. public function testLinkWithMapUrl() {
  59. $url = $this->GoogleMapV3->mapUrl(['to' => '<München>, Germany']);
  60. $result = $this->GoogleMapV3->Html->link('Some title', $url);
  61. $expected = '<a href="http://maps.google.com/maps?daddr=%3CM%C3%BCnchen%3E%2C+Germany">Some title</a>';
  62. //echo $result;
  63. $this->assertEquals($expected, $result);
  64. }
  65. /**
  66. * GoogleMapV3HelperTest::testStaticPaths()
  67. *
  68. * @return void
  69. */
  70. public function testStaticPaths() {
  71. $m = $this->pathElements = [
  72. [
  73. 'path' => ['Berlin', 'Stuttgart'],
  74. 'color' => 'green',
  75. ],
  76. [
  77. 'path' => ['44.2,11.1', '43.1,12.2', '44.3,11.3', '43.3,12.3'],
  78. ],
  79. [
  80. 'path' => [['lat' => '48.1', 'lng' => '11.1'], ['lat' => '48.4', 'lng' => '11.2']], //'Frankfurt'
  81. 'color' => 'red',
  82. 'weight' => 10
  83. ]
  84. ];
  85. $is = $this->GoogleMapV3->staticPaths($m);
  86. //echo pr(h($is));
  87. $options = [
  88. 'paths' => $is
  89. ];
  90. $is = $this->GoogleMapV3->staticMapLink('My Title', $options);
  91. //echo h($is).BR.BR;
  92. $is = $this->GoogleMapV3->staticMap($options);
  93. //echo $is;
  94. }
  95. /**
  96. * GoogleMapV3HelperTest::testStaticMarkers()
  97. *
  98. * @return void
  99. */
  100. public function testStaticMarkers() {
  101. $m = $this->markerElements = [
  102. [
  103. 'address' => '44.3,11.2',
  104. ],
  105. [
  106. 'address' => '44.2,11.1',
  107. ]
  108. ];
  109. $is = $this->GoogleMapV3->staticMarkers($m, ['color' => 'red', 'char' => 'C', 'shadow' => 'false']);
  110. //debug($is);
  111. $options = [
  112. 'markers' => $is
  113. ];
  114. $is = $this->GoogleMapV3->staticMap($options);
  115. //debug($is);
  116. //echo $is;
  117. }
  118. // http://maps.google.com/staticmap?size=500x500&maptype=hybrid&markers=color:red|label:S|48.3,11.2&sensor=false
  119. // http://maps.google.com/maps/api/staticmap?size=512x512&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false
  120. /**
  121. * GoogleMapV3HelperTest::testStatic()
  122. *
  123. * @return void
  124. */
  125. public function testStatic() {
  126. //echo '<h2>StaticMap</h2>';
  127. $m = [
  128. [
  129. 'address' => 'Berlin',
  130. 'color' => 'yellow',
  131. 'char' => 'Z',
  132. 'shadow' => 'true'
  133. ],
  134. [
  135. 'lat' => '44.2',
  136. 'lng' => '11.1',
  137. 'color' => '#0000FF',
  138. 'char' => '1',
  139. 'shadow' => 'false'
  140. ]
  141. ];
  142. $options = [
  143. 'markers' => $this->GoogleMapV3->staticMarkers($m)
  144. ];
  145. //debug($options['markers']).BR;
  146. $is = $this->GoogleMapV3->staticMapUrl($options);
  147. //echo h($is);
  148. //echo BR.BR;
  149. $is = $this->GoogleMapV3->staticMapLink('MyLink', $options);
  150. //echo h($is);
  151. //echo BR.BR;
  152. $is = $this->GoogleMapV3->staticMap($options);
  153. //echo h($is).BR;
  154. //echo $is;
  155. //echo BR.BR;
  156. $options = [
  157. 'size' => '200x100',
  158. 'center' => true
  159. ];
  160. $is = $this->GoogleMapV3->staticMapLink('MyTitle', $options);
  161. //echo h($is);
  162. //echo BR.BR;
  163. $attr = [
  164. 'title' => '<b>Yeah!</b>'
  165. ];
  166. $is = $this->GoogleMapV3->staticMap($options, $attr);
  167. //echo h($is).BR;
  168. //echo $is;
  169. //echo BR.BR;
  170. $pos = [
  171. ['lat' => 48.1, 'lng' => '11.1'],
  172. ['lat' => 48.2, 'lng' => '11.2'],
  173. ];
  174. $options = [
  175. 'markers' => $this->GoogleMapV3->staticMarkers($pos)
  176. ];
  177. $attr = ['url' => $this->GoogleMapV3->mapUrl(['to' => 'Munich, Germany'])];
  178. $is = $this->GoogleMapV3->staticMap($options, $attr);
  179. //echo h($is).BR;
  180. //echo $is;
  181. //echo BR.BR.BR;
  182. $url = $this->GoogleMapV3->mapUrl(['to' => 'Munich, Germany']);
  183. $attr = [
  184. 'title' => 'Yeah'
  185. ];
  186. $image = $this->GoogleMapV3->staticMap($options, $attr);
  187. $link = $this->GoogleMapV3->Html->link($image, $url, ['escape' => false, 'target' => '_blank']);
  188. //echo h($link).BR;
  189. //echo $link;
  190. }
  191. /**
  192. * GoogleMapV3HelperTest::testStaticMapWithStaticMapLink()
  193. *
  194. * @return void
  195. */
  196. public function testStaticMapWithStaticMapLink() {
  197. //echo '<h2>testStaticMapWithStaticMapLink</h2>';
  198. $markers = [];
  199. $markers[] = ['lat' => 48.2, 'lng' => 11.1, 'color' => 'red'];
  200. $mapMarkers = $this->GoogleMapV3->staticMarkers($markers);
  201. $staticMapUrl = $this->GoogleMapV3->staticMapUrl(['center' => 48 . ',' . 11, 'markers' => $mapMarkers, 'size' => '640x510', 'zoom' => 6]);
  202. //echo $this->GoogleMapV3->Html->link('Open Static Map', $staticMapUrl, array('class'=>'staticMap', 'title'=>__d('tools', 'click for full map'))); //, 'escape'=>false
  203. }
  204. /**
  205. * GoogleMapV3HelperTest::testMarkerIcons()
  206. *
  207. * @return void
  208. */
  209. public function testMarkerIcons() {
  210. $tests = [
  211. ['green', null],
  212. ['black', null],
  213. ['purple', 'E'],
  214. ['', 'Z'],
  215. ];
  216. foreach ($tests as $test) {
  217. $is = $this->GoogleMapV3->iconSet($test[0], $test[1]);
  218. //echo $this->GoogleMapV3->Html->image($is['url']).BR;
  219. }
  220. }
  221. /**
  222. * Test some basic map options
  223. */
  224. public function testMap() {
  225. $options = [
  226. 'autoScript' => true,
  227. 'inline' => true,
  228. ];
  229. $result = $this->GoogleMapV3->map($options);
  230. $result .= $this->GoogleMapV3->script();
  231. $expected = '<div id="map_canvas" class="map"';
  232. $this->assertTextContains($expected, $result);
  233. $expected = '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false';
  234. $this->assertTextContains($expected, $result);
  235. $expected = 'var map0 = new google.maps.Map(document.getElementById("map_canvas"), myOptions);';
  236. $this->assertTextContains($expected, $result);
  237. }
  238. /**
  239. * GoogleMapV3HelperTest::testMarker() and draggable property
  240. *
  241. * @return void
  242. */
  243. public function testMarker() {
  244. $this->GoogleMapV3->map();
  245. $this->GoogleMapV3->addMarker([
  246. 'lat' => 48.69847, 'lng' => 10.9514,
  247. 'title' => 'Marker', 'content' => 'Some Html-<b>Content</b>',
  248. 'draggable' => true]);
  249. $result = $this->GoogleMapV3->script();
  250. $this->assertContains('draggable: true,', $result);
  251. $this->GoogleMapV3->map(['marker' => ['draggable' => true]]);
  252. $this->GoogleMapV3->addMarker([
  253. 'lat' => 48.69847, 'lng' => 10.9514,
  254. 'title' => 'Marker', 'content' => 'Some Html-<b>Content</b>']);
  255. $result = $this->GoogleMapV3->script();
  256. $this->assertContains('draggable: true,', $result);
  257. $this->GoogleMapV3->map(['marker' => ['draggable' => true]]);
  258. $this->GoogleMapV3->addMarker([
  259. 'lat' => 48.69847, 'lng' => 10.9514,
  260. 'title' => 'Marker', 'content' => 'Some Html-<b>Content</b>',
  261. 'draggable' => false]);
  262. $result = $this->GoogleMapV3->script();
  263. $this->assertNotContains('draggable: true,', $result);
  264. }
  265. /**
  266. * With default options
  267. */
  268. public function testDynamic() {
  269. //echo '<h2>Map 1</h2>';
  270. //echo '<script type="text/javascript" src="http://ajax.google.com/ajax/libs/jquery/1/jquery.min.js"></script>';
  271. //echo $this->GoogleMapV3->map($defaul, array('style'=>'width:100%; height: 800px'));
  272. //echo '<script type="text/javascript" src="'.$this->GoogleMapV3->apiUrl().'"></script>';
  273. //echo '<script type="text/javascript" src="'.$this->GoogleMapV3->gearsUrl().'"></script>';
  274. $options = [
  275. 'zoom' => 6,
  276. 'type' => 'R',
  277. 'geolocate' => true,
  278. 'div' => ['id' => 'someothers'],
  279. 'map' => ['navOptions' => ['style' => 'SMALL'], 'typeOptions' => ['style' => 'HORIZONTAL_BAR', 'pos' => 'RIGHT_CENTER']]
  280. ];
  281. $result = $this->GoogleMapV3->map($options);
  282. $this->GoogleMapV3->addMarker(['lat' => 48.69847, 'lng' => 10.9514, 'title' => 'Marker', 'content' => 'Some Html-<b>Content</b>', 'icon' => $this->GoogleMapV3->iconSet('green', 'E')]);
  283. $this->GoogleMapV3->addMarker(['lat' => 47.69847, 'lng' => 11.9514, 'title' => 'Marker2', 'content' => 'Some more Html-<b>Content</b>']);
  284. $this->GoogleMapV3->addMarker(['lat' => 47.19847, 'lng' => 11.1514, 'title' => 'Marker3']);
  285. /*
  286. $options = array(
  287. 'lat'=>48.15144,
  288. 'lng'=>10.198,
  289. 'content'=>'Thanks for using this'
  290. );
  291. $this->GoogleMapV3->addInfoWindow($options);
  292. //$this->GoogleMapV3->addEvent();
  293. */
  294. $result .= $this->GoogleMapV3->script();
  295. //echo $result;
  296. }
  297. /**
  298. * More than 100 markers and it gets reaaally slow...
  299. */
  300. public function testDynamic2() {
  301. //echo '<h2>Map 2</h2>';
  302. $options = [
  303. 'zoom' => 6, 'type' => 'H',
  304. 'autoCenter' => true,
  305. 'div' => ['id' => 'someother'], //'height'=>'111',
  306. 'map' => ['typeOptions' => ['style' => 'DROPDOWN_MENU']]
  307. ];
  308. //echo $this->GoogleMapV3->map($options);
  309. $this->GoogleMapV3->addMarker(['lat' => 47.69847, 'lng' => 11.9514, 'title' => 'MarkerMUC', 'content' => 'Some more Html-<b>Content</b>']);
  310. for ($i = 0; $i < 100; $i++) {
  311. $lat = mt_rand(46000, 54000) / 1000;
  312. $lng = mt_rand(2000, 20000) / 1000;
  313. $this->GoogleMapV3->addMarker(['id' => 'm' . ($i + 1), 'lat' => $lat, 'lng' => $lng, 'title' => 'Marker' . ($i + 1), 'content' => 'Lat: <b>' . $lat . '</b><br>Lng: <b>' . $lng . '</b>', 'icon' => 'http://google-maps-icons.googlecode.com/files/home.png']);
  314. }
  315. $js = "$('.mapAnchor').live('click', function() {
  316. var id = $(this).attr('rel');
  317. var match = matching[id];
  318. /*
  319. map.panTo(mapPoints[match]);
  320. mapMarkers[match].openInfoWindowHtml(mapWindows[match]);
  321. */
  322. gInfoWindows1[0].setContent(gWindowContents1[match]);
  323. gInfoWindows1[0].open(map1, gMarkers1[match]);
  324. });";
  325. $this->GoogleMapV3->addCustom($js);
  326. //echo $this->GoogleMapV3->script();
  327. //echo '<a href="javascript:void(0)" class="mapAnchor" rel="m2">Marker2</a> ';
  328. //echo '<a href="javascript:void(0)" class="mapAnchor" rel="m3">Marker3</a>';
  329. }
  330. public function testDynamic3() {
  331. //echo '<h2>Map with Directions</h2>';
  332. $options = [
  333. 'zoom' => 5,
  334. 'type' => 'H',
  335. 'map' => []
  336. ];
  337. //echo $this->GoogleMapV3->map($options);
  338. $this->GoogleMapV3->addMarker(['lat' => 48.69847, 'lng' => 10.9514, 'content' => '<b>Bla</b>', 'title' => 'NoDirections']);
  339. $this->GoogleMapV3->addMarker(['lat' => 47.69847, 'lng' => 11.9514, 'title' => 'AutoToDirections', 'content' => '<b>Bla</b>', 'directions' => true]);
  340. $this->GoogleMapV3->addMarker(['lat' => 46.69847, 'lng' => 11.9514, 'title' => 'ManuelToDirections', 'content' => '<b>Bla</b>', 'directions' => ['to' => 'Munich, Germany']]);
  341. $this->GoogleMapV3->addMarker(['lat' => 45.69847, 'lng' => 11.9514, 'title' => 'ManuelFromDirections', 'content' => '<b>Bla</b>', 'directions' => ['from' => 'Munich, Germany']]);
  342. //echo $this->GoogleMapV3->script();
  343. }
  344. }