2012-10-13 17:51:37 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 10:38:52 +03:00
|
|
|
namespace Test;
|
|
|
|
|
2016-09-07 21:11:43 +03:00
|
|
|
use OC\Files\View;
|
2016-05-19 10:38:52 +03:00
|
|
|
use OC_Helper;
|
|
|
|
|
|
|
|
class LegacyHelperTest extends \Test\TestCase {
|
2017-12-28 16:19:13 +03:00
|
|
|
/** @var string */
|
|
|
|
private $originalWebRoot;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-12-28 16:19:13 +03:00
|
|
|
$this->originalWebRoot = \OC::$WEBROOT;
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function tearDown(): void {
|
2017-12-28 16:19:13 +03:00
|
|
|
// Reset webRoot
|
|
|
|
\OC::$WEBROOT = $this->originalWebRoot;
|
|
|
|
}
|
2012-10-13 17:51:37 +04:00
|
|
|
|
2013-09-13 19:22:45 +04:00
|
|
|
/**
|
|
|
|
* @dataProvider humanFileSizeProvider
|
|
|
|
*/
|
|
|
|
public function testHumanFileSize($expected, $input)
|
|
|
|
{
|
|
|
|
$result = OC_Helper::humanFileSize($input);
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-10-13 17:51:37 +04:00
|
|
|
}
|
|
|
|
|
2013-09-13 19:22:45 +04:00
|
|
|
public function humanFileSizeProvider()
|
|
|
|
{
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
['0 B', 0],
|
|
|
|
['1 KB', 1024],
|
|
|
|
['9.5 MB', 10000000],
|
|
|
|
['1.3 GB', 1395864371],
|
|
|
|
['465.7 GB', 500000000000],
|
|
|
|
['454.7 TB', 500000000000000],
|
|
|
|
['444.1 PB', 500000000000000000],
|
|
|
|
];
|
2013-09-13 19:22:45 +04:00
|
|
|
}
|
2012-10-13 17:51:37 +04:00
|
|
|
|
2013-09-13 19:22:45 +04:00
|
|
|
/**
|
2015-05-07 18:56:13 +03:00
|
|
|
* @dataProvider providesComputerFileSize
|
2013-09-13 19:22:45 +04:00
|
|
|
*/
|
|
|
|
function testComputerFileSize($expected, $input) {
|
|
|
|
$result = OC_Helper::computerFileSize($input);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2012-10-13 17:51:37 +04:00
|
|
|
|
2015-05-07 18:56:13 +03:00
|
|
|
function providesComputerFileSize(){
|
|
|
|
return [
|
|
|
|
[0.0, "0 B"],
|
2016-01-19 12:37:20 +03:00
|
|
|
[1024.0, "1 KB"],
|
2015-05-07 18:56:13 +03:00
|
|
|
[1395864371.0, '1.3 GB'],
|
|
|
|
[9961472.0, "9.5 MB"],
|
|
|
|
[500041567437.0, "465.7 GB"],
|
|
|
|
[false, "12 GB etfrhzui"]
|
|
|
|
];
|
2012-10-13 17:51:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function testMb_array_change_key_case() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$arrayStart = [
|
2012-10-13 17:51:37 +04:00
|
|
|
"Foo" => "bar",
|
|
|
|
"Bar" => "foo",
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
|
|
|
$arrayResult = [
|
2012-10-13 17:51:37 +04:00
|
|
|
"foo" => "bar",
|
|
|
|
"bar" => "foo",
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2012-10-13 17:51:37 +04:00
|
|
|
$result = OC_Helper::mb_array_change_key_case($arrayStart);
|
|
|
|
$expected = $arrayResult;
|
|
|
|
$this->assertEquals($result, $expected);
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$arrayStart = [
|
2012-10-13 17:51:37 +04:00
|
|
|
"foo" => "bar",
|
|
|
|
"bar" => "foo",
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
|
|
|
$arrayResult = [
|
2012-10-13 17:51:37 +04:00
|
|
|
"FOO" => "bar",
|
|
|
|
"BAR" => "foo",
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2012-10-13 17:51:37 +04:00
|
|
|
$result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER);
|
|
|
|
$expected = $arrayResult;
|
2014-05-19 19:50:53 +04:00
|
|
|
$this->assertEquals($result, $expected);
|
2012-10-13 17:51:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function testRecursiveArraySearch() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$haystack = [
|
2012-10-13 17:51:37 +04:00
|
|
|
"Foo" => "own",
|
|
|
|
"Bar" => "Cloud",
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2012-10-13 17:51:37 +04:00
|
|
|
|
|
|
|
$result = OC_Helper::recursiveArraySearch($haystack, "own");
|
|
|
|
$expected = "Foo";
|
|
|
|
$this->assertEquals($result, $expected);
|
|
|
|
|
|
|
|
$result = OC_Helper::recursiveArraySearch($haystack, "NotFound");
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2013-03-08 17:22:04 +04:00
|
|
|
|
|
|
|
function testBuildNotExistingFileNameForView() {
|
2016-09-07 21:11:43 +03:00
|
|
|
$viewMock = $this->createMock(View::class);
|
2013-03-08 17:22:04 +04:00
|
|
|
$this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock));
|
|
|
|
$this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
|
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename.ext exists
|
2013-03-08 17:22:04 +04:00
|
|
|
$this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
|
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename.ext exists
|
2013-03-08 17:22:04 +04:00
|
|
|
$viewMock->expects($this->at(1))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename (2).ext exists
|
2013-03-08 17:22:04 +04:00
|
|
|
$this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
|
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename (1).ext exists
|
2013-03-08 17:22:04 +04:00
|
|
|
$this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock));
|
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename (2).ext exists
|
2013-07-05 17:38:09 +04:00
|
|
|
$this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
|
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename (2).ext exists
|
2013-03-08 17:22:04 +04:00
|
|
|
$viewMock->expects($this->at(1))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename (3).ext exists
|
2013-07-05 17:38:09 +04:00
|
|
|
$this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
|
2013-03-08 17:22:04 +04:00
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename(1).ext exists
|
2013-03-08 17:22:04 +04:00
|
|
|
$this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock));
|
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename(1) (1).ext exists
|
2013-03-08 17:22:04 +04:00
|
|
|
$this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
|
2013-07-05 17:38:09 +04:00
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename(1) (1).ext exists
|
2013-07-05 17:38:09 +04:00
|
|
|
$viewMock->expects($this->at(1))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename(1) (2).ext exists
|
2013-07-05 17:38:09 +04:00
|
|
|
$this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
|
|
|
|
|
|
|
|
$viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true); // filename(1) (2) (3).ext exists
|
2013-07-05 17:38:09 +04:00
|
|
|
$this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock));
|
2013-03-08 17:22:04 +04:00
|
|
|
}
|
2013-10-14 23:33:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider streamCopyDataProvider
|
|
|
|
*/
|
|
|
|
public function testStreamCopy($expectedCount, $expectedResult, $source, $target) {
|
|
|
|
|
|
|
|
if (is_string($source)) {
|
|
|
|
$source = fopen($source, 'r');
|
|
|
|
}
|
|
|
|
if (is_string($target)) {
|
|
|
|
$target = fopen($target, 'w');
|
|
|
|
}
|
|
|
|
|
|
|
|
list($count, $result) = \OC_Helper::streamCopy($source, $target);
|
|
|
|
|
|
|
|
if (is_resource($source)) {
|
|
|
|
fclose($source);
|
|
|
|
}
|
|
|
|
if (is_resource($target)) {
|
|
|
|
fclose($target);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame($expectedCount, $count);
|
|
|
|
$this->assertSame($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function streamCopyDataProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
[0, false, false, false],
|
|
|
|
[0, false, \OC::$SERVERROOT . '/tests/data/lorem.txt', false],
|
|
|
|
[filesize(\OC::$SERVERROOT . '/tests/data/lorem.txt'), true, \OC::$SERVERROOT . '/tests/data/lorem.txt', \OC::$SERVERROOT . '/tests/data/lorem-copy.txt'],
|
|
|
|
[3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'],
|
|
|
|
];
|
2013-10-14 23:33:23 +04:00
|
|
|
}
|
2014-04-16 16:32:08 +04:00
|
|
|
|
2014-06-23 18:48:25 +04:00
|
|
|
/**
|
|
|
|
* Tests recursive folder deletion with rmdirr()
|
|
|
|
*/
|
|
|
|
public function testRecursiveFolderDeletion() {
|
2015-12-18 13:19:53 +03:00
|
|
|
$baseDir = \OC::$server->getTempManager()->getTemporaryFolder() . '/';
|
2014-06-23 18:48:25 +04:00
|
|
|
mkdir($baseDir . 'a/b/c/d/e', 0777, true);
|
|
|
|
mkdir($baseDir . 'a/b/c1/d/e', 0777, true);
|
|
|
|
mkdir($baseDir . 'a/b/c2/d/e', 0777, true);
|
|
|
|
mkdir($baseDir . 'a/b1/c1/d/e', 0777, true);
|
|
|
|
mkdir($baseDir . 'a/b2/c1/d/e', 0777, true);
|
|
|
|
mkdir($baseDir . 'a/b3/c1/d/e', 0777, true);
|
|
|
|
mkdir($baseDir . 'a1/b', 0777, true);
|
|
|
|
mkdir($baseDir . 'a1/c', 0777, true);
|
|
|
|
file_put_contents($baseDir . 'a/test.txt', 'Hello file!');
|
|
|
|
file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!');
|
|
|
|
file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!');
|
|
|
|
\OC_Helper::rmdirr($baseDir . 'a');
|
|
|
|
|
|
|
|
$this->assertFalse(file_exists($baseDir . 'a'));
|
|
|
|
$this->assertTrue(file_exists($baseDir . 'a1'));
|
|
|
|
|
|
|
|
\OC_Helper::rmdirr($baseDir);
|
|
|
|
$this->assertFalse(file_exists($baseDir));
|
|
|
|
}
|
2014-09-26 18:14:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows us to test private methods/properties
|
|
|
|
*
|
|
|
|
* @param $object
|
|
|
|
* @param $methodName
|
|
|
|
* @param array $parameters
|
|
|
|
* @return mixed
|
2015-06-03 13:03:02 +03:00
|
|
|
* @deprecated Please extend \Test\TestCase and use self::invokePrivate() then
|
2014-09-26 18:14:59 +04:00
|
|
|
*/
|
2020-03-26 11:30:18 +03:00
|
|
|
public static function invokePrivate($object, $methodName, array $parameters = []) {
|
2015-06-03 13:03:02 +03:00
|
|
|
return parent::invokePrivate($object, $methodName, $parameters);
|
2014-09-26 18:14:59 +04:00
|
|
|
}
|
2012-10-13 23:02:12 +04:00
|
|
|
}
|