Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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