GeocoderBehaviorTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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->db = ConnectionManager::getDataSource('test');
  109. $this->skipIf(!($this->db instanceof Mysql), 'The virtualFields test is only compatible with Mysql.');
  110. $this->Comment->validator()->add('lat', 'validateLatitude', array('rule' => 'validateLatitude', 'message' => 'validateLatitudeError'));
  111. $this->Comment->validator()->add('lng', 'validateLongitude', array('rule' => 'validateLongitude', 'message' => 'validateLongitudeError'));
  112. $data = array(
  113. 'lat' => 44,
  114. 'lng' => 190,
  115. );
  116. $this->Comment->set($data);
  117. $res = $this->Comment->validates();
  118. $this->assertFalse($res);
  119. $expectedErrors = array(
  120. 'lng' => array(__d('tools', 'validateLongitudeError'))
  121. );
  122. $this->assertEquals($expectedErrors, $this->Comment->validationErrors);
  123. }
  124. /**
  125. * Geocoding tests using the google webservice
  126. *
  127. * @return void
  128. */
  129. public function testBasic() {
  130. $this->db = ConnectionManager::getDataSource('test');
  131. $this->skipIf(!($this->db instanceof Mysql), 'The virtualFields test is only compatible with Mysql.');
  132. $data = array(
  133. 'street' => 'Krebenweg 22',
  134. 'zip' => '74523',
  135. 'city' => 'Bibersfeld'
  136. );
  137. $this->Comment->create();
  138. $res = $this->Comment->save($data);
  139. $this->debug($res);
  140. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']) && round($res['Comment']['lat']) === 49.0 && round($res['Comment']['lng']) === 10.0);
  141. // inconclusive
  142. $data = array(
  143. //'street' => 'Leopoldstraße',
  144. 'city' => 'München'
  145. );
  146. $this->Comment->create();
  147. $res = $this->Comment->save($data);
  148. $this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
  149. //debug($res);
  150. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  151. $this->assertEquals('München, Deutschland', $res['Comment']['geocoder_result']['formatted_address']);
  152. $data = array(
  153. 'city' => 'Bibersfeld'
  154. );
  155. $this->Comment->create();
  156. $res = $this->Comment->save($data);
  157. $this->debug($res);
  158. $this->assertTrue(!empty($res));
  159. $this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
  160. }
  161. /**
  162. * GeocoderBehaviorTest::testMinAccLow()
  163. *
  164. * @return void
  165. */
  166. public function testMinAccLow() {
  167. $this->Comment->Behaviors->unload('Geocoder');
  168. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'min_accuracy' => GeocodeLib::ACC_COUNTRY));
  169. $data = array(
  170. 'city' => 'Deutschland'
  171. );
  172. $this->Comment->create();
  173. $res = $this->Comment->save($data);
  174. $this->assertTrue((int)$res['Comment']['lat'] && (int)$res['Comment']['lng']);
  175. }
  176. /**
  177. * GeocoderBehaviorTest::testMinAccHigh()
  178. *
  179. * @return void
  180. */
  181. public function testMinAccHigh() {
  182. $this->Comment->Behaviors->unload('Geocoder');
  183. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'min_accuracy' => GeocodeLib::ACC_POSTAL));
  184. $data = array(
  185. 'city' => 'Deutschland'
  186. );
  187. $this->Comment->create();
  188. $res = $this->Comment->save($data);
  189. $this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
  190. }
  191. /**
  192. * GeocoderBehaviorTest::testMinInc()
  193. *
  194. * @return void
  195. */
  196. public function testMinInc() {
  197. $this->Comment->Behaviors->unload('Geocoder');
  198. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'min_accuracy' => GeocodeLib::ACC_SUBLOC));
  199. $this->assertEquals(GeocodeLib::ACC_SUBLOC, $this->Comment->Behaviors->Geocoder->settings['Comment']['min_accuracy']);
  200. $data = array(
  201. //'street' => 'Leopoldstraße',
  202. 'city' => 'Neustadt'
  203. );
  204. $this->Comment->create();
  205. $res = $this->Comment->save($data);
  206. $this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
  207. }
  208. /**
  209. * GeocoderBehaviorTest::testMinIncAllowed()
  210. *
  211. * @return void
  212. */
  213. public function testMinIncAllowed() {
  214. $this->Comment->Behaviors->unload('Geocoder');
  215. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'allow_inconclusive' => true));
  216. $data = array(
  217. 'city' => 'Neustadt'
  218. );
  219. $this->Comment->create();
  220. $res = $this->Comment->save($data);
  221. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  222. }
  223. /**
  224. * GeocoderBehaviorTest::testExpect()
  225. *
  226. * @return void
  227. */
  228. public function testExpect() {
  229. $this->Comment->Behaviors->unload('Geocoder');
  230. $this->Comment->Behaviors->load('Tools.Geocoder', array('real' => false, 'expect' => array('postal_code')));
  231. $data = array(
  232. 'city' => 'Bibersfeld'
  233. );
  234. $this->Comment->create();
  235. $res = $this->Comment->save($data);
  236. $this->assertTrue(empty($res['Comment']['lat']) && empty($res['Comment']['lng']));
  237. $data = array(
  238. 'city' => '74523'
  239. );
  240. $this->Comment->create();
  241. $res = $this->Comment->save($data);
  242. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  243. }
  244. }
  245. class TestController extends AppController {
  246. public $uses = array('Address');
  247. }