Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 11
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * Feedback module external functions tests
-
 
19
 *
-
 
20
 * @package    mod_feedback
-
 
21
 * @category   external
-
 
22
 * @copyright  2017 Juan Leyva <juan@moodle.com>
-
 
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
24
 * @since      Moodle 3.3
-
 
25
 */
-
 
26
 
16
 
Línea 27... Línea 17...
27
namespace mod_feedback\external;
17
namespace mod_feedback\external;
28
 
18
 
29
use core_external\external_api;
19
use core_external\external_api;
Línea 47... Línea 37...
47
 * @copyright  2017 Juan Leyva <juan@moodle.com>
37
 * @copyright  2017 Juan Leyva <juan@moodle.com>
48
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49
 * @since      Moodle 3.3
39
 * @since      Moodle 3.3
50
 * @covers     \mod_feedback_external
40
 * @covers     \mod_feedback_external
51
 */
41
 */
52
class external_test extends externallib_advanced_testcase {
42
final class external_test extends externallib_advanced_testcase {
Línea 53... Línea 43...
53
 
43
 
54
    // TODO These should be removed.
44
    // TODO These should be removed.
55
    // Testcase classes should not have any properties or store state.
45
    // Testcase classes should not have any properties or store state.
56
    protected $course;
46
    protected $course;
Línea 118... Línea 108...
118
 
108
 
119
 
109
 
120
    /**
110
    /**
121
     * Test test_mod_feedback_get_feedbacks_by_courses
111
     * Test test_mod_feedback_get_feedbacks_by_courses
Línea 122... Línea 112...
122
     */
112
     */
123
    public function test_mod_feedback_get_feedbacks_by_courses() {
113
    public function test_mod_feedback_get_feedbacks_by_courses(): void {
Línea 124... Línea 114...
124
 
114
 
Línea 234... Línea 224...
234
    }
224
    }
Línea 235... Línea 225...
235
 
225
 
236
    /**
226
    /**
237
     * Test get_feedback_access_information function with basic defaults for student.
227
     * Test get_feedback_access_information function with basic defaults for student.
238
     */
228
     */
Línea 239... Línea 229...
239
    public function test_get_feedback_access_information_student() {
229
    public function test_get_feedback_access_information_student(): void {
240
 
230
 
241
        self::setUser($this->student);
231
        self::setUser($this->student);
Línea 255... Línea 245...
255
    }
245
    }
Línea 256... Línea 246...
256
 
246
 
257
    /**
247
    /**
258
     * Test get_feedback_access_information function with basic defaults for teacher.
248
     * Test get_feedback_access_information function with basic defaults for teacher.
259
     */
249
     */
Línea 260... Línea 250...
260
    public function test_get_feedback_access_information_teacher() {
250
    public function test_get_feedback_access_information_teacher(): void {
261
 
251
 
262
        self::setUser($this->teacher);
252
        self::setUser($this->teacher);
Línea 282... Línea 272...
282
    }
272
    }
Línea 283... Línea 273...
283
 
273
 
284
    /**
274
    /**
285
     * Test view_feedback invalid id.
275
     * Test view_feedback invalid id.
286
     */
276
     */
287
    public function test_view_feedback_invalid_id() {
277
    public function test_view_feedback_invalid_id(): void {
288
        // Test invalid instance id.
278
        // Test invalid instance id.
289
        $this->expectException(moodle_exception::class);
279
        $this->expectException(moodle_exception::class);
290
        mod_feedback_external::view_feedback(0);
280
        mod_feedback_external::view_feedback(0);
291
    }
281
    }
292
    /**
282
    /**
293
     * Test view_feedback not enrolled user.
283
     * Test view_feedback not enrolled user.
294
     */
284
     */
295
    public function test_view_feedback_not_enrolled_user() {
285
    public function test_view_feedback_not_enrolled_user(): void {
296
        $usernotenrolled = self::getDataGenerator()->create_user();
286
        $usernotenrolled = self::getDataGenerator()->create_user();
297
        $this->setUser($usernotenrolled);
287
        $this->setUser($usernotenrolled);
298
        $this->expectException(moodle_exception::class);
288
        $this->expectException(moodle_exception::class);
299
        mod_feedback_external::view_feedback(0);
289
        mod_feedback_external::view_feedback(0);
300
    }
290
    }
301
    /**
291
    /**
302
     * Test view_feedback no capabilities.
292
     * Test view_feedback no capabilities.
303
     */
293
     */
304
    public function test_view_feedback_no_capabilities() {
294
    public function test_view_feedback_no_capabilities(): void {
305
        // Test user with no capabilities.
295
        // Test user with no capabilities.
306
        // We need a explicit prohibit since this capability is allowed for students by default.
296
        // We need a explicit prohibit since this capability is allowed for students by default.
307
        assign_capability('mod/feedback:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
297
        assign_capability('mod/feedback:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
308
        accesslib_clear_all_caches_for_unit_testing();
298
        accesslib_clear_all_caches_for_unit_testing();
309
        $this->expectException(moodle_exception::class);
299
        $this->expectException(moodle_exception::class);
310
        mod_feedback_external::view_feedback(0);
300
        mod_feedback_external::view_feedback(0);
311
    }
301
    }
312
    /**
302
    /**
313
     * Test view_feedback.
303
     * Test view_feedback.
314
     */
304
     */
315
    public function test_view_feedback() {
305
    public function test_view_feedback(): void {
316
        // Test user with full capabilities.
306
        // Test user with full capabilities.
317
        $this->setUser($this->student);
307
        $this->setUser($this->student);
318
        // Trigger and capture the event.
308
        // Trigger and capture the event.
319
        $sink = $this->redirectEvents();
309
        $sink = $this->redirectEvents();
Línea 332... Línea 322...
332
    }
322
    }
Línea 333... Línea 323...
333
 
323
 
334
    /**
324
    /**
335
     * Test get_current_completed_tmp.
325
     * Test get_current_completed_tmp.
336
     */
326
     */
337
    public function test_get_current_completed_tmp() {
327
    public function test_get_current_completed_tmp(): void {
Línea 338... Línea 328...
338
        global $DB;
328
        global $DB;
339
 
329
 
340
        // Force non anonymous.
330
        // Force non anonymous.
Línea 360... Línea 350...
360
    }
350
    }
Línea 361... Línea 351...
361
 
351
 
362
    /**
352
    /**
363
     * Test get_items.
353
     * Test get_items.
364
     */
354
     */
365
    public function test_get_items() {
355
    public function test_get_items(): void {
366
        // Test user with full capabilities.
356
        // Test user with full capabilities.
Línea 367... Línea 357...
367
        $this->setUser($this->student);
357
        $this->setUser($this->student);
368
 
358
 
Línea 457... Línea 447...
457
    /**
447
    /**
458
     * Data provider for test_get_items_validation() and test_get_page_items_validation().
448
     * Data provider for test_get_items_validation() and test_get_page_items_validation().
459
     *
449
     *
460
     * @return array
450
     * @return array
461
     */
451
     */
462
    public function items_provider(): array {
452
    public static function items_provider(): array {
463
        return [
453
        return [
464
            'Valid feedback (as student)' => [
454
            'Valid feedback (as student)' => [
465
                'role' => 'student',
455
                'role' => 'student',
466
                'info' => [],
456
                'info' => [],
467
                'warning' => null,
457
                'warning' => null,
Línea 474... Línea 464...
474
            'Empty feedback (as student)' => [
464
            'Empty feedback (as student)' => [
475
                'role' => 'student',
465
                'role' => 'student',
476
                'info' => ['empty' => true],
466
                'info' => ['empty' => true],
477
                'warning' => get_string('no_items_available_yet', 'feedback'),
467
                'warning' => get_string('no_items_available_yet', 'feedback'),
478
            ],
468
            ],
479
            'Closed feedback (as student)' => [
-
 
480
                'role' => 'student',
-
 
481
                'info' => ['closed' => true],
-
 
482
                'warning' => get_string('feedback_is_not_open', 'feedback'),
-
 
483
            ],
-
 
484
            'Cannot complete feedback (as student)' => [
469
            'Cannot complete feedback (as student)' => [
485
                'role' => 'student',
470
                'role' => 'student',
486
                'info' => ['complete' => false],
471
                'info' => ['complete' => false],
487
                'warning' => 'required_capability_exception',
472
                'warning' => 'required_capability_exception',
488
            ],
473
            ],
Línea 499... Línea 484...
499
            'Empty feedback (as teacher)' => [
484
            'Empty feedback (as teacher)' => [
500
                'role' => 'teacher',
485
                'role' => 'teacher',
501
                'info' => ['empty' => true],
486
                'info' => ['empty' => true],
502
                'warning' => null,
487
                'warning' => null,
503
            ],
488
            ],
504
            'Closed feedback (as teacher)' => [
-
 
505
                'role' => 'teacher',
-
 
506
                'info' => ['closed' => true],
-
 
507
                'warning' => null,
-
 
508
            ],
-
 
509
            'Cannot complete feedback (as teacher)' => [
489
            'Cannot complete feedback (as teacher)' => [
510
                'role' => 'teacher',
490
                'role' => 'teacher',
511
                'info' => ['complete' => false],
491
                'info' => ['complete' => false],
512
                'warning' => null,
492
                'warning' => null,
513
            ],
493
            ],
Línea 515... Línea 495...
515
    }
495
    }
Línea 516... Línea 496...
516
 
496
 
517
    /**
497
    /**
518
     * Test launch_feedback.
498
     * Test launch_feedback.
519
     */
499
     */
520
    public function test_launch_feedback() {
500
    public function test_launch_feedback(): void {
Línea 521... Línea 501...
521
        global $DB;
501
        global $DB;
522
 
502
 
Línea 570... Línea 550...
570
    }
550
    }
Línea 571... Línea 551...
571
 
551
 
572
    /**
552
    /**
573
     * Test get_page_items.
553
     * Test get_page_items.
574
     */
554
     */
575
    public function test_get_page_items() {
555
    public function test_get_page_items(): void {
576
        // Test user with full capabilities.
556
        // Test user with full capabilities.
Línea 577... Línea 557...
577
        $this->setUser($this->student);
557
        $this->setUser($this->student);
578
 
558
 
Línea 659... Línea 639...
659
    }
639
    }
Línea 660... Línea 640...
660
 
640
 
661
    /**
641
    /**
662
     * Test process_page.
642
     * Test process_page.
663
     */
643
     */
664
    public function test_process_page() {
644
    public function test_process_page(): void {
Línea 665... Línea 645...
665
        global $DB;
645
        global $DB;
666
 
646
 
667
        // Test user with full capabilities.
647
        // Test user with full capabilities.
Línea 763... Línea 743...
763
    }
743
    }
Línea 764... Línea 744...
764
 
744
 
765
    /**
745
    /**
766
     * Test process_page for a site feedback.
746
     * Test process_page for a site feedback.
767
     */
747
     */
768
    public function test_process_page_site_feedback() {
748
    public function test_process_page_site_feedback(): void {
769
        global $DB;
749
        global $DB;
770
        $pagecontents = 'You finished it!';
750
        $pagecontents = 'You finished it!';
771
        $this->feedback = $this->getDataGenerator()->create_module('feedback',
751
        $this->feedback = $this->getDataGenerator()->create_module('feedback',
Línea 840... Línea 820...
840
    }
820
    }
Línea 841... Línea 821...
841
 
821
 
842
    /**
822
    /**
843
     * Test get_analysis.
823
     * Test get_analysis.
844
     */
824
     */
845
    public function test_get_analysis() {
825
    public function test_get_analysis(): void {
846
        // Test user with full capabilities.
826
        // Test user with full capabilities.
Línea 847... Línea 827...
847
        $this->setUser($this->student);
827
        $this->setUser($this->student);
848
 
828
 
Línea 899... Línea 879...
899
    }
879
    }
Línea 900... Línea 880...
900
 
880
 
901
    /**
881
    /**
902
     * Test get_unfinished_responses.
882
     * Test get_unfinished_responses.
903
     */
883
     */
904
    public function test_get_unfinished_responses() {
884
    public function test_get_unfinished_responses(): void {
905
        // Test user with full capabilities.
885
        // Test user with full capabilities.
Línea 906... Línea 886...
906
        $this->setUser($this->student);
886
        $this->setUser($this->student);
907
 
887
 
Línea 937... Línea 917...
937
    }
917
    }
Línea 938... Línea 918...
938
 
918
 
939
    /**
919
    /**
940
     * Test get_finished_responses.
920
     * Test get_finished_responses.
941
     */
921
     */
942
    public function test_get_finished_responses() {
922
    public function test_get_finished_responses(): void {
943
        // Test user with full capabilities.
923
        // Test user with full capabilities.
Línea 944... Línea 924...
944
        $this->setUser($this->student);
924
        $this->setUser($this->student);
945
 
925
 
Línea 973... Línea 953...
973
    }
953
    }
Línea 974... Línea 954...
974
 
954
 
975
    /**
955
    /**
976
     * Test get_non_respondents (student trying to get this information).
956
     * Test get_non_respondents (student trying to get this information).
977
     */
957
     */
978
    public function test_get_non_respondents_no_permissions() {
958
    public function test_get_non_respondents_no_permissions(): void {
979
        $this->setUser($this->student);
959
        $this->setUser($this->student);
980
        $this->expectException(moodle_exception::class);
960
        $this->expectException(moodle_exception::class);
981
        mod_feedback_external::get_non_respondents($this->feedback->id);
961
        mod_feedback_external::get_non_respondents($this->feedback->id);
Línea 982... Línea 962...
982
    }
962
    }
983
 
963
 
984
    /**
964
    /**
985
     * Test get_non_respondents from an anonymous feedback.
965
     * Test get_non_respondents from an anonymous feedback.
986
     */
966
     */
987
    public function test_get_non_respondents_from_anonymous_feedback() {
967
    public function test_get_non_respondents_from_anonymous_feedback(): void {
988
        $this->setUser($this->student);
968
        $this->setUser($this->student);
989
        $this->expectException(moodle_exception::class);
969
        $this->expectException(moodle_exception::class);
990
        $this->expectExceptionMessage(get_string('anonymous', 'feedback'));
970
        $this->expectExceptionMessage(get_string('anonymous', 'feedback'));
Línea 991... Línea 971...
991
        mod_feedback_external::get_non_respondents($this->feedback->id);
971
        mod_feedback_external::get_non_respondents($this->feedback->id);
992
    }
972
    }
993
 
973
 
994
    /**
974
    /**
995
     * Test get_non_respondents.
975
     * Test get_non_respondents.
Línea 996... Línea 976...
996
     */
976
     */
997
    public function test_get_non_respondents() {
977
    public function test_get_non_respondents(): void {
Línea 1115... Línea 1095...
1115
    }
1095
    }
Línea 1116... Línea 1096...
1116
 
1096
 
1117
    /**
1097
    /**
1118
     * Test get_responses_analysis for anonymous feedback.
1098
     * Test get_responses_analysis for anonymous feedback.
1119
     */
1099
     */
1120
    public function test_get_responses_analysis_anonymous() {
1100
    public function test_get_responses_analysis_anonymous(): void {
Línea 1121... Línea 1101...
1121
        self::complete_basic_feedback();
1101
        self::complete_basic_feedback();
1122
 
1102
 
1123
        // Retrieve the responses analysis.
1103
        // Retrieve the responses analysis.
Línea 1134... Línea 1114...
1134
    }
1114
    }
Línea 1135... Línea 1115...
1135
 
1115
 
1136
    /**
1116
    /**
1137
     * Test get_responses_analysis for non-anonymous feedback.
1117
     * Test get_responses_analysis for non-anonymous feedback.
1138
     */
1118
     */
1139
    public function test_get_responses_analysis_non_anonymous() {
1119
    public function test_get_responses_analysis_non_anonymous(): void {
Línea 1140... Línea 1120...
1140
        global $DB;
1120
        global $DB;
1141
 
1121
 
Línea 1157... Línea 1137...
1157
    }
1137
    }
Línea 1158... Línea 1138...
1158
 
1138
 
1159
    /**
1139
    /**
1160
     * Test get_last_completed for feedback anonymous not completed.
1140
     * Test get_last_completed for feedback anonymous not completed.
1161
     */
1141
     */
1162
    public function test_get_last_completed_anonymous_not_completed() {
1142
    public function test_get_last_completed_anonymous_not_completed(): void {
Línea 1163... Línea 1143...
1163
        global $DB;
1143
        global $DB;
1164
 
1144
 
Línea 1174... Línea 1154...
1174
    }
1154
    }
Línea 1175... Línea 1155...
1175
 
1155
 
1176
    /**
1156
    /**
1177
     * Test get_last_completed for feedback anonymous and completed.
1157
     * Test get_last_completed for feedback anonymous and completed.
1178
     */
1158
     */
1179
    public function test_get_last_completed_anonymous_completed() {
1159
    public function test_get_last_completed_anonymous_completed(): void {
Línea 1180... Línea 1160...
1180
        global $DB;
1160
        global $DB;
1181
 
1161
 
1182
        // Force anonymous.
1162
        // Force anonymous.
Línea 1201... Línea 1181...
1201
    }
1181
    }
Línea 1202... Línea 1182...
1202
 
1182
 
1203
    /**
1183
    /**
1204
     * Test get_last_completed for feedback not anonymous and completed.
1184
     * Test get_last_completed for feedback not anonymous and completed.
1205
     */
1185
     */
1206
    public function test_get_last_completed_not_anonymous_completed() {
1186
    public function test_get_last_completed_not_anonymous_completed(): void {
Línea 1207... Línea 1187...
1207
        global $DB;
1187
        global $DB;
1208
 
1188
 
1209
        // Force non anonymous.
1189
        // Force non anonymous.
Línea 1227... Línea 1207...
1227
    }
1207
    }
Línea 1228... Línea 1208...
1228
 
1208
 
1229
    /**
1209
    /**
1230
     * Test get_last_completed for feedback not anonymous and not completed.
1210
     * Test get_last_completed for feedback not anonymous and not completed.
1231
     */
1211
     */
1232
    public function test_get_last_completed_not_anonymous_not_completed() {
1212
    public function test_get_last_completed_not_anonymous_not_completed(): void {
Línea 1233... Línea 1213...
1233
        global $DB;
1213
        global $DB;
1234
 
1214
 
Línea 1244... Línea 1224...
1244
    }
1224
    }
Línea 1245... Línea 1225...
1245
 
1225
 
1246
    /**
1226
    /**
1247
     * Test get_feedback_access_information for site feedback.
1227
     * Test get_feedback_access_information for site feedback.
1248
     */
1228
     */
Línea 1249... Línea 1229...
1249
    public function test_get_feedback_access_information_for_site_feedback() {
1229
    public function test_get_feedback_access_information_for_site_feedback(): void {
1250
 
1230
 
1251
        $sitefeedback = $this->getDataGenerator()->create_module('feedback', array('course' => SITEID));
1231
        $sitefeedback = $this->getDataGenerator()->create_module('feedback', array('course' => SITEID));
1252
        $this->setUser($this->student);
1232
        $this->setUser($this->student);
Línea 1270... Línea 1250...
1270
    }
1250
    }
Línea 1271... Línea 1251...
1271
 
1251
 
1272
    /**
1252
    /**
1273
     * Test get_feedback_access_information for site feedback mapped.
1253
     * Test get_feedback_access_information for site feedback mapped.
1274
     */
1254
     */
1275
    public function test_get_feedback_access_information_for_site_feedback_mapped() {
1255
    public function test_get_feedback_access_information_for_site_feedback_mapped(): void {
Línea 1276... Línea 1256...
1276
        global $DB;
1256
        global $DB;
1277
 
1257
 
1278
        $sitefeedback = $this->getDataGenerator()->create_module('feedback', array('course' => SITEID));
1258
        $sitefeedback = $this->getDataGenerator()->create_module('feedback', array('course' => SITEID));