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