| 1 | 
           efrain | 
           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 | 
           namespace mod_quiz;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use basic_testcase;
  | 
        
        
            | 
            | 
           20 | 
           use mod_quiz\question\display_options;
  | 
        
        
            | 
            | 
           21 | 
           use mod_quiz\quiz_settings;
  | 
        
        
            | 
            | 
           22 | 
           use stdClass;
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           global $CFG;
  | 
        
        
            | 
            | 
           27 | 
           require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           /**
  | 
        
        
            | 
            | 
           30 | 
            * Unit tests for the quiz class
  | 
        
        
            | 
            | 
           31 | 
            *
  | 
        
        
            | 
            | 
           32 | 
            * @package    mod_quiz
  | 
        
        
            | 
            | 
           33 | 
            * @copyright  2008 The Open University
  | 
        
        
            | 
            | 
           34 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           35 | 
            * @covers \mod_quiz\quiz_settings
  | 
        
        
            | 
            | 
           36 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           37 | 
           final class quizobj_test extends basic_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           38 | 
               /**
  | 
        
        
            | 
            | 
           39 | 
                * Test cases for {@see test_cannot_review_message()}.
  | 
        
        
            | 
            | 
           40 | 
                *
  | 
        
        
            | 
            | 
           41 | 
                * @return array[]
  | 
        
        
            | 
            | 
           42 | 
                */
  | 
        
        
            | 
            | 
           43 | 
               public static function cannot_review_message_testcases(): array {
  | 
        
        
            | 
            | 
           44 | 
                   return [
  | 
        
        
            | 
            | 
           45 | 
                       // Review       Time close
  | 
        
        
            | 
            | 
           46 | 
                       // Later close  quiz attempt    When                Expected
  | 
        
        
            | 
            | 
           47 | 
                       // Quiz with no close date.
  | 
        
        
            | 
            | 
           48 | 
                       [false, false, null, null, display_options::DURING, ''],
  | 
        
        
            | 
            | 
           49 | 
                       [false, false, null, -60, display_options::IMMEDIATELY_AFTER, 'noreview'],
  | 
        
        
            | 
            | 
           50 | 
                       [false, false, null, -180, display_options::LATER_WHILE_OPEN, 'noreview'],
  | 
        
        
            | 
            | 
           51 | 
                       [false, false, null, -180, display_options::AFTER_CLOSE, 'noreview'],
  | 
        
        
            | 
            | 
           52 | 
                       [false, true, null, null, display_options::DURING, ''],
  | 
        
        
            | 
            | 
           53 | 
                       [false, true, null, -60, display_options::IMMEDIATELY_AFTER, 'noreview'],
  | 
        
        
            | 
            | 
           54 | 
                       [false, true, null, -180, display_options::LATER_WHILE_OPEN, 'noreview'],
  | 
        
        
            | 
            | 
           55 | 
                       [false, true, null, -180, display_options::AFTER_CLOSE, 'noreview'],
  | 
        
        
            | 
            | 
           56 | 
                       // Quiz with a close in the future date, review only after close.
  | 
        
        
            | 
            | 
           57 | 
                       [false, true, 300, null, display_options::DURING, ''],
  | 
        
        
            | 
            | 
           58 | 
                       [false, true, 300, -60, display_options::IMMEDIATELY_AFTER, 300],
  | 
        
        
            | 
            | 
           59 | 
                       [false, true, 300, -180, display_options::LATER_WHILE_OPEN, 300],
  | 
        
        
            | 
            | 
           60 | 
                       // Quiz with a close in the future date, review later while open, or after close.
  | 
        
        
            | 
            | 
           61 | 
                       [true, true, 300, null, display_options::DURING, ''],
  | 
        
        
            | 
            | 
           62 | 
                       [true, true, 300, -60, display_options::IMMEDIATELY_AFTER, 60],
  | 
        
        
            | 
            | 
           63 | 
                       [true, false, 300, -60, display_options::IMMEDIATELY_AFTER, 60],
  | 
        
        
            | 
            | 
           64 | 
                       // Quiz with no closer date, review later while open.
  | 
        
        
            | 
            | 
           65 | 
                       [true, false, 300, null, display_options::DURING, ''],
  | 
        
        
            | 
            | 
           66 | 
                       [true, false, 300, -60, display_options::IMMEDIATELY_AFTER, 60],
  | 
        
        
            | 
            | 
           67 | 
                   ];
  | 
        
        
            | 
            | 
           68 | 
               }
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
               /**
  | 
        
        
            | 
            | 
           71 | 
                * Unit test for {@see quiz_settings::cannot_review_message()}.
  | 
        
        
            | 
            | 
           72 | 
                *
  | 
        
        
            | 
            | 
           73 | 
                * @dataProvider cannot_review_message_testcases
  | 
        
        
            | 
            | 
           74 | 
                * @param bool $reviewlater whether the quiz allows reivew 'later while the quiz is still open'.
  | 
        
        
            | 
            | 
           75 | 
                * @param bool $reviewafterclose whether the quiz allows rievew 'after the quiz is closed'.
  | 
        
        
            | 
            | 
           76 | 
                * @param int|null $quizcloseoffset quiz close date, relative to now. Null means not set.
  | 
        
        
            | 
            | 
           77 | 
                * @param int|null $attemptsubmitoffset quiz attempt sumbite time relative to now. Null means not submitted yet.
  | 
        
        
            | 
            | 
           78 | 
                * @param int $attemptstate current state of the attempt, one of the display_options constants.
  | 
        
        
            | 
            | 
           79 | 
                * @param string|int $expectation expected result: '' means '', 'noreview' means noreview lang string,
  | 
        
        
            | 
            | 
           80 | 
                *      int means noreviewuntil with that time relative to now.
  | 
        
        
            | 
            | 
           81 | 
                */
  | 
        
        
            | 
            | 
           82 | 
               public function test_cannot_review_message(
  | 
        
        
            | 
            | 
           83 | 
                   bool $reviewlater,
  | 
        
        
            | 
            | 
           84 | 
                   bool $reviewafterclose,
  | 
        
        
            | 
            | 
           85 | 
                   ?int $quizcloseoffset,
  | 
        
        
            | 
            | 
           86 | 
                   ?int $attemptsubmitoffset,
  | 
        
        
            | 
            | 
           87 | 
                   int $attemptstate,
  | 
        
        
            | 
            | 
           88 | 
                   string|int $expectation
  | 
        
        
            | 
            | 
           89 | 
               ): void {
  | 
        
        
            | 
            | 
           90 | 
                   $quiz = new stdClass();
  | 
        
        
            | 
            | 
           91 | 
                   $now = time();
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                   $cm = new stdClass();
  | 
        
        
            | 
            | 
           94 | 
                   $cm->id = 123;
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
            | 
            | 
           96 | 
                   // Prepare quiz settings.
  | 
        
        
            | 
            | 
           97 | 
                   $quiz->reviewattempt = display_options::DURING;
  | 
        
        
            | 
            | 
           98 | 
                   if ($reviewlater) {
  | 
        
        
            | 
            | 
           99 | 
                       $quiz->reviewattempt |= display_options::LATER_WHILE_OPEN;
  | 
        
        
            | 
            | 
           100 | 
                   }
  | 
        
        
            | 
            | 
           101 | 
                   if ($reviewafterclose) {
  | 
        
        
            | 
            | 
           102 | 
                       $quiz->reviewattempt |= display_options::AFTER_CLOSE;
  | 
        
        
            | 
            | 
           103 | 
                   }
  | 
        
        
            | 
            | 
           104 | 
                   $quiz->attempts = 0;
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   if ($quizcloseoffset === null) {
  | 
        
        
            | 
            | 
           107 | 
                       $quiz->timeclose = 0;
  | 
        
        
            | 
            | 
           108 | 
                   } else {
  | 
        
        
            | 
            | 
           109 | 
                       $quiz->timeclose = $now + $quizcloseoffset;
  | 
        
        
            | 
            | 
           110 | 
                   }
  | 
        
        
            | 
            | 
           111 | 
                   if ($attemptsubmitoffset === null) {
  | 
        
        
            | 
            | 
           112 | 
                       $submittime = 0;
  | 
        
        
            | 
            | 
           113 | 
                   } else {
  | 
        
        
            | 
            | 
           114 | 
                       $submittime = $now + $attemptsubmitoffset;
  | 
        
        
            | 
            | 
           115 | 
                   }
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
                   $quizobj = new quiz_settings($quiz, $cm, new stdClass(), false);
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
                   // Prepare expected message.
  | 
        
        
            | 
            | 
           120 | 
                   if ($expectation === 'noreview') {
  | 
        
        
            | 
            | 
           121 | 
                       $expectation = get_string('noreview', 'quiz');
  | 
        
        
            | 
            | 
           122 | 
                   } else if (is_int($expectation)) {
  | 
        
        
            | 
            | 
           123 | 
                       $expectation = get_string('noreviewuntil', 'quiz', userdate($now + $expectation));
  | 
        
        
            | 
            | 
           124 | 
                   }
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
                   // Test.
  | 
        
        
            | 
            | 
           127 | 
                   $this->assertEquals($expectation,
  | 
        
        
            | 
            | 
           128 | 
                       $quizobj->cannot_review_message($attemptstate, false, $submittime));
  | 
        
        
            | 
            | 
           129 | 
               }
  | 
        
        
            | 
            | 
           130 | 
           }
  |