/**
     * Test view_choice
     */
    public function test_view_choice() {
        global $DB;

        $this->resetAfterTest(true);

        // Setup test data.
        $course = $this->getDataGenerator()->create_course();
        $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
        $context = context_module::instance($choice->cmid);
        $cm = get_coursemodule_from_instance('choice', $choice->id);

        // Test invalid instance id.
        try {
            mod_choice_external::view_choice(0);
            $this->fail('Exception expected due to invalid mod_choice instance id.');
        } catch (moodle_exception $e) {
            $this->assertEquals('invalidcoursemodule', $e->errorcode);
        }

        // Test not-enrolled user.
        $user = self::getDataGenerator()->create_user();
        $this->setUser($user);
        try {
            mod_choice_external::view_choice($choice->id);
            $this->fail('Exception expected due to not enrolled user.');
        } catch (moodle_exception $e) {
            $this->assertEquals('requireloginerror', $e->errorcode);
        }

        // Test user with full capabilities.
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
        $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);

        // Trigger and capture the event.
        $sink = $this->redirectEvents();

        $result = mod_choice_external::view_choice($choice->id);
        $result = external_api::clean_returnvalue(mod_choice_external::view_choice_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_choice\event\course_module_viewed', $event);
        $this->assertEquals($context, $event->get_context());
        $moodlechoice = new \moodle_url('/mod/choice/view.php', array('id' => $cm->id));
        $this->assertEquals($moodlechoice, $event->get_url());
        $this->assertEventContextNotUsed($event);
        $this->assertNotEmpty($event->get_name());

    }
Exemple #2
0
 /**
  * Test delete_choice_responses
  */
 public function test_delete_choice_responses()
 {
     global $DB;
     $this->resetAfterTest(true);
     $course = self::getDataGenerator()->create_course();
     $params = new stdClass();
     $params->course = $course->id;
     $params->option = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza');
     $params->name = 'First Choice Activity';
     $params->showresults = CHOICE_SHOWRESULTS_ALWAYS;
     $params->allowmultiple = 1;
     $params->showunanswered = 1;
     $choice = self::getDataGenerator()->create_module('choice', $params);
     $cm = get_coursemodule_from_id('choice', $choice->cmid);
     $choiceinstance = choice_get_choice($cm->instance);
     $options = array_keys($choiceinstance->option);
     $student = $this->getDataGenerator()->create_user();
     $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     // Enroll student in Course1.
     self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
     $this->setUser($student);
     $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2]));
     $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results);
     $myresponses = array_keys(choice_get_my_response($choice));
     // Try to delete responses when allow update is false.
     try {
         mod_choice_external::delete_choice_responses($choice->id, array($myresponses[0], $myresponses[0]));
         $this->fail('Exception expected due to missing permissions.');
     } catch (required_capability_exception $e) {
         $this->assertEquals('nopermissions', $e->errorcode);
     }
     // Set allow update to true, and a passed time close.
     $DB->set_field('choice', 'allowupdate', 1, array('id' => $choice->id));
     $DB->set_field('choice', 'timeclose', time() - DAYSECS, array('id' => $choice->id));
     try {
         mod_choice_external::delete_choice_responses($choice->id, array($myresponses[0], $myresponses[1]));
         $this->fail('Exception expected due to expired choice.');
     } catch (moodle_exception $e) {
         $this->assertEquals('expired', $e->errorcode);
     }
     // Reset time close. We should be able now to delete all the responses.
     $DB->set_field('choice', 'timeclose', 0, array('id' => $choice->id));
     $results = mod_choice_external::delete_choice_responses($choice->id, array($myresponses[0], $myresponses[1]));
     $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results);
     $this->assertTrue($results['status']);
     $this->assertCount(0, $results['warnings']);
     // Now, in the DB 0 responses.
     $this->assertCount(0, choice_get_my_response($choice));
     // Submit again the responses.
     $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2]));
     $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results);
     $myresponses = array_keys(choice_get_my_response($choice));
     // Delete only one response.
     $results = mod_choice_external::delete_choice_responses($choice->id, array($myresponses[0]));
     $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results);
     $this->assertTrue($results['status']);
     $this->assertCount(0, $results['warnings']);
     // Now, in the DB 1 response still.
     $this->assertCount(1, choice_get_my_response($choice));
     // Delete the remaining response, passing 2 invalid responses ids.
     $results = mod_choice_external::delete_choice_responses($choice->id, array($myresponses[1], $myresponses[0] + 2, $myresponses[0] + 3));
     $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results);
     $this->assertTrue($results['status']);
     // 2 warnings, 2 invalid responses.
     $this->assertCount(2, $results['warnings']);
     // Now, in the DB 0 responses.
     $this->assertCount(0, choice_get_my_response($choice));
     // Now, as an admin we must be able to delete all the responses under any condition.
     // Submit again the responses.
     $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2]));
     $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results);
     $studentresponses = array_keys(choice_get_my_response($choice));
     $this->setAdminUser();
     $DB->set_field('choice', 'allowupdate', 0, array('id' => $choice->id));
     $DB->set_field('choice', 'timeclose', time() - DAYSECS, array('id' => $choice->id));
     $results = mod_choice_external::delete_choice_responses($choice->id, array($studentresponses[0], $studentresponses[1]));
     $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results);
     $this->assertTrue($results['status']);
     $this->assertCount(0, $results['warnings']);
     // Submit again the responses.
     $DB->set_field('choice', 'timeclose', 0, array('id' => $choice->id));
     $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2]));
     $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results);
     // With other user account too, so we can test all the responses are deleted.
     choice_user_submit_response(array($options[1], $options[2]), $choice, $student->id, $course, $cm);
     // Test deleting all (not passing the answers ids), event not only mine.
     $results = mod_choice_external::delete_choice_responses($choice->id);
     $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results);
     $this->assertTrue($results['status']);
     $this->assertCount(0, $results['warnings']);
     $this->assertCount(0, choice_get_all_responses($choice));
     // Now, in the DB 0 responses.
     $this->setUser($student);
     // Submit again respones.
     $DB->set_field('choice', 'allowupdate', 1, array('id' => $choice->id));
     $DB->set_field('choice', 'timeclose', 0, array('id' => $choice->id));
     $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2]));
     $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results);
     // Delete all responses.
     $results = mod_choice_external::delete_choice_responses($choice->id);
     $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results);
     $this->assertTrue($results['status']);
     $this->assertCount(0, $results['warnings']);
     $this->assertCount(0, choice_get_my_response($choice));
 }
Exemple #3
0
    /**
     * Test get_choices_by_courses
     */
    public function test_get_choices_by_courses() {
        global $DB;
        $this->resetAfterTest(true);
        // As admin.
        $this->setAdminUser();
        $course1 = self::getDataGenerator()->create_course();
        $choiceoptions1 = array(
          'course' => $course1->id,
          'name' => 'First IMSCP'
        );
        $choice1 = self::getDataGenerator()->create_module('choice', $choiceoptions1);
        $course2 = self::getDataGenerator()->create_course();

        $choiceoptions2 = array(
          'course' => $course2->id,
          'name' => 'Second IMSCP'
        );
        $choice2 = self::getDataGenerator()->create_module('choice', $choiceoptions2);
        $student1 = $this->getDataGenerator()->create_user();

        $studentrole = $DB->get_record('role', array('shortname' => 'student'));

        // Enroll Student1 in Course1.
        self::getDataGenerator()->enrol_user($student1->id,  $course1->id, $studentrole->id);

        $this->setUser($student1);
        $choices = mod_choice_external::get_choices_by_courses(array());
        $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices);
        $this->assertCount(1, $choices['choices']);
        $this->assertEquals('First IMSCP', $choices['choices'][0]['name']);
        // As Student you cannot see some IMSCP properties like 'section'.
        $this->assertFalse(isset($choices['choices'][0]['section']));

        // Student1 is not enrolled in this Course.
        // The webservice will give a warning!
        $choices = mod_choice_external::get_choices_by_courses(array($course2->id));
        $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices);
        $this->assertCount(0, $choices['choices']);
        $this->assertEquals(1, $choices['warnings'][0]['warningcode']);

        // Now as admin.
        $this->setAdminUser();
        // As Admin we can see this IMSCP.
        $choices = mod_choice_external::get_choices_by_courses(array($course2->id));
        $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices);
        $this->assertCount(1, $choices['choices']);
        $this->assertEquals('Second IMSCP', $choices['choices'][0]['name']);
        // As an Admin you can see some IMSCP properties like 'section'.
        $this->assertEquals(0, $choices['choices'][0]['section']);

        // Now, prohibit capabilities.
        $this->setUser($student1);
        $contextcourse1 = context_course::instance($course1->id);
        // Prohibit capability = mod:choice:choose on Course1 for students.
        assign_capability('mod/choice:choose', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id);
        accesslib_clear_all_caches_for_unit_testing();

        $choices = mod_choice_external::get_choices_by_courses(array($course1->id));
        $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices);
        $this->assertFalse(isset($choices['choices'][0]['timeopen']));
    }