GeocoderBehaviorTest.php 9.5 KB

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