MDL-49796 assign: Unit texts for view_grading_table
authorJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 5 Jun 2015 10:24:54 +0000 (12:24 +0200)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 13 Jul 2015 07:25:24 +0000 (09:25 +0200)
mod/assign/tests/externallib_test.php

index a6e9426..0dd6de1 100644 (file)
@@ -1486,4 +1486,73 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
         $this->assertEquals($USER->id, $updateduserflag->allocatedmarker);
     }
 
+    /**
+     * Test view_grading_table
+     */
+    public function test_view_grading_table() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $this->setAdminUser();
+        // Setup test data.
+        $course = $this->getDataGenerator()->create_course();
+        $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
+        $context = context_module::instance($assign->cmid);
+        $cm = get_coursemodule_from_instance('assign', $assign->id);
+
+        // Test invalid instance id.
+        try {
+            mod_assign_external::view_grading_table(0);
+            $this->fail('Exception expected due to invalid mod_assign instance id.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('invalidrecord', $e->errorcode);
+        }
+
+        // Test not-enrolled user.
+        $user = self::getDataGenerator()->create_user();
+        $this->setUser($user);
+        try {
+            mod_assign_external::view_grading_table($assign->id);
+            $this->fail('Exception expected due to not enrolled user.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('requireloginerror', $e->errorcode);
+        }
+
+        // Test user with full capabilities.
+        $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
+        $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+
+        $result = mod_assign_external::view_grading_table($assign->id);
+        $result = external_api::clean_returnvalue(mod_assign_external::view_grading_table_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_assign\event\grading_table_viewed', $event);
+        $this->assertEquals($context, $event->get_context());
+        $moodleurl = new \moodle_url('/mod/assign/view.php', array('id' => $cm->id));
+        $this->assertEquals($moodleurl, $event->get_url());
+        $this->assertEventContextNotUsed($event);
+        $this->assertNotEmpty($event->get_name());
+
+        // Test user with no capabilities.
+        // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
+        assign_capability('mod/assign:view', CAP_PROHIBIT, $teacherrole->id, $context->id);
+        accesslib_clear_all_caches_for_unit_testing();
+
+        try {
+            mod_assign_external::view_grading_table($assign->id);
+            $this->fail('Exception expected due to missing capability.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('nopermissions', $e->errorcode);
+        }
+
+    }
+
 }