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] [Versions 39 and 310]

   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   * Unit tests for the class in load_data.php
  19   *
  20   * @package    gradeimport_csv
  21   * @category   phpunit
  22   * @copyright  2014 Adrian Greeve
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/grade/import/csv/tests/fixtures/phpunit_gradeimport_csv_load_data.php');
  30  require_once($CFG->libdir . '/csvlib.class.php');
  31  require_once($CFG->libdir . '/grade/grade_item.php');
  32  require_once($CFG->libdir . '/grade/tests/fixtures/lib.php');
  33  
  34  /**
  35   * Unit tests for lib.php
  36   *
  37   * @package    gradeimport_csv
  38   * @copyright  2014 Adrian Greeve
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class gradeimport_csv_load_data_testcase extends grade_base_testcase {
  42  
  43      /** @var string $oktext Text to be imported. This data should have no issues being imported. */
  44      protected $oktext = '"First name",Surname,"ID number",Institution,Department,"Email address","Assignment: Assignment for grape group", "Feedback: Assignment for grape group","Assignment: Second new grade item","Course total"
  45  Anne,Able,,"Moodle HQ","Rock on!",student7@example.com,56.00,"We welcome feedback",,56.00
  46  Bobby,Bunce,,"Moodle HQ","Rock on!",student5@example.com,75.00,,45.0,75.00';
  47  
  48      /** @var string $badtext Text to be imported. This data has an extra column and should not succeed in being imported. */
  49      protected $badtext = '"First name",Surname,"ID number",Institution,Department,"Email address","Assignment: Assignment for grape group","Course total"
  50  Anne,Able,,"Moodle HQ","Rock on!",student7@example.com,56.00,56.00,78.00
  51  Bobby,Bunce,,"Moodle HQ","Rock on!",student5@example.com,75.00,75.00';
  52  
  53      /** @var string $csvtext CSV data to be imported with Last download from this course column. */
  54      protected $csvtext = '"First name",Surname,"ID number",Institution,Department,"Email address","Assignment: Assignment for grape group", "Feedback: Assignment for grape group","Course total","Last downloaded from this course"
  55  Anne,Able,,"Moodle HQ","Rock on!",student7@example.com,56.00,"We welcome feedback",56.00,{exportdate}
  56  Bobby,Bunce,,"Moodle HQ","Rock on!",student5@example.com,75.00,,75.00,{exportdate}';
  57  
  58      /** @var int $iid Import ID. */
  59      protected $iid;
  60  
  61      /** @var object $csvimport a csv_import_reader object that handles the csv import. */
  62      protected $csvimport;
  63  
  64      /** @var array $columns The first row of the csv file. These are the columns of the import file.*/
  65      protected $columns;
  66  
  67      public function tearDown(): void {
  68          $this->csvimport = null;
  69      }
  70  
  71      /**
  72       * Load up the above text through the csv import.
  73       *
  74       * @param string $content Text to be imported into the gradebook.
  75       * @return array All text separated by commas now in an array.
  76       */
  77      protected function csv_load($content) {
  78          // Import the csv strings.
  79          $this->iid = csv_import_reader::get_new_iid('grade');
  80          $this->csvimport = new csv_import_reader($this->iid, 'grade');
  81  
  82          $this->csvimport->load_csv_content($content, 'utf8', 'comma');
  83          $this->columns = $this->csvimport->get_columns();
  84  
  85          $this->csvimport->init();
  86          while ($line = $this->csvimport->next()) {
  87              $testarray[] = $line;
  88          }
  89  
  90          return $testarray;
  91      }
  92  
  93      /**
  94       * Test loading data and returning preview content.
  95       */
  96      public function test_load_csv_content() {
  97          $encoding = 'utf8';
  98          $separator = 'comma';
  99          $previewrows = 5;
 100          $csvpreview = new phpunit_gradeimport_csv_load_data();
 101          $csvpreview->load_csv_content($this->oktext, $encoding, $separator, $previewrows);
 102  
 103          $expecteddata = array(array(
 104                  'Anne',
 105                  'Able',
 106                  '',
 107                  'Moodle HQ',
 108                  'Rock on!',
 109                  'student7@example.com',
 110                  56.00,
 111                  'We welcome feedback',
 112                  '',
 113                  56.00
 114              ),
 115              array(
 116                  'Bobby',
 117                  'Bunce',
 118                  '',
 119                  'Moodle HQ',
 120                  'Rock on!',
 121                  'student5@example.com',
 122                  75.00,
 123                  '',
 124                  45.0,
 125                  75.00
 126              )
 127          );
 128  
 129          $expectedheaders = array(
 130              'First name',
 131              'Surname',
 132              'ID number',
 133              'Institution',
 134              'Department',
 135              'Email address',
 136              'Assignment: Assignment for grape group',
 137              'Feedback: Assignment for grape group',
 138              'Assignment: Second new grade item',
 139              'Course total'
 140          );
 141          // Check that general data is returned as expected.
 142          $this->assertEquals($csvpreview->get_previewdata(), $expecteddata);
 143          // Check that headers are returned as expected.
 144          $this->assertEquals($csvpreview->get_headers(), $expectedheaders);
 145  
 146          // Check that errors are being recorded.
 147          $csvpreview = new phpunit_gradeimport_csv_load_data();
 148          $csvpreview->load_csv_content($this->badtext, $encoding, $separator, $previewrows);
 149          // Columns shouldn't match.
 150          $this->assertEquals($csvpreview->get_error(), get_string('csvweirdcolumns', 'error'));
 151      }
 152  
 153      /**
 154       * Test fetching grade items for the course.
 155       */
 156      public function test_fetch_grade_items() {
 157  
 158          $gradeitemsarray = grade_item::fetch_all(array('courseid' => $this->courseid));
 159          $gradeitems = phpunit_gradeimport_csv_load_data::fetch_grade_items($this->courseid);
 160  
 161          // Make sure that each grade item is located in the gradeitemsarray.
 162          foreach ($gradeitems as $key => $gradeitem) {
 163              $this->assertArrayHasKey($key, $gradeitemsarray);
 164          }
 165  
 166          // Get the key for a specific grade item.
 167          $quizkey = null;
 168          foreach ($gradeitemsarray as $key => $value) {
 169              if ($value->itemname == "Quiz grade item") {
 170                  $quizkey = $key;
 171              }
 172          }
 173  
 174          // Expected modified item name.
 175          $testitemname = get_string('modulename', $gradeitemsarray[$quizkey]->itemmodule) . ': ' .
 176                  $gradeitemsarray[$quizkey]->itemname;
 177          // Check that an item that is a module, is concatenated properly.
 178          $this->assertEquals($testitemname, $gradeitems[$quizkey]);
 179      }
 180  
 181      /**
 182       * Test the inserting of grade record data.
 183       */
 184      public function test_insert_grade_record() {
 185          global $DB, $USER;
 186  
 187          $user = $this->getDataGenerator()->create_user();
 188          $this->setAdminUser();
 189  
 190          $record = new stdClass();
 191          $record->itemid = 4;
 192          $record->newgradeitem = 25;
 193          $record->finalgrade = 62.00;
 194          $record->feedback = 'Some test feedback';
 195  
 196          $testobject = new phpunit_gradeimport_csv_load_data();
 197          $testobject->test_insert_grade_record($record, $user->id);
 198  
 199          $gradeimportvalues = $DB->get_records('grade_import_values');
 200          // Get the insert id.
 201          $key = key($gradeimportvalues);
 202  
 203          $testarray = array();
 204          $testarray[$key] = new stdClass();
 205          $testarray[$key]->id = $key;
 206          $testarray[$key]->itemid = $record->itemid;
 207          $testarray[$key]->newgradeitem = $record->newgradeitem;
 208          $testarray[$key]->userid = $user->id;
 209          $testarray[$key]->finalgrade = $record->finalgrade;
 210          $testarray[$key]->feedback = $record->feedback;
 211          $testarray[$key]->importcode = $testobject->get_importcode();
 212          $testarray[$key]->importer = $USER->id;
 213          $testarray[$key]->importonlyfeedback = 0;
 214  
 215          // Check that the record was inserted into the database.
 216          $this->assertEquals($gradeimportvalues, $testarray);
 217      }
 218  
 219      /**
 220       * Test preparing a new grade item for import into the gradebook.
 221       */
 222      public function test_import_new_grade_item() {
 223          global $DB;
 224  
 225          $this->setAdminUser();
 226          $this->csv_load($this->oktext);
 227          $columns = $this->columns;
 228  
 229          // The assignment is item 6.
 230          $key = 6;
 231          $testobject = new phpunit_gradeimport_csv_load_data();
 232  
 233          // Key for this assessment.
 234          $this->csvimport->init();
 235          $testarray = array();
 236          while ($line = $this->csvimport->next()) {
 237              $testarray[] = $testobject->test_import_new_grade_item($columns, $key, $line[$key]);
 238          }
 239  
 240          // Query the database and check how many results were inserted.
 241          $newgradeimportitems = $DB->get_records('grade_import_newitem');
 242          $this->assertEquals(count($testarray), count($newgradeimportitems));
 243      }
 244  
 245      /**
 246       * Data provider for \gradeimport_csv_load_data_testcase::test_check_user_exists().
 247       *
 248       * @return array
 249       */
 250      public function check_user_exists_provider() {
 251          return [
 252              'Fetch by email' => [
 253                  'email', 's1@example.com', true
 254              ],
 255              'Fetch by email, different case' => [
 256                  'email', 'S1@EXAMPLE.COM', true
 257              ],
 258              'Fetch data using a non-existent email' => [
 259                  'email', 's2@example.com', false
 260              ],
 261              'Multiple accounts with the same email' => [
 262                  'email', 's1@example.com', false, 1
 263              ],
 264              'Fetch data using a valid user ID' => [
 265                  'id', true, true
 266              ],
 267              'Fetch data using a non-existent user ID' => [
 268                  'id', false, false
 269              ],
 270              'Fetch data using a valid username' => [
 271                  'username', 's1', true
 272              ],
 273              'Fetch data using a valid username, different case' => [
 274                  'username', 'S1', true
 275              ],
 276              'Fetch data using an invalid username' => [
 277                  'username', 's2', false
 278              ],
 279              'Fetch data using a valid ID Number' => [
 280                  'idnumber', 's1', true
 281              ],
 282              'Fetch data using an invalid ID Number' => [
 283                  'idnumber', 's2', false
 284              ],
 285          ];
 286      }
 287  
 288      /**
 289       * Check that the user matches a user in the system.
 290       *
 291       * @dataProvider check_user_exists_provider
 292       * @param string $field The field to use for the query.
 293       * @param string|boolean $value The field value. When fetching by ID, set true to fetch valid user ID, false otherwise.
 294       * @param boolean $successexpected Whether we expect for a user to be found or not.
 295       * @param int $allowaccountssameemail Value for $CFG->allowaccountssameemail
 296       */
 297      public function test_check_user_exists($field, $value, $successexpected, $allowaccountssameemail = 0) {
 298          $this->resetAfterTest();
 299  
 300          $generator = $this->getDataGenerator();
 301  
 302          // Need to add one of the users into the system.
 303          $user = $generator->create_user([
 304              'firstname' => 'Anne',
 305              'lastname' => 'Able',
 306              'email' => 's1@example.com',
 307              'idnumber' => 's1',
 308              'username' => 's1',
 309          ]);
 310  
 311          if ($allowaccountssameemail) {
 312              // Create another user with the same email address.
 313              $generator->create_user(['email' => 's1@example.com']);
 314          }
 315  
 316          // Since the data provider can't know what user ID to use, do a special handling for ID field tests.
 317          if ($field === 'id') {
 318              if ($value) {
 319                  // Test for fetching data using a valid user ID. Use the generated user's ID.
 320                  $value = $user->id;
 321              } else {
 322                  // Test for fetching data using a non-existent user ID.
 323                  $value = $user->id + 1;
 324              }
 325          }
 326  
 327          $userfields = [
 328              'field' => $field,
 329              'label' => 'Field label: ' . $field
 330          ];
 331  
 332          $testobject = new phpunit_gradeimport_csv_load_data();
 333  
 334          // Check whether the user exists. If so, then the user id is returned. Otherwise, it returns null.
 335          $userid = $testobject->test_check_user_exists($value, $userfields);
 336  
 337          if ($successexpected) {
 338              // Check that the user id returned matches with the user that we created.
 339              $this->assertEquals($user->id, $userid);
 340  
 341              // Check that there are no errors.
 342              $this->assertEmpty($testobject->get_gradebookerrors());
 343  
 344          } else {
 345              // Check that the userid is null.
 346              $this->assertNull($userid);
 347  
 348              // Check that expected error message and actual message match.
 349              $gradebookerrors = $testobject->get_gradebookerrors();
 350              $mappingobject = (object)[
 351                  'field' => $userfields['label'],
 352                  'value' => $value,
 353              ];
 354              if ($allowaccountssameemail) {
 355                  $expectederrormessage = get_string('usermappingerrormultipleusersfound', 'grades', $mappingobject);
 356              } else {
 357                  $expectederrormessage = get_string('usermappingerror', 'grades', $mappingobject);
 358              }
 359  
 360              $this->assertEquals($expectederrormessage, $gradebookerrors[0]);
 361          }
 362      }
 363  
 364      /**
 365       * Test preparing feedback for inserting / updating into the gradebook.
 366       */
 367      public function test_create_feedback() {
 368  
 369          $testarray = $this->csv_load($this->oktext);
 370          $testobject = new phpunit_gradeimport_csv_load_data();
 371  
 372          // Try to insert some feedback for an assessment.
 373          $feedback = $testobject->test_create_feedback($this->courseid, 1, $testarray[0][7]);
 374  
 375          // Expected result.
 376          $expectedfeedback = array('itemid' => 1, 'feedback' => $testarray[0][7]);
 377          $this->assertEquals((array)$feedback, $expectedfeedback);
 378      }
 379  
 380      /**
 381       * Test preparing grade_items for upgrading into the gradebook.
 382       */
 383      public function test_update_grade_item() {
 384  
 385          $testarray = $this->csv_load($this->oktext);
 386          $testobject = new phpunit_gradeimport_csv_load_data();
 387  
 388          // We're not using scales so no to this option.
 389          $verbosescales = 0;
 390          // Map and key are to retrieve the grade_item that we are updating.
 391          $map = array(1);
 392          $key = 0;
 393          // We return the new grade array for saving.
 394          $newgrades = $testobject->test_update_grade_item($this->courseid, $map, $key, $verbosescales, $testarray[0][6]);
 395  
 396          $expectedresult = array();
 397          $expectedresult[0] = new stdClass();
 398          $expectedresult[0]->itemid = 1;
 399          $expectedresult[0]->finalgrade = $testarray[0][6];
 400  
 401          $this->assertEquals($newgrades, $expectedresult);
 402  
 403          // Try sending a bad grade value (A letter instead of a float / int).
 404          $newgrades = $testobject->test_update_grade_item($this->courseid, $map, $key, $verbosescales, 'A');
 405          // The $newgrades variable should be null.
 406          $this->assertNull($newgrades);
 407          $expectederrormessage = get_string('badgrade', 'grades');
 408          // Check that the error message is what we expect.
 409          $gradebookerrors = $testobject->get_gradebookerrors();
 410          $this->assertEquals($expectederrormessage, $gradebookerrors[0]);
 411      }
 412  
 413      /**
 414       * Test importing data and mapping it with items in the course.
 415       */
 416      public function test_map_user_data_with_value() {
 417          // Need to add one of the users into the system.
 418          $user = new stdClass();
 419          $user->firstname = 'Anne';
 420          $user->lastname = 'Able';
 421          $user->email = 'student7@example.com';
 422          $userdetail = $this->getDataGenerator()->create_user($user);
 423  
 424          $testarray = $this->csv_load($this->oktext);
 425          $testobject = new phpunit_gradeimport_csv_load_data();
 426  
 427          // We're not using scales so no to this option.
 428          $verbosescales = 0;
 429          // Map and key are to retrieve the grade_item that we are updating.
 430          $map = array(1);
 431          $key = 0;
 432  
 433          // Test new user mapping. This should return the user id if there were no problems.
 434          $userid = $testobject->test_map_user_data_with_value('useremail', $testarray[0][5], $this->columns, $map, $key,
 435                  $this->courseid, $map[$key], $verbosescales);
 436          $this->assertEquals($userid, $userdetail->id);
 437  
 438          $newgrades = $testobject->test_map_user_data_with_value('new', $testarray[0][6], $this->columns, $map, $key,
 439                  $this->courseid, $map[$key], $verbosescales);
 440          // Check that the final grade is the same as the one inserted.
 441          $this->assertEquals($testarray[0][6], $newgrades[0]->finalgrade);
 442  
 443          $newgrades = $testobject->test_map_user_data_with_value('new', $testarray[0][8], $this->columns, $map, $key,
 444                  $this->courseid, $map[$key], $verbosescales);
 445          // Check that the final grade is the same as the one inserted.
 446          // The testobject should now contain 2 new grade items.
 447          $this->assertEquals(2, count($newgrades));
 448          // Because this grade item is empty, the value for final grade should be null.
 449          $this->assertNull($newgrades[1]->finalgrade);
 450  
 451          $feedback = $testobject->test_map_user_data_with_value('feedback', $testarray[0][7], $this->columns, $map, $key,
 452                  $this->courseid, $map[$key], $verbosescales);
 453          // Expected result.
 454          $resultarray = array();
 455          $resultarray[0] = new stdClass();
 456          $resultarray[0]->itemid = 1;
 457          $resultarray[0]->feedback = $testarray[0][7];
 458          $this->assertEquals($feedback, $resultarray);
 459  
 460          // Default behaviour (update a grade item).
 461          $newgrades = $testobject->test_map_user_data_with_value('default', $testarray[0][6], $this->columns, $map, $key,
 462                  $this->courseid, $map[$key], $verbosescales);
 463          $this->assertEquals($testarray[0][6], $newgrades[0]->finalgrade);
 464      }
 465  
 466      /**
 467       * Test importing data into the gradebook.
 468       */
 469      public function test_prepare_import_grade_data() {
 470          global $DB;
 471  
 472          // Need to add one of the users into the system.
 473          $user = new stdClass();
 474          $user->firstname = 'Anne';
 475          $user->lastname = 'Able';
 476          $user->email = 'student7@example.com';
 477          // Insert user 1.
 478          $this->getDataGenerator()->create_user($user);
 479          $user = new stdClass();
 480          $user->firstname = 'Bobby';
 481          $user->lastname = 'Bunce';
 482          $user->email = 'student5@example.com';
 483          // Insert user 2.
 484          $this->getDataGenerator()->create_user($user);
 485  
 486          $this->csv_load($this->oktext);
 487  
 488          $importcode = 007;
 489          $verbosescales = 0;
 490  
 491          // Form data object.
 492          $formdata = new stdClass();
 493          $formdata->mapfrom = 5;
 494          $formdata->mapto = 'useremail';
 495          $formdata->mapping_0 = 0;
 496          $formdata->mapping_1 = 0;
 497          $formdata->mapping_2 = 0;
 498          $formdata->mapping_3 = 0;
 499          $formdata->mapping_4 = 0;
 500          $formdata->mapping_5 = 0;
 501          $formdata->mapping_6 = 'new';
 502          $formdata->mapping_7 = 'feedback_2';
 503          $formdata->mapping_8 = 0;
 504          $formdata->mapping_9 = 0;
 505          $formdata->map = 1;
 506          $formdata->id = 2;
 507          $formdata->iid = $this->iid;
 508          $formdata->importcode = $importcode;
 509          $formdata->forceimport = false;
 510  
 511          // Blam go time.
 512          $testobject = new phpunit_gradeimport_csv_load_data();
 513          $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport, $this->courseid, '', '',
 514                  $verbosescales);
 515          // If everything inserted properly then this should be true.
 516          $this->assertTrue($dataloaded);
 517      }
 518  
 519      /*
 520       * Test importing csv data into the gradebook using "Last downloaded from this course" column and force import option.
 521       */
 522      public function test_force_import_option () {
 523  
 524          // Need to add users into the system.
 525          $user = new stdClass();
 526          $user->firstname = 'Anne';
 527          $user->lastname = 'Able';
 528          $user->email = 'student7@example.com';
 529          $user->id_number = 1;
 530          $user1 = $this->getDataGenerator()->create_user($user);
 531          $user = new stdClass();
 532          $user->firstname = 'Bobby';
 533          $user->lastname = 'Bunce';
 534          $user->email = 'student5@example.com';
 535          $user->id_number = 2;
 536          $user2 = $this->getDataGenerator()->create_user($user);
 537  
 538          // Create a new grade item.
 539          $params = array(
 540              'itemtype'  => 'manual',
 541              'itemname'  => 'Grade item 1',
 542              'gradetype' => GRADE_TYPE_VALUE,
 543              'courseid'  => $this->courseid
 544          );
 545          $gradeitem = new grade_item($params, false);
 546          $gradeitemid = $gradeitem->insert();
 547  
 548          $importcode = 001;
 549          $verbosescales = 0;
 550  
 551          // Form data object.
 552          $formdata = new stdClass();
 553          $formdata->mapfrom = 5;
 554          $formdata->mapto = 'useremail';
 555          $formdata->mapping_0 = 0;
 556          $formdata->mapping_1 = 0;
 557          $formdata->mapping_2 = 0;
 558          $formdata->mapping_3 = 0;
 559          $formdata->mapping_4 = 0;
 560          $formdata->mapping_5 = 0;
 561          $formdata->mapping_6 = $gradeitemid;
 562          $formdata->mapping_7 = 'feedback_2';
 563          $formdata->mapping_8 = 0;
 564          $formdata->mapping_9 = 0;
 565          $formdata->map = 1;
 566          $formdata->id = 2;
 567          $formdata->iid = $this->iid;
 568          $formdata->importcode = $importcode;
 569          $formdata->forceimport = false;
 570  
 571          // Add last download from this course column to csv content.
 572          $exportdate = time();
 573          $newcsvdata = str_replace('{exportdate}', $exportdate, $this->csvtext);
 574          $this->csv_load($newcsvdata);
 575          $testobject = new phpunit_gradeimport_csv_load_data();
 576          $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
 577                  $this->courseid, '', '', $verbosescales);
 578          $this->assertTrue($dataloaded);
 579  
 580          // We must update the last modified date.
 581          grade_import_commit($this->courseid, $importcode, false, false);
 582  
 583          // Test using force import disabled and a date in the past.
 584          $pastdate = strtotime('-1 day', time());
 585          $newcsvdata = str_replace('{exportdate}', $pastdate, $this->csvtext);
 586          $this->csv_load($newcsvdata);
 587          $testobject = new phpunit_gradeimport_csv_load_data();
 588          $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
 589                  $this->courseid, '', '', $verbosescales);
 590          $this->assertFalse($dataloaded);
 591          $errors = $testobject->get_gradebookerrors();
 592          $this->assertEquals($errors[0], get_string('gradealreadyupdated', 'grades', fullname($user1)));
 593  
 594          // Test using force import enabled and a date in the past.
 595          $formdata->forceimport = true;
 596          $testobject = new phpunit_gradeimport_csv_load_data();
 597          $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
 598                  $this->courseid, '', '', $verbosescales);
 599          $this->assertTrue($dataloaded);
 600  
 601          // Test importing using an old exported file (2 years ago).
 602          $formdata->forceimport = false;
 603          $twoyearsago = strtotime('-2 year', time());
 604          $newcsvdata = str_replace('{exportdate}', $twoyearsago, $this->csvtext);
 605          $this->csv_load($newcsvdata);
 606          $testobject = new phpunit_gradeimport_csv_load_data();
 607          $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
 608                  $this->courseid, '', '', $verbosescales);
 609          $this->assertFalse($dataloaded);
 610          $errors = $testobject->get_gradebookerrors();
 611          $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
 612  
 613          // Test importing using invalid exported date.
 614          $baddate = '0123A56B89';
 615          $newcsvdata = str_replace('{exportdate}', $baddate, $this->csvtext);
 616          $this->csv_load($newcsvdata);
 617          $formdata->mapping_6 = $gradeitemid;
 618          $testobject = new phpunit_gradeimport_csv_load_data();
 619          $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
 620                  $this->courseid, '', '', $verbosescales);
 621          $this->assertFalse($dataloaded);
 622          $errors = $testobject->get_gradebookerrors();
 623          $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
 624  
 625          // Test importing using date in the future.
 626          $oneyearahead = strtotime('+1 year', time());
 627          $oldcsv = str_replace('{exportdate}', $oneyearahead, $this->csvtext);
 628          $this->csv_load($oldcsv);
 629          $formdata->mapping_6 = $gradeitemid;
 630          $testobject = new phpunit_gradeimport_csv_load_data();
 631          $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
 632              $this->courseid, '', '', $verbosescales);
 633          $this->assertFalse($dataloaded);
 634          $errors = $testobject->get_gradebookerrors();
 635          $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
 636      }
 637  }