GeocoderBehaviorTest.php 9.0 KB

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