GeocoderBehaviorTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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'
  9. );
  10. public function setUp() {
  11. $this->Comment = ClassRegistry::init('Comment');
  12. $this->Comment->Behaviors->load('Tools.Geocoder', array('real'=>false));
  13. }
  14. public function testDistance() {
  15. $res = $this->Comment->distance(12, 14);
  16. $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)))';
  17. $this->assertEquals($expected, $res);
  18. $this->Comment->Behaviors->unload('Geocoder');
  19. $this->Comment->Behaviors->load('Tools.Geocoder', array('lat'=>'x', 'lng'=>'y'));
  20. $res = $this->Comment->distance(12.1, 14.2);
  21. $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)))';
  22. $this->assertEquals($expected, $res);
  23. $this->Comment->Behaviors->unload('Geocoder');
  24. $this->Comment->Behaviors->load('Tools.Geocoder', array('lat'=>'x', 'lng'=>'y'));
  25. $res = $this->Comment->distance('User.lat', 'User.lng');
  26. $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)))';
  27. $this->assertEquals($expected, $res);
  28. }
  29. public function testDistanceField() {
  30. $res = $this->Comment->distanceField(12, 14);
  31. $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';
  32. $this->assertEquals($expected, $res);
  33. }
  34. public function testSetDistanceAsVirtualField() {
  35. $this->Address = ClassRegistry::init('Address');
  36. $this->Address->Behaviors->load('Tools.Geocoder');
  37. $this->Address->setDistanceAsVirtualField(13.3, 19.2);
  38. $options = array('order' => array('Address.distance' => 'ASC'));
  39. $res = $this->Address->find('all', $options);
  40. $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
  41. $this->assertTrue($res[1]['Address']['distance'] < $res[2]['Address']['distance']);
  42. $this->assertTrue($res[0]['Address']['distance'] > 640 && $res[0]['Address']['distance'] < 650);
  43. }
  44. public function testSetDistanceAsVirtualFieldInMiles() {
  45. $this->Address = ClassRegistry::init('Address');
  46. $this->Address->Behaviors->load('Tools.Geocoder', array('unit' => GeocodeLib::UNIT_MILES));
  47. $this->Address->setDistanceAsVirtualField(13.3, 19.2);
  48. $options = array('order' => array('Address.distance' => 'ASC'));
  49. $res = $this->Address->find('all', $options);
  50. $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
  51. $this->assertTrue($res[1]['Address']['distance'] < $res[2]['Address']['distance']);
  52. $this->assertTrue($res[0]['Address']['distance'] > 390 && $res[0]['Address']['distance'] < 410);
  53. }
  54. public function testPagination() {
  55. $this->Controller = new TestController(new CakeRequest(null, false), null);
  56. $this->Controller->constructClasses();
  57. $this->Controller->Address->Behaviors->load('Tools.Geocoder');
  58. $this->Controller->Address->setDistanceAsVirtualField(13.3, 19.2);
  59. $this->Controller->paginate = array(
  60. 'conditions'=>array('distance <' => 3000),
  61. 'order' => array('distance' => 'ASC')
  62. );
  63. $res = $this->Controller->paginate();
  64. $this->assertEquals(2, count($res));
  65. $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
  66. }
  67. public function testValidate() {
  68. $is = $this->Comment->validateLatitude(44);
  69. $this->assertTrue($is);
  70. $is = $this->Comment->validateLatitude(110);
  71. $this->assertFalse($is);
  72. $is = $this->Comment->validateLongitude(150);
  73. $this->assertTrue($is);
  74. $is = $this->Comment->validateLongitude(-190);
  75. $this->assertFalse($is);
  76. $this->Comment->validator()->add('lat', 'validateLatitude', array('rule'=>'validateLatitude', 'message'=>'validateLatitudeError'));
  77. $this->Comment->validator()->add('lng', 'validateLongitude', array('rule'=>'validateLongitude', 'message'=>'validateLongitudeError'));
  78. $data = array(
  79. 'lat' => 44,
  80. 'lng' => 190,
  81. );
  82. $this->Comment->set($data);
  83. $res = $this->Comment->validates();
  84. $this->assertFalse($res);
  85. $expectedErrors = array(
  86. 'lng' => array(__('validateLongitudeError'))
  87. );
  88. $this->assertEquals($expectedErrors, $this->Comment->validationErrors);
  89. }
  90. /**
  91. * geocoding tests using the google webservice
  92. */
  93. public function testBasic() {
  94. //echo '<h3>'.__FUNCTION__.'</h3>';
  95. $data = array(
  96. 'street' => 'Krebenweg 22',
  97. 'zip' => '74523',
  98. 'city' => 'Bibersfeld'
  99. );
  100. $res = $this->Comment->save($data);
  101. //debug($res);
  102. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']) && round($res['Comment']['lat']) === 49.0 && round($res['Comment']['lng']) === 10.0);
  103. // accuracy = 4
  104. # inconclusive
  105. $data = array(
  106. //'street' => 'Leopoldstraße',
  107. 'city' => 'München'
  108. );
  109. $res = $this->Comment->save($data);
  110. $this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
  111. //debug($res);
  112. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  113. $this->assertEquals('München, Deutschland', $res['Comment']['geocoder_result']['formatted_address']);
  114. $data = array(
  115. 'city' => 'Bibersfeld'
  116. );
  117. $res = $this->Comment->save($data);
  118. //debug($res);
  119. $this->assertTrue(!empty($res));
  120. $this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
  121. }
  122. public function testMinAccLow() {
  123. //echo '<h3>'.__FUNCTION__.'</h3>';
  124. $this->Comment->Behaviors->unload('Geocoder');
  125. $this->Comment->Behaviors->load('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>0));
  126. // accuracy = 1
  127. $data = array(
  128. //'street' => 'Leopoldstraße',
  129. 'city' => 'Deutschland'
  130. );
  131. $res = $this->Comment->save($data);
  132. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  133. //debug($res);
  134. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  135. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  136. }
  137. public function testMinAccHigh() {
  138. //echo '<h3>'.__FUNCTION__.'</h3>';
  139. $this->Comment->Behaviors->unload('Geocoder');
  140. $this->Comment->Behaviors->load('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>4));
  141. // accuracy = 1
  142. $data = array(
  143. //'street' => 'Leopoldstraße',
  144. 'city' => 'Deutschland'
  145. );
  146. $res = $this->Comment->save($data);
  147. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  148. //debug($res);
  149. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  150. $this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
  151. }
  152. public function testMinInc() {
  153. //echo '<h3>'.__FUNCTION__.'</h3>';
  154. $this->Comment->Behaviors->unload('Geocoder');
  155. $this->Comment->Behaviors->load('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>GeocodeLib::ACC_SUBLOC));
  156. $this->assertEquals(GeocodeLib::ACC_SUBLOC, $this->Comment->Behaviors->Geocoder->settings['Comment']['min_accuracy']);
  157. // accuracy = 1
  158. $data = array(
  159. //'street' => 'Leopoldstraße',
  160. 'city' => 'Neustadt'
  161. );
  162. $res = $this->Comment->save($data);
  163. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  164. //debug($this->Comment->Behaviors->Geocoder->Geocode->getResult()).BR;
  165. //debug($res);
  166. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  167. $this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
  168. }
  169. public function testMinIncAllowed() {
  170. //echo '<h3>'.__FUNCTION__.'</h3>';
  171. $this->Comment->Behaviors->unload('Geocoder');
  172. $this->Comment->Behaviors->load('Tools.Geocoder', array('real'=>false, 'allow_inconclusive'=>true));
  173. // accuracy = 1
  174. $data = array(
  175. //'street' => 'Leopoldstraße',
  176. 'city' => 'Neustadt'
  177. );
  178. $res = $this->Comment->save($data);
  179. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  180. //debug($this->Comment->Behaviors->Geocoder->Geocode->url()).BR;
  181. //debug($this->Comment->Behaviors->Geocoder->Geocode->getResult()).BR;
  182. //debug($res);
  183. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  184. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  185. }
  186. public function testExpect() {
  187. $this->Comment->Behaviors->unload('Geocoder');
  188. $this->Comment->Behaviors->load('Tools.Geocoder', array('real'=>false, 'expect'=>array('postal_code')));
  189. // accuracy = 1
  190. $data = array(
  191. //'street' => 'Leopoldstraße',
  192. 'city' => 'Bibersfeld'
  193. );
  194. $res = $this->Comment->save($data);
  195. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  196. //debug($res);
  197. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  198. $this->assertTrue(empty($res['Comment']['lat']) && empty($res['Comment']['lng']));
  199. $data = array(
  200. //'street' => 'Leopoldstraße',
  201. 'city' => '74523'
  202. );
  203. $res = $this->Comment->save($data);
  204. //debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  205. //debug($res);
  206. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  207. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  208. }
  209. }
  210. class TestController extends AppController {
  211. public $uses = array('Address');
  212. }