external_test.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * Auth email external functions tests.
  18. *
  19. * @package auth_emailrut
  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.2
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. global $CFG;
  27. require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28. /**
  29. * External auth email API tests.
  30. *
  31. * @package auth_emailrut
  32. * @copyright 2016 Juan Leyva
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. * @since Moodle 3.2
  35. */
  36. class auth_emailrut_external_testcase extends externallib_advanced_testcase {
  37. /**
  38. * Set up for every test
  39. */
  40. public function setUp() {
  41. global $CFG, $DB;
  42. $this->resetAfterTest(true);
  43. $CFG->registerauth = 'email';
  44. $categoryid = $DB->insert_record('user_info_category', array('name' => 'Cat 1', 'sortorder' => 1));
  45. $this->field1 = $DB->insert_record('user_info_field', array(
  46. 'shortname' => 'frogname', 'name' => 'Name of frog', 'categoryid' => $categoryid,
  47. 'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 1));
  48. $this->field2 = $DB->insert_record('user_info_field', array(
  49. 'shortname' => 'sometext', 'name' => 'Some text in textarea', 'categoryid' => $categoryid,
  50. 'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2));
  51. }
  52. public function test_get_signup_settings() {
  53. global $CFG;
  54. $CFG->defaultcity = 'Bcn';
  55. $CFG->country = 'ES';
  56. $CFG->sitepolicy = 'https://moodle.org';
  57. $result = auth_emailrut_external::get_signup_settings();
  58. $result = external_api::clean_returnvalue(auth_emailrut_external::get_signup_settings_returns(), $result);
  59. // Check expected data.
  60. $this->assertEquals(array('firstname', 'lastname'), $result['namefields']);
  61. $this->assertEquals($CFG->defaultcity, $result['defaultcity']);
  62. $this->assertEquals($CFG->country, $result['country']);
  63. $this->assertEquals($CFG->sitepolicy, $result['sitepolicy']);
  64. $this->assertEquals(print_password_policy(), $result['passwordpolicy']);
  65. $this->assertNotContains('recaptchachallengehash', $result);
  66. $this->assertNotContains('recaptchachallengeimage', $result);
  67. // Whip up a array with named entries to easily check against.
  68. $namedarray = array();
  69. foreach ($result['profilefields'] as $key => $value) {
  70. $namedarray[$value['shortname']] = array(
  71. 'datatype' => $value['datatype']
  72. );
  73. }
  74. // Just check if we have the fields from this test. If a plugin adds fields we'll let it slide.
  75. $this->assertArrayHasKey('frogname', $namedarray);
  76. $this->assertArrayHasKey('sometext', $namedarray);
  77. $this->assertEquals('text', $namedarray['frogname']['datatype']);
  78. $this->assertEquals('textarea', $namedarray['sometext']['datatype']);
  79. }
  80. public function test_signup_user() {
  81. global $DB;
  82. $username = 'pepe';
  83. $password = 'abcdefAª.ªª!!3';
  84. $firstname = 'Pepe';
  85. $lastname = 'Pérez';
  86. $email = 'myemail@no.zbc';
  87. $city = 'Bcn';
  88. $country = 'ES';
  89. $customprofilefields = array(
  90. array(
  91. 'type' => 'text',
  92. 'name' => 'profile_field_frogname',
  93. 'value' => 'random text',
  94. ),
  95. array(
  96. 'type' => 'textarea',
  97. 'name' => 'profile_field_sometext',
  98. 'value' => json_encode(
  99. array(
  100. 'text' => 'blah blah',
  101. 'format' => FORMAT_HTML
  102. )
  103. ),
  104. )
  105. );
  106. // Create new user.
  107. $result = auth_emailrut_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country,
  108. '', '', $customprofilefields);
  109. $result = external_api::clean_returnvalue(auth_emailrut_external::signup_user_returns(), $result);
  110. $this->assertTrue($result['success']);
  111. $this->assertEmpty($result['warnings']);
  112. $user = $DB->get_record('user', array('username' => $username));
  113. $this->assertEquals($firstname, $user->firstname);
  114. $this->assertEquals($lastname, $user->lastname);
  115. $this->assertEquals($email, $user->email);
  116. $this->assertEquals($city, $user->city);
  117. $this->assertEquals($country, $user->country);
  118. $this->assertEquals(0, $user->confirmed);
  119. $this->assertEquals(current_language(), $user->lang);
  120. $this->assertEquals('email', $user->auth);
  121. $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field1));
  122. $this->assertEquals($customprofilefields[0]['value'], $infofield->data);
  123. $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field2));
  124. $this->assertEquals(json_decode($customprofilefields[1]['value'])->text, $infofield->data);
  125. // Try to create a user with the same username, email and password. We ommit also the profile fields.
  126. $password = 'abc';
  127. $result = auth_emailrut_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country,
  128. '', '', $customprofilefields);
  129. $result = external_api::clean_returnvalue(auth_emailrut_external::signup_user_returns(), $result);
  130. $this->assertFalse($result['success']);
  131. $this->assertCount(3, $result['warnings']);
  132. $expectederrors = array('username', 'email', 'password');
  133. $finalerrors = [];
  134. foreach ($result['warnings'] as $warning) {
  135. $finalerrors[] = $warning['item'];
  136. }
  137. $this->assertEquals($expectederrors, $finalerrors);
  138. // Do not pass the required profile fields.
  139. $this->expectException('invalid_parameter_exception');
  140. $result = auth_emailrut_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country);
  141. }
  142. }