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]

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