Merge branch 'MDL-46599-master' of git://github.com/lameze/moodle
[moodle.git] / grade / import / csv / tests / load_data_test.php
index 1d57c7d..68a1521 100644 (file)
@@ -50,6 +50,11 @@ Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,,75.00';
 Anne,Able,,"Moodle HQ","Rock on!",student7@mail.com,56.00,56.00,78.00
 Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,75.00';
 
+    /** @var string $csvtext CSV data to be imported with Last download from this course column. */
+    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"
+Anne,Able,,"Moodle HQ","Rock on!",student7@mail.com,56.00,"We welcome feedback",56.00,{exportdate}
+Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,,75.00,{exportdate}';
+
     /** @var int $iid Import ID. */
     protected $iid;
 
@@ -419,10 +424,12 @@ Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,75.00';
         $formdata->mapping_6 = 'new';
         $formdata->mapping_7 = 'feedback_2';
         $formdata->mapping_8 = 0;
+        $formdata->mapping_9 = 0;
         $formdata->map = 1;
         $formdata->id = 2;
         $formdata->iid = $this->iid;
         $formdata->importcode = $importcode;
+        $formdata->forceimport = false;
 
         // Blam go time.
         $testobject = new phpunit_gradeimport_csv_load_data();
@@ -431,4 +438,123 @@ Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,75.00';
         // If everything inserted properly then this should be true.
         $this->assertTrue($dataloaded);
     }
+
+    /*
+     * Test importing csv data into the gradebook using "Last downloaded from this course" column and force import option.
+     */
+    public function test_force_import_option () {
+
+        // Need to add users into the system.
+        $user = new stdClass();
+        $user->firstname = 'Anne';
+        $user->lastname = 'Able';
+        $user->email = 'student7@mail.com';
+        $user->id_number = 1;
+        $user1 = $this->getDataGenerator()->create_user($user);
+        $user = new stdClass();
+        $user->firstname = 'Bobby';
+        $user->lastname = 'Bunce';
+        $user->email = 'student5@mail.com';
+        $user->id_number = 2;
+        $user2 = $this->getDataGenerator()->create_user($user);
+
+        // Create a new grade item.
+        $params = array(
+            'itemtype'  => 'manual',
+            'itemname'  => 'Grade item 1',
+            'gradetype' => GRADE_TYPE_VALUE,
+            'courseid'  => $this->courseid
+        );
+        $gradeitem = new grade_item($params, false);
+        $gradeitemid = $gradeitem->insert();
+
+        $importcode = 001;
+        $verbosescales = 0;
+
+        // Form data object.
+        $formdata = new stdClass();
+        $formdata->mapfrom = 5;
+        $formdata->mapto = 'useremail';
+        $formdata->mapping_0 = 0;
+        $formdata->mapping_1 = 0;
+        $formdata->mapping_2 = 0;
+        $formdata->mapping_3 = 0;
+        $formdata->mapping_4 = 0;
+        $formdata->mapping_5 = 0;
+        $formdata->mapping_6 = $gradeitemid;
+        $formdata->mapping_7 = 'feedback_2';
+        $formdata->mapping_8 = 0;
+        $formdata->mapping_9 = 0;
+        $formdata->map = 1;
+        $formdata->id = 2;
+        $formdata->iid = $this->iid;
+        $formdata->importcode = $importcode;
+        $formdata->forceimport = false;
+
+        // Add last download from this course column to csv content.
+        $exportdate = time();
+        $newcsvdata = str_replace('{exportdate}', $exportdate, $this->csvtext);
+        $this->csv_load($newcsvdata);
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertTrue($dataloaded);
+
+        // We must update the last modified date.
+        grade_import_commit($this->courseid, $importcode, false, false);
+
+        // Test using force import disabled and a date in the past.
+        $pastdate = strtotime('-1 day', time());
+        $newcsvdata = str_replace('{exportdate}', $pastdate, $this->csvtext);
+        $this->csv_load($newcsvdata);
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertFalse($dataloaded);
+        $errors = $testobject->get_gradebookerrors();
+        $this->assertEquals($errors[0], get_string('gradealreadyupdated', 'grades', fullname($user1)));
+
+        // Test using force import enabled and a date in the past.
+        $formdata->forceimport = true;
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertTrue($dataloaded);
+
+        // Test importing using an old exported file (2 years ago).
+        $formdata->forceimport = false;
+        $twoyearsago = strtotime('-2 year', time());
+        $newcsvdata = str_replace('{exportdate}', $twoyearsago, $this->csvtext);
+        $this->csv_load($newcsvdata);
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertFalse($dataloaded);
+        $errors = $testobject->get_gradebookerrors();
+        $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
+
+        // Test importing using invalid exported date.
+        $baddate = '0123A56B89';
+        $newcsvdata = str_replace('{exportdate}', $baddate, $this->csvtext);
+        $this->csv_load($newcsvdata);
+        $formdata->mapping_6 = $gradeitemid;
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertFalse($dataloaded);
+        $errors = $testobject->get_gradebookerrors();
+        $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
+
+        // Test importing using date in the future.
+        $oneyearahead = strtotime('+1 year', time());
+        $oldcsv = str_replace('{exportdate}', $oneyearahead, $this->csvtext);
+        $this->csv_load($oldcsv);
+        $formdata->mapping_6 = $gradeitemid;
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+            $this->courseid, '', '', $verbosescales);
+        $this->assertFalse($dataloaded);
+        $errors = $testobject->get_gradebookerrors();
+        $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
+    }
 }