GeocoderBehaviorTest.php 8.3 KB

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