openlayers.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * @file
  4. * Main OpenLayers Test file
  5. *
  6. * This file contains the tests for the openlayers
  7. * module. Please note that since this module
  8. * depends heavily on Javascript, these tests are
  9. * limited in ensuring that the OpenLayers module
  10. * functions 100% correctly.
  11. *
  12. * For reference, @see:
  13. * http://api.drupal.org/api/drupal/modules--simpletest--drupal_web_test_case.php/class/DrupalWebTestCase/7
  14. *
  15. * @ingroup openlayers
  16. */
  17. /**
  18. * Test the OpenLayers core functionality.
  19. */
  20. class OpenLayersCore extends DrupalWebTestCase {
  21. public static function getInfo() {
  22. return array(
  23. 'name' => 'OpenLayers Core',
  24. 'description' => 'Tests OpenLayers core functions.',
  25. 'group' => 'OpenLayers',
  26. );
  27. }
  28. function setUp() {
  29. // Install modules
  30. parent::setUp('openlayers', 'ctools', 'openlayers_ui', 'openlayers_test');
  31. }
  32. /**
  33. * Test openlayers_include
  34. */
  35. function testInclude() {
  36. $user = $this->drupalCreateUser(array('administer openlayers'));
  37. $this->drupalLogin($user);
  38. // Go to test page and ensure includes happened.
  39. $this->drupalGet('admin/structure/openlayers/test');
  40. $this->assertRaw('openlayers.css', t('The OpenLayers CSS file was included'));
  41. $this->assertRaw('openlayers.js', t('The OpenLayers JS file was included'));
  42. }
  43. /**
  44. * Test map rendering
  45. */
  46. function testRender() {
  47. $rendered = openlayers_render_map();
  48. //$this->verbose('Map output: ' . var_export($rendered, TRUE));
  49. // Check output
  50. $this->assertTrue((stripos($rendered, 'default') !== FALSE), t('Default name found'));
  51. $this->assertTrue((stripos($rendered, 'id="openlayers-container-openlayers-map') !== FALSE),
  52. t('Container ID found'));
  53. $this->assertTrue((stripos($rendered, 'id="openlayers-map') !== FALSE),
  54. t('Map ID found'));
  55. $this->assertTrue((stripos($rendered, 'class="openlayers-map') !== FALSE),
  56. t('Map Class found'));
  57. }
  58. }
  59. /**
  60. * Test OpenLayers basic UI
  61. */
  62. class OpenLayersUI extends DrupalWebTestCase {
  63. public static function getInfo() {
  64. return array(
  65. 'name' => 'OpenLayers UI',
  66. 'description' => 'Tests OpenLayers basic UI.',
  67. 'group' => 'OpenLayers',
  68. );
  69. }
  70. function setUp() {
  71. // Install modules
  72. parent::setUp('openlayers', 'ctools', 'openlayers_ui', 'openlayers_test');
  73. }
  74. /**
  75. * Ensure that the source can be updated
  76. */
  77. function testSourceChange() {
  78. $user = $this->drupalCreateUser(array('administer openlayers'));
  79. $this->drupalLogin($user);
  80. // Update source settings
  81. $new_openlayers_source = 'http://openlayers.org/nightly/OpenLayers.js';
  82. $openlayers_settings = array('openlayers_source' => $new_openlayers_source);
  83. $this->drupalPost('admin/structure/openlayers', $openlayers_settings, t('Save configuration'));
  84. // Go to map page
  85. $this->drupalGet('admin/structure/openlayers');
  86. $this->assertResponse(200, t('User can reach the settings page.'));
  87. // Check that the new source change is represented
  88. $this->drupalGet('admin/structure/openlayers');
  89. $this->assertRaw($new_openlayers_source, t('The OpenLayers source was changed'));
  90. }
  91. }
  92. /**
  93. * Test OpenLayers maps.
  94. */
  95. class OpenLayersMaps extends DrupalWebTestCase {
  96. public static function getInfo() {
  97. return array(
  98. 'name' => 'OpenLayers Maps',
  99. 'description' => 'Tests the OpenLayers maps system.',
  100. 'group' => 'OpenLayers',
  101. );
  102. }
  103. function setUp() {
  104. // Install modules
  105. parent::setUp('openlayers', 'ctools', 'openlayers_ui', 'openlayers_test');
  106. }
  107. /**
  108. * Ensure that the map shows up in the interface
  109. */
  110. function testOpenLayersMaps() {
  111. $user = $this->drupalCreateUser(array('administer openlayers'));
  112. $this->drupalLogin($user);
  113. // Go to map page
  114. $this->drupalGet('admin/structure/openlayers/maps');
  115. $this->assertResponse(200, t('User can reach map list.'));
  116. // Ensure that the default test is listed
  117. $this->assertText(t('Default Map'),
  118. t('Assert that the default map\'s title appears.'));
  119. // Maps
  120. $this->drupalGet('admin/structure/openlayers/maps/default/export');
  121. $this->assertResponse(200, t('User can reach default map export page.'));
  122. $this->drupalGet('admin/structure/openlayers/maps/clone/default');
  123. $this->assertResponse(200, t('User can reach default map clone page.'));
  124. $this->drupalGet('admin/structure/openlayers/maps/add');
  125. $this->assertResponse(200, t('User can reach map add page.'));
  126. }
  127. /**
  128. * Add a new map
  129. */
  130. function testMapAdd() {
  131. $user = $this->drupalCreateUser(array('administer openlayers'));
  132. $this->drupalLogin($user);
  133. // Add new map. Add scaleline so we can look for it later
  134. $map_settings = array(
  135. 'name' => 'testing_map',
  136. 'title' => t('Testing Map'),
  137. 'description' => t('Testing Map description.'),
  138. 'behaviors[openlayers_behavior_scaleline][enabled]' => TRUE,
  139. );
  140. $this->drupalPost('admin/structure/openlayers/maps/add', $map_settings, t('Save'));
  141. $this->assertText(t('Map saved.'), t('The map was successfully saved.'));
  142. // Remove Map
  143. $this->drupalPost('admin/structure/openlayers/maps/list/' . $map_settings['name'] . '/delete',
  144. array(), t('Delete'));
  145. $this->assertText('Map was deleted.', t('Map removed succesfully.'));
  146. }
  147. }
  148. /**
  149. * Test OpenLayers styles.
  150. */
  151. class OpenLayersStyles extends DrupalWebTestCase {
  152. public static function getInfo() {
  153. return array(
  154. 'name' => 'OpenLayers Styles',
  155. 'description' => 'Tests the OpenLayers Styles system.',
  156. 'group' => 'OpenLayers',
  157. );
  158. }
  159. function setUp() {
  160. // Install modules
  161. parent::setUp('openlayers', 'ctools', 'openlayers_ui', 'openlayers_test');
  162. }
  163. function testOpenLayersStyles() {
  164. $user = $this->drupalCreateUser(array('administer openlayers'));
  165. $this->drupalLogin($user);
  166. // New style.
  167. $new_style = array(
  168. 'name' => 'unitstyle',
  169. 'title' => 'Unit Style',
  170. 'description' => 'Unit Style for Drupal',
  171. );
  172. // Check basic navigation.
  173. $this->drupalGet('admin/structure/openlayers/styles/list/default/export');
  174. $this->assertResponse(200, t('User can reach default style export page.'));
  175. $this->drupalGet('admin/structure/openlayers/styles/add');
  176. $this->assertResponse(200, t('User can reach style add page.'));
  177. // Add new style.
  178. $this->drupalPost('admin/structure/openlayers/styles/add', $new_style, t('Save'));
  179. $this->assertText(t('Style saved.'),
  180. t('The style was successfully saved.'));
  181. // Test that the style shows up in the style list.
  182. $this->drupalGet('admin/structure/openlayers/styles');
  183. $this->assertRaw(t('Unit Style'),
  184. t('A new style has been added by the test'));
  185. // Test removing style.
  186. $this->drupalPost('admin/structure/openlayers/styles/list/unitstyle/delete',
  187. array(), t('Delete'));
  188. $this->assertText('Style was deleted.',
  189. t('Style removed succesfully.'));
  190. }
  191. }
  192. /**
  193. * Test OpenLayers layers.
  194. */
  195. class OpenLayersLayers extends DrupalWebTestCase {
  196. public static function getInfo() {
  197. return array(
  198. 'name' => 'OpenLayers Layers',
  199. 'description' => 'Tests the OpenLayers Layers system.',
  200. 'group' => 'OpenLayers',
  201. );
  202. }
  203. function setUp() {
  204. // Install modules
  205. parent::setUp('openlayers', 'ctools', 'openlayers_ui', 'openlayers_test');
  206. }
  207. function testOpenLayersLayers() {
  208. $user = $this->drupalCreateUser(array('administer openlayers'));
  209. $this->drupalLogin($user);
  210. // New layers to add
  211. $layer_inputs = array(
  212. 'openlayers_layer_type_osm' => array(
  213. 'name' => 'unitlayerosm',
  214. 'title' => 'Unit Layer',
  215. 'description' => 'Unit Layer for Drupal',
  216. ),
  217. 'openlayers_layer_type_xyz' => array(
  218. 'name' => 'unitlayerxyz',
  219. 'title' => 'Unit Layer',
  220. 'description' => 'Unit Layer for Drupal',
  221. ),
  222. 'openlayers_layer_type_kml' => array(
  223. 'name' => 'unitlayerkml',
  224. 'title' => 'Unit Layer',
  225. 'description' => 'Unit Layer for Drupal',
  226. ),
  227. 'openlayers_layer_type_wms' => array(
  228. 'name' => 'unitlayerwms',
  229. 'title' => 'Unit Layer',
  230. 'description' => 'Unit Layer for Drupal',
  231. ),
  232. 'openlayers_layer_type_cloudmade' => array(
  233. 'name' => 'unitlayercloudmade',
  234. 'title' => 'Unit Layer',
  235. 'description' => 'Unit Layer for Drupal',
  236. ),
  237. 'openlayers_layer_type_yahoo' => array(
  238. 'name' => 'unitlayeryahoo',
  239. 'title' => 'Unit Layer',
  240. 'description' => 'Unit Layer for Drupal',
  241. ),
  242. 'openlayers_layer_type_bing' => array(
  243. 'name' => 'unitlayerbing',
  244. 'title' => 'Unit Layer',
  245. 'description' => 'Unit Layer for Drupal',
  246. ),
  247. 'openlayers_layer_type_google' => array(
  248. 'name' => 'unitlayergoogle',
  249. 'title' => 'Unit Layer',
  250. 'description' => 'Unit Layer for Drupal',
  251. )
  252. );
  253. // Basic navigation.
  254. $this->drupalGet('admin/structure/openlayers/layers/add');
  255. $this->assertResponse(200, t('User can reach layer add page.'));
  256. // Go through each new layer for testing.
  257. foreach ($layer_inputs as $layer_type => $input_data) {
  258. // Test saving new layer.
  259. $input_data['layer_type'] = $layer_type;
  260. $this->drupalPost('admin/structure/openlayers/layers/add',
  261. $input_data, t('Save'));
  262. $this->assertText(t('Layer saved.'),
  263. t('The layer was successfully saved.'));
  264. // Ensure that the layer shows up in the list.
  265. $this->drupalGet('admin/structure/openlayers/layers');
  266. $this->assertRaw($input_data['title'],
  267. t('A new layer of layer type %type_name has been added by the test',
  268. array('%type_name' => $layer_type)));
  269. // Test removing layer.
  270. $this->drupalPost('admin/structure/openlayers/layers/list/' .
  271. $input_data['name'] . '/delete', array(), t('Delete'));
  272. $this->assertText('Layer was deleted.',
  273. t('Layer removed succesfully.'));
  274. }
  275. }
  276. }
  277. /**
  278. * Test Case for Map Alters
  279. */
  280. class OpenLayersMapAlters extends DrupalWebTestCase {
  281. public static function getInfo() {
  282. return array(
  283. 'name' => 'OpenLayers Map Alters',
  284. 'description' => 'Tests the OpenLayers map alter hooks.',
  285. 'group' => 'OpenLayers',
  286. );
  287. }
  288. function setUp() {
  289. // Install modules
  290. parent::setUp('openlayers', 'ctools', 'openlayers_ui', 'openlayers_test');
  291. }
  292. function testOpenLayersMapAlters() {
  293. $user = $this->drupalCreateUser(array('administer openlayers'));
  294. $this->drupalLogin($user);
  295. // Styles
  296. $this->drupalGet('admin/structure/openlayers/test');
  297. $this->assertText(t('OpenLayers preprocess map alter hook fired.'),
  298. t('Map preprocess alter fired correctly.'));
  299. $this->assertText(t('OpenLayers map alter hook fired.'),
  300. t('Map alter fired correctly.'));
  301. }
  302. }
  303. /**
  304. * OpenLayers Unit Tests
  305. *
  306. * Do not use t() in Unit tests.
  307. * https://drupal.org/node/811254
  308. *
  309. * Unit tests do not bootstrap Drupal very
  310. * far which makes them a bit useless if
  311. * we are utilizing Drupal API functions.
  312. */
  313. class OpenLayersUnitTests extends DrupalUnitTestCase {
  314. public static function getInfo() {
  315. return array(
  316. 'name' => 'OpenLayers Unit Tests',
  317. 'description' => 'Tests functions for OpenLayers.',
  318. 'group' => 'OpenLayers',
  319. );
  320. }
  321. function testOpenLayersStylePath() {
  322. module_load_include('inc', 'openlayers', 'includes/openlayers.render');
  323. global $base_url;
  324. // Full URL
  325. $path = 'http://example.org/sites/all/modules/openlayers/example/${path}.jpg';
  326. $styled_path = openlayers_style_path($path);
  327. $this->assertTrue($styled_path === $path, 'OpenLayers Style Path function with full URL.');
  328. // Just replacement value
  329. $path = '${path}';
  330. $styled_path = openlayers_style_path($path);
  331. $this->assertTrue($styled_path === $path, 'OpenLayers Style Path function with just replacement value.');
  332. // Relative URL
  333. $path = 'sites/all/modules/openlayers/example/${path}.jpg';
  334. $styled_path = openlayers_style_path($path);
  335. $this->assertTrue($styled_path === $base_url . '/' . $path, 'OpenLayers Style Path function with relative URL.');
  336. }
  337. }