diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/files/storage/local.php | 59 | ||||
-rw-r--r-- | tests/lib/tempmanager.php | 143 |
2 files changed, 181 insertions, 21 deletions
diff --git a/tests/lib/files/storage/local.php b/tests/lib/files/storage/local.php index 1aad138aa33..8fd9f0648ad 100644 --- a/tests/lib/files/storage/local.php +++ b/tests/lib/files/storage/local.php @@ -1,24 +1,24 @@ <?php /** -* ownCloud -* -* @author Robin Appelman -* @copyright 2012 Robin Appelman icewind@owncloud.com -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE -* License as published by the Free Software Foundation; either -* version 3 of the License, or any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU AFFERO GENERAL PUBLIC LICENSE for more details. -* -* You should have received a copy of the GNU Affero General Public -* License along with this library. If not, see <http://www.gnu.org/licenses/>. -* -*/ + * ownCloud + * + * @author Robin Appelman + * @copyright 2012 Robin Appelman icewind@owncloud.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ namespace Test\Files\Storage; @@ -27,13 +27,30 @@ class Local extends Storage { * @var string tmpDir */ private $tmpDir; + public function setUp() { - $this->tmpDir=\OC_Helper::tmpFolder(); - $this->instance=new \OC\Files\Storage\Local(array('datadir'=>$this->tmpDir)); + $this->tmpDir = \OC_Helper::tmpFolder(); + $this->instance = new \OC\Files\Storage\Local(array('datadir' => $this->tmpDir)); } public function tearDown() { \OC_Helper::rmdirr($this->tmpDir); } + + public function testStableEtag() { + $this->instance->file_put_contents('test.txt', 'foobar'); + $etag1 = $this->instance->getETag('test.txt'); + $etag2 = $this->instance->getETag('test.txt'); + $this->assertEquals($etag1, $etag2); + } + + public function testEtagChange() { + $this->instance->file_put_contents('test.txt', 'foo'); + $this->instance->touch('test.txt', time() - 2); + $etag1 = $this->instance->getETag('test.txt'); + $this->instance->file_put_contents('test.txt', 'bar'); + $etag2 = $this->instance->getETag('test.txt'); + $this->assertNotEquals($etag1, $etag2); + } } diff --git a/tests/lib/tempmanager.php b/tests/lib/tempmanager.php new file mode 100644 index 00000000000..f16fbce2c7c --- /dev/null +++ b/tests/lib/tempmanager.php @@ -0,0 +1,143 @@ +<?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 \PHPUnit_Framework_TestCase { + protected $baseDir; + + public function setUp() { + $this->baseDir = get_temp_dir() . '/oc_tmp_test'; + if (!is_dir($this->baseDir)) { + mkdir($this->baseDir); + } + } + + public function tearDown() { + \OC_Helper::rmdirr($this->baseDir); + } + + /** + * @param \Psr\Log\LoggerInterface $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() { + $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() { + $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()); + } +} |