Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  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/>.
  16  
  17  /**
  18   * Unit tests for mod_survey lib
  19   *
  20   * @package    mod_survey
  21   * @category   external
  22   * @copyright  2015 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.0
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Unit tests for mod_survey lib
  32   *
  33   * @package    mod_survey
  34   * @category   external
  35   * @copyright  2015 Juan Leyva <juan@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @since      Moodle 3.0
  38   */
  39  class mod_survey_lib_testcase extends advanced_testcase {
  40  
  41      /**
  42       * Prepares things before this test case is initialised
  43       * @return void
  44       */
  45      public static function setUpBeforeClass(): void {
  46          global $CFG;
  47          require_once($CFG->dirroot . '/mod/survey/lib.php');
  48      }
  49  
  50      /**
  51       * Test survey_view
  52       * @return void
  53       */
  54      public function test_survey_view() {
  55          global $CFG;
  56  
  57          $CFG->enablecompletion = 1;
  58          $this->resetAfterTest();
  59  
  60          $this->setAdminUser();
  61          // Setup test data.
  62          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  63          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id),
  64                                                              array('completion' => 2, 'completionview' => 1));
  65          $context = context_module::instance($survey->cmid);
  66          $cm = get_coursemodule_from_instance('survey', $survey->id);
  67  
  68          // Trigger and capture the event.
  69          $sink = $this->redirectEvents();
  70  
  71          survey_view($survey, $course, $cm, $context, 'form');
  72  
  73          $events = $sink->get_events();
  74          // 2 additional events thanks to completion.
  75          $this->assertCount(3, $events);
  76          $event = array_shift($events);
  77  
  78          // Checking that the event contains the expected values.
  79          $this->assertInstanceOf('\mod_survey\event\course_module_viewed', $event);
  80          $this->assertEquals($context, $event->get_context());
  81          $moodleurl = new \moodle_url('/mod/survey/view.php', array('id' => $cm->id));
  82          $this->assertEquals($moodleurl, $event->get_url());
  83          $this->assertEquals('form', $event->other['viewed']);
  84          $this->assertEventContextNotUsed($event);
  85          $this->assertNotEmpty($event->get_name());
  86          // Check completion status.
  87          $completion = new completion_info($course);
  88          $completiondata = $completion->get_data($cm);
  89          $this->assertEquals(1, $completiondata->completionstate);
  90  
  91      }
  92  
  93      /**
  94       * Test survey_order_questions
  95       */
  96      public function test_survey_order_questions() {
  97          global $DB;
  98  
  99          $this->resetAfterTest();
 100          $course = $this->getDataGenerator()->create_course();
 101          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
 102  
 103          $orderedquestionids = explode(',', $survey->questions);
 104          $surveyquestions = $DB->get_records_list("survey_questions", "id", $orderedquestionids);
 105  
 106          $questionsordered = survey_order_questions($surveyquestions, $orderedquestionids);
 107  
 108          // Check one by one the correct order.
 109          for ($i = 0; $i < count($orderedquestionids); $i++) {
 110              $this->assertEquals($orderedquestionids[$i], $questionsordered[$i]->id);
 111          }
 112      }
 113  
 114      /**
 115       * Test survey_save_answers
 116       */
 117      public function test_survey_save_answers() {
 118          global $DB;
 119  
 120          $this->resetAfterTest();
 121          $this->setAdminUser();
 122  
 123          // Setup test data.
 124          $course = $this->getDataGenerator()->create_course();
 125          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
 126          $context = context_module::instance($survey->cmid);
 127  
 128          // Build our questions and responses array.
 129          $realquestions = array();
 130          $questions = survey_get_questions($survey);
 131          $i = 5;
 132          foreach ($questions as $q) {
 133              if ($q->type > 0) {
 134                  if ($q->multi) {
 135                      $subquestions = survey_get_subquestions($q);
 136                      foreach ($subquestions as $sq) {
 137                          $key = 'q' . $sq->id;
 138                          $realquestions[$key] = $i % 5 + 1;
 139                          $i++;
 140                      }
 141                  } else {
 142                      $key = 'q' . $q->id;
 143                      $realquestions[$key] = $i % 5 + 1;
 144                      $i++;
 145                  }
 146              }
 147          }
 148  
 149          $sink = $this->redirectEvents();
 150          survey_save_answers($survey, $realquestions, $course, $context);
 151  
 152          // Check the stored answers, they must match.
 153          $dbanswers = $DB->get_records_menu('survey_answers', array('survey' => $survey->id), '', 'question, answer1');
 154          foreach ($realquestions as $key => $value) {
 155              $id = str_replace('q', '', $key);
 156              $this->assertEquals($value, $dbanswers[$id]);
 157          }
 158  
 159          // Check events.
 160          $events = $sink->get_events();
 161          $this->assertCount(1, $events);
 162          $event = array_shift($events);
 163  
 164          // Checking that the event contains the expected values.
 165          $this->assertInstanceOf('\mod_survey\event\response_submitted', $event);
 166          $this->assertEquals($context, $event->get_context());
 167          $this->assertEquals($survey->id, $event->other['surveyid']);
 168      }
 169  
 170      public function test_survey_core_calendar_provide_event_action() {
 171          $this->resetAfterTest();
 172          $this->setAdminUser();
 173  
 174          // Create the activity.
 175          $course = $this->getDataGenerator()->create_course();
 176          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
 177  
 178          // Create a calendar event.
 179          $event = $this->create_action_event($course->id, $survey->id,
 180              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 181  
 182          // Create an action factory.
 183          $factory = new \core_calendar\action_factory();
 184  
 185          // Decorate action event.
 186          $actionevent = mod_survey_core_calendar_provide_event_action($event, $factory);
 187  
 188          // Confirm the event was decorated.
 189          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
 190          $this->assertEquals(get_string('view'), $actionevent->get_name());
 191          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
 192          $this->assertEquals(1, $actionevent->get_item_count());
 193          $this->assertTrue($actionevent->is_actionable());
 194      }
 195  
 196      public function test_survey_core_calendar_provide_event_action_for_user() {
 197          global $CFG;
 198  
 199          $this->resetAfterTest();
 200          $this->setAdminUser();
 201  
 202          // Create the activity.
 203          $course = $this->getDataGenerator()->create_course();
 204          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
 205  
 206          // Create a student and enrol into the course.
 207          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 208  
 209          // Create a calendar event.
 210          $event = $this->create_action_event($course->id, $survey->id,
 211              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 212  
 213          // Now log out.
 214          $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
 215          $this->setUser();
 216  
 217          // Create an action factory.
 218          $factory = new \core_calendar\action_factory();
 219  
 220          // Decorate action event for the student.
 221          $actionevent = mod_survey_core_calendar_provide_event_action($event, $factory, $student->id);
 222  
 223          // Confirm the event was decorated.
 224          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
 225          $this->assertEquals(get_string('view'), $actionevent->get_name());
 226          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
 227          $this->assertEquals(1, $actionevent->get_item_count());
 228          $this->assertTrue($actionevent->is_actionable());
 229      }
 230  
 231      public function test_survey_core_calendar_provide_event_action_as_non_user() {
 232          global $CFG;
 233  
 234          $this->resetAfterTest();
 235          $this->setAdminUser();
 236  
 237          // Create the activity.
 238          $course = $this->getDataGenerator()->create_course();
 239          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
 240  
 241          // Create a calendar event.
 242          $event = $this->create_action_event($course->id, $survey->id,
 243              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 244  
 245          // Log out the user and set force login to true.
 246          \core\session\manager::init_empty_session();
 247          $CFG->forcelogin = true;
 248  
 249          // Create an action factory.
 250          $factory = new \core_calendar\action_factory();
 251  
 252          // Decorate action event.
 253          $actionevent = mod_survey_core_calendar_provide_event_action($event, $factory);
 254  
 255          // Ensure result was null.
 256          $this->assertNull($actionevent);
 257      }
 258  
 259      public function test_survey_core_calendar_provide_event_action_already_completed() {
 260          global $CFG;
 261  
 262          $this->resetAfterTest();
 263          $this->setAdminUser();
 264  
 265          $CFG->enablecompletion = 1;
 266  
 267          // Create the activity.
 268          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 269          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id),
 270              array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
 271  
 272          // Get some additional data.
 273          $cm = get_coursemodule_from_instance('survey', $survey->id);
 274  
 275          // Create a calendar event.
 276          $event = $this->create_action_event($course->id, $survey->id,
 277              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 278  
 279          // Mark the activity as completed.
 280          $completion = new completion_info($course);
 281          $completion->set_module_viewed($cm);
 282  
 283          // Create an action factory.
 284          $factory = new \core_calendar\action_factory();
 285  
 286          // Decorate action event.
 287          $actionevent = mod_survey_core_calendar_provide_event_action($event, $factory);
 288  
 289          // Ensure result was null.
 290          $this->assertNull($actionevent);
 291      }
 292  
 293      public function test_survey_core_calendar_provide_event_action_already_completed_for_user() {
 294          global $CFG;
 295  
 296          $this->resetAfterTest();
 297          $this->setAdminUser();
 298  
 299          $CFG->enablecompletion = 1;
 300  
 301          // Create the activity.
 302          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 303          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id),
 304              array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
 305  
 306          // Create 2 students and enrol them into the course.
 307          $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 308          $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 309  
 310          // Get some additional data.
 311          $cm = get_coursemodule_from_instance('survey', $survey->id);
 312  
 313          // Create a calendar event.
 314          $event = $this->create_action_event($course->id, $survey->id,
 315              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 316  
 317          // Mark the activity as completed for the $student1.
 318          $completion = new completion_info($course);
 319          $completion->set_module_viewed($cm, $student1->id);
 320  
 321          // Now log in as $student2.
 322          $this->setUser($student2);
 323  
 324          // Create an action factory.
 325          $factory = new \core_calendar\action_factory();
 326  
 327          // Decorate action event for $student1.
 328          $actionevent = mod_survey_core_calendar_provide_event_action($event, $factory, $student1->id);
 329  
 330          // Ensure result was null.
 331          $this->assertNull($actionevent);
 332      }
 333  
 334      /**
 335       * Creates an action event.
 336       *
 337       * @param int $courseid The course id.
 338       * @param int $instanceid The instance id.
 339       * @param string $eventtype The event type.
 340       * @return bool|calendar_event
 341       */
 342      private function create_action_event($courseid, $instanceid, $eventtype) {
 343          $event = new stdClass();
 344          $event->name = 'Calendar event';
 345          $event->modulename  = 'survey';
 346          $event->courseid = $courseid;
 347          $event->instance = $instanceid;
 348          $event->type = CALENDAR_EVENT_TYPE_ACTION;
 349          $event->eventtype = $eventtype;
 350          $event->timestart = time();
 351  
 352          return calendar_event::create($event);
 353      }
 354  
 355      /**
 356       * Test the callback responsible for returning the completion rule descriptions.
 357       * This function should work given either an instance of the module (cm_info), such as when checking the active rules,
 358       * or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
 359       */
 360      public function test_mod_survey_completion_get_active_rule_descriptions() {
 361          $this->resetAfterTest();
 362          $this->setAdminUser();
 363  
 364          // Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
 365          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
 366          $survey1 = $this->getDataGenerator()->create_module('survey', [
 367              'course' => $course->id,
 368              'completion' => 2,
 369              'completionsubmit' => 1,
 370          ]);
 371          $survey2 = $this->getDataGenerator()->create_module('survey', [
 372              'course' => $course->id,
 373              'completion' => 2,
 374              'completionsubmit' => 0,
 375          ]);
 376          $cm1 = cm_info::create(get_coursemodule_from_instance('survey', $survey1->id));
 377          $cm2 = cm_info::create(get_coursemodule_from_instance('survey', $survey2->id));
 378  
 379          // Data for the stdClass input type.
 380          // This type of input would occur when checking the default completion rules for an activity type, where we don't have
 381          // any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
 382          $moddefaults = new stdClass();
 383          $moddefaults->customdata = ['customcompletionrules' => ['completionsubmit' => 1]];
 384          $moddefaults->completion = 2;
 385  
 386          $activeruledescriptions = [get_string('completionsubmit', 'survey')];
 387          $this->assertEquals(mod_survey_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
 388          $this->assertEquals(mod_survey_get_completion_active_rule_descriptions($cm2), []);
 389          $this->assertEquals(mod_survey_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
 390          $this->assertEquals(mod_survey_get_completion_active_rule_descriptions(new stdClass()), []);
 391      }
 392  }