Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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