MDL-53700 competency: Migrating data generator to core
[moodle.git] / admin / tool / lp / tests / template_test.php
CommitLineData
a967d2aa
FM
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 tool_lp
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
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27
67bc0eaf 28use core_competency\template;
a967d2aa
FM
29
30/**
31 * Template persistent testcase.
32 *
33 * @package tool_lp
34 * @copyright 2016 Frédéric Massart - FMCorz.net
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_lp_template_testcase extends advanced_testcase {
38
39 public function test_validate_duedate() {
40 global $DB;
41
42 $this->resetAfterTest();
922634d3 43 $tpl = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_template();
a967d2aa
FM
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}