diff --git a/mod/choice/tests/externallib_test.php b/mod/choice/tests/externallib_test.php new file mode 100644 index 00000000000..f227c322a6a --- /dev/null +++ b/mod/choice/tests/externallib_test.php @@ -0,0 +1,353 @@ +. + +/** + * External choice functions unit tests + * + * @package mod_choice + * @category external + * @copyright 2015 Costantino Cito + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); +require_once($CFG->dirroot . '/mod/choice/lib.php'); + +/** + * External choice functions unit tests + * + * @package mod_choice + * @category external + * @copyright 2015 Costantino Cito + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_choice_externallib_testcase extends externallib_advanced_testcase { + + /** + * Test get_choice_results + */ + public function test_get_choice_results() { + 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_AFTER_ANSWER; + $params->publish = 1; + $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); + $student1 = $this->getDataGenerator()->create_user(); + $student2 = $this->getDataGenerator()->create_user(); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + + // Enroll Students in Course1. + self::getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); + self::getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id); + + $this->setUser($student1); + $myanswer = $options[2]; + choice_user_submit_response($myanswer, $choice, $student1->id, $course, $cm); + $results = mod_choice_external::get_choice_results($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + + // Create an array with optionID as Key. + $resultsarr = array(); + foreach ($results['options'] as $option) { + $resultsarr[$option['id']] = $option['userresponses']; + } + // The stundent1 is the userid who choosed the myanswer(option 3). + $this->assertEquals($resultsarr[$myanswer][0]['userid'], $student1->id); + // The stundent2 is the userid who didn't answered yet. + $this->assertEquals($resultsarr[0][0]['userid'], $student2->id); + + // As Stundent2 we cannot see results (until we answered). + $this->setUser($student2); + $results = mod_choice_external::get_choice_results($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + // We do not retrieve any response! + foreach ($results['options'] as $option) { + $this->assertCount(0, $option['userresponses']); + } + + $timenow = time(); + // We can see results only after activity close (even if we didn't answered). + $choice->showresults = CHOICE_SHOWRESULTS_AFTER_CLOSE; + // Set timeopen and timeclose in the past. + $choice->timeopen = $timenow - (60 * 60 * 24 * 3); + $choice->timeclose = $timenow + (60 * 60 * 24 * 2); + $DB->update_record('choice', $choice); + + $results = mod_choice_external::get_choice_results($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + // We do not retrieve any response (activity is still open). + foreach ($results['options'] as $option) { + $this->assertCount(0, $option['userresponses']); + } + + // We close the activity (setting timeclose in the past). + $choice->timeclose = $timenow - (60 * 60 * 24 * 2); + $DB->update_record('choice', $choice); + // Now as Stundent2 we will see results! + $results = mod_choice_external::get_choice_results($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + // Create an array with optionID as Key. + $resultsarr = array(); + foreach ($results['options'] as $option) { + $resultsarr[$option['id']] = $option['userresponses']; + } + // The stundent1 is the userid who choosed the myanswer(option 3). + $this->assertEquals($resultsarr[$myanswer][0]['userid'], $student1->id); + // The stundent2 is the userid who didn't answered yet. + $this->assertEquals($resultsarr[0][0]['userid'], $student2->id); + + // Do not publish user names! + $choice->publish = 0; + $DB->update_record('choice', $choice); + $results = mod_choice_external::get_choice_results($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + // Create an array with optionID as Key. + $resultsarr = array(); + // Does not show any user response! + foreach ($results['options'] as $option) { + $this->assertCount(0, $option['userresponses']); + $resultsarr[$option['id']] = $option; + } + // But we can see totals and percentages. + $this->assertEquals(1, $resultsarr[$myanswer]['numberofuser']); + } + + /** + * Test get_choice_options + */ + public function test_get_choice_options() { + global $DB; + + // Warningcodes. + $notopenyet = 1; + $previewonly = 2; + $expired = 3; + + $this->resetAfterTest(true); + $timenow = time(); + $timeopen = $timenow + (60 * 60 * 24 * 2); + $timeclose = $timenow + (60 * 60 * 24 * 7); + $course = self::getDataGenerator()->create_course(); + $possibleoptions = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'); + $params = array(); + $params['course'] = $course->id; + $params['option'] = $possibleoptions; + $params['name'] = 'First Choice Activity'; + $params['showpreview'] = 0; + + $generator = $this->getDataGenerator()->get_plugin_generator('mod_choice'); + $choice = $generator->create_instance($params); + + $student1 = $this->getDataGenerator()->create_user(); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + // Enroll Students in Course. + self::getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); + $this->setUser($student1); + + $results = mod_choice_external::get_choice_options($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + // We should retrieve all options. + $this->assertCount(count($possibleoptions), $results['options']); + + // Here we force timeopen/close in the future. + $choice->timeopen = $timeopen; + $choice->timeclose = $timeclose; + $DB->update_record('choice', $choice); + + $results = mod_choice_external::get_choice_options($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + // We should retrieve no options. + $this->assertCount(0, $results['options']); + $this->assertEquals($notopenyet, $results['warnings'][0]['warningcode']); + + // Here we see the options because of preview! + $choice->showpreview = 1; + $DB->update_record('choice', $choice); + $results = mod_choice_external::get_choice_options($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + // We should retrieve all options. + $this->assertCount(count($possibleoptions), $results['options']); + + foreach ($results['options'] as $option) { + // Each option is disabled as this is only the preview! + $this->assertEquals(1, $option['disabled']); + } + $warnings = array(); + foreach ($results['warnings'] as $warning) { + $warnings[$warning['warningcode']] = $warning['message']; + } + $this->assertTrue(isset($warnings[$previewonly])); + $this->assertTrue(isset($warnings[$notopenyet])); + + // Simulate activity as opened! + $choice->timeopen = $timenow - (60 * 60 * 24 * 3); + $choice->timeclose = $timenow + (60 * 60 * 24 * 2); + $DB->update_record('choice', $choice); + $cm = get_coursemodule_from_id('choice', $choice->cmid); + $choiceinstance = choice_get_choice($cm->instance); + $optionsids = array_keys($choiceinstance->option); + $myanswerid = $optionsids[2]; + choice_user_submit_response($myanswerid, $choice, $student1->id, $course, $cm); + + $results = mod_choice_external::get_choice_options($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + // We should retrieve all options. + $this->assertCount(count($possibleoptions), $results['options']); + foreach ($results['options'] as $option) { + // When we answered and we cannot update our choice. + if ($option['id'] == $myanswerid and !$choice->allowupdate) { + $this->assertEquals(1, $option['disabled']); + $this->assertEquals(1, $option['checked']); + } else { + $this->assertEquals(0, $option['disabled']); + } + } + + // Set timeopen and timeclose as older than today! + // We simulate what happens when the activity is closed. + $choice->timeopen = $timenow - (60 * 60 * 24 * 3); + $choice->timeclose = $timenow - (60 * 60 * 24 * 2); + $DB->update_record('choice', $choice); + $results = mod_choice_external::get_choice_options($choice->id); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + // We should retrieve no options. + $this->assertCount(0, $results['options']); + $this->assertEquals($expired, $results['warnings'][0]['warningcode']); + + } + + /** + * Test submit_choice_response + */ + public function test_submit_choice_response() { + 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); + $student1 = $this->getDataGenerator()->create_user(); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + + // Enroll Students in Course1. + self::getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); + + $this->setUser($student1); + $myresponse = $options[2]; + $results = mod_choice_external::submit_choice_response($choice->id, array($myresponse)); + // We need to execute the return values cleaning process to simulate the web service server. + $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); + $myanswers = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $student1->id)); + $myanswer = reset($myanswers); + $this->assertEquals($results['answers'][0]['id'], $myanswer->id); + $this->assertEquals($results['answers'][0]['choiceid'], $myanswer->choiceid); + $this->assertEquals($results['answers'][0]['userid'], $myanswer->userid); + $this->assertEquals($results['answers'][0]['timemodified'], $myanswer->timemodified); + } + + /** + * 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()); + + } +} diff --git a/mod/choice/tests/lib_test.php b/mod/choice/tests/lib_test.php new file mode 100644 index 00000000000..0f581170313 --- /dev/null +++ b/mod/choice/tests/lib_test.php @@ -0,0 +1,175 @@ +. + +/** + * Choice module library functions tests + * + * @package mod_choice + * @category test + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); +require_once($CFG->dirroot . '/mod/choice/lib.php'); + +/** + * Choice module library functions tests + * + * @package mod_choice + * @category test + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ +class mod_choice_lib_testcase extends externallib_advanced_testcase { + + /** + * Test choice_view + * @return void + */ + public function test_choice_view() { + global $CFG; + + $this->resetAfterTest(); + + $this->setAdminUser(); + // 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); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + choice_view($choice, $course, $cm, $context); + + $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()); + $url = new \moodle_url('/mod/choice/view.php', array('id' => $cm->id)); + $this->assertEquals($url, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + } + + /** + * Test choice_can_view_results + * @return void + */ + public function test_choice_can_view_results() { + global $DB, $USER; + + $this->resetAfterTest(); + + $this->setAdminUser(); + // 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); + + // Default values are false, user cannot view results. + $canview = choice_can_view_results($choice); + $this->assertFalse($canview); + + // Show results forced. + $choice->showresults = CHOICE_SHOWRESULTS_ALWAYS; + $DB->update_record('choice', $choice); + $canview = choice_can_view_results($choice); + $this->assertTrue($canview); + + // Show results after closing. + $choice->showresults = CHOICE_SHOWRESULTS_AFTER_CLOSE; + $DB->update_record('choice', $choice); + $canview = choice_can_view_results($choice); + $this->assertFalse($canview); + + $choice->timeclose = time() - HOURSECS; + $DB->update_record('choice', $choice); + $canview = choice_can_view_results($choice); + $this->assertTrue($canview); + + // Show results after answering. + $choice->timeclose = 0; + $choice->showresults = CHOICE_SHOWRESULTS_AFTER_ANSWER; + $DB->update_record('choice', $choice); + $canview = choice_can_view_results($choice); + $this->assertFalse($canview); + + // Get the first option. + $choicewithoptions = choice_get_choice($choice->id); + $optionids = array_keys($choicewithoptions->option); + + choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm); + + $canview = choice_can_view_results($choice); + $this->assertTrue($canview); + + } + + /** + * Test choice_get_my_response + * @return void + */ + public function test_choice_get_my_response() { + global $USER; + + $this->resetAfterTest(); + + $this->setAdminUser(); + // 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); + + $choicewithoptions = choice_get_choice($choice->id); + $optionids = array_keys($choicewithoptions->option); + + choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm); + $responses = choice_get_my_response($choice, $course, $cm, $context); + $this->assertCount(1, $responses); + $response = array_shift($responses); + $this->assertEquals($optionids[0], $response->optionid); + + // Multiple responses. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, 'allowmultiple' => 1)); + $context = context_module::instance($choice->cmid); + $cm = get_coursemodule_from_instance('choice', $choice->id); + + $choicewithoptions = choice_get_choice($choice->id); + $optionids = array_keys($choicewithoptions->option); + + choice_user_submit_response($optionids, $choice, $USER->id, $course, $cm); + $responses = choice_get_my_response($choice, $course, $cm, $context); + $this->assertCount(count($optionids), $responses); + foreach ($responses as $resp) { + $this->assertContains($resp->optionid, $optionids); + } + } + +}