PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/plugins/geoencoder/tests/TestOfGeoEncoderPluginConfigurationController.php

http://github.com/ginatrapani/ThinkUp
PHP | 253 lines | 161 code | 30 blank | 62 comment | 1 complexity | 6bc1986828bc2dc8a8666487e5c26151 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/plugins/geoencoder/tests/TestOfGeoEncoderPluginConfigurationController.php
  5. *
  6. * Copyright (c) 2009-2012 Mark Wilkie, Ekansh Preet Singh
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * Test of TestOfGeoEncoderPluginConfigurationController
  25. *
  26. * @license http://www.gnu.org/licenses/gpl.html
  27. * @copyright 2009-2012 Mark Wilkie, Ekansh Preet Singh
  28. * @author Mark Wilkie <mwilkie[at]gmail[dot]com>
  29. *
  30. */
  31. require_once dirname(__FILE__) . '/../../../../tests/init.tests.php';
  32. require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
  33. require_once THINKUP_WEBAPP_PATH.'plugins/geoencoder/controller/class.GeoEncoderPluginConfigurationController.php';
  34. class TestOfGeoEncoderPluginConfigurationController extends ThinkUpUnitTestCase {
  35. public function setUp(){
  36. parent::setUp();
  37. $webapp = Webapp::getInstance();
  38. $webapp->registerPlugin('geoencoder', 'GeoEncoderPlugin');
  39. }
  40. public function tearDown(){
  41. parent::tearDown();
  42. }
  43. /**
  44. * Test Constructor
  45. */
  46. public function testConstructor() {
  47. $controller = new GeoEncoderPluginConfigurationController(null, 'geoencoder');
  48. $this->assertTrue(isset($controller), 'constructor test');
  49. }
  50. /**
  51. * Test output
  52. */
  53. public function testOutput() {
  54. //not logged in, no owner set
  55. $controller = new GeoEncoderPluginConfigurationController(null, 'geoencoder');
  56. $output = $controller->go();
  57. $v_mgr = $controller->getViewManager();
  58. $config = Config::getInstance();
  59. $this->assertEqual('You must <a href="'.$config->getValue('site_root_path').
  60. 'session/login.php">log in</a> to do this.', $v_mgr->getTemplateDataItem('error_msg'));
  61. // logged in
  62. // build a user
  63. $builder = FixtureBuilder::build('owners', array('email' => 'me@example.com', 'user_activated' => 1) );
  64. $this->simulateLogin('me@example.com');
  65. $owner_dao = DAOFactory::getDAO('OwnerDAO');
  66. $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
  67. $controller = new GeoEncoderPluginConfigurationController($owner, 'geoencoder');
  68. $output = $controller->go();
  69. $v_mgr = $controller->getViewManager();
  70. $message = $v_mgr->getTemplateDataItem('message');
  71. $this->assertEqual($message,
  72. 'This is the GeoEncoder plugin configuration page for me@example.com.', 'message set ' . $message);
  73. }
  74. public function testAddGmapsAPIKey() {
  75. $build_data = $this->buildController();
  76. $controller = $build_data[0];
  77. $owner = $build_data[1];
  78. $plugin = $build_data[2];
  79. $plugin_option = $build_data[3];
  80. // just name, not an admin, so view only
  81. $output = $controller->go();
  82. $this->assertNotNull($controller->option_elements);
  83. $this->assertEqual(count($controller->option_elements), 2);
  84. $this->assertEqual(
  85. PluginConfigurationController::FORM_TEXT_ELEMENT, $controller->option_elements['gmaps_api_key']['type']);
  86. $this->assertTrue(!isset($controller->option_elements['gmaps_api_key']['default_value']));
  87. $this->assertEqual($controller->option_required_message['gmaps_api_key'],
  88. 'Please enter your Google Maps API Key');
  89. $v_mgr = $controller->getViewManager();
  90. $options_markup = $v_mgr->getTemplateDataItem('options_markup');
  91. $this->assertNotNull($options_markup);
  92. //parse option_markup
  93. $doc = new DOMDocument();
  94. // parse our html
  95. $doc = DOMDocument::loadHTML("<html><body>" . $options_markup . "</body></html>");
  96. // we have a text form element with proper data
  97. $input_field = $this->getElementById($doc, 'plugin_options_gmaps_api_key');
  98. $submit_p = $this->getElementById($doc, 'plugin_option_submit_p');
  99. $this->assertPattern('/^\s+$/', $submit_p->nodeValue); //should be empty, no submit
  100. //now as an admin...
  101. $is_admin = 1;
  102. $this->simulateLogin('admin@example.com', true);
  103. $controller = $build_data[0];
  104. $owner = $build_data[1];
  105. $plugin = $build_data[2];
  106. $plugin_option = $build_data[3];
  107. $output = $controller->go();
  108. $v_mgr = $controller->getViewManager();
  109. $options_markup = $v_mgr->getTemplateDataItem('options_markup');
  110. // parse our html
  111. $doc = DOMDocument::loadHTML("<html><body>" . $options_markup . "</body></html>");
  112. // we have a text form element with proper data
  113. $input_field = $this->getElementById($doc, 'plugin_options_gmaps_api_key');
  114. // submit elements should be disbaled
  115. $this->assertFalse($input_field->getAttribute('disabled'));
  116. $submit_p = $this->getElementById($doc, 'plugin_option_submit_p');
  117. $this->assertPattern('/type="submit".*Save Settings/', $doc->saveXML( $submit_p ) );
  118. }
  119. public function testSelectDistanceUnit() {
  120. $this->simulateLogin('me@example.com', true);
  121. $build_data = $this->buildController();
  122. $controller = $build_data[0];
  123. $owner = $build_data[1];
  124. $plugin = $build_data[2];
  125. $plugin_option = $build_data[3];
  126. // radio options name, is admin, so form should be enabled
  127. $output = $controller->go();
  128. $this->assertNotNull($controller->option_elements);
  129. $this->assertEqual(
  130. PluginConfigurationController::FORM_RADIO_ELEMENT, $controller->option_elements['distance_unit']['type'] );
  131. $this->assertTrue(isset($controller->option_elements['distance_unit']['default_value']) );
  132. $this->assertFalse(isset($controller->option_required_message['distance_unit']));
  133. $v_mgr = $controller->getViewManager();
  134. $options_markup = $v_mgr->getTemplateDataItem('options_markup');
  135. $this->assertNotNull($options_markup);
  136. //parse option_markup
  137. $doc = new DOMDocument();
  138. // parse our html
  139. $doc = DOMDocument::loadHTML("<html><body>" . $options_markup . "</body></html>");
  140. // we have a text form element with proper data
  141. $radio_div = $this->getElementById($doc, 'plugin_options_distance_unit');
  142. $radios = $radio_div->getElementsByTagName('input');
  143. $this->assertEqual(2, $radios->length);
  144. $this->assertEqual($radios->item(0)->getAttribute('value'), 'km');
  145. $this->assertEqual($radios->item(1)->getAttribute('value'), 'mi');
  146. $submit_p = $this->getElementById($doc, 'plugin_option_submit_p');
  147. $this->assertPattern('/type="submit".*Save Settings/', $doc->saveXML( $submit_p ) );
  148. }
  149. public function testGetPluginOptions() {
  150. $build_data = $this->buildController();
  151. $controller = $build_data[0];
  152. $options_hash = $controller->getPluginOptions();
  153. $this->assertEqual($options_hash['gmaps_api_key']->id, 2);
  154. $this->assertEqual($options_hash['gmaps_api_key']->option_name, 'gmaps_api_key');
  155. $this->assertEqual($options_hash['gmaps_api_key']->option_value, '1234');
  156. // get a single undefined option
  157. $this->assertFalse($controller->getPluginOption('not defined'));
  158. // get a single defined option
  159. $this->assertEqual($controller->getPluginOption('gmaps_api_key'), '1234');
  160. }
  161. /**
  162. * Test config not admin
  163. */
  164. public function testConfigOptionsNotAdmin() {
  165. $build_data = $this->buildController();
  166. $this->simulateLogin('me@example.com');
  167. $owner_dao = DAOFactory::getDAO('OwnerDAO');
  168. $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
  169. $controller = new GeoEncoderPluginConfigurationController($owner, 'geoencoder');
  170. $output = $controller->go();
  171. // we have a text form element with proper data
  172. $this->assertNoPattern('/Save Settings/', $output); // should have no submit option
  173. $this->assertNoPattern('/plugin_options_error_gmaps_api_key/', $output); // should have no api key
  174. $this->assertPattern('/var is_admin = false/', $output); // not a js admin
  175. $this->assertPattern('/var required_values_set = true/', $output); // is configured
  176. // not configured
  177. $prefix = Config::getInstance()->getValue('table_prefix');
  178. $namespace = $build_data[3]->columns['namespace'];
  179. OwnerMySQLDAO::$PDO->query("delete from " . $prefix . "options where namespace = '$namespace'");
  180. $controller = new GeoEncoderPluginConfigurationController($owner, 'geoencoder');
  181. $output = $controller->go();
  182. $this->assertPattern('/var required_values_set = false/', $output); // is not configured
  183. }
  184. /**
  185. * Test config isa admin
  186. */
  187. public function testConfigOptionsIsAdmin() {
  188. $build_data = $this->buildController();
  189. $this->simulateLogin('me@example.com', true);
  190. $owner_dao = DAOFactory::getDAO('OwnerDAO');
  191. $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
  192. $controller = new GeoEncoderPluginConfigurationController($owner, 'geoencoder');
  193. $output = $controller->go();
  194. // we have a text form element with proper data
  195. $this->assertPattern('/Save Settings/', $output); // should have submit option
  196. $this->assertPattern('/plugin_options_error_gmaps_api_key/', $output); // should have api key option
  197. $this->assertPattern('/var is_admin = true/', $output); // is a js admin
  198. $this->assertPattern('/var required_values_set = true/', $output); // is configured
  199. //app not configured
  200. $prefix = Config::getInstance()->getValue('table_prefix');
  201. $namespace = $build_data[3]->columns['namespace'];
  202. OwnerMySQLDAO::$PDO->query("delete from " . $prefix . "options where namespace = '$namespace'");
  203. $controller = new GeoEncoderPluginConfigurationController($owner, 'geoencoder');
  204. $output = $controller->go();
  205. $this->assertPattern('/var required_values_set = false/', $output); // is not configured
  206. }
  207. private function buildController($build_owner=true) {
  208. $builder_owner = null;
  209. if ($build_owner) {
  210. $builder_owner = FixtureBuilder::build('owners', array('email' => 'me@example.com', 'user_activated' => 1));
  211. }
  212. $builder_plugin = FixtureBuilder::build('plugins', array('folder_name' => 'geoencoder', 'is_active' => 1) );
  213. $plugin_id = $builder_plugin->columns['last_insert_id'];
  214. $namespace = OptionDAO::PLUGIN_OPTIONS . '-' . $plugin_id;
  215. $builder_plugin_options = FixtureBuilder::build('options',
  216. array('namespace'=>$namespace, 'option_name' => 'gmaps_api_key', 'option_value' => "1234"));
  217. $this->simulateLogin('me@example.com');
  218. $owner_dao = DAOFactory::getDAO('OwnerDAO');
  219. $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
  220. $controller = new GeoEncoderPluginConfigurationController($owner, 'geoencoder');
  221. return array($controller, $builder_owner, $builder_plugin, $builder_plugin_options);
  222. }
  223. }