[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/availability/condition/date/tests/ -> condition_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   * Unit tests for the date condition.
  19   *
  20   * @package availability_date
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use availability_date\condition;
  28  
  29  /**
  30   * Unit tests for the date condition.
  31   *
  32   * @package availability_date
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class availability_date_condition_testcase extends advanced_testcase {
  37      /**
  38       * Load required classes.
  39       */
  40      public function setUp() {
  41          // Load the mock info class so that it can be used.
  42          global $CFG;
  43          require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php');
  44      }
  45  
  46      /**
  47       * Tests constructing and using date condition as part of tree.
  48       */
  49      public function test_in_tree() {
  50          global $SITE, $USER;
  51          $this->resetAfterTest();
  52          $this->setAdminUser();
  53  
  54          // Set server timezone for test. (Important as otherwise the timezone
  55          // could be anything - this is modified by other unit tests, too.)
  56          date_default_timezone_set('UTC');
  57  
  58          // SEt user to GMT+5.
  59          $USER->timezone = 5;
  60  
  61          // Construct tree with date condition.
  62          $time = strtotime('2014-02-18 14:20:00 GMT');
  63          $structure = (object)array('op' => '|', 'show' => true, 'c' => array(
  64                  (object)array('type' => 'date', 'd' => '>=', 't' => $time)));
  65          $tree = new \core_availability\tree($structure);
  66          $info = new \core_availability\mock_info();
  67  
  68          // Check if available (when not available).
  69          condition::set_current_time_for_test($time - 1);
  70          $information = '';
  71          $result = $tree->check_available(false, $info, true, $USER->id);
  72          $this->assertFalse($result->is_available());
  73          $information = $tree->get_result_information($info, $result);
  74  
  75          // Note: PM is normally upper-case, but an issue with PHP on Mac means
  76          // that on that platform, it is reported lower-case.
  77          $this->assertRegExp('~from.*18 February 2014, 7:20 (PM|pm)~', $information);
  78  
  79          // Check if available (when available).
  80          condition::set_current_time_for_test($time);
  81          $result = $tree->check_available(false, $info, true, $USER->id);
  82          $this->assertTrue($result->is_available());
  83          $information = $tree->get_result_information($info, $result);
  84          $this->assertEquals('', $information);
  85      }
  86  
  87      /**
  88       * Tests the constructor including error conditions. Also tests the
  89       * string conversion feature (intended for debugging only).
  90       */
  91      public function test_constructor() {
  92          // No parameters.
  93          $structure = (object)array();
  94          try {
  95              $date = new condition($structure);
  96              $this->fail();
  97          } catch (coding_exception $e) {
  98              $this->assertContains('Missing or invalid ->d', $e->getMessage());
  99          }
 100  
 101          // Invalid ->d.
 102          $structure->d = 'woo hah!!';
 103          try {
 104              $date = new condition($structure);
 105              $this->fail();
 106          } catch (coding_exception $e) {
 107              $this->assertContains('Missing or invalid ->d', $e->getMessage());
 108          }
 109  
 110          // Missing ->t.
 111          $structure->d = '>=';
 112          try {
 113              $date = new condition($structure);
 114              $this->fail();
 115          } catch (coding_exception $e) {
 116              $this->assertContains('Missing or invalid ->t', $e->getMessage());
 117          }
 118  
 119          // Invalid ->t.
 120          $structure->t = 'got you all in check';
 121          try {
 122              $date = new condition($structure);
 123              $this->fail();
 124          } catch (coding_exception $e) {
 125              $this->assertContains('Missing or invalid ->t', $e->getMessage());
 126          }
 127  
 128          // Valid conditions of both types.
 129          $structure = (object)array('d' => '>=', 't' => strtotime('2014-02-18 14:43:17 GMT'));
 130          $date = new condition($structure);
 131          $this->assertEquals('{date:>= 2014-02-18 14:43:17}', (string)$date);
 132          $structure->d = '<';
 133          $date = new condition($structure);
 134          $this->assertEquals('{date:< 2014-02-18 14:43:17}', (string)$date);
 135      }
 136  
 137      /**
 138       * Tests the save() function.
 139       */
 140      public function test_save() {
 141          $structure = (object)array('d' => '>=', 't' => 12345);
 142          $cond = new condition($structure);
 143          $structure->type = 'date';
 144          $this->assertEquals($structure, $cond->save());
 145      }
 146  
 147      /**
 148       * Tests the is_available() and is_available_to_all() functions.
 149       */
 150      public function test_is_available() {
 151          global $SITE, $USER;
 152  
 153          $time = strtotime('2014-02-18 14:50:10 GMT');
 154          $info = new \core_availability\mock_info();
 155  
 156          // Test with >=.
 157          $date = new condition((object)array('d' => '>=', 't' => $time));
 158          condition::set_current_time_for_test($time - 1);
 159          $this->assertFalse($date->is_available(false, $info, true, $USER->id));
 160          condition::set_current_time_for_test($time);
 161          $this->assertTrue($date->is_available(false, $info, true, $USER->id));
 162  
 163          // Test with <.
 164          $date = new condition((object)array('d' => '<', 't' => $time));
 165          condition::set_current_time_for_test($time);
 166          $this->assertFalse($date->is_available(false, $info, true, $USER->id));
 167          condition::set_current_time_for_test($time - 1);
 168          $this->assertTrue($date->is_available(false, $info, true, $USER->id));
 169  
 170          // Repeat this test with is_available_to_all() - it should be the same.
 171          $date = new condition((object)array('d' => '<', 't' => $time));
 172          condition::set_current_time_for_test($time);
 173          $this->assertFalse($date->is_available_for_all(false));
 174          condition::set_current_time_for_test($time - 1);
 175          $this->assertTrue($date->is_available_for_all(false));
 176      }
 177  
 178      /**
 179       * Tests the get_description and get_standalone_description functions.
 180       */
 181      public function test_get_description() {
 182          global $SITE;
 183  
 184          $modinfo = get_fast_modinfo($SITE);
 185          $info = new \core_availability\mock_info();
 186          $time = strtotime('2014-02-18 14:55:01 GMT');
 187  
 188          // Test with >=.
 189          $date = new condition((object)array('d' => '>=', 't' => $time));
 190          $information = $date->get_description(true, false, $info);
 191          $this->assertRegExp('~after.*18 February 2014, 2:55 (PM|pm)~', $information);
 192          $information = $date->get_description(true, true, $info);
 193          $this->assertRegExp('~before.*18 February 2014, 2:55 (PM|pm)~', $information);
 194          $information = $date->get_standalone_description(true, false, $info);
 195          $this->assertRegExp('~from.*18 February 2014, 2:55 (PM|pm)~', $information);
 196          $information = $date->get_standalone_description(true, true, $info);
 197          $this->assertRegExp('~until.*18 February 2014, 2:55 (PM|pm)~', $information);
 198  
 199          // Test with <.
 200          $date = new condition((object)array('d' => '<', 't' => $time));
 201          $information = $date->get_description(true, false, $info);
 202          $this->assertRegExp('~before.*18 February 2014, 2:55 (PM|pm)~', $information);
 203          $information = $date->get_description(true, true, $info);
 204          $this->assertRegExp('~after.*18 February 2014, 2:55 (PM|pm)~', $information);
 205          $information = $date->get_standalone_description(true, false, $info);
 206          $this->assertRegExp('~until.*18 February 2014, 2:55 (PM|pm)~', $information);
 207          $information = $date->get_standalone_description(true, true, $info);
 208          $this->assertRegExp('~from.*18 February 2014, 2:55 (PM|pm)~', $information);
 209  
 210          // Test special case for dates that are midnight.
 211          $date = new condition((object)array('d' => '>=',
 212                  't' => strtotime('2014-03-05 00:00 GMT')));
 213          $information = $date->get_description(true, false, $info);
 214          $this->assertRegExp('~on or after.*5 March 2014([^0-9]*)$~', $information);
 215          $information = $date->get_description(true, true, $info);
 216          $this->assertRegExp('~before.*end of.*4 March 2014([^0-9]*)$~', $information);
 217          $information = $date->get_standalone_description(true, false, $info);
 218          $this->assertRegExp('~from.*5 March 2014([^0-9]*)$~', $information);
 219          $information = $date->get_standalone_description(true, true, $info);
 220          $this->assertRegExp('~until end of.*4 March 2014([^0-9]*)$~', $information);
 221  
 222          // In the 'until' case for midnight, it shows the previous day. (I.e.
 223          // if the date is 5 March 00:00, then we show it as available until 4
 224          // March, implying 'the end of'.)
 225          $date = new condition((object)array('d' => '<',
 226                  't' => strtotime('2014-03-05 00:00 GMT')));
 227          $information = $date->get_description(true, false, $info);
 228          $this->assertRegExp('~before end of.*4 March 2014([^0-9]*)$~', $information);
 229          $information = $date->get_description(true, true, $info);
 230          $this->assertRegExp('~on or after.*5 March 2014([^0-9]*)$~', $information);
 231          $information = $date->get_standalone_description(true, false, $info);
 232          $this->assertRegExp('~until end of.*4 March 2014([^0-9]*)$~', $information);
 233          $information = $date->get_standalone_description(true, true, $info);
 234          $this->assertRegExp('~from.*5 March 2014([^0-9]*)$~', $information);
 235      }
 236  }


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