GeocoderBehaviorTest.php 9.2 KB

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