Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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  /**
  18   * Tests for CLI tool_uploaduser.
  19   *
  20   * @package    tool_uploaduser
  21   * @copyright  2020 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use \tool_uploaduser\cli_helper;
  26  
  27  /**
  28   * Tests for CLI tool_uploaduser.
  29   *
  30   * @package    tool_uploaduser
  31   * @copyright  2020 Marina Glancy
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class tool_uploaduser_cli_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Generate cli_helper and mock $_SERVER['argv']
  38       *
  39       * @param array $mockargv
  40       * @return \tool_uploaduser\cli_helper
  41       */
  42      protected function construct_helper(array $mockargv = []) {
  43          if (array_key_exists('argv', $_SERVER)) {
  44              $oldservervars = $_SERVER['argv'];
  45          }
  46          $_SERVER['argv'] = array_merge([''], $mockargv);
  47          $clihelper = new cli_helper(\tool_uploaduser\local\text_progress_tracker::class);
  48          if (isset($oldservervars)) {
  49              $_SERVER['argv'] = $oldservervars;
  50          } else {
  51              unset($_SERVER['argv']);
  52          }
  53          return $clihelper;
  54      }
  55  
  56      /**
  57       * Tests simple upload with course enrolment and group allocation
  58       */
  59      public function test_upload_with_course_enrolment() {
  60          global $CFG;
  61          $this->resetAfterTest();
  62          set_config('passwordpolicy', 0);
  63          $this->setAdminUser();
  64  
  65          $course = $this->getDataGenerator()->create_course(['fullname' => 'Maths', 'shortname' => 'math102']);
  66          $g1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 1', 'idnumber' => 'S1']);
  67          $g2 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 3', 'idnumber' => 'S3']);
  68  
  69          $filepath = $CFG->dirroot.'/lib/tests/fixtures/upload_users.csv';
  70  
  71          $clihelper = $this->construct_helper(["--file=$filepath"]);
  72          ob_start();
  73          $clihelper->process();
  74          $output = ob_get_contents();
  75          ob_end_clean();
  76  
  77          // CLI output suggests that 2 users were created.
  78          $stats = $clihelper->get_stats();
  79          $this->assertEquals(2, preg_match_all('/New user/', $output));
  80          $this->assertEquals('Users created: 2', $stats[0]);
  81  
  82          // Tom Jones and Trent Reznor are enrolled into the course, first one to group $g1 and second to group $g2.
  83          $enrols = array_values(enrol_get_course_users($course->id));
  84          $this->assertEqualsCanonicalizing(['reznor', 'jonest'], [$enrols[0]->username, $enrols[1]->username]);
  85          $g1members = groups_get_groups_members($g1->id);
  86          $this->assertEquals(1, count($g1members));
  87          $this->assertEquals('Jones', $g1members[key($g1members)]->lastname);
  88          $g2members = groups_get_groups_members($g2->id);
  89          $this->assertEquals(1, count($g2members));
  90          $this->assertEquals('Reznor', $g2members[key($g2members)]->lastname);
  91      }
  92  
  93      /**
  94       * Test applying defaults during the user upload
  95       */
  96      public function test_upload_with_applying_defaults() {
  97          global $CFG;
  98          $this->resetAfterTest();
  99          set_config('passwordpolicy', 0);
 100          $this->setAdminUser();
 101  
 102          $course = $this->getDataGenerator()->create_course(['fullname' => 'Maths', 'shortname' => 'math102']);
 103          $g1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 1', 'idnumber' => 'S1']);
 104          $g2 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 3', 'idnumber' => 'S3']);
 105  
 106          $filepath = $CFG->dirroot.'/lib/tests/fixtures/upload_users.csv';
 107  
 108          $clihelper = $this->construct_helper(["--file=$filepath", '--city=Brighton', '--department=Purchasing']);
 109          ob_start();
 110          $clihelper->process();
 111          $output = ob_get_contents();
 112          ob_end_clean();
 113  
 114          // CLI output suggests that 2 users were created.
 115          $stats = $clihelper->get_stats();
 116          $this->assertEquals(2, preg_match_all('/New user/', $output));
 117          $this->assertEquals('Users created: 2', $stats[0]);
 118  
 119          // Users have default values applied.
 120          $user1 = core_user::get_user_by_username('jonest');
 121          $this->assertEquals('Brighton', $user1->city);
 122          $this->assertEquals('Purchasing', $user1->department);
 123      }
 124  
 125      /**
 126       * User upload with user profile fields
 127       */
 128      public function test_upload_with_profile_fields() {
 129          global $DB, $CFG;
 130          $this->resetAfterTest();
 131          set_config('passwordpolicy', 0);
 132          $this->setAdminUser();
 133  
 134          $categoryid = $DB->insert_record('user_info_category', ['name' => 'Cat 1', 'sortorder' => 1]);
 135          $this->field1 = $DB->insert_record('user_info_field', [
 136              'shortname' => 'superfield', 'name' => 'Super field', 'categoryid' => $categoryid,
 137              'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 1]);
 138  
 139          $filepath = $CFG->dirroot.'/lib/tests/fixtures/upload_users_profile.csv';
 140  
 141          $clihelper = $this->construct_helper(["--file=$filepath"]);
 142          ob_start();
 143          $clihelper->process();
 144          $output = ob_get_contents();
 145          ob_end_clean();
 146  
 147          // CLI output suggests that 2 users were created.
 148          $stats = $clihelper->get_stats();
 149          $this->assertEquals(2, preg_match_all('/New user/', $output));
 150          $this->assertEquals('Users created: 2', $stats[0]);
 151  
 152          // Created users have data in the profile fields.
 153          $user1 = core_user::get_user_by_username('reznort');
 154          $profilefields1 = profile_user_record($user1->id);
 155          $this->assertEquals((object)['superfield' => 'Loves cats'], $profilefields1);
 156      }
 157  
 158      /**
 159       * Testing that help for CLI does not throw errors
 160       */
 161      public function test_cli_help() {
 162          $this->resetAfterTest();
 163          $this->setAdminUser();
 164          $clihelper = $this->construct_helper(["--help"]);
 165          ob_start();
 166          $clihelper->print_help();
 167          $output = ob_get_contents();
 168          ob_end_clean();
 169  
 170          // Basically a test that everything can be parsed and displayed without errors. Check that some options are present.
 171          $this->assertEquals(1, preg_match('/--delimiter_name=VALUE/', $output));
 172          $this->assertEquals(1, preg_match('/--uutype=VALUE/', $output));
 173          $this->assertEquals(1, preg_match('/--auth=VALUE/', $output));
 174      }
 175  
 176      /**
 177       * Testing skipped user when one exists
 178       */
 179      public function test_create_when_user_exists() {
 180          global $CFG;
 181          $this->resetAfterTest();
 182          set_config('passwordpolicy', 0);
 183          $this->setAdminUser();
 184  
 185          $course = $this->getDataGenerator()->create_course(['fullname' => 'Maths', 'shortname' => 'math102']);
 186          $g1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 1', 'idnumber' => 'S1']);
 187          $g2 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 3', 'idnumber' => 'S3']);
 188  
 189          // Create a user with username jonest.
 190          $user1 = $this->getDataGenerator()->create_user(['username' => 'jonest', 'email' => 'jonest@someplace.edu']);
 191  
 192          $filepath = $CFG->dirroot.'/lib/tests/fixtures/upload_users.csv';
 193  
 194          $clihelper = $this->construct_helper(["--file=$filepath"]);
 195          ob_start();
 196          $clihelper->process();
 197          $output = ob_get_contents();
 198          ob_end_clean();
 199  
 200          // CLI output suggests that 1 user was created and 1 skipped.
 201          $stats = $clihelper->get_stats();
 202          $this->assertEquals(1, preg_match_all('/New user/', $output));
 203          $this->assertEquals('Users created: 1', $stats[0]);
 204          $this->assertEquals('Users skipped: 1', $stats[1]);
 205  
 206          // Trent Reznor is enrolled into the course, Tom Jones is not!
 207          $enrols = array_values(enrol_get_course_users($course->id));
 208          $this->assertEqualsCanonicalizing(['reznor'], [$enrols[0]->username]);
 209      }
 210  
 211      /**
 212       * Testing update mode - do not update user records but allow enrolments
 213       */
 214      public function test_enrolments_when_user_exists() {
 215          global $CFG;
 216          require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/uploaduser/locallib.php');
 217  
 218          $this->resetAfterTest();
 219          set_config('passwordpolicy', 0);
 220          $this->setAdminUser();
 221  
 222          $course = $this->getDataGenerator()->create_course(['fullname' => 'Maths', 'shortname' => 'math102']);
 223          $g1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 1', 'idnumber' => 'S1']);
 224          $g2 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 3', 'idnumber' => 'S3']);
 225  
 226          // Create a user with username jonest.
 227          $this->getDataGenerator()->create_user(['username' => 'jonest', 'email' => 'jonest@someplace.edu',
 228              'firstname' => 'OLDNAME']);
 229  
 230          $filepath = $CFG->dirroot.'/lib/tests/fixtures/upload_users.csv';
 231  
 232          $clihelper = $this->construct_helper(["--file=$filepath", '--uutype='.UU_USER_UPDATE]);
 233          ob_start();
 234          $clihelper->process();
 235          $output = ob_get_contents();
 236          ob_end_clean();
 237  
 238          // CLI output suggests that 1 user was created and 1 skipped.
 239          $stats = $clihelper->get_stats();
 240          $this->assertEquals(0, preg_match_all('/New user/', $output));
 241          $this->assertEquals('Users updated: 0', $stats[0]);
 242          $this->assertEquals('Users skipped: 1', $stats[1]);
 243  
 244          // Tom Jones is enrolled into the course.
 245          $enrols = array_values(enrol_get_course_users($course->id));
 246          $this->assertEqualsCanonicalizing(['jonest'], [$enrols[0]->username]);
 247          // User reznor is not created.
 248          $this->assertFalse(core_user::get_user_by_username('reznor'));
 249          // User jonest is not updated.
 250          $this->assertEquals('OLDNAME', core_user::get_user_by_username('jonest')->firstname);
 251      }
 252  
 253      /**
 254       * Testing update mode - update user records and perform enrolments.
 255       */
 256      public function test_udpate_user() {
 257          global $CFG;
 258          require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/uploaduser/locallib.php');
 259  
 260          $this->resetAfterTest();
 261          set_config('passwordpolicy', 0);
 262          $this->setAdminUser();
 263  
 264          $course = $this->getDataGenerator()->create_course(['fullname' => 'Maths', 'shortname' => 'math102']);
 265          $g1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 1', 'idnumber' => 'S1']);
 266          $g2 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Section 3', 'idnumber' => 'S3']);
 267  
 268          // Create a user with username jonest.
 269          $this->getDataGenerator()->create_user(['username' => 'jonest',
 270              'email' => 'jonest@someplace.edu', 'firstname' => 'OLDNAME']);
 271  
 272          $filepath = $CFG->dirroot.'/lib/tests/fixtures/upload_users.csv';
 273  
 274          $clihelper = $this->construct_helper(["--file=$filepath", '--uutype='.UU_USER_UPDATE,
 275              '--uuupdatetype='.UU_UPDATE_FILEOVERRIDE]);
 276          ob_start();
 277          $clihelper->process();
 278          $output = ob_get_contents();
 279          ob_end_clean();
 280  
 281          // CLI output suggests that 1 user was created and 1 skipped.
 282          $stats = $clihelper->get_stats();
 283          $this->assertEquals(0, preg_match_all('/New user/', $output));
 284          $this->assertEquals('Users updated: 1', $stats[0]);
 285          $this->assertEquals('Users skipped: 1', $stats[1]);
 286  
 287          // Tom Jones is enrolled into the course.
 288          $enrols = array_values(enrol_get_course_users($course->id));
 289          $this->assertEqualsCanonicalizing(['jonest'], [$enrols[0]->username]);
 290          // User reznor is not created.
 291          $this->assertFalse(core_user::get_user_by_username('reznor'));
 292          // User jonest is updated, new first name is Tom.
 293          $this->assertEquals('Tom', core_user::get_user_by_username('jonest')->firstname);
 294      }
 295  }