[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/tests/ -> conditionlib_test.php (source)

   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   * Tests for deprecated conditional activities classes.
  19   *
  20   * @package core_availability
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/lib/conditionlib.php');
  29  
  30  
  31  /**
  32   * Tests for deprecated conditional activities classes.
  33   *
  34   * @package core_availability
  35   * @copyright 2014 The Open University
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  37   */
  38  class core_conditionlib_testcase extends advanced_testcase {
  39  
  40      protected function setUp() {
  41          global $CFG;
  42          parent::setUp();
  43  
  44          $this->resetAfterTest();
  45  
  46          $CFG->enableavailability = 1;
  47          $CFG->enablecompletion = 1;
  48          $user = $this->getDataGenerator()->create_user();
  49          $this->setUser($user);
  50      }
  51  
  52      public function test_constructor() {
  53          $generator = $this->getDataGenerator();
  54          $course = $generator->create_course();
  55          $page = $generator->get_plugin_generator('mod_page')->create_instance(
  56                  array('course' => $course));
  57          $modinfo = get_fast_modinfo($course);
  58  
  59          // No ID.
  60          try {
  61              $test = new condition_info((object)array());
  62              $this->fail();
  63          } catch (coding_exception $e) {
  64              // Do nothing.
  65              $this->assertDebuggingCalled();
  66          }
  67  
  68          // Get actual cm_info for comparison.
  69          $realcm = $modinfo->get_cm($page->cmid);
  70  
  71          // No other data.
  72          $test = new condition_info((object)array('id' => $page->cmid));
  73          $this->assertDebuggingCalled();
  74          $this->assertEquals($realcm, $test->get_full_course_module());
  75          $this->assertDebuggingCalled();
  76  
  77          // Course id.
  78          $test = new condition_info((object)array('id' => $page->cmid, 'course' => $course->id));
  79          $this->assertDebuggingCalled();
  80          $this->assertEquals($realcm, $test->get_full_course_module());
  81          $this->assertDebuggingCalled();
  82  
  83          // Full cm.
  84          $test = new condition_info($realcm);
  85          $this->assertDebuggingCalled();
  86          $this->assertEquals($realcm, $test->get_full_course_module());
  87          $this->assertDebuggingCalled();
  88      }
  89  
  90      /**
  91       * Same as above test but for course_sections instead of course_modules.
  92       */
  93      public function test_section_constructor() {
  94          $generator = $this->getDataGenerator();
  95          $course = $generator->create_course(
  96                  array('numsections' => 1), array('createsections' => true));
  97          $modinfo = get_fast_modinfo($course);
  98  
  99          // No ID.
 100          try {
 101              $test = new condition_info_section(((object)array()));
 102              $this->fail();
 103          } catch (coding_exception $e) {
 104              // Do nothing.
 105              $this->assertDebuggingCalled();
 106          }
 107  
 108          // Get actual cm_info for comparison.
 109          $realsection = $modinfo->get_section_info(1);
 110  
 111          // No other data.
 112          $test = new condition_info_section((object)array('id' => $realsection->id));
 113          $this->assertDebuggingCalled();
 114          $this->assertEquals($realsection, $test->get_full_section());
 115          $this->assertDebuggingCalled();
 116  
 117          // Course id.
 118          $test = new condition_info_section((object)array('id' => $realsection->id,
 119                  'course' => $course->id));
 120          $this->assertDebuggingCalled();
 121          $this->assertEquals($realsection, $test->get_full_section());
 122          $this->assertDebuggingCalled();
 123  
 124          // Full object.
 125          $test = new condition_info_section($realsection);
 126          $this->assertDebuggingCalled();
 127          $this->assertEquals($realsection, $test->get_full_section());
 128          $this->assertDebuggingCalled();
 129      }
 130  
 131      /**
 132       * Tests the is_available function for modules. This does not test all the
 133       * conditions and stuff, because it only needs to check that the system
 134       * connects through to the real availability API. Also tests
 135       * get_full_information function.
 136       */
 137      public function test_is_available() {
 138          // Create course.
 139          $generator = $this->getDataGenerator();
 140          $course = $generator->create_course();
 141  
 142          // Create activity with no restrictions and one with date restriction.
 143          $page1 = $generator->get_plugin_generator('mod_page')->create_instance(
 144                  array('course' => $course));
 145          $time = time() + 100;
 146          $avail = '{"op":"|","show":true,"c":[{"type":"date","d":">=","t":' . $time . '}]}';
 147          $page2 = $generator->get_plugin_generator('mod_page')->create_instance(
 148                  array('course' => $course, 'availability' => $avail));
 149  
 150          // No conditions.
 151          $ci = new condition_info((object)array('id' => $page1->cmid),
 152                  CONDITION_MISSING_EVERYTHING);
 153          $this->assertDebuggingCalled();
 154          $this->assertTrue($ci->is_available($text, false, 0));
 155          $this->assertDebuggingCalled();
 156          $this->assertEquals('', $text);
 157  
 158          // Date condition.
 159          $ci = new condition_info((object)array('id' => $page2->cmid),
 160              CONDITION_MISSING_EVERYTHING);
 161          $this->assertDebuggingCalled();
 162          $this->assertFalse($ci->is_available($text));
 163          $this->assertDebuggingCalled();
 164          $expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
 165          $this->assertContains($expectedtime, $text);
 166  
 167          // Full information display.
 168          $text = $ci->get_full_information();
 169          $this->assertDebuggingCalled();
 170          $expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
 171          $this->assertContains($expectedtime, $text);
 172      }
 173  
 174      /**
 175       * Tests the is_available function for sections.
 176       */
 177      public function test_section_is_available() {
 178          global $DB;
 179  
 180          // Create course.
 181          $generator = $this->getDataGenerator();
 182          $course = $generator->create_course(
 183                  array('numsections' => 2), array('createsections' => true));
 184  
 185          // Set one of the sections unavailable.
 186          $time = time() + 100;
 187          $avail = '{"op":"|","show":true,"c":[{"type":"date","d":">=","t":' . $time . '}]}';
 188          $DB->set_field('course_sections', 'availability', $avail, array(
 189                  'course' => $course->id, 'section' => 2));
 190  
 191          $modinfo = get_fast_modinfo($course);
 192  
 193          // No conditions.
 194          $ci = new condition_info_section($modinfo->get_section_info(1));
 195          $this->assertDebuggingCalled();
 196          $this->assertTrue($ci->is_available($text, false, 0));
 197          $this->assertDebuggingCalled();
 198          $this->assertEquals('', $text);
 199  
 200          // Date condition.
 201          $ci = new condition_info_section($modinfo->get_section_info(2));
 202          $this->assertDebuggingCalled();
 203          $this->assertFalse($ci->is_available($text));
 204          $this->assertDebuggingCalled();
 205          $expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
 206          $this->assertContains($expectedtime, $text);
 207  
 208          // Full information display.
 209          $text = $ci->get_full_information();
 210          $this->assertDebuggingCalled();
 211          $expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
 212          $this->assertContains($expectedtime, $text);
 213      }
 214  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1