Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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_feedback;
  18  
  19  use mod_feedback_completion;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  global $CFG;
  23  require_once($CFG->dirroot . '/mod/feedback/classes/completion.php');
  24  
  25  /**
  26   * Unit tests for (some of) mod/feedback/classes/completion.php.
  27   *
  28   * @package    mod_feedback
  29   * @copyright  2019 Tobias Reischmann
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class completion_test extends \advanced_testcase {
  33      /**
  34       * Returns the number of pages with visible elements for the current state of the feedback completion.
  35       * @param mod_feedback_completion $completion
  36       * @return int number of pages with at least one visible item.
  37       */
  38      private function get_number_of_visible_pages(mod_feedback_completion $completion) {
  39          $pages = $completion->get_pages();
  40          $result = 0;
  41          foreach ($pages as $items) {
  42              if (count($items) > 0) {
  43                  $result++;
  44              }
  45          }
  46          return $result;
  47      }
  48  
  49      /**
  50       * Tests get_pages for transitive dependencies.
  51       * @throws coding_exception
  52       */
  53      public function test_get_pages() {
  54          $this->resetAfterTest();
  55          $this->setAdminUser();
  56  
  57          // Setup test data.
  58          $course = $this->getDataGenerator()->create_course();
  59          $feedback = $this->getDataGenerator()->create_module('feedback',
  60              array('course' => $course->id));
  61          $cm = get_coursemodule_from_instance('feedback', $feedback->id);
  62  
  63          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
  64          $itemscreated = [];
  65  
  66          // Create at least one page.
  67          $itemscreated[] = $feedbackgenerator->create_item_multichoice($feedback,
  68              $record = ['values' => "y\nn"]);
  69          $itemscreated[] = $feedbackgenerator->create_item_pagebreak($feedback);
  70          $itemscreated[] = $feedbackgenerator->create_item_multichoice($feedback,
  71              $record = ['values' => "y\nn", 'dependitem' => $itemscreated[0]->id, 'dependvalue' => 'n']);
  72          $itemscreated[] = $feedbackgenerator->create_item_pagebreak($feedback);
  73          $itemscreated[] = $feedbackgenerator->create_item_multichoice($feedback,
  74              $record = ['values' => "y\nn", 'dependitem' => $itemscreated[0]->id, 'dependvalue' => 'y']);
  75          $itemscreated[] = $feedbackgenerator->create_item_pagebreak($feedback);
  76          $itemscreated[] = $feedbackgenerator->create_item_multichoice($feedback,
  77              $record = ['values' => "y\nn", 'dependitem' => $itemscreated[2]->id, 'dependvalue' => 'y']);
  78  
  79          // Test hiding item since transitive dependency is not met.
  80          // Answering the first multichoice with 'y', should hide the second and therefor also the fourth.
  81          $user1 = $this->getDataGenerator()->create_and_enrol($course);
  82          $completion = new mod_feedback_completion($feedback, $cm, $course,
  83              false, null, $user1->id);
  84  
  85          // Initially, all pages should be visible.
  86          $this->assertEquals(4, $this->get_number_of_visible_pages($completion));
  87  
  88          // Answer the first multichoice with 'y', which should exclude the second and the fourth.
  89          $answers = ['multichoice_' . $itemscreated[0]->id => [1]];
  90          $completion->save_response_tmp((object) $answers);
  91  
  92          $this->assertEquals(2, $this->get_number_of_visible_pages($completion));
  93  
  94          // Answer the third multichoice with 'n', which should exclude the last one.
  95          $answers = ['multichoice_' . $itemscreated[4]->id => [2]];
  96          $completion->save_response_tmp((object) $answers);
  97  
  98          $this->assertEquals(2, $this->get_number_of_visible_pages($completion));
  99  
 100          $completion->save_response();
 101  
 102          // Test showing item since transitive dependency is met.
 103          // Answering the first multichoice with 'n' should hide the third multichoice.
 104          $user2 = $this->getDataGenerator()->create_and_enrol($course);
 105          $completion2 = new mod_feedback_completion($feedback, $cm, $course,
 106              false, null, $user2->id);
 107  
 108          // Initially, all pages should be visible.
 109          $this->assertEquals(4, $this->get_number_of_visible_pages($completion2));
 110  
 111          // Answer the first multichoice with 'n' should hide the third multichoice.
 112          $answers = ['multichoice_' . $itemscreated[0]->id => [2]];
 113          $completion2->save_response_tmp((object) $answers);
 114  
 115          $this->assertEquals(3, $this->get_number_of_visible_pages($completion2));
 116  
 117          // Answering the second multichoice with 'n' should hide the fourth one.
 118          $answers = ['multichoice_' . $itemscreated[2]->id => [2]];
 119          $completion2->save_response_tmp((object) $answers);
 120  
 121          $this->assertEquals(2, $this->get_number_of_visible_pages($completion2));
 122      }
 123  
 124  }