mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-30 21:40:43 +02:00
[ticket/15253] Add storage abstraction
PHPBB3-15253
This commit is contained in:
86
tests/storage/adapter/local_test.php
Normal file
86
tests/storage/adapter/local_test.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?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_test extends phpbb_test_case
|
||||
{
|
||||
protected $adapter;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->adapter = new \phpbb\storage\adapter\local();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$this->adapter = null;
|
||||
}
|
||||
|
||||
public function test_put_contents()
|
||||
{
|
||||
$this->adapter->put_contents('file.txt', 'abc');
|
||||
$this->assertTrue(file_exists('file.txt'));
|
||||
$this->assertEquals(file_get_contents('file.txt'), 'abc');
|
||||
unlink('file.txt');
|
||||
}
|
||||
|
||||
public function test_get_contents()
|
||||
{
|
||||
file_put_contents('file.txt', 'abc');
|
||||
$this->assertEquals($this->adapter->get_contents('file.txt'), 'abc');
|
||||
unlink('file.txt');
|
||||
}
|
||||
|
||||
public function test_exists()
|
||||
{
|
||||
// Exists with files
|
||||
$this->assertTrue($this->adapter->exists(__DIR__.'/local_test.php'));
|
||||
$this->assertFalse($this->adapter->exists(__DIR__.'/nonexistent_file.php'));
|
||||
// exists with directory
|
||||
$this->assertTrue($this->adapter->exists(__DIR__.'/../adapter'));
|
||||
$this->assertFalse($this->adapter->exists(__DIR__.'/../nonexistet_folder'));
|
||||
}
|
||||
|
||||
public function test_delete()
|
||||
{
|
||||
// Delete with files
|
||||
file_put_contents('file.txt', '');
|
||||
$this->assertTrue(file_exists('file.txt'));
|
||||
$this->adapter->delete('file.txt');
|
||||
$this->assertFalse(file_exists('file.txt'));
|
||||
// Delete with directories
|
||||
mkdir('path/to/dir', 0777, true);
|
||||
$this->assertTrue(file_exists('path/to/dir'));
|
||||
$this->adapter->delete('path');
|
||||
$this->assertFalse(file_exists('path/to/dir'));
|
||||
}
|
||||
|
||||
public function test_rename()
|
||||
{
|
||||
file_put_contents('file.txt', '');
|
||||
$this->adapter->rename('file.txt', 'file2.txt');
|
||||
$this->assertFalse(file_exists('file.txt'));
|
||||
$this->assertTrue(file_exists('file2.txt'));
|
||||
unlink('file2.txt');
|
||||
}
|
||||
|
||||
public function test_copy()
|
||||
{
|
||||
file_put_contents('file.txt', 'abc');
|
||||
$this->adapter->copy('file.txt', 'file2.txt');
|
||||
$this->assertEquals(file_get_contents('file.txt'), 'abc');
|
||||
$this->assertEquals(file_get_contents('file.txt'), 'abc');
|
||||
unlink('file.txt');
|
||||
unlink('file2.txt');
|
||||
}
|
||||
}
|
52
tests/storage/helper_clean_path_test.php
Normal file
52
tests/storage/helper_clean_path_test.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?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_helper_clean_path_test extends phpbb_test_case
|
||||
{
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function clean_path_data()
|
||||
{
|
||||
return array(
|
||||
array('foo', 'foo'),
|
||||
array('foo/bar', 'foo/bar'),
|
||||
array('foo/bar/', 'foo/bar/'),
|
||||
array('foo/./bar', 'foo/bar'),
|
||||
array('foo/./././bar', 'foo/bar'),
|
||||
array('foo/bar/.', 'foo/bar'),
|
||||
array('./foo/bar', './foo/bar'),
|
||||
array('../foo/bar', '../foo/bar'),
|
||||
array('./../foo/bar', './../foo/bar'),
|
||||
array('././../foo/bar', './../foo/bar'),
|
||||
array('one/two/three', 'one/two/three'),
|
||||
array('one/two/../three', 'one/three'),
|
||||
array('one/../two/three', 'two/three'),
|
||||
array('one/two/..', 'one'),
|
||||
array('one/two/../', 'one/'),
|
||||
array('one/two/../three/../four', 'one/four'),
|
||||
array('one/two/three/../../four', 'one/four'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider clean_path_data
|
||||
*/
|
||||
public function test_clean_path($input, $expected)
|
||||
{
|
||||
$this->assertEquals($expected, \phpbb\storage\helper::clean_path($input));
|
||||
}
|
||||
}
|
64
tests/storage/helper_is_absolute_test.php
Normal file
64
tests/storage/helper_is_absolute_test.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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_helper_is_absolute_test extends phpbb_test_case
|
||||
{
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
static public function is_absolute_data()
|
||||
{
|
||||
return array(
|
||||
// Empty
|
||||
array('', false),
|
||||
|
||||
// Absolute unix style
|
||||
array('/etc/phpbb', true),
|
||||
// Unix does not support \ so that is not an absolute path
|
||||
array('\etc\phpbb', false),
|
||||
|
||||
// Absolute windows style
|
||||
array('c:\windows', true),
|
||||
array('C:\Windows', true),
|
||||
array('c:/windows', true),
|
||||
array('C:/Windows', true),
|
||||
|
||||
// Executable
|
||||
array('etc/phpbb', false),
|
||||
array('explorer.exe', false),
|
||||
|
||||
// Relative subdir
|
||||
array('Windows\System32', false),
|
||||
array('Windows\System32\explorer.exe', false),
|
||||
array('Windows/System32', false),
|
||||
array('Windows/System32/explorer.exe', false),
|
||||
|
||||
// Relative updir
|
||||
array('..\Windows\System32', false),
|
||||
array('..\Windows\System32\explorer.exe', false),
|
||||
array('../Windows/System32', false),
|
||||
array('../Windows/System32/explorer.exe', false),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider is_absolute_data
|
||||
*/
|
||||
public function test_is_absolute($path, $expected)
|
||||
{
|
||||
$this->assertEquals($expected, \phpbb\storage\helper::is_absolute_path($path));
|
||||
}
|
||||
}
|
83
tests/storage/helper_realpath_test.php
Normal file
83
tests/storage/helper_realpath_test.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?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_helper_realpath_test extends phpbb_test_case
|
||||
{
|
||||
protected static $storage_helper_phpbb_own_realpath;
|
||||
|
||||
static public function setUpBeforeClass()
|
||||
{
|
||||
parent::setUpBeforeClass();
|
||||
|
||||
self::$storage_helper_phpbb_own_realpath = new ReflectionMethod('\phpbb\storage\helper', 'phpbb_own_realpath');
|
||||
self::$storage_helper_phpbb_own_realpath->setAccessible(true);
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function realpath_resolve_absolute_without_symlinks_data()
|
||||
{
|
||||
return array(
|
||||
// Constant data
|
||||
array(__DIR__, __DIR__),
|
||||
array(__DIR__ . '/../storage/../storage', __DIR__),
|
||||
array(__DIR__ . '/././', __DIR__),
|
||||
array(__DIR__ . '/non_existent', false),
|
||||
|
||||
array(__FILE__, __FILE__),
|
||||
array(__FILE__ . '../', false),
|
||||
);
|
||||
}
|
||||
|
||||
public function realpath_resolve_relative_without_symlinks_data()
|
||||
{
|
||||
if (!function_exists('getcwd'))
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
$relative_path = \phpbb\storage\helper::make_path_relative(__DIR__, getcwd());
|
||||
|
||||
return array(
|
||||
array($relative_path, __DIR__),
|
||||
array($relative_path . '../storage/../storage', __DIR__),
|
||||
array($relative_path . '././', __DIR__),
|
||||
|
||||
array($relative_path . 'helper_realpath_test.php', __FILE__),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider realpath_resolve_absolute_without_symlinks_data
|
||||
*/
|
||||
public function test_realpath_absolute_without_links($path, $expected)
|
||||
{
|
||||
$this->assertEquals($expected, self::$storage_helper_phpbb_own_realpath->invoke(null, $path));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider realpath_resolve_relative_without_symlinks_data
|
||||
*/
|
||||
public function test_realpath_relative_without_links($path, $expected)
|
||||
{
|
||||
if (!function_exists('getcwd'))
|
||||
{
|
||||
$this->markTestSkipped('phpbb_own_realpath() cannot be tested with relative paths: getcwd is not available.');
|
||||
}
|
||||
|
||||
$this->assertEquals($expected, self::$storage_helper_phpbb_own_realpath->invoke(null, $path));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user