mirrored from git://git.moodle.org/moodle.git
/
externallib_test.php
1331 lines (1163 loc) · 60 KB
/
externallib_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Database module external functions tests
*
* @package mod_data
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Database module external functions tests
*
* @package mod_data
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class mod_data_external_testcase extends externallib_advanced_testcase {
/** @var stdClass Test module context. */
protected $context;
/** @var stdClass Test course.*/
protected $course;
/** @var stdClass Test course module. */
protected $cm;
/** @var stdClass Test database activity. */
protected $database;
/** @var stdClass Test group 1. */
protected $group1;
/** @var stdClass Test group 2. */
protected $group2;
/** @var stdClass Test student 1. */
protected $student1;
/** @var stdClass Test student 2. */
protected $student2;
/** @var stdClass Test student 3. */
protected $student3;
/** @var stdClass Test student 4. */
protected $student4;
/** @var stdClass Student role. */
protected $studentrole;
/** @var stdClass Test teacher. */
protected $teacher;
/** @var stdClass Teacher role. */
protected $teacherrole;
/**
* Set up for every test
*/
public function setUp() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$course = new stdClass();
$course->groupmode = SEPARATEGROUPS;
$course->groupmodeforce = true;
$this->course = $this->getDataGenerator()->create_course($course);
$this->database = $this->getDataGenerator()->create_module('data', array('course' => $this->course->id));
$this->context = context_module::instance($this->database->cmid);
$this->cm = get_coursemodule_from_instance('data', $this->database->id);
// Create users.
$this->student1 = self::getDataGenerator()->create_user(['firstname' => 'Olivia', 'lastname' => 'Smith']);
$this->student2 = self::getDataGenerator()->create_user(['firstname' => 'Ezra', 'lastname' => 'Johnson']);
$this->student3 = self::getDataGenerator()->create_user(['firstname' => 'Amelia', 'lastname' => 'Williams']);
$this->teacher = self::getDataGenerator()->create_user(['firstname' => 'Asher', 'lastname' => 'Jones']);
// Users enrolments.
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($this->student1->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($this->student2->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($this->student3->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
$this->group1 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
$this->group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
groups_add_member($this->group1, $this->student1);
groups_add_member($this->group1, $this->student2);
groups_add_member($this->group2, $this->student3);
}
/**
* Test get databases by courses
*/
public function test_mod_data_get_databases_by_courses() {
global $DB;
$this->resetAfterTest(true);
// Create users.
$student = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();
// Set to the student user.
self::setUser($student);
// Create courses to add the modules.
$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course();
// First database.
$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course1->id;
$database1 = self::getDataGenerator()->create_module('data', $record);
// Second database.
$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course2->id;
$database2 = self::getDataGenerator()->create_module('data', $record);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
// Users enrolments.
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacher->id, $course1->id, $teacherrole->id, 'manual');
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course2->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance2 = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance2, $student->id, $studentrole->id);
// Create what we expect to be returned when querying the two courses.
// First for the student user.
$expectedfields = array('id', 'coursemodule', 'course', 'name', 'comments', 'timeavailablefrom',
'timeavailableto', 'timeviewfrom', 'timeviewto', 'requiredentries', 'requiredentriestoview',
'intro', 'introformat', 'introfiles', 'maxentries', 'rssarticles', 'singletemplate', 'listtemplate',
'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate',
'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'defaultsort', 'defaultsortdir', 'manageapproved');
// Add expected coursemodule.
$database1->coursemodule = $database1->cmid;
$database1->introfiles = [];
$database2->coursemodule = $database2->cmid;
$database2->introfiles = [];
$expected1 = array();
$expected2 = array();
foreach ($expectedfields as $field) {
if ($field == 'approval' or $field == 'manageapproved') {
$database1->{$field} = (bool) $database1->{$field};
$database2->{$field} = (bool) $database2->{$field};
}
$expected1[$field] = $database1->{$field};
$expected2[$field] = $database2->{$field};
}
$expected1['comments'] = (bool) $expected1['comments'];
$expected2['comments'] = (bool) $expected2['comments'];
$expecteddatabases = array();
$expecteddatabases[] = $expected2;
$expecteddatabases[] = $expected1;
// Call the external function passing course ids.
$result = mod_data_external::get_databases_by_courses(array($course2->id, $course1->id));
$result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
// Call the external function without passing course id.
$result = mod_data_external::get_databases_by_courses();
$result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
// Unenrol user from second course and alter expected databases.
$enrol->unenrol_user($instance2, $student->id);
array_shift($expecteddatabases);
// Call the external function without passing course id.
$result = mod_data_external::get_databases_by_courses();
$result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
// Call for the second course we unenrolled the user from, expected warning.
$result = mod_data_external::get_databases_by_courses(array($course2->id));
$this->assertCount(1, $result['warnings']);
$this->assertEquals('1', $result['warnings'][0]['warningcode']);
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
// Now, try as a teacher for getting all the additional fields.
self::setUser($teacher);
$additionalfields = array('scale', 'assessed', 'assesstimestart', 'assesstimefinish', 'editany', 'notification', 'timemodified');
foreach ($additionalfields as $field) {
if ($field == 'editany') {
$database1->{$field} = (bool) $database1->{$field};
}
$expecteddatabases[0][$field] = $database1->{$field};
}
$result = mod_data_external::get_databases_by_courses();
$result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
// Admin should get all the information.
self::setAdminUser();
$result = mod_data_external::get_databases_by_courses(array($course1->id));
$result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
}
/**
* Test view_database invalid id.
*/
public function test_view_database_invalid_id() {
// Test invalid instance id.
$this->expectException('moodle_exception');
mod_data_external::view_database(0);
}
/**
* Test view_database not enrolled user.
*/
public function test_view_database_not_enrolled_user() {
$usernotenrolled = self::getDataGenerator()->create_user();
$this->setUser($usernotenrolled);
$this->expectException('moodle_exception');
mod_data_external::view_database(0);
}
/**
* Test view_database no capabilities.
*/
public function test_view_database_no_capabilities() {
// Test user with no capabilities.
// We need a explicit prohibit since this capability is allowed for students by default.
assign_capability('mod/data:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
accesslib_clear_all_caches_for_unit_testing();
$this->expectException('moodle_exception');
mod_data_external::view_database(0);
}
/**
* Test view_database.
*/
public function test_view_database() {
// Test user with full capabilities.
$this->setUser($this->student1);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = mod_data_external::view_database($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::view_database_returns(), $result);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_data\event\course_module_viewed', $event);
$this->assertEquals($this->context, $event->get_context());
$moodledata = new \moodle_url('/mod/data/view.php', array('id' => $this->cm->id));
$this->assertEquals($moodledata, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
/**
* Test get_data_access_information for student.
*/
public function test_get_data_access_information_student() {
global $DB;
// Modify the database to add access restrictions.
$this->database->timeavailablefrom = time() + DAYSECS;
$this->database->requiredentries = 2;
$this->database->requiredentriestoview = 2;
$DB->update_record('data', $this->database);
// Test user with full capabilities.
$this->setUser($this->student1);
$result = mod_data_external::get_data_access_information($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result);
$this->assertEquals($this->group1->id, $result['groupid']);
$this->assertFalse($result['canmanageentries']);
$this->assertFalse($result['canapprove']);
$this->assertTrue($result['canaddentry']); // It return true because it doen't check time restrictions.
$this->assertFalse($result['timeavailable']);
$this->assertFalse($result['inreadonlyperiod']);
$this->assertEquals(0, $result['numentries']);
$this->assertEquals($this->database->requiredentries, $result['entrieslefttoadd']);
$this->assertEquals($this->database->requiredentriestoview, $result['entrieslefttoview']);
}
/**
* Test get_data_access_information for teacher.
*/
public function test_get_data_access_information_teacher() {
global $DB;
// Modify the database to add access restrictions.
$this->database->timeavailablefrom = time() + DAYSECS;
$this->database->requiredentries = 2;
$this->database->requiredentriestoview = 2;
$DB->update_record('data', $this->database);
// Test user with full capabilities.
$this->setUser($this->teacher);
$result = mod_data_external::get_data_access_information($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result);
$this->assertEquals(0, $result['groupid']);
$this->assertTrue($result['canmanageentries']);
$this->assertTrue($result['canapprove']);
$this->assertTrue($result['canaddentry']); // It return true because it doen't check time restrictions.
$this->assertTrue($result['timeavailable']);
$this->assertFalse($result['inreadonlyperiod']);
$this->assertEquals(0, $result['numentries']);
$this->assertEquals(0, $result['entrieslefttoadd']);
$this->assertEquals(0, $result['entrieslefttoview']);
}
/**
* Helper method to populate the database with some entries.
*
* @return array the entry ids created
*/
public function populate_database_with_entries() {
global $DB;
// Force approval.
$DB->set_field('data', 'approval', 1, array('id' => $this->database->id));
$generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url');
$count = 1;
// Creating test Fields with default parameter values.
foreach ($fieldtypes as $fieldtype) {
$fieldname = 'field-' . $count;
$record = new StdClass();
$record->name = $fieldname;
$record->type = $fieldtype;
$record->required = 1;
$generator->create_field($record, $this->database);
$count++;
}
// Get all the fields created.
$fields = $DB->get_records('data_fields', array('dataid' => $this->database->id), 'id');
// Populate with contents, creating a new entry.
$contents = array();
$contents[] = array('opt1', 'opt2', 'opt3', 'opt4');
$contents[] = '01-01-2037'; // It should be lower than 2038, to avoid failing on 32-bit windows.
$contents[] = 'menu1';
$contents[] = array('multimenu1', 'multimenu2', 'multimenu3', 'multimenu4');
$contents[] = '12345';
$contents[] = 'radioopt1';
$contents[] = 'text for testing';
$contents[] = '<p>text area testing<br /></p>';
$contents[] = array('example.url', 'sampleurl');
$count = 0;
$fieldcontents = array();
foreach ($fields as $fieldrecord) {
$fieldcontents[$fieldrecord->id] = $contents[$count++];
}
$this->setUser($this->student1);
$entry11 = $generator->create_entry($this->database, $fieldcontents, $this->group1->id);
$this->setUser($this->student2);
$entry12 = $generator->create_entry($this->database, $fieldcontents, $this->group1->id);
$entry13 = $generator->create_entry($this->database, $fieldcontents, $this->group1->id);
// Entry not in group.
$entry14 = $generator->create_entry($this->database, $fieldcontents, 0);
$this->setUser($this->student3);
$entry21 = $generator->create_entry($this->database, $fieldcontents, $this->group2->id);
// Approve all except $entry13.
$DB->set_field('data_records', 'approved', 1, ['id' => $entry11]);
$DB->set_field('data_records', 'approved', 1, ['id' => $entry12]);
$DB->set_field('data_records', 'approved', 1, ['id' => $entry14]);
$DB->set_field('data_records', 'approved', 1, ['id' => $entry21]);
return [$entry11, $entry12, $entry13, $entry14, $entry21];
}
/**
* Test get_entries
*/
public function test_get_entries() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
// First of all, expect to see only my group entries (not other users in other groups ones).
// We may expect entries without group also.
$this->setUser($this->student1);
$result = mod_data_external::get_entries($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(3, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
$this->assertEquals($entry11, $result['entries'][0]['id']);
$this->assertEquals($this->student1->id, $result['entries'][0]['userid']);
$this->assertEquals($this->group1->id, $result['entries'][0]['groupid']);
$this->assertEquals($this->database->id, $result['entries'][0]['dataid']);
$this->assertEquals($entry12, $result['entries'][1]['id']);
$this->assertEquals($this->student2->id, $result['entries'][1]['userid']);
$this->assertEquals($this->group1->id, $result['entries'][1]['groupid']);
$this->assertEquals($this->database->id, $result['entries'][1]['dataid']);
$this->assertEquals($entry14, $result['entries'][2]['id']);
$this->assertEquals($this->student2->id, $result['entries'][2]['userid']);
$this->assertEquals(0, $result['entries'][2]['groupid']);
$this->assertEquals($this->database->id, $result['entries'][2]['dataid']);
// Other user in same group.
$this->setUser($this->student2);
$result = mod_data_external::get_entries($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(4, $result['entries']); // I can see my entry not approved yet.
$this->assertEquals(4, $result['totalcount']);
// Now try with the user in the second group that must see only two entries (his group entry and the one without group).
$this->setUser($this->student3);
$result = mod_data_external::get_entries($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(2, $result['entries']);
$this->assertEquals(2, $result['totalcount']);
$this->assertEquals($entry14, $result['entries'][0]['id']);
$this->assertEquals($this->student2->id, $result['entries'][0]['userid']);
$this->assertEquals(0, $result['entries'][0]['groupid']);
$this->assertEquals($this->database->id, $result['entries'][0]['dataid']);
$this->assertEquals($entry21, $result['entries'][1]['id']);
$this->assertEquals($this->student3->id, $result['entries'][1]['userid']);
$this->assertEquals($this->group2->id, $result['entries'][1]['groupid']);
$this->assertEquals($this->database->id, $result['entries'][1]['dataid']);
// Now, as teacher we should see all (we have permissions to view all groups).
$this->setUser($this->teacher);
$result = mod_data_external::get_entries($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(5, $result['entries']); // I can see the not approved one.
$this->assertEquals(5, $result['totalcount']);
$entries = $DB->get_records('data_records', array('dataid' => $this->database->id), 'id');
$this->assertCount(5, $entries);
$count = 0;
foreach ($entries as $entry) {
$this->assertEquals($entry->id, $result['entries'][$count]['id']);
$count++;
}
// Basic test passing the parameter (instead having to calculate it).
$this->setUser($this->student1);
$result = mod_data_external::get_entries($this->database->id, $this->group1->id);
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(3, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
// Test ordering (reverse).
$this->setUser($this->student1);
$result = mod_data_external::get_entries($this->database->id, $this->group1->id, false, null, 'DESC');
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(3, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
$this->assertEquals($entry14, $result['entries'][0]['id']);
// Test pagination.
$this->setUser($this->student1);
$result = mod_data_external::get_entries($this->database->id, $this->group1->id, false, null, null, 0, 1);
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(1, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
$this->assertEquals($entry11, $result['entries'][0]['id']);
$result = mod_data_external::get_entries($this->database->id, $this->group1->id, false, null, null, 1, 1);
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(1, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
$this->assertEquals($entry12, $result['entries'][0]['id']);
// Now test the return contents.
data_generate_default_template($this->database, 'listtemplate', 0, false, true); // Generate a default list template.
$result = mod_data_external::get_entries($this->database->id, $this->group1->id, true, null, null, 0, 2);
$result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(2, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
$this->assertCount(9, $result['entries'][0]['contents']);
$this->assertCount(9, $result['entries'][1]['contents']);
// Search for some content.
$this->assertTrue(strpos($result['listviewcontents'], 'opt1') !== false);
$this->assertTrue(strpos($result['listviewcontents'], 'January') !== false);
$this->assertTrue(strpos($result['listviewcontents'], 'menu1') !== false);
$this->assertTrue(strpos($result['listviewcontents'], 'text for testing') !== false);
$this->assertTrue(strpos($result['listviewcontents'], 'sampleurl') !== false);
}
/**
* Test get_entry_visible_groups.
*/
public function test_get_entry_visible_groups() {
global $DB;
$DB->set_field('course', 'groupmode', VISIBLEGROUPS, ['id' => $this->course->id]);
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
// Check I can see my approved group entries.
$this->setUser($this->student1);
$result = mod_data_external::get_entry($entry11);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertEquals($entry11, $result['entry']['id']);
$this->assertTrue($result['entry']['approved']);
$this->assertTrue($result['entry']['canmanageentry']); // Is mine, I can manage it.
// Entry from other group.
$result = mod_data_external::get_entry($entry21);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertEquals($entry21, $result['entry']['id']);
}
/**
* Test get_entry_separated_groups.
*/
public function test_get_entry_separated_groups() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
// Check I can see my approved group entries.
$this->setUser($this->student1);
$result = mod_data_external::get_entry($entry11);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertEquals($entry11, $result['entry']['id']);
$this->assertTrue($result['entry']['approved']);
$this->assertTrue($result['entry']['canmanageentry']); // Is mine, I can manage it.
// Retrieve contents.
data_generate_default_template($this->database, 'singletemplate', 0, false, true);
$result = mod_data_external::get_entry($entry11, true);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(9, $result['entry']['contents']);
$this->assertTrue(strpos($result['entryviewcontents'], 'opt1') !== false);
$this->assertTrue(strpos($result['entryviewcontents'], 'January') !== false);
$this->assertTrue(strpos($result['entryviewcontents'], 'menu1') !== false);
$this->assertTrue(strpos($result['entryviewcontents'], 'text for testing') !== false);
$this->assertTrue(strpos($result['entryviewcontents'], 'sampleurl') !== false);
// This is in my group but I'm not the author.
$result = mod_data_external::get_entry($entry12);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertEquals($entry12, $result['entry']['id']);
$this->assertTrue($result['entry']['approved']);
$this->assertFalse($result['entry']['canmanageentry']); // Not mine.
$this->setUser($this->student3);
$result = mod_data_external::get_entry($entry21);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertEquals($entry21, $result['entry']['id']);
$this->assertTrue($result['entry']['approved']);
$this->assertTrue($result['entry']['canmanageentry']); // Is mine, I can manage it.
// As teacher I should be able to see all the entries.
$this->setUser($this->teacher);
$result = mod_data_external::get_entry($entry11);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertEquals($entry11, $result['entry']['id']);
$result = mod_data_external::get_entry($entry12);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertEquals($entry12, $result['entry']['id']);
// This is the not approved one.
$result = mod_data_external::get_entry($entry13);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertEquals($entry13, $result['entry']['id']);
$result = mod_data_external::get_entry($entry21);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertEquals($entry21, $result['entry']['id']);
// Now, try to get an entry not approved yet.
$this->setUser($this->student1);
$this->expectException('moodle_exception');
$result = mod_data_external::get_entry($entry13);
}
/**
* Test get_entry from other group in separated groups.
*/
public function test_get_entry_other_group_separated_groups() {
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
// We should not be able to view other gropu entries (in separated groups).
$this->setUser($this->student1);
$this->expectException('moodle_exception');
$result = mod_data_external::get_entry($entry21);
}
/**
* Test get_fields.
*/
public function test_get_fields() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->student1);
$result = mod_data_external::get_fields($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::get_fields_returns(), $result);
// Basically compare we retrieve all the fields and the correct values.
$fields = $DB->get_records('data_fields', array('dataid' => $this->database->id), 'id');
foreach ($result['fields'] as $field) {
$this->assertEquals($field, (array) $fields[$field['id']]);
}
}
/**
* Test get_fields_database_without_fields.
*/
public function test_get_fields_database_without_fields() {
$this->setUser($this->student1);
$result = mod_data_external::get_fields($this->database->id);
$result = external_api::clean_returnvalue(mod_data_external::get_fields_returns(), $result);
$this->assertEmpty($result['fields']);
}
/**
* Test search_entries.
*/
public function test_search_entries() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->student1);
// Empty search, it should return all the visible entries.
$result = mod_data_external::search_entries($this->database->id, 0, false);
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(3, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
// Search for something that does not exists.
$result = mod_data_external::search_entries($this->database->id, 0, false, 'abc');
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(0, $result['entries']);
$this->assertEquals(0, $result['totalcount']);
// Search by text matching all the entries.
$result = mod_data_external::search_entries($this->database->id, 0, false, 'text');
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(3, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
$this->assertEquals(3, $result['maxcount']);
// Now as the other student I should receive my not approved entry. Apply ordering here.
$this->setUser($this->student2);
$result = mod_data_external::search_entries($this->database->id, 0, false, 'text', [], DATA_APPROVED, 'ASC');
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(4, $result['entries']);
$this->assertEquals(4, $result['totalcount']);
$this->assertEquals(4, $result['maxcount']);
// The not approved one should be the first.
$this->assertEquals($entry13, $result['entries'][0]['id']);
// Now as the other group student.
$this->setUser($this->student3);
$result = mod_data_external::search_entries($this->database->id, 0, false, 'text');
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(2, $result['entries']);
$this->assertEquals(2, $result['totalcount']);
$this->assertEquals(2, $result['maxcount']);
$this->assertEquals($this->student2->id, $result['entries'][0]['userid']);
$this->assertEquals($this->student3->id, $result['entries'][1]['userid']);
// Same normal text search as teacher.
$this->setUser($this->teacher);
$result = mod_data_external::search_entries($this->database->id, 0, false, 'text');
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(5, $result['entries']); // I can see all groups and non approved.
$this->assertEquals(5, $result['totalcount']);
$this->assertEquals(5, $result['maxcount']);
// Pagination.
$this->setUser($this->teacher);
$result = mod_data_external::search_entries($this->database->id, 0, false, 'text', [], DATA_TIMEADDED, 'ASC', 0, 2);
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(2, $result['entries']); // Only 2 per page.
$this->assertEquals(5, $result['totalcount']);
$this->assertEquals(5, $result['maxcount']);
// Now advanced search or not dinamic fields (user firstname for example).
$this->setUser($this->student1);
$advsearch = [
['name' => 'fn', 'value' => json_encode($this->student2->firstname)]
];
$result = mod_data_external::search_entries($this->database->id, 0, false, '', $advsearch);
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(2, $result['entries']);
$this->assertEquals(2, $result['totalcount']);
$this->assertEquals(3, $result['maxcount']);
$this->assertEquals($this->student2->id, $result['entries'][0]['userid']); // I only found mine!
// Advanced search for fields.
$field = $DB->get_record('data_fields', array('type' => 'url'));
$advsearch = [
['name' => 'f_' . $field->id , 'value' => 'sampleurl']
];
$result = mod_data_external::search_entries($this->database->id, 0, false, '', $advsearch);
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(3, $result['entries']); // Found two entries matching this.
$this->assertEquals(3, $result['totalcount']);
$this->assertEquals(3, $result['maxcount']);
// Combined search.
$field2 = $DB->get_record('data_fields', array('type' => 'number'));
$advsearch = [
['name' => 'f_' . $field->id , 'value' => 'sampleurl'],
['name' => 'f_' . $field2->id , 'value' => '12345'],
['name' => 'ln', 'value' => json_encode($this->student2->lastname)]
];
$result = mod_data_external::search_entries($this->database->id, 0, false, '', $advsearch);
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(2, $result['entries']); // Only one matching everything.
$this->assertEquals(2, $result['totalcount']);
$this->assertEquals(3, $result['maxcount']);
// Combined search (no results).
$field2 = $DB->get_record('data_fields', array('type' => 'number'));
$advsearch = [
['name' => 'f_' . $field->id , 'value' => 'sampleurl'],
['name' => 'f_' . $field2->id , 'value' => '98780333'], // Non existent number.
];
$result = mod_data_external::search_entries($this->database->id, 0, false, '', $advsearch);
$result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result);
$this->assertCount(0, $result['entries']); // Only one matching everything.
$this->assertEquals(0, $result['totalcount']);
$this->assertEquals(3, $result['maxcount']);
}
/**
* Test approve_entry.
*/
public function test_approve_entry() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->teacher);
$this->assertEquals(0, $DB->get_field('data_records', 'approved', array('id' => $entry13)));
$result = mod_data_external::approve_entry($entry13);
$result = external_api::clean_returnvalue(mod_data_external::approve_entry_returns(), $result);
$this->assertEquals(1, $DB->get_field('data_records', 'approved', array('id' => $entry13)));
}
/**
* Test unapprove_entry.
*/
public function test_unapprove_entry() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->teacher);
$this->assertEquals(1, $DB->get_field('data_records', 'approved', array('id' => $entry11)));
$result = mod_data_external::approve_entry($entry11, false);
$result = external_api::clean_returnvalue(mod_data_external::approve_entry_returns(), $result);
$this->assertEquals(0, $DB->get_field('data_records', 'approved', array('id' => $entry11)));
}
/**
* Test approve_entry missing permissions.
*/
public function test_approve_entry_missing_permissions() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->student1);
$this->expectException('moodle_exception');
mod_data_external::approve_entry($entry13);
}
/**
* Test delete_entry as teacher. Check I can delete any entry.
*/
public function test_delete_entry_as_teacher() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->teacher);
$result = mod_data_external::delete_entry($entry11);
$result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result);
$this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry11)));
// Entry in other group.
$result = mod_data_external::delete_entry($entry21);
$result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result);
$this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry21)));
}
/**
* Test delete_entry as student. Check I can delete my own entries.
*/
public function test_delete_entry_as_student() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->student1);
$result = mod_data_external::delete_entry($entry11);
$result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result);
$this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry11)));
}
/**
* Test delete_entry as student in read only mode period. Check I cannot delete my own entries in that period.
*/
public function test_delete_entry_as_student_in_read_only_period() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
// Set a time period.
$this->database->timeviewfrom = time() - HOURSECS;
$this->database->timeviewto = time() + HOURSECS;
$DB->update_record('data', $this->database);
$this->setUser($this->student1);
$this->expectException('moodle_exception');
mod_data_external::delete_entry($entry11);
}
/**
* Test delete_entry with an user missing permissions.
*/
public function test_delete_entry_missing_permissions() {
global $DB;
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->student1);
$this->expectException('moodle_exception');
mod_data_external::delete_entry($entry21);
}
/**
* Test add_entry.
*/
public function test_add_entry() {
global $DB;
// First create the record structure and add some entries.
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
$this->setUser($this->student1);
$newentrydata = [];
$fields = $DB->get_records('data_fields', array('dataid' => $this->database->id), 'id');
// Prepare the new entry data.
foreach ($fields as $field) {
$subfield = $value = '';
switch ($field->type) {
case 'checkbox':
$value = ['opt1', 'opt2'];
break;
case 'date':
// Add two extra.
$newentrydata[] = [
'fieldid' => $field->id,
'subfield' => 'day',
'value' => json_encode('5')
];
$newentrydata[] = [
'fieldid' => $field->id,
'subfield' => 'month',
'value' => json_encode('1')
];
$subfield = 'year';
$value = '1981';
break;
case 'menu':
$value = 'menu1';
break;
case 'multimenu':
$value = ['multimenu1', 'multimenu4'];
break;
case 'number':
$value = 6;
break;
case 'radiobutton':
$value = 'radioopt1';
break;
case 'text':
$value = 'some text';
break;
case 'textarea':
$newentrydata[] = [
'fieldid' => $field->id,
'subfield' => 'content1',
'value' => json_encode(FORMAT_MOODLE)
];
$newentrydata[] = [
'fieldid' => $field->id,
'subfield' => 'itemid',
'value' => json_encode(0)
];
$value = 'more text';
break;
case 'url':
$value = 'https://moodle.org';
$subfield = 0;
break;
}
$newentrydata[] = [
'fieldid' => $field->id,
'subfield' => $subfield,
'value' => json_encode($value)
];
}
$result = mod_data_external::add_entry($this->database->id, 0, $newentrydata);
$result = external_api::clean_returnvalue(mod_data_external::add_entry_returns(), $result);
$newentryid = $result['newentryid'];
$result = mod_data_external::get_entry($newentryid, true);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertEquals($this->student1->id, $result['entry']['userid']);
$this->assertCount(9, $result['entry']['contents']);
foreach ($result['entry']['contents'] as $content) {
$field = $fields[$content['fieldid']];
// Stored content same that the one retrieved by WS.
$dbcontent = $DB->get_record('data_content', array('fieldid' => $field->id, 'recordid' => $newentryid));
$this->assertEquals($dbcontent->content, $content['content']);
// Now double check everything stored is correct.
if ($field->type == 'checkbox') {
$this->assertEquals('opt1##opt2', $content['content']);
continue;
}
if ($field->type == 'date') {
$this->assertEquals(347500800, $content['content']); // Date in gregorian format.
continue;
}
if ($field->type == 'menu') {
$this->assertEquals('menu1', $content['content']);
continue;
}
if ($field->type == 'multimenu') {
$this->assertEquals('multimenu1##multimenu4', $content['content']);
continue;