Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  18   * Template persistent class tests.
  19   *
  20   * @package    core_competency
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  global $CFG;
  27  
  28  use core_competency\template;
  29  
  30  /**
  31   * Template persistent testcase.
  32   *
  33   * @package    core_competency
  34   * @copyright  2016 Frédéric Massart - FMCorz.net
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class core_competency_template_testcase extends advanced_testcase {
  38  
  39      public function test_validate_duedate() {
  40          global $DB;
  41  
  42          $this->resetAfterTest();
  43          $tpl = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_template();
  44  
  45          // No due date -> pass.
  46          $tpl->set('duedate', 0);
  47          $this->assertTrue($tpl->is_valid());
  48  
  49          // Setting new due date in the past -> fail.
  50          $tpl->set('duedate', 1);
  51          $errors = $tpl->get_errors();
  52          $this->assertCount(1, $errors);
  53          $this->assertArrayHasKey('duedate', $errors);
  54  
  55          // Setting new due date in very close past -> pass.
  56          $tpl->set('duedate', time() - 10);
  57          $this->assertTrue($tpl->is_valid());
  58  
  59          // Setting new due date in future -> pass.
  60          $tpl->set('duedate', time() + 600);
  61          $this->assertTrue($tpl->is_valid());
  62  
  63          // Save due date in the future.
  64          $tpl->update();
  65  
  66          // Going from future date to past -> fail.
  67          $tpl->set('duedate', 1);
  68          $errors = $tpl->get_errors();
  69          $this->assertCount(1, $errors);
  70          $this->assertArrayHasKey('duedate', $errors);
  71  
  72          // Going from future date to none -> pass.
  73          $tpl->set('duedate', 0);
  74          $this->assertTrue($tpl->is_valid());
  75  
  76          // Going from future date to other future -> pass.
  77          $tpl->set('duedate', time() + 6000);
  78          $this->assertTrue($tpl->is_valid());
  79  
  80          // Going from future date to close past -> pass.
  81          $tpl->set('duedate', time() - 10);
  82          $this->assertTrue($tpl->is_valid());
  83  
  84          // Mocking past due date.
  85          $record = $tpl->to_record();
  86          $record->duedate = 1;
  87          $DB->update_record(template::TABLE, $record);
  88          $tpl->read();
  89          $this->assertEquals(1, $tpl->get('duedate'));
  90  
  91          // Not changing the past due date -> pass.
  92          // Note: changing visibility to force validation.
  93          $tpl->set('visible', 0);
  94          $tpl->set('visible', 1);
  95          $this->assertTrue($tpl->is_valid());
  96  
  97          // Changing past due date to other past -> fail.
  98          $tpl->set('duedate', 10);
  99          $errors = $tpl->get_errors();
 100          $this->assertCount(1, $errors);
 101          $this->assertArrayHasKey('duedate', $errors);
 102  
 103          // Changing past due date close past -> pass.
 104          $tpl->set('duedate', time() + 10);
 105          $this->assertTrue($tpl->is_valid());
 106  
 107          // Changing past due date to future -> pass.
 108          $tpl->set('duedate', time() + 1000);
 109          $this->assertTrue($tpl->is_valid());
 110  
 111          // Changing past due date to none -> pass.
 112          $tpl->set('duedate', 0);
 113          $this->assertTrue($tpl->is_valid());
 114      }
 115  }