PageRenderTime 26ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/tool/mobile/tests/externallib_test.php

https://github.com/Beedell/moodle
PHP | 286 lines | 170 code | 48 blank | 68 comment | 3 complexity | dadeaf2087aa86a2c8e6701d7e2cc310 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Moodle Mobile admin tool external functions tests.
  18. *
  19. * @package tool_mobile
  20. * @category external
  21. * @copyright 2016 Juan Leyva
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @since Moodle 3.1
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. global $CFG;
  27. require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28. use tool_mobile\external;
  29. use tool_mobile\api;
  30. /**
  31. * Moodle Mobile admin tool external functions tests.
  32. *
  33. * @package tool_mobile
  34. * @copyright 2016 Juan Leyva
  35. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36. * @since Moodle 3.1
  37. */
  38. class tool_mobile_external_testcase extends externallib_advanced_testcase {
  39. /**
  40. * Test get_plugins_supporting_mobile.
  41. * This is a very basic test because currently there aren't plugins supporting Mobile in core.
  42. */
  43. public function test_get_plugins_supporting_mobile() {
  44. $result = external::get_plugins_supporting_mobile();
  45. $result = external_api::clean_returnvalue(external::get_plugins_supporting_mobile_returns(), $result);
  46. $this->assertCount(0, $result['warnings']);
  47. $this->assertArrayHasKey('plugins', $result);
  48. $this->assertTrue(is_array($result['plugins']));
  49. }
  50. public function test_get_public_config() {
  51. global $CFG, $SITE, $OUTPUT;
  52. $this->resetAfterTest(true);
  53. $result = external::get_public_config();
  54. $result = external_api::clean_returnvalue(external::get_public_config_returns(), $result);
  55. // Test default values.
  56. $context = context_system::instance();
  57. list($authinstructions, $notusedformat) = external_format_text($CFG->auth_instructions, FORMAT_MOODLE, $context->id);
  58. list($maintenancemessage, $notusedformat) = external_format_text($CFG->maintenance_message, FORMAT_MOODLE, $context->id);
  59. $expected = array(
  60. 'wwwroot' => $CFG->wwwroot,
  61. 'httpswwwroot' => $CFG->httpswwwroot,
  62. 'sitename' => external_format_string($SITE->fullname, $context->id, true),
  63. 'guestlogin' => $CFG->guestloginbutton,
  64. 'rememberusername' => $CFG->rememberusername,
  65. 'authloginviaemail' => $CFG->authloginviaemail,
  66. 'registerauth' => $CFG->registerauth,
  67. 'forgottenpasswordurl' => $CFG->forgottenpasswordurl,
  68. 'authinstructions' => $authinstructions,
  69. 'authnoneenabled' => (int) is_enabled_auth('none'),
  70. 'enablewebservices' => $CFG->enablewebservices,
  71. 'enablemobilewebservice' => $CFG->enablemobilewebservice,
  72. 'maintenanceenabled' => $CFG->maintenance_enabled,
  73. 'maintenancemessage' => $maintenancemessage,
  74. 'typeoflogin' => api::LOGIN_VIA_APP,
  75. 'mobilecssurl' => '',
  76. 'tool_mobile_disabledfeatures' => '',
  77. 'launchurl' => "$CFG->wwwroot/$CFG->admin/tool/mobile/launch.php",
  78. 'warnings' => array()
  79. );
  80. $this->assertEquals($expected, $result);
  81. // Change some values.
  82. set_config('registerauth', 'email');
  83. $authinstructions = 'Something with <b>html tags</b>';
  84. set_config('auth_instructions', $authinstructions);
  85. set_config('typeoflogin', api::LOGIN_VIA_BROWSER, 'tool_mobile');
  86. set_config('logo', 'mock.png', 'core_admin');
  87. set_config('logocompact', 'mock.png', 'core_admin');
  88. list($authinstructions, $notusedformat) = external_format_text($authinstructions, FORMAT_MOODLE, $context->id);
  89. $expected['registerauth'] = 'email';
  90. $expected['authinstructions'] = $authinstructions;
  91. $expected['typeoflogin'] = api::LOGIN_VIA_BROWSER;
  92. if ($logourl = $OUTPUT->get_logo_url()) {
  93. $expected['logourl'] = $logourl->out(false);
  94. }
  95. if ($compactlogourl = $OUTPUT->get_compact_logo_url()) {
  96. $expected['compactlogourl'] = $compactlogourl->out(false);
  97. }
  98. $result = external::get_public_config();
  99. $result = external_api::clean_returnvalue(external::get_public_config_returns(), $result);
  100. $this->assertEquals($expected, $result);
  101. }
  102. /**
  103. * Test get_config
  104. */
  105. public function test_get_config() {
  106. global $CFG, $SITE;
  107. require_once($CFG->dirroot . '/course/format/lib.php');
  108. $this->resetAfterTest(true);
  109. $result = external::get_config();
  110. $result = external_api::clean_returnvalue(external::get_config_returns(), $result);
  111. // SITE summary is null in phpunit which gets transformed to an empty string by format_text.
  112. list($sitesummary, $unused) = external_format_text($SITE->summary, $SITE->summaryformat, context_system::instance()->id);
  113. // Test default values.
  114. $context = context_system::instance();
  115. $expected = array(
  116. array('name' => 'fullname', 'value' => $SITE->fullname),
  117. array('name' => 'shortname', 'value' => $SITE->shortname),
  118. array('name' => 'summary', 'value' => $sitesummary),
  119. array('name' => 'summaryformat', 'value' => FORMAT_HTML),
  120. array('name' => 'frontpage', 'value' => $CFG->frontpage),
  121. array('name' => 'frontpageloggedin', 'value' => $CFG->frontpageloggedin),
  122. array('name' => 'maxcategorydepth', 'value' => $CFG->maxcategorydepth),
  123. array('name' => 'frontpagecourselimit', 'value' => $CFG->frontpagecourselimit),
  124. array('name' => 'numsections', 'value' => course_get_format($SITE)->get_last_section_number()),
  125. array('name' => 'newsitems', 'value' => $SITE->newsitems),
  126. array('name' => 'commentsperpage', 'value' => $CFG->commentsperpage),
  127. array('name' => 'disableuserimages', 'value' => $CFG->disableuserimages),
  128. array('name' => 'mygradesurl', 'value' => user_mygrades_url()->out(false)),
  129. array('name' => 'tool_mobile_forcelogout', 'value' => 0),
  130. array('name' => 'tool_mobile_customlangstrings', 'value' => ''),
  131. array('name' => 'tool_mobile_disabledfeatures', 'value' => ''),
  132. array('name' => 'tool_mobile_custommenuitems', 'value' => ''),
  133. );
  134. $this->assertCount(0, $result['warnings']);
  135. $this->assertEquals($expected, $result['settings']);
  136. // Change a value and retrieve filtering by section.
  137. set_config('commentsperpage', 1);
  138. $expected[10]['value'] = 1;
  139. // Remove not expected elements.
  140. array_splice($expected, 11);
  141. $result = external::get_config('frontpagesettings');
  142. $result = external_api::clean_returnvalue(external::get_config_returns(), $result);
  143. $this->assertCount(0, $result['warnings']);
  144. $this->assertEquals($expected, $result['settings']);
  145. }
  146. /*
  147. * Test get_autologin_key.
  148. */
  149. public function test_get_autologin_key() {
  150. global $DB, $CFG, $USER;
  151. $this->resetAfterTest(true);
  152. $user = $this->getDataGenerator()->create_user();
  153. $this->setUser($user);
  154. $service = $DB->get_record('external_services', array('shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE));
  155. $token = external_generate_token_for_current_user($service);
  156. // Check we got the private token.
  157. $this->assertTrue(isset($token->privatetoken));
  158. // Enable requeriments.
  159. $_GET['wstoken'] = $token->token; // Mock parameters.
  160. // Even if we force the password change for the current user we should be able to retrieve the key.
  161. set_user_preference('auth_forcepasswordchange', 1, $user->id);
  162. $this->setCurrentTimeStart();
  163. $result = external::get_autologin_key($token->privatetoken);
  164. $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result);
  165. // Validate the key.
  166. $this->assertEquals(32, core_text::strlen($result['key']));
  167. $key = $DB->get_record('user_private_key', array('value' => $result['key']));
  168. $this->assertEquals($USER->id, $key->userid);
  169. $this->assertTimeCurrent($key->validuntil - api::LOGIN_KEY_TTL);
  170. // Now, try with an invalid private token.
  171. set_user_preference('tool_mobile_autologin_request_last', time() - HOURSECS, $USER);
  172. $this->expectException('moodle_exception');
  173. $this->expectExceptionMessage(get_string('invalidprivatetoken', 'tool_mobile'));
  174. $result = external::get_autologin_key(random_string('64'));
  175. }
  176. /**
  177. * Test get_autologin_key missing ws.
  178. */
  179. public function test_get_autologin_key_missing_ws() {
  180. global $CFG;
  181. $this->resetAfterTest(true);
  182. // Need to disable webservices to verify that's checked.
  183. $CFG->enablewebservices = 0;
  184. $CFG->enablemobilewebservice = 0;
  185. $this->setAdminUser();
  186. $this->expectException('moodle_exception');
  187. $this->expectExceptionMessage(get_string('enablewsdescription', 'webservice'));
  188. $result = external::get_autologin_key('');
  189. }
  190. /**
  191. * Test get_autologin_key missing https.
  192. */
  193. public function test_get_autologin_key_missing_https() {
  194. global $CFG;
  195. // Need to simulate a non HTTPS site here.
  196. $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
  197. $CFG->httpswwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
  198. $this->resetAfterTest(true);
  199. $this->setAdminUser();
  200. $this->expectException('moodle_exception');
  201. $this->expectExceptionMessage(get_string('httpsrequired', 'tool_mobile'));
  202. $result = external::get_autologin_key('');
  203. }
  204. /**
  205. * Test get_autologin_key missing admin.
  206. */
  207. public function test_get_autologin_key_missing_admin() {
  208. global $CFG;
  209. $this->resetAfterTest(true);
  210. $this->setAdminUser();
  211. $this->expectException('moodle_exception');
  212. $this->expectExceptionMessage(get_string('autologinnotallowedtoadmins', 'tool_mobile'));
  213. $result = external::get_autologin_key('');
  214. }
  215. /**
  216. * Test get_autologin_key locked.
  217. */
  218. public function test_get_autologin_key_missing_locked() {
  219. global $CFG, $DB, $USER;
  220. $this->resetAfterTest(true);
  221. $user = $this->getDataGenerator()->create_user();
  222. $this->setUser($user);
  223. $service = $DB->get_record('external_services', array('shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE));
  224. $token = external_generate_token_for_current_user($service);
  225. $_GET['wstoken'] = $token->token; // Mock parameters.
  226. $result = external::get_autologin_key($token->privatetoken);
  227. $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result);
  228. // Mock last time request.
  229. $mocktime = time() - 7 * MINSECS;
  230. set_user_preference('tool_mobile_autologin_request_last', $mocktime, $USER);
  231. $result = external::get_autologin_key($token->privatetoken);
  232. $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result);
  233. // We just requested one token, we must wait.
  234. $this->expectException('moodle_exception');
  235. $this->expectExceptionMessage(get_string('autologinkeygenerationlockout', 'tool_mobile'));
  236. $result = external::get_autologin_key($token->privatetoken);
  237. }
  238. }