Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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