MDL-47572 unit tests: make cronlib_testcase DST immune
[moodle.git] / lib / tests / cronlib_test.php
CommitLineData
179961e8
TG
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 cron.
19 *
20 * @package core
21 * @category test
22 * @copyright 2013 Tim Gusak <tim.gusak@remote-learner.net>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir.'/cronlib.php');
30
31class cronlib_testcase extends basic_testcase {
32
33 /**
34 * Data provider for cron_delete_from_temp.
35 *
36 * @return array Provider data
37 */
38 public function cron_delete_from_temp_provider() {
39 global $CFG;
c4012e7e
TG
40
41 $tmpdir = realpath($CFG->tempdir);
3f86b023
DW
42 // This is a relative time.
43 $time = 0;
179961e8 44
20f29d2c 45 // Relative time stamps. Did you know data providers get executed during phpunit init?
54f7d2d7 46 $lastweekstime = strtotime('-1 week') - time();
68632b6d
EL
47 $beforelastweekstime = $lastweekstime - 3600 - 1; // At least 1h and 1s diff (make it DST immune).
48 $afterlastweekstime = $lastweekstime + 3600 + 1; // At least 1h and 1s diff (make it DST immune).
75b70ab6
RN
49
50 $nodes = array();
51 // Really old directory to remove.
20f29d2c 52 $nodes[] = $this->generate_test_path('/dir1/dir1_1/dir1_1_1/dir1_1_1_1/', true, $lastweekstime * 52, false);
75b70ab6
RN
53
54 // New Directory to keep.
20f29d2c 55 $nodes[] = $this->generate_test_path('/dir1/dir1_2/', true, $time, true);
75b70ab6 56
54f7d2d7
MG
57 // Directory a little less than 1 week old, keep.
58 $nodes[] = $this->generate_test_path('/dir2/', true, $afterlastweekstime, true);
75b70ab6
RN
59
60 // Directory older than 1 week old, remove.
20f29d2c 61 $nodes[] = $this->generate_test_path('/dir3/', true, $beforelastweekstime, false);
75b70ab6
RN
62
63 // File older than 1 week old, remove.
20f29d2c 64 $nodes[] = $this->generate_test_path('/dir1/dir1_1/dir1_1_1/file1_1_1_1', false, $beforelastweekstime, false);
75b70ab6
RN
65
66 // New File to keep.
20f29d2c 67 $nodes[] = $this->generate_test_path('/dir1/dir1_1/dir1_1_1/file1_1_1_2', false, $time, true);
75b70ab6
RN
68
69 // File older than 1 week old, remove.
20f29d2c 70 $nodes[] = $this->generate_test_path('/dir1/dir1_2/file1_1_2_1', false, $beforelastweekstime, false);
75b70ab6
RN
71
72 // New file to keep.
20f29d2c 73 $nodes[] = $this->generate_test_path('/dir1/dir1_2/file1_1_2_2', false, $time, true);
75b70ab6
RN
74
75 // New file to keep.
20f29d2c 76 $nodes[] = $this->generate_test_path('/file1', false, $time, true);
75b70ab6
RN
77
78 // File older than 1 week, keep.
20f29d2c 79 $nodes[] = $this->generate_test_path('/file2', false, $beforelastweekstime, false);
75b70ab6
RN
80
81 // Directory older than 1 week to keep.
82 // Note: Since this directory contains a directory that contains a file that is also older than a week
83 // the directory won't be deleted since it's mtime will be updated when the file is deleted.
84
20f29d2c 85 $nodes[] = $this->generate_test_path('/dir4/dir4_1', true, $beforelastweekstime, true);
75b70ab6 86
20f29d2c 87 $nodes[] = $this->generate_test_path('/dir4/dir4_1/dir4_1_1/', true, $beforelastweekstime, true);
75b70ab6
RN
88
89 // File older than 1 week to remove.
20f29d2c 90 $nodes[] = $this->generate_test_path('/dir4/dir4_1/dir4_1_1/file4_1_1_1', false, $beforelastweekstime, false);
75b70ab6
RN
91
92 $expectednodes = array();
93 foreach ($nodes as $node) {
94 if ($node->keep) {
95 $path = $tmpdir;
96 $pelements = preg_split('/\//', $node->path);
97 foreach ($pelements as $pelement) {
98 if ($pelement === '') {
99 continue;
100 }
101 $path .= DIRECTORY_SEPARATOR . $pelement;
102 if (!in_array($path, $expectednodes)) {
103 $expectednodes[] = $path;
104 }
105 }
106 }
107 }
108 sort($expectednodes);
179961e8
TG
109
110 $data = array(
111 array(
75b70ab6
RN
112 $nodes,
113 $expectednodes
179961e8
TG
114 ),
115 array(
116 array(),
117 array()
118 )
119 );
120
121 return $data;
122 }
123
75b70ab6
RN
124 /**
125 * Function to populate node array.
126 *
127 * @param string $path Path of directory or file
128 * @param bool $isdir Is the node a directory
129 * @param int $time modified time of the node in epoch
130 * @param bool $keep Should the node exist after the delete function has run
131 */
132 private function generate_test_path($path, $isdir = false, $time = 0, $keep = false) {
133 $node = new stdClass();
134 $node->path = $path;
135 $node->isdir = $isdir;
136 $node->time = $time;
137 $node->keep = $keep;
138 return $node;
139 }
179961e8
TG
140 /**
141 * Test removing files and directories from tempdir.
142 *
143 * @dataProvider cron_delete_from_temp_provider
144 * @param array $nodes List of files and directories
145 * @param array $expected The expected results
146 */
147 public function test_cron_delete_from_temp($nodes, $expected) {
148 global $CFG;
149
150 $tmpdir = $CFG->tempdir;
151
152 foreach ($nodes as $data) {
153 if ($data->isdir) {
154 mkdir($tmpdir.$data->path, $CFG->directorypermissions, true);
155 }
179961e8 156 }
75b70ab6
RN
157 // We need to iterate through again since adding a file to a directory will
158 // update the modified time of the directory.
159 foreach ($nodes as $data) {
20f29d2c 160 touch($tmpdir.$data->path, time() + $data->time);
75b70ab6 161 }
179961e8 162
309ae892
DW
163 $task = new \core\task\file_temp_cleanup_task();
164 $task->execute();
179961e8
TG
165
166 $dir = new RecursiveDirectoryIterator($tmpdir);
167 $iter = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
168
169 $actual = array();
170 for ($iter->rewind(); $iter->valid(); $iter->next()) {
171 if (!$iter->isDot()) {
c4012e7e 172 $actual[] = $iter->getRealPath();
179961e8
TG
173 }
174 }
175
176 // Sort results to guarantee actual order.
177 sort($actual);
178
179 $this->assertEquals($expected, $actual);
180 }
181}