GeocoderBehaviorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. App::uses('GeocoderBehavior', 'Tools.Model/Behavior');
  3. App::uses('Set', 'Utility');
  4. App::uses('AppModel', 'Model');
  5. App::uses('AppController', 'Controller');
  6. class GeocoderBehaviorTest extends CakeTestCase {
  7. public $fixtures = array(
  8. 'core.comment', 'plugin.tools.address', 'core.cake_session'
  9. );
  10. public function setUp() {
  11. parent::setUp();
  12. $this->Comment = ClassRegistry::init('Comment');
  13. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false));
  14. }
  15. /**
  16. * GeocoderBehaviorTest::testDistance()
  17. *
  18. * @return void
  19. */
  20. public function testDistance() {
  21. $res = $this->Comment->distance(12, 14);
  22. $expected = '6371.04 * ACOS(COS(PI()/2 - RADIANS(90 - Comment.lat)) * COS(PI()/2 - RADIANS(90 - 12)) * COS(RADIANS(Comment.lng) - RADIANS(14)) + SIN(PI()/2 - RADIANS(90 - Comment.lat)) * SIN(PI()/2 - RADIANS(90 - 12)))';
  23. $this->assertEquals($expected, $res);
  24. $this->Comment->Behaviors->unload('Geocoder');
  25. $this->Comment->Behaviors->load('Tools.Geocoder', array('lat' => 'x', 'lng' => 'y'));
  26. $res = $this->Comment->distance(12.1, 14.2);
  27. $expected = '6371.04 * ACOS(COS(PI()/2 - RADIANS(90 - Comment.x)) * COS(PI()/2 - RADIANS(90 - 12.1)) * COS(RADIANS(Comment.y) - RADIANS(14.2)) + SIN(PI()/2 - RADIANS(90 - Comment.x)) * SIN(PI()/2 - RADIANS(90 - 12.1)))';
  28. $this->assertEquals($expected, $res);
  29. $this->Comment->Behaviors->unload('Geocoder');
  30. $this->Comment->Behaviors->load('Tools.Geocoder', array('lat' => 'x', 'lng' => 'y'));
  31. $res = $this->Comment->distance('User.lat', 'User.lng');
  32. $expected = '6371.04 * ACOS(COS(PI()/2 - RADIANS(90 - Comment.x)) * COS(PI()/2 - RADIANS(90 - User.lat)) * COS(RADIANS(Comment.y) - RADIANS(User.lng)) + SIN(PI()/2 - RADIANS(90 - Comment.x)) * SIN(PI()/2 - RADIANS(90 - User.lat)))';
  33. $this->assertEquals($expected, $res);
  34. }
  35. /**
  36. * GeocoderBehaviorTest::testDistanceField()
  37. *
  38. * @return void
  39. */
  40. public function testDistanceField() {
  41. $res = $this->Comment->distanceField(12, 14);
  42. $expected = '6371.04 * ACOS(COS(PI()/2 - RADIANS(90 - Comment.lat)) * COS(PI()/2 - RADIANS(90 - 12)) * COS(RADIANS(Comment.lng) - RADIANS(14)) + SIN(PI()/2 - RADIANS(90 - Comment.lat)) * SIN(PI()/2 - RADIANS(90 - 12))) AS Comment.distance';
  43. $this->assertEquals($expected, $res);
  44. }
  45. /**
  46. * GeocoderBehaviorTest::testSetDistanceAsVirtualField()
  47. *
  48. * @return void
  49. */
  50. public function testSetDistanceAsVirtualField() {
  51. $this->Address = ClassRegistry::init('Address');
  52. $this->Address->Behaviors->load('Tools.Geocoder');
  53. $this->Address->setDistanceAsVirtualField(13.3, 19.2);
  54. $options = array('order' => array('Address.distance' => 'ASC'));
  55. $res = $this->Address->find('all', $options);
  56. $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
  57. $this->assertTrue($res[1]['Address']['distance'] < $res[2]['Address']['distance']);
  58. $this->assertTrue($res[0]['Address']['distance'] > 640 && $res[0]['Address']['distance'] < 650);
  59. }
  60. /**
  61. * GeocoderBehaviorTest::testSetDistanceAsVirtualFieldInMiles()
  62. *
  63. * @return void
  64. */
  65. public function testSetDistanceAsVirtualFieldInMiles() {
  66. $this->Address = ClassRegistry::init('Address');
  67. $this->Address->Behaviors->load('Tools.Geocoder', array('unit' => GeocodeLib::UNIT_MILES));
  68. $this->Address->setDistanceAsVirtualField(13.3, 19.2);
  69. $options = array('order' => array('Address.distance' => 'ASC'));
  70. $res = $this->Address->find('all', $options);
  71. $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
  72. $this->assertTrue($res[1]['Address']['distance'] < $res[2]['Address']['distance']);
  73. $this->assertTrue($res[0]['Address']['distance'] > 390 && $res[0]['Address']['distance'] < 410);
  74. }
  75. /**
  76. * GeocoderBehaviorTest::testPagination()
  77. *
  78. * @return void
  79. */
  80. public function testPagination() {
  81. $this->Controller = new TestController(new CakeRequest(null, false), null);
  82. $this->Controller->constructClasses();
  83. $this->Controller->Address->Behaviors->load('Tools.Geocoder');
  84. $this->Controller->Address->setDistanceAsVirtualField(13.3, 19.2);
  85. $this->Controller->paginate = array(
  86. 'conditions' => array('distance <' => 3000),
  87. 'order' => array('distance' => 'ASC')
  88. );
  89. $res = $this->Controller->paginate();
  90. $this->assertEquals(2, count($res));
  91. $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
  92. }
  93. /**
  94. * GeocoderBehaviorTest::testValidate()
  95. *
  96. * @return void
  97. */
  98. public function testValidate() {
  99. $is = $this->Comment->validateLatitude(44);
  100. $this->assertTrue($is);
  101. $is = $this->Comment->validateLatitude(110);
  102. $this->assertFalse($is);
  103. $is = $this->Comment->validateLongitude(150);
  104. $this->assertTrue($is);
  105. $is = $this->Comment->validateLongitude(-190);
  106. $this->assertFalse($is);
  107. $this->Comment->validator()->add('lat', 'validateLatitude', array('rule' => 'validateLatitude', 'message' => 'validateLatitudeError'));
  108. $this->Comment->validator()->add('lng', 'validateLongitude', array('rule' => 'validateLongitude', 'message' => 'validateLongitudeError'));
  109. $data = array(
  110. 'lat' => 44,
  111. 'lng' => 190,
  112. );
  113. $this->Comment->set($data);
  114. $res = $this->Comment->validates();
  115. $this->assertFalse($res);
  116. $expectedErrors = array(
  117. 'lng' => array(__('validateLongitudeError'))
  118. );
  119. $this->assertEquals($expectedErrors, $this->Comment->validationErrors);
  120. }
  121. /**
  122. * Geocoding tests using the google webservice
  123. *
  124. * @return void
  125. */
  126. public function testBasic() {
  127. //echo '<h3>'.__FUNCTION__.'</h3>';
  128. $data = array(
  129. 'street' => 'Krebenweg 22',
  130. 'zip' => '74523',
  131. 'city' => 'Bibersfeld'
  132. );
  133. $res = $this->Comment->save($data);
  134. //debug($res);
  135. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']) && round($res['Comment']['lat']) === 49.0 && round($res['Comment']['lng']) === 10.0);
  136. // accuracy = 4
  137. // inconclusive
  138. $data = array(
  139. //'street' => 'Leopoldstraße',
  140. 'city' => 'München'
  141. );
  142. $res = $this->Comment->save($data);
  143. $this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
  144. //debug($res);
  145. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  146. $this->assertEquals('München, Deutschland', $res['Comment']['geocoder_result']['formatted_address']);
  147. $data = array(
  148. 'city' => 'Bibersfeld'
  149. );
  150. $res = $this->Comment->save($data);
  151. //debug($res);
  152. $this->assertTrue(!empty($res));
  153. $this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
  154. }
  155. /**
  156. * GeocoderBehaviorTest::testMinAccLow()
  157. *
  158. * @return void
  159. */
  160. public function testMinAccLow() {
  161. //echo '<h3>'.__FUNCTION__.'</h3>';
  162. $this->Comment->Behaviors->unload('Geocoder');
  163. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'min_accuracy' => 0));
  164. // accuracy = 1
  165. $data = array(
  166. //'street' => 'Leopoldstraße',
  167. 'city' => 'Deutschland'
  168. );
  169. $res = $this->Comment->save($data);
  170. //debug($this->Comment->Behaviors->Geocoder->Geocode->error());
  171. //debug($res);
  172. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  173. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  174. }
  175. /**
  176. * GeocoderBehaviorTest::testMinAccHigh()
  177. *
  178. * @return void
  179. */
  180. public function testMinAccHigh() {
  181. //echo '<h3>'.__FUNCTION__.'</h3>';
  182. $this->Comment->Behaviors->unload('Geocoder');
  183. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'min_accuracy' => 4));
  184. // accuracy = 1
  185. $data = array(
  186. //'street' => 'Leopoldstraße',
  187. 'city' => 'Deutschland'
  188. );
  189. $res = $this->Comment->save($data);
  190. //debug($this->Comment->Behaviors->Geocoder->Geocode->error());
  191. //debug($res);
  192. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  193. $this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
  194. }
  195. /**
  196. * GeocoderBehaviorTest::testMinInc()
  197. *
  198. * @return void
  199. */
  200. public function testMinInc() {
  201. //echo '<h3>'.__FUNCTION__.'</h3>';
  202. $this->Comment->Behaviors->unload('Geocoder');
  203. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'min_accuracy' => GeocodeLib::ACC_SUBLOC));
  204. $this->assertEquals(GeocodeLib::ACC_SUBLOC, $this->Comment->Behaviors->Geocoder->settings['Comment']['min_accuracy']);
  205. // accuracy = 1
  206. $data = array(
  207. //'street' => 'Leopoldstraße',
  208. 'city' => 'Neustadt'
  209. );
  210. $res = $this->Comment->save($data);
  211. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  212. //debug($this->Comment->Behaviors->Geocoder->Geocode->getResult()).BR;
  213. //debug($res);
  214. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  215. $this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
  216. }
  217. /**
  218. * GeocoderBehaviorTest::testMinIncAllowed()
  219. *
  220. * @return void
  221. */
  222. public function testMinIncAllowed() {
  223. //echo '<h3>'.__FUNCTION__.'</h3>';
  224. $this->Comment->Behaviors->unload('Geocoder');
  225. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'allow_inconclusive' => true));
  226. // accuracy = 1
  227. $data = array(
  228. //'street' => 'Leopoldstraße',
  229. 'city' => 'Neustadt'
  230. );
  231. $res = $this->Comment->save($data);
  232. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  233. //debug($this->Comment->Behaviors->Geocoder->Geocode->url()).BR;
  234. //debug($this->Comment->Behaviors->Geocoder->Geocode->getResult()).BR;
  235. //debug($res);
  236. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  237. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  238. }
  239. /**
  240. * GeocoderBehaviorTest::testExpect()
  241. *
  242. * @return void
  243. */
  244. public function testExpect() {
  245. $this->Comment->Behaviors->unload('Geocoder');
  246. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'expect' => array('postal_code')));
  247. // accuracy = 1
  248. $data = array(
  249. //'street' => 'Leopoldstraße',
  250. 'city' => 'Bibersfeld'
  251. );
  252. $res = $this->Comment->save($data);
  253. //debug($this->Comment->Behaviors->Geocoder->Geocode->error());
  254. //debug($res);
  255. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  256. $this->assertTrue(empty($res['Comment']['lat']) && empty($res['Comment']['lng']));
  257. $data = array(
  258. //'street' => 'Leopoldstraße',
  259. 'city' => '74523'
  260. );
  261. $res = $this->Comment->save($data);
  262. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  263. //debug($res);
  264. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  265. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  266. }
  267. }
  268. class TestController extends AppController {
  269. public $uses = array('Address');
  270. }