Merge branch 'MDL-66992-master' of https://github.com/tungthai/moodle
[moodle.git] / competency / tests / template_test.php
blob5ac64e0f26fdbda0a6e8fd5b8eddf0e7fb8c6e9e
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.
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/>.
17 /**
18 * Template persistent class tests.
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
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
28 use core_competency\template;
30 /**
31 * Template persistent testcase.
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
37 class core_competency_template_testcase extends advanced_testcase {
39 public function test_validate_duedate() {
40 global $DB;
42 $this->resetAfterTest();
43 $tpl = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_template();
45 // No due date -> pass.
46 $tpl->set('duedate', 0);
47 $this->assertTrue($tpl->is_valid());
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);
55 // Setting new due date in very close past -> pass.
56 $tpl->set('duedate', time() - 10);
57 $this->assertTrue($tpl->is_valid());
59 // Setting new due date in future -> pass.
60 $tpl->set('duedate', time() + 600);
61 $this->assertTrue($tpl->is_valid());
63 // Save due date in the future.
64 $tpl->update();
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);
72 // Going from future date to none -> pass.
73 $tpl->set('duedate', 0);
74 $this->assertTrue($tpl->is_valid());
76 // Going from future date to other future -> pass.
77 $tpl->set('duedate', time() + 6000);
78 $this->assertTrue($tpl->is_valid());
80 // Going from future date to close past -> pass.
81 $tpl->set('duedate', time() - 10);
82 $this->assertTrue($tpl->is_valid());
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'));
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());
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);
103 // Changing past due date close past -> pass.
104 $tpl->set('duedate', time() + 10);
105 $this->assertTrue($tpl->is_valid());
107 // Changing past due date to future -> pass.
108 $tpl->set('duedate', time() + 1000);
109 $this->assertTrue($tpl->is_valid());
111 // Changing past due date to none -> pass.
112 $tpl->set('duedate', 0);
113 $this->assertTrue($tpl->is_valid());