1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test;
use OC\Log;
class NullLogger extends Log {
public function __construct($logger = null) {
//disable original constructor
}
public function log($level, $message, array $context = array()) {
//noop
}
}
class TempManager extends \Test\TestCase {
protected $baseDir;
protected function setUp() {
parent::setUp();
$this->baseDir = get_temp_dir() . $this->getUniqueID('/oc_tmp_test');
if (!is_dir($this->baseDir)) {
mkdir($this->baseDir);
}
}
protected function tearDown() {
\OC_Helper::rmdirr($this->baseDir);
parent::tearDown();
}
/**
* @param \OCP\ILogger $logger
* @return \OC\TempManager
*/
protected function getManager($logger = null) {
if (!$logger) {
$logger = new NullLogger();
}
return new \OC\TempManager($this->baseDir, $logger);
}
public function testGetFile() {
$manager = $this->getManager();
$file = $manager->getTemporaryFile('txt');
$this->assertStringEndsWith('.txt', $file);
$this->assertTrue(is_file($file));
$this->assertTrue(is_writable($file));
file_put_contents($file, 'bar');
$this->assertEquals('bar', file_get_contents($file));
}
public function testGetFolder() {
$manager = $this->getManager();
$folder = $manager->getTemporaryFolder();
$this->assertStringEndsWith('/', $folder);
$this->assertTrue(is_dir($folder));
$this->assertTrue(is_writable($folder));
file_put_contents($folder . 'foo.txt', 'bar');
$this->assertEquals('bar', file_get_contents($folder . 'foo.txt'));
}
public function testCleanFiles() {
$manager = $this->getManager();
$file1 = $manager->getTemporaryFile('txt');
$file2 = $manager->getTemporaryFile('txt');
$this->assertTrue(file_exists($file1));
$this->assertTrue(file_exists($file2));
$manager->clean();
$this->assertFalse(file_exists($file1));
$this->assertFalse(file_exists($file2));
}
public function testCleanFolder() {
$manager = $this->getManager();
$folder1 = $manager->getTemporaryFolder();
$folder2 = $manager->getTemporaryFolder();
touch($folder1 . 'foo.txt');
touch($folder1 . 'bar.txt');
$this->assertTrue(file_exists($folder1));
$this->assertTrue(file_exists($folder2));
$this->assertTrue(file_exists($folder1 . 'foo.txt'));
$this->assertTrue(file_exists($folder1 . 'bar.txt'));
$manager->clean();
$this->assertFalse(file_exists($folder1));
$this->assertFalse(file_exists($folder2));
$this->assertFalse(file_exists($folder1 . 'foo.txt'));
$this->assertFalse(file_exists($folder1 . 'bar.txt'));
}
public function testCleanOld() {
$manager = $this->getManager();
$oldFile = $manager->getTemporaryFile('txt');
$newFile = $manager->getTemporaryFile('txt');
$folder = $manager->getTemporaryFolder();
$nonOcFile = $this->baseDir . '/foo.txt';
file_put_contents($nonOcFile, 'bar');
$past = time() - 2 * 3600;
touch($oldFile, $past);
touch($folder, $past);
touch($nonOcFile, $past);
$manager2 = $this->getManager();
$manager2->cleanOld();
$this->assertFalse(file_exists($oldFile));
$this->assertFalse(file_exists($folder));
$this->assertTrue(file_exists($nonOcFile));
$this->assertTrue(file_exists($newFile));
}
public function testLogCantCreateFile() {
if (\OC_Util::runningOnWindows()) {
$this->markTestSkipped('[Windows] chmod() does not work as intended on Windows.');
}
$logger = $this->getMock('\Test\NullLogger');
$manager = $this->getManager($logger);
chmod($this->baseDir, 0500);
$logger->expects($this->once())
->method('warning')
->with($this->stringContains('Can not create a temporary file in directory'));
$this->assertFalse($manager->getTemporaryFile('txt'));
}
public function testLogCantCreateFolder() {
if (\OC_Util::runningOnWindows()) {
$this->markTestSkipped('[Windows] chmod() does not work as intended on Windows.');
}
$logger = $this->getMock('\Test\NullLogger');
$manager = $this->getManager($logger);
chmod($this->baseDir, 0500);
$logger->expects($this->once())
->method('warning')
->with($this->stringContains('Can not create a temporary folder in directory'));
$this->assertFalse($manager->getTemporaryFolder());
}
}
|