mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/15747] Add test
PHPBB3-15747
This commit is contained in:
parent
225a6e2cbe
commit
b2d7406381
3 changed files with 179 additions and 64 deletions
|
@ -255,6 +255,8 @@ class local implements adapter_interface, stream_interface
|
||||||
* @param string $path The file path
|
* @param string $path The file path
|
||||||
*/
|
*/
|
||||||
protected function remove_empty_dirs($path)
|
protected function remove_empty_dirs($path)
|
||||||
|
{
|
||||||
|
if ($this->subfolders)
|
||||||
{
|
{
|
||||||
$dirpath = dirname($this->root_path . $path);
|
$dirpath = dirname($this->root_path . $path);
|
||||||
$filepath = dirname($this->root_path . $this->get_path($path) . $this->get_filename($path));
|
$filepath = dirname($this->root_path . $this->get_path($path) . $this->get_filename($path));
|
||||||
|
@ -268,6 +270,7 @@ class local implements adapter_interface, stream_interface
|
||||||
}
|
}
|
||||||
while ($path && @rmdir($dirpath . '/' . $path));
|
while ($path && @rmdir($dirpath . '/' . $path));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the path to the file, appending subdirectories for directory depth
|
* Get the path to the file, appending subdirectories for directory depth
|
||||||
|
@ -277,6 +280,9 @@ class local implements adapter_interface, stream_interface
|
||||||
*/
|
*/
|
||||||
protected function get_path($path)
|
protected function get_path($path)
|
||||||
{
|
{
|
||||||
|
$dirname = dirname($path);
|
||||||
|
$dirname = ($dirname != '.') ? $dirname . DIRECTORY_SEPARATOR : '';
|
||||||
|
|
||||||
if ($this->subfolders)
|
if ($this->subfolders)
|
||||||
{
|
{
|
||||||
$hash = md5(basename($path));
|
$hash = md5(basename($path));
|
||||||
|
@ -284,17 +290,13 @@ class local implements adapter_interface, stream_interface
|
||||||
$parts = str_split($hash, 2);
|
$parts = str_split($hash, 2);
|
||||||
$parts = array_slice($parts, 0, $this->dir_depth);
|
$parts = array_slice($parts, 0, $this->dir_depth);
|
||||||
|
|
||||||
// Create path
|
|
||||||
$dirname = dirname($path);
|
|
||||||
$path = $dirname . DIRECTORY_SEPARATOR;
|
|
||||||
|
|
||||||
if (!empty($parts))
|
if (!empty($parts))
|
||||||
{
|
{
|
||||||
$path .= implode(DIRECTORY_SEPARATOR, $parts) . DIRECTORY_SEPARATOR;
|
$dirname .= implode(DIRECTORY_SEPARATOR, $parts) . DIRECTORY_SEPARATOR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $path;
|
return $dirname;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
136
tests/storage/adapter/local_subfolders_test.php
Normal file
136
tests/storage/adapter/local_subfolders_test.php
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* This file is part of the phpBB Forum Software package.
|
||||||
|
*
|
||||||
|
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
||||||
|
* @license GNU General Public License, version 2 (GPL-2.0)
|
||||||
|
*
|
||||||
|
* For full copyright and license information, please see
|
||||||
|
* the docs/CREDITS.txt file.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
class phpbb_storage_adapter_local_subfolders_test extends phpbb_test_case
|
||||||
|
{
|
||||||
|
protected $adapter;
|
||||||
|
|
||||||
|
protected $path;
|
||||||
|
|
||||||
|
protected $filesystem;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->filesystem = new \phpbb\filesystem\filesystem();
|
||||||
|
$phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
|
$this->adapter = new \phpbb\storage\adapter\local($this->filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path);
|
||||||
|
$this->adapter->configure(['path' => 'test_path', 'subfolders' => true]);
|
||||||
|
|
||||||
|
$this->path = $phpbb_root_path . 'test_path/';
|
||||||
|
mkdir($this->path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
$this->adapter = null;
|
||||||
|
rmdir($this->path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_put_contents()
|
||||||
|
{
|
||||||
|
$this->adapter->put_contents('file.txt', 'abc');
|
||||||
|
$this->assertTrue(file_exists($this->path . '3d/8e/file.txt'));
|
||||||
|
$this->assertEquals(file_get_contents($this->path . '3d/8e/file.txt'), 'abc');
|
||||||
|
unlink($this->path . '3d/8e/file.txt');
|
||||||
|
rmdir($this->path . '3d/8e');
|
||||||
|
rmdir($this->path . '3d');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_get_contents()
|
||||||
|
{
|
||||||
|
mkdir($this->path . '3d/8e', 0777, true);
|
||||||
|
file_put_contents($this->path . '3d/8e/file.txt', 'abc');
|
||||||
|
$this->assertEquals($this->adapter->get_contents('file.txt'), 'abc');
|
||||||
|
unlink($this->path . '3d/8e/file.txt');
|
||||||
|
rmdir($this->path . '3d/8e');
|
||||||
|
rmdir($this->path . '3d');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_exists()
|
||||||
|
{
|
||||||
|
mkdir($this->path . '3d/8e', 0777, true);
|
||||||
|
touch($this->path . '3d/8e/file.txt');
|
||||||
|
$this->assertTrue($this->adapter->exists('file.txt'));
|
||||||
|
$this->assertFalse($this->adapter->exists('3d/8e/file.txt'));
|
||||||
|
unlink($this->path . '3d/8e/file.txt');
|
||||||
|
rmdir($this->path . '3d/8e');
|
||||||
|
rmdir($this->path . '3d');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_delete_file()
|
||||||
|
{
|
||||||
|
mkdir($this->path . '3d/8e', 0777, true);
|
||||||
|
touch($this->path . '3d/8e/file.txt');
|
||||||
|
$this->assertTrue(file_exists($this->path . '3d/8e/file.txt'));
|
||||||
|
$this->adapter->delete('file.txt');
|
||||||
|
$this->assertFalse(file_exists($this->path . '3d/8e/file.txt'));
|
||||||
|
$this->assertFalse(file_exists($this->path . '3d'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_rename()
|
||||||
|
{
|
||||||
|
mkdir($this->path . '3d/8e', 0777, true);
|
||||||
|
touch($this->path . '3d/8e/file.txt');
|
||||||
|
$this->adapter->rename('file.txt', 'file2.txt');
|
||||||
|
$this->assertFalse(file_exists($this->path . '3d/8e/file.txt'));
|
||||||
|
$this->assertTrue(file_exists($this->path . '27/36/file2.txt'));
|
||||||
|
$this->assertFalse(file_exists($this->path . '3d'));
|
||||||
|
unlink($this->path . '27/36/file2.txt');
|
||||||
|
rmdir($this->path . '27/36');
|
||||||
|
rmdir($this->path . '27');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_copy()
|
||||||
|
{
|
||||||
|
mkdir($this->path . '3d/8e', 0777, true);
|
||||||
|
file_put_contents($this->path . '3d/8e/file.txt', 'abc');
|
||||||
|
$this->adapter->copy('file.txt', 'file2.txt');
|
||||||
|
$this->assertEquals(file_get_contents($this->path . '3d/8e/file.txt'), 'abc');
|
||||||
|
$this->assertEquals(file_get_contents($this->path . '27/36/file2.txt'), 'abc');
|
||||||
|
unlink($this->path . '3d/8e/file.txt');
|
||||||
|
rmdir($this->path . '3d/8e');
|
||||||
|
rmdir($this->path . '3d');
|
||||||
|
unlink($this->path . '27/36/file2.txt');
|
||||||
|
rmdir($this->path . '27/36');
|
||||||
|
rmdir($this->path . '27');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_read_stream()
|
||||||
|
{
|
||||||
|
mkdir($this->path . '3d/8e', 0777, true);
|
||||||
|
touch($this->path . '3d/8e/file.txt');
|
||||||
|
$stream = $this->adapter->read_stream('file.txt');
|
||||||
|
$this->assertTrue(is_resource($stream));
|
||||||
|
fclose($stream);
|
||||||
|
unlink($this->path . '3d/8e/file.txt');
|
||||||
|
rmdir($this->path . '3d/8e');
|
||||||
|
rmdir($this->path . '3d');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_write_stream()
|
||||||
|
{
|
||||||
|
file_put_contents($this->path . 'file.txt', 'abc');
|
||||||
|
$stream = fopen($this->path . 'file.txt', 'rb');
|
||||||
|
$this->adapter->write_stream('file2.txt', $stream);
|
||||||
|
fclose($stream);
|
||||||
|
$this->assertEquals(file_get_contents($this->path . '27/36/file2.txt'), 'abc');
|
||||||
|
unlink($this->path . 'file.txt');
|
||||||
|
unlink($this->path . '27/36/file2.txt');
|
||||||
|
rmdir($this->path . '27/36');
|
||||||
|
rmdir($this->path . '27');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -27,7 +27,7 @@
|
||||||
$phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR;
|
$phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
$this->adapter = new \phpbb\storage\adapter\local($this->filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path);
|
$this->adapter = new \phpbb\storage\adapter\local($this->filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path);
|
||||||
$this->adapter->configure(['path' => 'test_path', 'subfolders' => true]);
|
$this->adapter->configure(['path' => 'test_path', 'subfolders' => false]);
|
||||||
|
|
||||||
$this->path = $phpbb_root_path . 'test_path/';
|
$this->path = $phpbb_root_path . 'test_path/';
|
||||||
mkdir($this->path);
|
mkdir($this->path);
|
||||||
|
@ -42,82 +42,61 @@
|
||||||
public function test_put_contents()
|
public function test_put_contents()
|
||||||
{
|
{
|
||||||
$this->adapter->put_contents('file.txt', 'abc');
|
$this->adapter->put_contents('file.txt', 'abc');
|
||||||
$this->assertTrue(file_exists($this->path . '3d/8e/file.txt'));
|
$this->assertTrue(file_exists($this->path . 'file.txt'));
|
||||||
$this->assertEquals(file_get_contents($this->path . '3d/8e/file.txt'), 'abc');
|
$this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc');
|
||||||
unlink($this->path . '3d/8e/file.txt');
|
unlink($this->path . 'file.txt');
|
||||||
rmdir($this->path . '3d/8e');
|
|
||||||
rmdir($this->path . '3d');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_get_contents()
|
public function test_get_contents()
|
||||||
{
|
{
|
||||||
mkdir($this->path . '3d/8e', 0777, true);
|
file_put_contents($this->path . 'file.txt', 'abc');
|
||||||
file_put_contents($this->path . '3d/8e/file.txt', 'abc');
|
|
||||||
$this->assertEquals($this->adapter->get_contents('file.txt'), 'abc');
|
$this->assertEquals($this->adapter->get_contents('file.txt'), 'abc');
|
||||||
unlink($this->path . '3d/8e/file.txt');
|
unlink($this->path . 'file.txt');
|
||||||
rmdir($this->path . '3d/8e');
|
|
||||||
rmdir($this->path . '3d');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_exists()
|
public function test_exists()
|
||||||
{
|
{
|
||||||
mkdir($this->path . '3d/8e', 0777, true);
|
touch($this->path . 'file.txt');
|
||||||
touch($this->path . '3d/8e/file.txt');
|
|
||||||
$this->assertTrue($this->adapter->exists('file.txt'));
|
$this->assertTrue($this->adapter->exists('file.txt'));
|
||||||
$this->assertFalse($this->adapter->exists('3d/8e/file.txt'));
|
$this->assertFalse($this->adapter->exists('noexist.txt'));
|
||||||
unlink($this->path . '3d/8e/file.txt');
|
unlink($this->path . 'file.txt');
|
||||||
rmdir($this->path . '3d/8e');
|
|
||||||
rmdir($this->path . '3d');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_delete_file()
|
public function test_delete_file()
|
||||||
{
|
{
|
||||||
mkdir($this->path . '3d/8e', 0777, true);
|
touch($this->path . 'file.txt');
|
||||||
touch($this->path . '3d/8e/file.txt');
|
$this->assertTrue(file_exists($this->path . 'file.txt'));
|
||||||
$this->assertTrue(file_exists($this->path . '3d/8e/file.txt'));
|
|
||||||
$this->adapter->delete('file.txt');
|
$this->adapter->delete('file.txt');
|
||||||
$this->assertFalse(file_exists($this->path . '3d/8e/file.txt'));
|
$this->assertFalse(file_exists($this->path . 'file.txt'));
|
||||||
$this->assertFalse(file_exists($this->path . '3d'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_rename()
|
public function test_rename()
|
||||||
{
|
{
|
||||||
mkdir($this->path . '3d/8e', 0777, true);
|
touch($this->path . 'file.txt');
|
||||||
touch($this->path . '3d/8e/file.txt');
|
|
||||||
$this->adapter->rename('file.txt', 'file2.txt');
|
$this->adapter->rename('file.txt', 'file2.txt');
|
||||||
$this->assertFalse(file_exists($this->path . '3d/8e/file.txt'));
|
$this->assertFalse(file_exists($this->path . 'file.txt'));
|
||||||
$this->assertTrue(file_exists($this->path . '27/36/file2.txt'));
|
$this->assertTrue(file_exists($this->path . 'file2.txt'));
|
||||||
$this->assertFalse(file_exists($this->path . '3d'));
|
$this->assertFalse(file_exists($this->path . 'file.txt'));
|
||||||
unlink($this->path . '27/36/file2.txt');
|
unlink($this->path . 'file2.txt');
|
||||||
rmdir($this->path . '27/36');
|
|
||||||
rmdir($this->path . '27');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_copy()
|
public function test_copy()
|
||||||
{
|
{
|
||||||
mkdir($this->path . '3d/8e', 0777, true);
|
file_put_contents($this->path . 'file.txt', 'abc');
|
||||||
file_put_contents($this->path . '3d/8e/file.txt', 'abc');
|
|
||||||
$this->adapter->copy('file.txt', 'file2.txt');
|
$this->adapter->copy('file.txt', 'file2.txt');
|
||||||
$this->assertEquals(file_get_contents($this->path . '3d/8e/file.txt'), 'abc');
|
$this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc');
|
||||||
$this->assertEquals(file_get_contents($this->path . '27/36/file2.txt'), 'abc');
|
$this->assertEquals(file_get_contents($this->path . 'file2.txt'), 'abc');
|
||||||
unlink($this->path . '3d/8e/file.txt');
|
unlink($this->path . 'file.txt');
|
||||||
rmdir($this->path . '3d/8e');
|
unlink($this->path . 'file2.txt');
|
||||||
rmdir($this->path . '3d');
|
|
||||||
unlink($this->path . '27/36/file2.txt');
|
|
||||||
rmdir($this->path . '27/36');
|
|
||||||
rmdir($this->path . '27');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_read_stream()
|
public function test_read_stream()
|
||||||
{
|
{
|
||||||
mkdir($this->path . '3d/8e', 0777, true);
|
touch($this->path . 'file.txt');
|
||||||
touch($this->path . '3d/8e/file.txt');
|
|
||||||
$stream = $this->adapter->read_stream('file.txt');
|
$stream = $this->adapter->read_stream('file.txt');
|
||||||
$this->assertTrue(is_resource($stream));
|
$this->assertTrue(is_resource($stream));
|
||||||
fclose($stream);
|
fclose($stream);
|
||||||
unlink($this->path . '3d/8e/file.txt');
|
unlink($this->path . 'file.txt');
|
||||||
rmdir($this->path . '3d/8e');
|
|
||||||
rmdir($this->path . '3d');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_write_stream()
|
public function test_write_stream()
|
||||||
|
@ -126,11 +105,9 @@
|
||||||
$stream = fopen($this->path . 'file.txt', 'rb');
|
$stream = fopen($this->path . 'file.txt', 'rb');
|
||||||
$this->adapter->write_stream('file2.txt', $stream);
|
$this->adapter->write_stream('file2.txt', $stream);
|
||||||
fclose($stream);
|
fclose($stream);
|
||||||
$this->assertEquals(file_get_contents($this->path . '27/36/file2.txt'), 'abc');
|
$this->assertEquals(file_get_contents($this->path . 'file2.txt'), 'abc');
|
||||||
unlink($this->path . 'file.txt');
|
unlink($this->path . 'file.txt');
|
||||||
unlink($this->path . '27/36/file2.txt');
|
unlink($this->path . 'file2.txt');
|
||||||
rmdir($this->path . '27/36');
|
|
||||||
rmdir($this->path . '27');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue