Merge branch 'MDL-38570-master' of https://github.com/timgus/moodle
[moodle.git] / lib / tests / cronlib_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir.'/cronlib.php');
31 class cronlib_testcase extends basic_testcase {
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;
40         $time = time();
42         $weekstime = $time - strtotime('1 week');
43         $beforeweekstime = $time - strtotime('1 week') - 1;
44         $afterweekstime = $time + strtotime('1 week') + 1;
46         $node1 = new stdClass();
47         $node1->path = '/dir1/dir1_1/dir1_2/dir1_3/';
48         $node1->time = 1;
49         $node1->isdir = true;
51         $node2 = new stdClass();
52         $node2->path = '/dir1/dir1_4/';
53         $node2->time = $time;
54         $node2->isdir = true;
56         $node3 = new stdClass();
57         $node3->path = '/dir2/';
58         $node3->isdir = true;
59         $node3->time = $time - $weekstime;
61         $node4 = new stdClass();
62         $node4->path = '/dir3/';
63         $node4->isdir = true;
64         $node4->time = $time - $afterweekstime;
66         $node5 = new stdClass();
67         $node5->path = '/dir1/dir1_1/dir1_2/file1';
68         $node5->isdir = false;
69         $node5->time = $beforeweekstime;
71         $node6 = new stdClass();
72         $node6->path = '/dir1/dir1_1/dir1_2/file2';
73         $node6->isdir = false;
74         $node6->time = $time;
76         $node7 = new stdClass();
77         $node7->path = '/dir1/dir1_4/file1';
78         $node7->isdir = false;
79         $node7->time = $time - $afterweekstime;
81         $node8 = new stdClass();
82         $node8->path = '/dir1/dir1_4/file2';
83         $node8->isdir = false;
84         $node8->time = $time;
86         $node9 = new stdClass();
87         $node9->path = '/file1';
88         $node9->isdir = false;
89         $node9->time = $time;
91         $node10 = new stdClass();
92         $node10->path = '/file2';
93         $node10->isdir = false;
94         $node10->time = $time - $afterweekstime;
96         $data = array(
97                 array(
98                     array($node1, $node2, $node3, $node4, $node5, $node6, $node7, $node8, $node9, $node10),
99                     array(
100                         "$CFG->tempdir/dir1",
101                         "$CFG->tempdir/dir1/dir1_1",
102                         "$CFG->tempdir/dir1/dir1_1/dir1_2",
103                         "$CFG->tempdir/dir1/dir1_1/dir1_2/file2",
104                         "$CFG->tempdir/dir1/dir1_4",
105                         "$CFG->tempdir/dir1/dir1_4/file2",
106                         "$CFG->tempdir/dir2",
107                         "$CFG->tempdir/file1",
108                     )
109                 ),
110                 array(
111                     array(),
112                     array()
113                 )
114         );
116         return $data;
117     }
119     /**
120      * Test removing files and directories from tempdir.
121      *
122      * @dataProvider cron_delete_from_temp_provider
123      * @param array $nodes List of files and directories
124      * @param array $expected The expected results
125      */
126     public function test_cron_delete_from_temp($nodes, $expected) {
127         global $CFG;
129         $tmpdir = $CFG->tempdir;
131         foreach ($nodes as $data) {
132             if ($data->isdir) {
133                 mkdir($tmpdir.$data->path, $CFG->directorypermissions, true);
134             }
135             touch($tmpdir.$data->path, $data->time);
136         }
138         cron_delete_from_temp();
140         $dir = new RecursiveDirectoryIterator($tmpdir);
141         $iter = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
143         $actual = array();
144         for ($iter->rewind(); $iter->valid(); $iter->next()) {
145             if (!$iter->isDot()) {
146                 $actual[] = $iter->getPathname();
147             }
148         }
150         // Sort results to guarantee actual order.
151         sort($actual);
153         $this->assertEquals($expected, $actual);
154     }