/**
  * 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();
     $sepl = $this->getDataGenerator()->create_module('sepl', array('course' => $course->id));
     $context = context_module::instance($sepl->cmid);
     $cm = get_coursemodule_from_instance('sepl', $sepl->id);
     // Test invalid instance id.
     try {
         mod_sepl_external::view_grading_table(0);
         $this->fail('Exception expected due to invalid mod_sepl 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_sepl_external::view_grading_table($sepl->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_sepl_external::view_grading_table($sepl->id);
     $result = external_api::clean_returnvalue(mod_sepl_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_sepl\\event\\grading_table_viewed', $event);
     $this->assertEquals($context, $event->get_context());
     $moodleurl = new \moodle_url('/mod/sepl/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.
     sepl_capability('mod/sepl:view', CAP_PROHIBIT, $teacherrole->id, $context->id);
     accesslib_clear_all_caches_for_unit_testing();
     try {
         mod_sepl_external::view_grading_table($sepl->id);
         $this->fail('Exception expected due to missing capability.');
     } catch (moodle_exception $e) {
         $this->assertEquals('nopermissions', $e->errorcode);
     }
 }
 /**
  * Testing can_edit_submission
  */
 public function test_can_edit_submission()
 {
     global $PAGE, $DB;
     $this->create_extra_users();
     $this->setAdminUser();
     // Create seplment (onlinetext).
     $sepl = $this->create_instance(array('seplsubmission_onlinetext_enabled' => 1, 'submissiondrafts' => 1));
     $PAGE->set_url(new moodle_url('/mod/sepl/view.php', array('id' => $sepl->get_course_module()->id)));
     // Check student can edit their own submission.
     $this->assertTrue($sepl->can_edit_submission($this->students[0]->id, $this->students[0]->id));
     // Check student cannot edit others submission.
     $this->assertFalse($sepl->can_edit_submission($this->students[0]->id, $this->students[1]->id));
     // Check teacher cannot (by default) edit a students submission.
     $this->assertFalse($sepl->can_edit_submission($this->students[0]->id, $this->teachers[0]->id));
     // Add the required capability to edit a student submission.
     $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
     sepl_capability('mod/sepl:editothersubmission', CAP_ALLOW, $roleid, $sepl->get_context()->id);
     role_sepl($roleid, $this->teachers[0]->id, $sepl->get_context()->id);
     accesslib_clear_all_caches_for_unit_testing();
     // Retest - should now have access.
     $this->assertTrue($sepl->can_edit_submission($this->students[0]->id, $this->teachers[0]->id));
     // Force create an seplment with SEPARATEGROUPS.
     $data = new stdClass();
     $data->courseid = $this->course->id;
     $data->name = 'Grouping';
     $groupingid = groups_create_grouping($data);
     groups_sepl_grouping($groupingid, $this->groups[0]->id);
     groups_sepl_grouping($groupingid, $this->groups[1]->id);
     $sepl = $this->create_instance(array('groupingid' => $groupingid, 'groupmode' => SEPARATEGROUPS));
     // Add the capability to the new seplment for extra students 0 and 1.
     sepl_capability('mod/sepl:editothersubmission', CAP_ALLOW, $roleid, $sepl->get_context()->id);
     role_sepl($roleid, $this->extrastudents[0]->id, $sepl->get_context()->id);
     role_sepl($roleid, $this->extrastudents[1]->id, $sepl->get_context()->id);
     accesslib_clear_all_caches_for_unit_testing();
     // Verify the extra student does not have the capability to edit a submission not in their group.
     $this->assertFalse($sepl->can_edit_submission($this->students[0]->id, $this->extrastudents[1]->id));
     // Verify the extra student does have the capability to edit a submission in their group.
     $this->assertTrue($sepl->can_edit_submission($this->students[0]->id, $this->extrastudents[0]->id));
 }