mirror of
https://github.com/filegator/filegator.git
synced 2025-08-25 16:51:13 +02:00
initial commit
This commit is contained in:
132
tests/backend/Unit/ArchiverTest.php
Normal file
132
tests/backend/Unit/ArchiverTest.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Services\Archiver\Adapters\ZipArchiver;
|
||||
use Filegator\Services\Storage\Filesystem;
|
||||
use Filegator\Services\Tmpfs\Adapters\Tmpfs;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ArchiverTest extends TestCase
|
||||
{
|
||||
protected $archiver;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$tmpfs = new Tmpfs();
|
||||
$tmpfs->init([
|
||||
'path' => TEST_TMP_PATH,
|
||||
'gc_probability_perc' => 10,
|
||||
'gc_older_than' => 60 * 60 * 24 * 2, // 2 days
|
||||
]);
|
||||
|
||||
$this->archiver = new ZipArchiver($tmpfs);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testCreatingArchive()
|
||||
{
|
||||
$storage = new Filesystem();
|
||||
$storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'nulladapter',
|
||||
'adapters' => [
|
||||
'nulladapter' => function () {
|
||||
return new \League\Flysystem\Adapter\NullAdapter();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$uniqid = $this->archiver->createArchive($storage);
|
||||
|
||||
$this->assertNotNull($uniqid);
|
||||
$this->assertFileExists(TEST_TMP_PATH.$uniqid);
|
||||
}
|
||||
|
||||
public function testAddingDirectoryWithFilesAndSubdir()
|
||||
{
|
||||
$storage = new Filesystem();
|
||||
$storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'memoryadapter',
|
||||
'adapters' => [
|
||||
'memoryadapter' => function () {
|
||||
return new \League\Flysystem\Memory\MemoryAdapter();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$storage->createDir('/', 'test');
|
||||
$storage->createDir('/test', 'sub');
|
||||
$storage->createFile('/test', 'file1.txt');
|
||||
$storage->createFile('/test', 'file2.txt');
|
||||
|
||||
$name = $this->archiver->createArchive($storage);
|
||||
$this->archiver->addDirectoryFromStorage('/test');
|
||||
$this->archiver->closeArchive();
|
||||
|
||||
$this->assertGreaterThan(0, filesize(TEST_TMP_PATH.$name));
|
||||
}
|
||||
|
||||
public function testUploadingArchiveToStorage()
|
||||
{
|
||||
$storage = new Filesystem();
|
||||
$storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'memoryadapter',
|
||||
'adapters' => [
|
||||
'memoryadapter' => function () {
|
||||
return new \League\Flysystem\Memory\MemoryAdapter();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$storage->createDir('/', 'test');
|
||||
$storage->createDir('/test', 'sub');
|
||||
$storage->createFile('/test', 'file1.txt');
|
||||
$storage->createFile('/test', 'file2.txt');
|
||||
|
||||
$name = $this->archiver->createArchive($storage);
|
||||
$this->archiver->addDirectoryFromStorage('/test');
|
||||
$this->archiver->storeArchive('/destination', 'myarchive.zip');
|
||||
|
||||
$this->assertFileNotExists(TEST_TMP_PATH.$name);
|
||||
}
|
||||
|
||||
public function testUncompressingArchiveFromStorage()
|
||||
{
|
||||
$storage = new Filesystem();
|
||||
$storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'memoryadapter',
|
||||
'adapters' => [
|
||||
'memoryadapter' => function () {
|
||||
return new \League\Flysystem\Memory\MemoryAdapter();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$stream = fopen(TEST_ARCHIVE, 'r');
|
||||
$storage->store('/', 'testarchive.zip', $stream);
|
||||
fclose($stream);
|
||||
|
||||
$storage->createDir('/', 'result');
|
||||
|
||||
$this->archiver->uncompress('/testarchive.zip', '/result', $storage);
|
||||
|
||||
$this->assertStringContainsString('testarchive', (json_encode($storage->getDirectoryCollection('/'))));
|
||||
$this->assertStringContainsString('onetwo', (json_encode($storage->getDirectoryCollection('/result'))));
|
||||
}
|
||||
}
|
226
tests/backend/Unit/Auth/AuthTest.php
Normal file
226
tests/backend/Unit/Auth/AuthTest.php
Normal file
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Auth;
|
||||
|
||||
use Filegator\Services\Auth\User;
|
||||
use Tests\TestCase;
|
||||
|
||||
abstract class AuthTest extends TestCase
|
||||
{
|
||||
public $auth;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->setAuth();
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
abstract public function setAuth();
|
||||
|
||||
public function addAdmin($password = '123456')
|
||||
{
|
||||
$admin = new User();
|
||||
$admin->setRole('admin');
|
||||
$admin->setHomedir('/');
|
||||
$admin->setUsername('admin@example.com');
|
||||
$admin->setName('Admin');
|
||||
|
||||
return $this->auth->add($admin, $password);
|
||||
}
|
||||
|
||||
public function addMike($password = '98747')
|
||||
{
|
||||
$mike = new User();
|
||||
$mike->setRole('user');
|
||||
$mike->setHomedir('/');
|
||||
$mike->setUsername('mike@example.com');
|
||||
$mike->setName('Mike');
|
||||
|
||||
return $this->auth->add($mike, $password);
|
||||
}
|
||||
|
||||
public function addGuest()
|
||||
{
|
||||
$guest = new User();
|
||||
$guest->setRole('guest');
|
||||
$guest->setHomedir('/');
|
||||
$guest->setUsername('guest');
|
||||
$guest->setName('Guest');
|
||||
|
||||
return $this->auth->add($guest, '');
|
||||
}
|
||||
|
||||
public function testWeCanFindAUser()
|
||||
{
|
||||
$admin = $this->addAdmin();
|
||||
|
||||
$user = $this->auth->find('admin@example.com');
|
||||
|
||||
$this->assertTrue($user->isAdmin());
|
||||
}
|
||||
|
||||
public function testWeCanAddAUser()
|
||||
{
|
||||
$mike = $this->addMike();
|
||||
|
||||
$user = $this->auth->find('mike@example.com');
|
||||
$this->assertEquals($user, $mike);
|
||||
}
|
||||
|
||||
public function testWeCanUpdateExistingUser()
|
||||
{
|
||||
$this->addAdmin();
|
||||
$user = $this->auth->find('admin@example.com');
|
||||
$this->assertNotNull($user);
|
||||
|
||||
$user->setName('Jonny B');
|
||||
$user->setHomeDir('/jonnyshome');
|
||||
$user->setUsername('jonny@example.com');
|
||||
$user->setRole('user');
|
||||
|
||||
$updated_user = $this->auth->update('admin@example.com', $user);
|
||||
|
||||
$this->assertEquals($updated_user->getName(), 'Jonny B');
|
||||
$this->assertEquals($updated_user->getHomeDir(), '/jonnyshome');
|
||||
$this->assertEquals($updated_user->getUsername(), 'jonny@example.com');
|
||||
$this->assertTrue($updated_user->isUser());
|
||||
}
|
||||
|
||||
public function testWeCanAuthenticateUser()
|
||||
{
|
||||
$admin = $this->addAdmin('test123');
|
||||
|
||||
$auth_attempt1 = $this->auth->authenticate('admin@example.com', 'test123');
|
||||
$auth_user = $this->auth->user();
|
||||
$this->assertEquals($auth_user->getUsername(), $admin->getUsername());
|
||||
|
||||
$this->assertFalse($this->auth->authenticate('test123@example.com', 'xxxxxx'));
|
||||
}
|
||||
|
||||
public function testWeCanForgetUser()
|
||||
{
|
||||
$admin = $this->addAdmin('test123');
|
||||
|
||||
$this->auth->authenticate('admin@example.com', 'test123');
|
||||
$auth_user = $this->auth->user();
|
||||
$this->assertEquals($auth_user->getUsername(), $admin->getUsername());
|
||||
|
||||
$this->auth->forget();
|
||||
|
||||
$auth_user = $this->auth->user();
|
||||
$this->assertEquals($auth_user, null);
|
||||
}
|
||||
|
||||
public function testWeCanUpdateUsersPassword()
|
||||
{
|
||||
$admin = $this->addAdmin('test123');
|
||||
|
||||
$this->auth->update('admin@example.com', $admin, 'newpassword');
|
||||
|
||||
$this->assertFalse($this->auth->authenticate('test123@example.com', 'test123'));
|
||||
|
||||
$auth_attempt1 = $this->auth->authenticate('admin@example.com', 'newpassword');
|
||||
$auth_user = $this->auth->user();
|
||||
|
||||
$this->assertEquals($auth_user->getUsername(), $admin->getUsername());
|
||||
}
|
||||
|
||||
public function testWeCanDeleteUser()
|
||||
{
|
||||
$admin = $this->addAdmin();
|
||||
$find = $this->auth->find('admin@example.com');
|
||||
$this->assertNotNull($find);
|
||||
|
||||
$this->auth->delete($admin);
|
||||
$find = $this->auth->find('admin@example.com');
|
||||
$this->assertNull($find);
|
||||
}
|
||||
|
||||
public function testWeCannotUpdateNonExistingUser()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$user = new User();
|
||||
$user->setRole('user');
|
||||
$user->setHomedir('/');
|
||||
$user->setUsername('tim@example.com');
|
||||
$user->setName('Tim');
|
||||
|
||||
$this->auth->update('somebody@example.com', $user);
|
||||
}
|
||||
|
||||
public function testWeCannotDeleteNonExistingUser()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setRole('user');
|
||||
$user->setHomedir('/');
|
||||
$user->setUsername('tim@example.com');
|
||||
$user->setName('Tim');
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$this->auth->delete($user);
|
||||
}
|
||||
|
||||
public function testWeCannotAddUserWithTheSameUsername()
|
||||
{
|
||||
$this->addAdmin();
|
||||
|
||||
$second_admin = new User();
|
||||
$second_admin->setRole('user');
|
||||
$second_admin->setHomedir('/');
|
||||
$second_admin->setUsername('admin@example.com');
|
||||
$second_admin->setName('Admin2');
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$updated_user = $this->auth->add($second_admin, 'pass444');
|
||||
}
|
||||
|
||||
public function testWeCannotEditUserAndSetUsernameThatIsAlreadyTaken()
|
||||
{
|
||||
$this->addMike();
|
||||
$this->addAdmin();
|
||||
$user = $this->auth->find('mike@example.com');
|
||||
$this->assertNotNull($user);
|
||||
|
||||
$user->setName('Jonny B');
|
||||
$user->setHomeDir('/jonnyshome');
|
||||
$user->setUsername('admin@example.com');
|
||||
$user->setRole('user');
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$updated_user = $this->auth->update('mike@example.com', $user);
|
||||
}
|
||||
|
||||
public function testNoGuestException()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$guest = $this->auth->getGuest();
|
||||
}
|
||||
|
||||
public function testGetGuest()
|
||||
{
|
||||
$this->addGuest();
|
||||
$guest = $this->auth->getGuest();
|
||||
|
||||
$this->assertNotNull($guest);
|
||||
}
|
||||
|
||||
public function testGetAllUsers()
|
||||
{
|
||||
$this->addAdmin();
|
||||
$this->addMike();
|
||||
|
||||
$this->assertEquals($this->auth->allUsers()->length(), 2);
|
||||
}
|
||||
}
|
58
tests/backend/Unit/Auth/DatabaseAuthTest.php
Normal file
58
tests/backend/Unit/Auth/DatabaseAuthTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Auth;
|
||||
|
||||
use Filegator\Kernel\Request;
|
||||
use Filegator\Services\Auth\Adapters\Database;
|
||||
use Filegator\Services\Session\Adapters\SessionStorage;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class DatabaseAuthTest extends AuthTest
|
||||
{
|
||||
protected $conn;
|
||||
|
||||
public function setAuth()
|
||||
{
|
||||
$session = new SessionStorage(new Request());
|
||||
$session->init([
|
||||
'session_handler' => 'mockfilesession',
|
||||
'available' => [
|
||||
'mockfilesession' => function () {
|
||||
return new \Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$this->auth = new Database($session);
|
||||
$this->auth->init([
|
||||
'driver' => 'pdo',
|
||||
'dsn' => 'sqlite::memory:',
|
||||
'database' => 'users',
|
||||
]);
|
||||
|
||||
$this->conn = $this->auth->getConnection();
|
||||
|
||||
$this->conn->query('DROP TABLE IF EXISTS [users]');
|
||||
$this->conn->query('CREATE TABLE [users] (
|
||||
[id] INTEGER PRIMARY KEY NOT NULL,
|
||||
[username] VARCHAR(255) NOT NULL,
|
||||
[name] VARCHAR(255) NOT NULL,
|
||||
[role] VARCHAR(20) NOT NULL,
|
||||
[permissions] VARCHAR(100) NOT NULL,
|
||||
[homedir] VARCHAR(1000) NOT NULL,
|
||||
[password] VARCHAR(255) NOT NULL
|
||||
|
||||
)');
|
||||
$ret = $this->conn->fetch('SELECT * FROM users WHERE username = ?', 'admin');
|
||||
}
|
||||
}
|
50
tests/backend/Unit/Auth/JsonAuthTest.php
Normal file
50
tests/backend/Unit/Auth/JsonAuthTest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Auth;
|
||||
|
||||
use Filegator\Kernel\Request;
|
||||
use Filegator\Services\Auth\Adapters\JsonFile;
|
||||
use Filegator\Services\Session\Adapters\SessionStorage;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class JsonFileTest extends AuthTest
|
||||
{
|
||||
private $mock_file = TEST_DIR.'/mockusers.json';
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
@unlink($this->mock_file);
|
||||
@unlink($this->mock_file.'.blank');
|
||||
}
|
||||
|
||||
public function setAuth()
|
||||
{
|
||||
@unlink($this->mock_file);
|
||||
@touch($this->mock_file.'.blank');
|
||||
|
||||
$session = new SessionStorage(new Request());
|
||||
$session->init([
|
||||
'session_handler' => 'mockfilesession',
|
||||
'available' => [
|
||||
'mockfilesession' => function () {
|
||||
return new \Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$this->auth = new JsonFile($session);
|
||||
$this->auth->init([
|
||||
'file' => $this->mock_file,
|
||||
]);
|
||||
}
|
||||
}
|
115
tests/backend/Unit/CollectionTest.php
Normal file
115
tests/backend/Unit/CollectionTest.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Services\Auth\User;
|
||||
use Filegator\Services\Auth\UsersCollection;
|
||||
use Filegator\Services\Storage\DirectoryCollection;
|
||||
use Filegator\Utils\Collection;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class CollectionTest extends TestCase
|
||||
{
|
||||
public function testAddToCollection()
|
||||
{
|
||||
$mock = $this->getMockForTrait(Collection::class);
|
||||
$mock->add('one');
|
||||
$mock->add('two');
|
||||
|
||||
$this->assertEquals($mock->length(), 2);
|
||||
}
|
||||
|
||||
public function testDeleteFromCollection()
|
||||
{
|
||||
$mock = $this->getMockForTrait(Collection::class);
|
||||
$mock->add('one');
|
||||
$mock->delete('one');
|
||||
|
||||
$this->assertEquals($mock->length(), 0);
|
||||
}
|
||||
|
||||
public function testSort()
|
||||
{
|
||||
$mock = $this->getMockForTrait(Collection::class);
|
||||
$mock->add(['val' => 'b']);
|
||||
$mock->add(['val' => 'a']);
|
||||
$mock->add(['val' => 'c']);
|
||||
|
||||
$this->assertEquals($mock->all()[0]['val'], 'b');
|
||||
|
||||
$mock->sortByValue('val');
|
||||
|
||||
$this->assertEquals($mock->all()[0]['val'], 'a');
|
||||
|
||||
$mock->sortByValue('val', true);
|
||||
|
||||
$this->assertEquals($mock->all()[0]['val'], 'c');
|
||||
}
|
||||
|
||||
public function testUsersCollection()
|
||||
{
|
||||
$mock = new UsersCollection();
|
||||
|
||||
$user1 = new User();
|
||||
$user2 = new User();
|
||||
$user3 = new User();
|
||||
|
||||
$mock->addUser($user1);
|
||||
$mock->addUser($user2);
|
||||
$mock->addUser($user3);
|
||||
|
||||
$this->assertEquals($mock->length(), 3);
|
||||
}
|
||||
|
||||
public function testUserSerialization()
|
||||
{
|
||||
$mock = new UsersCollection();
|
||||
$mock->add(['val' => 'b']);
|
||||
$mock->add(['val' => 'a']);
|
||||
$mock->add(['val' => 'c']);
|
||||
|
||||
$json = json_encode($mock);
|
||||
|
||||
$this->assertEquals($json, '[{"val":"b"},{"val":"a"},{"val":"c"}]');
|
||||
}
|
||||
|
||||
public function testDirectoryCollection()
|
||||
{
|
||||
$dir = new DirectoryCollection('/sub1/sub2');
|
||||
|
||||
$dir->addFile('back', '/sub1', '..', 0, 1558942228);
|
||||
$dir->addFile('file', '/sub1/sub2/test.txt', 'test.txt', 30000, 1558942228);
|
||||
$dir->addFile('file', '/sub1/sub2/test2.txt', 'test.txt', 30000, 1558942228);
|
||||
$dir->addFile('dir', '/sub1/sub2/sub3', 'sub3', 0, 1558942228);
|
||||
|
||||
$json = json_encode($dir);
|
||||
|
||||
$this->assertEquals($json, '{"location":"\/sub1\/sub2","files":[{"type":"back","path":"\/sub1","name":"..","size":0,"time":1558942228},{"type":"dir","path":"\/sub1\/sub2\/sub3","name":"sub3","size":0,"time":1558942228},{"type":"file","path":"\/sub1\/sub2\/test.txt","name":"test.txt","size":30000,"time":1558942228},{"type":"file","path":"\/sub1\/sub2\/test2.txt","name":"test.txt","size":30000,"time":1558942228}]}');
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$dir->addFile('badType', 'aaa', 'aa', 0, 1558942228);
|
||||
}
|
||||
|
||||
public function testUserCollection()
|
||||
{
|
||||
$user = new UsersCollection();
|
||||
|
||||
$user->addUser(new User());
|
||||
$user->addUser(new User());
|
||||
|
||||
$json = json_encode($user);
|
||||
|
||||
$this->assertEquals($json, '[{"role":"guest","permissions":[],"homedir":"","username":"","name":""},{"role":"guest","permissions":[],"homedir":"","username":"","name":""}]');
|
||||
}
|
||||
}
|
45
tests/backend/Unit/ConfigTest.php
Normal file
45
tests/backend/Unit/ConfigTest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Config\Config;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ConfigTest extends TestCase
|
||||
{
|
||||
public function testGettingAnItemFromConfigUsingDotNotation()
|
||||
{
|
||||
$sample = [
|
||||
'test' => 'something',
|
||||
'test2' => [
|
||||
'deep' => 123,
|
||||
],
|
||||
'test3' => [
|
||||
'sub' => [
|
||||
'subsub' => 2,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$config = new Config($sample);
|
||||
|
||||
$this->assertEquals($config->get(), $sample);
|
||||
$this->assertEquals($config->get('test'), 'something');
|
||||
$this->assertEquals($config->get('test2.deep'), 123);
|
||||
$this->assertEquals($config->get('test3.sub.subsub'), 2);
|
||||
$this->assertEquals($config->get('not-found'), null);
|
||||
$this->assertEquals($config->get('not-found', 'default'), 'default');
|
||||
$this->assertEquals($config->get('not.found', 'default'), 'default');
|
||||
}
|
||||
}
|
747
tests/backend/Unit/FilesystemTest.php
Normal file
747
tests/backend/Unit/FilesystemTest.php
Normal file
@@ -0,0 +1,747 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Services\Storage\Filesystem;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class FilesystemTest extends TestCase
|
||||
{
|
||||
protected $storage;
|
||||
|
||||
protected $timestamp;
|
||||
|
||||
protected $separator = '/';
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->resetTempDir();
|
||||
|
||||
$this->timestamp = time();
|
||||
|
||||
$this->storage = new Filesystem();
|
||||
$this->storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'localfilesystem',
|
||||
'adapters' => [
|
||||
'localfilesystem' => function () {
|
||||
return new \League\Flysystem\Adapter\Local(
|
||||
TEST_REPOSITORY
|
||||
);
|
||||
},
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
$this->resetTempDir();
|
||||
}
|
||||
|
||||
public function testGetDirectyoryFileCount()
|
||||
{
|
||||
$this->storage->createFile('/', '1.txt');
|
||||
$this->storage->createFile('/', '2.txt');
|
||||
$this->storage->createFile('/', '3.txt');
|
||||
$this->storage->createFile('/', '4.txt');
|
||||
$this->storage->createFile('/', '5.txt');
|
||||
$this->storage->createDir('/', 'tmp');
|
||||
|
||||
$ret = $this->storage->getDirectoryCollection('/');
|
||||
|
||||
$ret_array = json_decode(json_encode($ret), true);
|
||||
|
||||
$this->assertCount(6, $ret_array['files']);
|
||||
}
|
||||
|
||||
public function testGetSubDirectyoryFileCount()
|
||||
{
|
||||
$this->storage->createDir('/', 'sub');
|
||||
$this->storage->createFile('/sub', '1.txt');
|
||||
$this->storage->createFile('/sub', '2.txt');
|
||||
$this->storage->createFile('/sub', '3.txt');
|
||||
$this->storage->createFile('/sub', '4.txt');
|
||||
$this->storage->createDir('/sub', 'deep');
|
||||
$this->storage->createFile('/sub/deep', '1.txt');
|
||||
|
||||
$ret = $this->storage->getDirectoryCollection('/sub');
|
||||
|
||||
$ret_array = json_decode(json_encode($ret), true);
|
||||
|
||||
// back + 4 files + 1 deep dir
|
||||
$this->assertCount(6, $ret_array['files']);
|
||||
|
||||
$ret = $this->storage->getDirectoryCollection('/sub/deep');
|
||||
|
||||
$ret_array = json_decode(json_encode($ret), true);
|
||||
|
||||
// back + 1 file
|
||||
$this->assertCount(2, $ret_array['files']);
|
||||
}
|
||||
|
||||
public function testInvalidDirReturnsBackLinkOnly()
|
||||
{
|
||||
$ret = $this->storage->getDirectoryCollection('/etc');
|
||||
|
||||
$this->assertJsonStringEqualsJsonString(json_encode($ret), json_encode([
|
||||
'location' => '/etc',
|
||||
'files' => [
|
||||
0 => [
|
||||
'type' => 'back',
|
||||
'path' => '/',
|
||||
'name' => '..',
|
||||
'size' => 0,
|
||||
'time' => 0,
|
||||
],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function testListSubDirContents()
|
||||
{
|
||||
$this->storage->createDir('/', 'john');
|
||||
$this->storage->createDir('/john', 'johnsub');
|
||||
$this->storage->createFile('/john/johnsub', 'john2.txt');
|
||||
|
||||
$ret = $this->storage->getDirectoryCollection('/john/johnsub');
|
||||
$ret->resetTimestamps();
|
||||
|
||||
$this->assertJsonStringEqualsJsonString(json_encode($ret), json_encode([
|
||||
'location' => '/john/johnsub',
|
||||
'files' => [
|
||||
0 => [
|
||||
'type' => 'back',
|
||||
'path' => '/john',
|
||||
'name' => '..',
|
||||
'size' => 0,
|
||||
'time' => 0,
|
||||
],
|
||||
1 => [
|
||||
'type' => 'file',
|
||||
'path' => '/john/johnsub/john2.txt',
|
||||
'name' => 'john2.txt',
|
||||
'size' => 0,
|
||||
'time' => 0,
|
||||
],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function testHomeDirContentsUsingPathPrefix()
|
||||
{
|
||||
$this->storage->setPathPrefix('/john');
|
||||
$this->storage->createDir('/', 'johnsub');
|
||||
$this->storage->createFile('/', 'john.txt');
|
||||
|
||||
$ret = $this->storage->getDirectoryCollection('/');
|
||||
$ret->resetTimestamps(-1);
|
||||
|
||||
$this->assertJsonStringEqualsJsonString(json_encode($ret), json_encode([
|
||||
'location' => '/',
|
||||
'files' => [
|
||||
0 => [
|
||||
'type' => 'dir',
|
||||
'path' => '/johnsub',
|
||||
'name' => 'johnsub',
|
||||
'size' => 0,
|
||||
'time' => -1,
|
||||
],
|
||||
1 => [
|
||||
'type' => 'file',
|
||||
'path' => '/john.txt',
|
||||
'name' => 'john.txt',
|
||||
'size' => 0,
|
||||
'time' => -1,
|
||||
],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function testSubDirContentsUsingPathPrefix()
|
||||
{
|
||||
$this->storage->setPathPrefix('/john');
|
||||
$this->storage->createDir('/', 'johnsub');
|
||||
$this->storage->createFile('/johnsub', 'john2.txt');
|
||||
|
||||
$ret = $this->storage->getDirectoryCollection('/johnsub');
|
||||
|
||||
$ret->resetTimestamps();
|
||||
|
||||
$this->assertJsonStringEqualsJsonString(json_encode($ret), json_encode([
|
||||
'location' => '/johnsub',
|
||||
'files' => [
|
||||
0 => [
|
||||
'type' => 'back',
|
||||
'path' => '/',
|
||||
'name' => '..',
|
||||
'size' => 0,
|
||||
'time' => 0,
|
||||
],
|
||||
1 => [
|
||||
'type' => 'file',
|
||||
'path' => '/johnsub/john2.txt',
|
||||
'name' => 'john2.txt',
|
||||
'size' => 0,
|
||||
'time' => 0,
|
||||
],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function testStoringFileToRoot()
|
||||
{
|
||||
// create dummy file
|
||||
file_put_contents(TEST_FILE, 'lorem ipsum');
|
||||
|
||||
$resource = fopen(TEST_FILE, 'r');
|
||||
$ret = $this->storage->store('/', 'loremfile.txt', $resource);
|
||||
fclose($resource);
|
||||
|
||||
$this->assertTrue($ret);
|
||||
|
||||
$this->assertFileExists(TEST_REPOSITORY.'/loremfile.txt');
|
||||
}
|
||||
|
||||
public function testStoringFileToRootSubFolder()
|
||||
{
|
||||
// create dummy file
|
||||
file_put_contents(TEST_FILE, 'lorem ipsum');
|
||||
|
||||
$resource = fopen(TEST_FILE, 'r');
|
||||
$ret = $this->storage->store('/sub/sub1', 'loremfile.txt', $resource);
|
||||
fclose($resource);
|
||||
|
||||
$this->assertTrue($ret);
|
||||
|
||||
$this->assertFileExists(TEST_REPOSITORY.'/sub/sub1/loremfile.txt');
|
||||
$this->assertFileNotExists(TEST_REPOSITORY.'/loremfile.txt');
|
||||
}
|
||||
|
||||
public function testUpcountingFilenameOrDirname()
|
||||
{
|
||||
$this->assertEquals('test (1).txt', $this->invokeMethod($this->storage, 'upcountName', ['test.txt']));
|
||||
$this->assertEquals('test (2).txt', $this->invokeMethod($this->storage, 'upcountName', ['test (1).txt']));
|
||||
$this->assertEquals('test (100).txt', $this->invokeMethod($this->storage, 'upcountName', ['test (99).txt']));
|
||||
$this->assertEquals('test (1)', $this->invokeMethod($this->storage, 'upcountName', ['test']));
|
||||
$this->assertEquals('test (9) (2) (1)', $this->invokeMethod($this->storage, 'upcountName', ['test (9) (2)']));
|
||||
$this->assertEquals('test (2) (3) (4).txt', $this->invokeMethod($this->storage, 'upcountName', ['test (2) (3) (3).txt']));
|
||||
$this->assertEquals('1 (1)', $this->invokeMethod($this->storage, 'upcountName', ['1']));
|
||||
$this->assertEquals('test (1).txt (1).zip', $this->invokeMethod($this->storage, 'upcountName', ['test (1).txt.zip']));
|
||||
$this->assertEquals('test(1) (1)', $this->invokeMethod($this->storage, 'upcountName', ['test(1)']));
|
||||
}
|
||||
|
||||
public function testStoringFileWithTheSameNameUpcountsSecondFilename()
|
||||
{
|
||||
// create dummy file
|
||||
file_put_contents(TEST_FILE, 'lorem ipsum');
|
||||
|
||||
$resource = fopen(TEST_FILE, 'r');
|
||||
$this->storage->store('/', 'singletone.txt', $resource);
|
||||
fclose($resource);
|
||||
|
||||
// create another dummy file witht the same name but different content
|
||||
file_put_contents(TEST_FILE, 'croissant');
|
||||
|
||||
$resource = fopen(TEST_FILE, 'r');
|
||||
$this->storage->store('/', 'singletone.txt', $resource);
|
||||
fclose($resource);
|
||||
|
||||
// first file is not overwritten
|
||||
$ret = $this->storage->readStream('singletone.txt');
|
||||
$this->assertEquals(stream_get_contents($ret['stream']), 'lorem ipsum');
|
||||
|
||||
// second file is also here but with upcounted name
|
||||
$ret = $this->storage->readStream('singletone (1).txt');
|
||||
$this->assertEquals(stream_get_contents($ret['stream']), 'croissant');
|
||||
}
|
||||
|
||||
public function testCreatingFileWithTheSameNameUpcountsFilenameRecursively()
|
||||
{
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
$this->storage->createFile('/', 'test (1).txt');
|
||||
|
||||
$resource = fopen(TEST_FILE, 'r');
|
||||
$this->storage->store('/', 'test.txt', $resource);
|
||||
fclose($resource);
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/test.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (1).txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (2).txt')); // created with (2)
|
||||
}
|
||||
|
||||
public function testCreatingDirectoryWithTheSameNameAsNonEmptyDirUpcountsDestinationDir()
|
||||
{
|
||||
$this->storage->createDir('/', 'test');
|
||||
$this->storage->createFile('/test', 'a.txt');
|
||||
// this dir
|
||||
$this->storage->createDir('/', 'test');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/test');
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/test (1)'); // goes here
|
||||
}
|
||||
|
||||
public function testCreatingDirectoryWithTheSameNameAsNonEmptyDirUpcountsDestinationDirRecursively()
|
||||
{
|
||||
$this->storage->createDir('/', 'test');
|
||||
$this->storage->createFile('/test', 'a.txt');
|
||||
$this->storage->createDir('/', 'test (1)');
|
||||
$this->storage->createFile('/test (1)', 'b.txt');
|
||||
|
||||
// this dir
|
||||
$this->storage->createDir('/', 'test');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/test');
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/test (1)');
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/test (1) (1)'); // goes here
|
||||
}
|
||||
|
||||
public function testMovingFileWithTheSameNameUpcountsSecondFilename()
|
||||
{
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
$this->storage->createFile('/sub', 'test.txt');
|
||||
|
||||
// move second file over the first one
|
||||
$this->storage->move('/sub/test.txt', '/test.txt');
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/test.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (1).txt'));
|
||||
}
|
||||
|
||||
public function testMovingFileWithTheSameNameUpcountsSecondFilenameUntilTheNameIsUnique()
|
||||
{
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
$this->storage->createFile('/', 'test (1).txt');
|
||||
$this->storage->createFile('/', 'test (2).txt');
|
||||
$this->storage->createFile('/sub', 'test.txt');
|
||||
|
||||
// move second file over the first one
|
||||
$this->storage->move('/sub/test.txt', '/test.txt');
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/test.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (1).txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (2).txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (3).txt')); // file is moved here
|
||||
}
|
||||
|
||||
public function testCopyingFileWithTheSameNameUpcountsSecondFilename()
|
||||
{
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
$this->storage->createFile('/', 'test (1).txt');
|
||||
$this->storage->createFile('/', 'test (2).txt');
|
||||
$this->storage->createFile('/sub', 'test.txt');
|
||||
|
||||
// move second file over the first one
|
||||
$this->storage->copyFile('/sub/test.txt', '/');
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/test.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (1).txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (2).txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (3).txt')); // file is copied here
|
||||
}
|
||||
|
||||
public function testGetPathPrefix()
|
||||
{
|
||||
$this->storage->setPathPrefix('/john/');
|
||||
$this->assertEquals($this->storage->getPathPrefix(), '/john/');
|
||||
|
||||
$this->storage->setPathPrefix('/john');
|
||||
$this->assertEquals($this->storage->getPathPrefix(), '/john/');
|
||||
|
||||
$this->storage->setPathPrefix('john/');
|
||||
$this->assertEquals($this->storage->getPathPrefix(), '/john/');
|
||||
|
||||
$this->storage->setPathPrefix('john');
|
||||
$this->assertEquals($this->storage->getPathPrefix(), '/john/');
|
||||
}
|
||||
|
||||
public function testApplyPathPrefix()
|
||||
{
|
||||
$this->storage->setPathPrefix('/john/');
|
||||
|
||||
$this->assertEquals('/john/test', $this->invokeMethod($this->storage, 'applyPathPrefix', ['test']));
|
||||
$this->assertEquals('/john/test/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['test/']));
|
||||
$this->assertEquals('/john/test/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['/test/']));
|
||||
$this->assertEquals('/john/test', $this->invokeMethod($this->storage, 'applyPathPrefix', ['/test']));
|
||||
$this->assertEquals('/john/test.txt', $this->invokeMethod($this->storage, 'applyPathPrefix', ['test.txt']));
|
||||
$this->assertEquals('/john/test.txt/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['test.txt/']));
|
||||
}
|
||||
|
||||
public function testStripPathPrefix()
|
||||
{
|
||||
$this->storage->setPathPrefix('/john/');
|
||||
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'stripPathPrefix', ['/john/']));
|
||||
$this->assertEquals('/test/', $this->invokeMethod($this->storage, 'stripPathPrefix', ['/john/test/']));
|
||||
$this->assertEquals('/test', $this->invokeMethod($this->storage, 'stripPathPrefix', ['/john/test']));
|
||||
$this->assertEquals('/doe/test', $this->invokeMethod($this->storage, 'stripPathPrefix', ['/john/doe/test']));
|
||||
$this->assertEquals('/doe/test.txt', $this->invokeMethod($this->storage, 'stripPathPrefix', ['john/doe/test.txt']));
|
||||
$this->assertEquals('/john', $this->invokeMethod($this->storage, 'stripPathPrefix', ['/john']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'stripPathPrefix', ['/']));
|
||||
}
|
||||
|
||||
public function testAddSeparators()
|
||||
{
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'addSeparators', ['']));
|
||||
$this->assertEquals('/ /', $this->invokeMethod($this->storage, 'addSeparators', [' ']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'addSeparators', ['/']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'addSeparators', ['//']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'addSeparators', ['////']));
|
||||
$this->assertEquals('/b/', $this->invokeMethod($this->storage, 'addSeparators', ['b']));
|
||||
$this->assertEquals('/b/', $this->invokeMethod($this->storage, 'addSeparators', ['/b']));
|
||||
$this->assertEquals('/b/', $this->invokeMethod($this->storage, 'addSeparators', ['/b/']));
|
||||
$this->assertEquals('/b/', $this->invokeMethod($this->storage, 'addSeparators', ['/b//']));
|
||||
$this->assertEquals('/b/', $this->invokeMethod($this->storage, 'addSeparators', ['//b//']));
|
||||
$this->assertEquals('/a/b/', $this->invokeMethod($this->storage, 'addSeparators', ['a/b']));
|
||||
$this->assertEquals('/a/b/', $this->invokeMethod($this->storage, 'addSeparators', ['a/b/']));
|
||||
$this->assertEquals('/a/b/', $this->invokeMethod($this->storage, 'addSeparators', ['/a/b/']));
|
||||
$this->assertEquals('/a b/', $this->invokeMethod($this->storage, 'addSeparators', ['a b']));
|
||||
$this->assertEquals('/a b/c/', $this->invokeMethod($this->storage, 'addSeparators', ['a b/c']));
|
||||
}
|
||||
|
||||
public function testJoinPaths()
|
||||
{
|
||||
$this->assertEquals('/1/2', $this->invokeMethod($this->storage, 'joinPaths', ['1', '2']));
|
||||
$this->assertEquals('/1/2', $this->invokeMethod($this->storage, 'joinPaths', ['/1', '/2']));
|
||||
$this->assertEquals('/1/2/', $this->invokeMethod($this->storage, 'joinPaths', ['1/', '2/']));
|
||||
$this->assertEquals('/1/2', $this->invokeMethod($this->storage, 'joinPaths', ['1/', '/2']));
|
||||
$this->assertEquals('/1/2/', $this->invokeMethod($this->storage, 'joinPaths', ['/1', '2/']));
|
||||
$this->assertEquals('/1/2/', $this->invokeMethod($this->storage, 'joinPaths', ['/1/', '/2/']));
|
||||
}
|
||||
|
||||
public function testGetBaseName()
|
||||
{
|
||||
$this->assertEquals('test.txt', $this->invokeMethod($this->storage, 'getBaseName', ['test.txt']));
|
||||
$this->assertEquals('test.txt', $this->invokeMethod($this->storage, 'getBaseName', ['/test.txt']));
|
||||
$this->assertEquals('test.txt', $this->invokeMethod($this->storage, 'getBaseName', ['/mike/test.txt']));
|
||||
$this->assertEquals('b', $this->invokeMethod($this->storage, 'getBaseName', ['/a/b']));
|
||||
$this->assertEquals('b', $this->invokeMethod($this->storage, 'getBaseName', ['/a/b/']));
|
||||
$this->assertEquals('b', $this->invokeMethod($this->storage, 'getBaseName', ['a/b']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'getBaseName', ['']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'getBaseName', ['/']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'getBaseName', ['/////']));
|
||||
}
|
||||
|
||||
public function testGetParent()
|
||||
{
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'getParent', ['']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'getParent', [' ']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'getParent', ['/']));
|
||||
$this->assertEquals('/', $this->invokeMethod($this->storage, 'getParent', ['////']));
|
||||
$this->assertEquals('/parent', $this->invokeMethod($this->storage, 'getParent', ['/parent/child/']));
|
||||
$this->assertEquals('/1/2/3/4', $this->invokeMethod($this->storage, 'getParent', ['/1/2/3/4/5/']));
|
||||
$this->assertEquals('/1/2', $this->invokeMethod($this->storage, 'getParent', ['1/2/3']));
|
||||
$this->assertEquals('/1/2', $this->invokeMethod($this->storage, 'getParent', ['1/2/3/']));
|
||||
$this->assertEquals('/1/2', $this->invokeMethod($this->storage, 'getParent', ['/1/2/3/']));
|
||||
}
|
||||
|
||||
public function testDeleteFiles()
|
||||
{
|
||||
$this->storage->createFile('/', 'sample22.txt');
|
||||
$this->assertFileExists(TEST_REPOSITORY.'/sample22.txt');
|
||||
|
||||
$this->storage->deleteFile('sample22.txt');
|
||||
|
||||
$this->assertFileNotExists(TEST_REPOSITORY.'/sample22.txt');
|
||||
}
|
||||
|
||||
public function testCreateAndDeleteDirectory()
|
||||
{
|
||||
$this->storage->createDir('/', 'sample22');
|
||||
$this->storage->createDir('/sample22/subsample', 'sample22');
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/sample22');
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/sample22/subsample');
|
||||
|
||||
$this->storage->deleteDir('sample22');
|
||||
|
||||
$this->assertDirectoryNotExists(TEST_REPOSITORY.'/sample22');
|
||||
$this->assertDirectoryNotExists(TEST_REPOSITORY.'/sample22/subsample');
|
||||
}
|
||||
|
||||
public function testReadFileStream()
|
||||
{
|
||||
$this->storage->createFile('/', 'a.txt');
|
||||
$ret = $this->storage->readStream('a.txt');
|
||||
|
||||
$this->assertEquals($ret['filename'], 'a.txt');
|
||||
$this->assertTrue(is_resource($ret['stream']));
|
||||
}
|
||||
|
||||
public function testReadFileStreamMissingFileThrowsException()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$this->storage->readStream('missing');
|
||||
}
|
||||
|
||||
public function testCannotStreamDirectory()
|
||||
{
|
||||
$this->storage->createDir('/', 'sub');
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$this->storage->readStream('sub');
|
||||
}
|
||||
|
||||
public function testDirCheck()
|
||||
{
|
||||
$this->storage->createDir('/', 'sub');
|
||||
$this->storage->createDir('/', 'empty');
|
||||
$this->storage->createFile('/sub', 'd.txt');
|
||||
$this->storage->createDir('/sub', 'sub1');
|
||||
$this->storage->createFile('/sub/sub1', 'f.txt');
|
||||
$this->storage->createDir('/sub', 'empty');
|
||||
$this->storage->createDir('/', 'john');
|
||||
$this->storage->createFile('/', 'a.txt');
|
||||
|
||||
$this->assertTrue($this->storage->isDir('/sub'));
|
||||
$this->assertTrue($this->storage->isDir('/sub/sub1'));
|
||||
$this->assertTrue($this->storage->isDir('/john'));
|
||||
$this->assertTrue($this->storage->isDir('/empty'));
|
||||
$this->assertTrue($this->storage->isDir('/sub/empty'));
|
||||
$this->assertFalse($this->storage->isDir('a.txt'));
|
||||
$this->assertFalse($this->storage->isDir('/sub/d.txt'));
|
||||
$this->assertFalse($this->storage->isDir('/sub/sub1/f.txt'));
|
||||
}
|
||||
|
||||
public function testRenameFile()
|
||||
{
|
||||
$this->storage->createFile('/', 'a.txt');
|
||||
|
||||
$this->storage->rename('/', 'a.txt', 'a1.txt');
|
||||
|
||||
$this->assertFalse($this->storage->fileExists('/a.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/a1.txt'));
|
||||
}
|
||||
|
||||
public function testRenameFileToExistingDestinationUpcountsFilenameRecursively()
|
||||
{
|
||||
$this->storage->createFile('/', 'a.txt');
|
||||
$this->storage->createFile('/', 'a (1).txt');
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
|
||||
$this->storage->rename('/', 'test.txt', 'a.txt');
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/a.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/a (1).txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/a (2).txt')); // result
|
||||
}
|
||||
|
||||
public function testRenameFileInSubfolder()
|
||||
{
|
||||
$this->storage->createDir('/', 'john');
|
||||
$this->storage->createFile('/john', 'john.txt');
|
||||
|
||||
$this->storage->rename('/john/', 'john.txt', 'john2.txt');
|
||||
|
||||
$this->assertFalse($this->storage->fileExists('/john/john.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/john/john2.txt'));
|
||||
}
|
||||
|
||||
public function testRenameFileWithPathPrefix()
|
||||
{
|
||||
$this->storage->setPathPrefix('/john/');
|
||||
$this->storage->createFile('/', 'john.txt');
|
||||
$this->storage->rename('/', 'john.txt', 'john2.txt');
|
||||
|
||||
$this->assertFalse($this->storage->fileExists('/john.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/john2.txt'));
|
||||
}
|
||||
|
||||
public function testRenameNonexistingFileThrowsException()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$this->storage->move('/', 'nonexisting.txt', 'a1.txt');
|
||||
}
|
||||
|
||||
public function testCreatingFile()
|
||||
{
|
||||
$this->storage->createFile('/', 'sample22');
|
||||
$ret = $this->storage->getDirectoryCollection('/');
|
||||
$this->assertStringContainsString('sample22', json_encode($ret));
|
||||
|
||||
$this->storage->createFile('/sub/', 'sample33');
|
||||
$ret = $this->storage->getDirectoryCollection('/sub/');
|
||||
$this->assertStringContainsString('sample33', json_encode($ret));
|
||||
}
|
||||
|
||||
public function testCreatingFileUpcountsNameIfAlreadyExists()
|
||||
{
|
||||
$this->assertFalse($this->storage->fileExists('/test.txt'));
|
||||
$this->assertFalse($this->storage->fileExists('/test (1).txt'));
|
||||
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/test.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (1).txt'));
|
||||
}
|
||||
|
||||
public function testCreatingFileUpcountsNameRecursivelyIfAlreadyExists()
|
||||
{
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
$this->storage->createFile('/', 'test (1).txt');
|
||||
|
||||
// this file
|
||||
$this->storage->createFile('/', 'test.txt');
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/test.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (1).txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/test (2).txt')); // ends up here
|
||||
}
|
||||
|
||||
public function testGetSeparator()
|
||||
{
|
||||
$separator = $this->storage->getSeparator();
|
||||
|
||||
$this->assertEquals($this->separator, $separator);
|
||||
}
|
||||
|
||||
public function testCopyFile()
|
||||
{
|
||||
$this->storage->setPathPrefix('/john');
|
||||
$this->storage->createFile('/', 'john.txt');
|
||||
$this->storage->createDir('/', 'johnsub');
|
||||
$this->storage->createFile('/johnsub', 'sub.txt');
|
||||
|
||||
$this->assertFalse($this->storage->fileExists('/johnsub/john.txt'));
|
||||
|
||||
$this->storage->copyFile('/john.txt', '/johnsub/');
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/johnsub/john.txt'));
|
||||
|
||||
$this->assertFalse($this->storage->fileExists('/sub.txt'));
|
||||
|
||||
$this->storage->copyFile('/johnsub/sub.txt', '/');
|
||||
|
||||
$this->assertTrue($this->storage->fileExists('/sub.txt'));
|
||||
}
|
||||
|
||||
public function testCopyMissingFileThrowsException()
|
||||
{
|
||||
$this->storage->createDir('/', 'tmp');
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->storage->copyFile('/missing.txt', '/tmp/');
|
||||
}
|
||||
|
||||
public function testCopyMissingDirCreatedADirOnDestination()
|
||||
{
|
||||
$this->storage->createDir('/', 'tmp');
|
||||
|
||||
$this->storage->copyDir('/missing/', '/tmp/');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/tmp/missing/');
|
||||
}
|
||||
|
||||
public function testCopyDir()
|
||||
{
|
||||
$this->storage->createDir('/', '/john');
|
||||
$this->storage->createDir('/john', '/johnsub');
|
||||
$this->storage->createDir('/', '/jane');
|
||||
|
||||
$this->storage->copyDir('/john/johnsub', '/jane/');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/jane/johnsub');
|
||||
}
|
||||
|
||||
public function testCopyDirWithSubDirs()
|
||||
{
|
||||
$this->storage->createDir('/', '/sub');
|
||||
$this->storage->createDir('/sub', '/sub1');
|
||||
$this->storage->createDir('/', '/jane');
|
||||
|
||||
$this->storage->copyDir('/sub', '/jane/');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/jane/sub');
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/jane/sub/sub1');
|
||||
}
|
||||
|
||||
public function testCopyDirWithEmptySubDir()
|
||||
{
|
||||
$this->storage->createDir('/', 'tmp');
|
||||
$this->storage->createDir('/tmp/', 'sample22');
|
||||
$this->storage->createDir('/tmp/sample22/', 'subsample1');
|
||||
$this->storage->createDir('/tmp/sample22/', 'subsample2');
|
||||
$this->storage->createFile('/tmp/sample22/subsample2', 'zzzz');
|
||||
|
||||
$this->assertDirectoryNotExists(TEST_REPOSITORY.'/jane/sample22');
|
||||
|
||||
$this->storage->copyDir('/tmp/sample22', '/jane/');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/jane/sample22');
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/jane/sample22/subsample2');
|
||||
$this->assertTrue($this->storage->fileExists('/jane/sample22/subsample2/zzzz'));
|
||||
}
|
||||
|
||||
public function testCopyEmptyDir()
|
||||
{
|
||||
$this->storage->createDir('/', 'dest');
|
||||
$this->storage->createDir('/', 'tmp');
|
||||
|
||||
$this->storage->copyDir('/tmp', '/dest');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/dest/tmp');
|
||||
}
|
||||
|
||||
public function testCopyDirOverExistingUpcountsDestinationDirname()
|
||||
{
|
||||
/*
|
||||
* /dest/tmp/
|
||||
* /dest/tmp/a.txt
|
||||
* /tmp/
|
||||
* /tmp/b.txt
|
||||
*
|
||||
* copy /tmp/ => /dest/
|
||||
*
|
||||
* /dest/tmp/
|
||||
* /dest/tmp/a.txt
|
||||
* /dest/tmp (1)/
|
||||
* /dest/tmp (1)/b.txt
|
||||
*
|
||||
*/
|
||||
$this->storage->createDir('/', 'dest');
|
||||
$this->storage->createDir('/dest', 'tmp');
|
||||
$this->storage->createFile('/dest/tmp/', 'a.txt');
|
||||
$this->storage->createDir('/', 'tmp');
|
||||
$this->storage->createFile('/tmp/', 'b.txt');
|
||||
|
||||
$this->storage->copyDir('/tmp', '/dest');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/dest/tmp/');
|
||||
$this->assertTrue($this->storage->fileExists('/dest/tmp/a.txt'));
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/dest/tmp (1)');
|
||||
$this->assertTrue($this->storage->fileExists('/dest/tmp (1)/b.txt'));
|
||||
}
|
||||
|
||||
public function testMoveFile()
|
||||
{
|
||||
$this->storage->createFile('/', 'file.txt');
|
||||
$this->storage->createDir('/', 'tmp');
|
||||
$this->storage->move('/file.txt', '/tmp/file.txt');
|
||||
|
||||
$this->assertFalse($this->storage->fileExists('/file.txt'));
|
||||
$this->assertTrue($this->storage->fileExists('/tmp/file.txt'));
|
||||
}
|
||||
|
||||
public function testMoveDirectory()
|
||||
{
|
||||
$this->storage->createDir('/', 'test1');
|
||||
$this->storage->createDir('/', 'test2');
|
||||
$this->storage->move('/test1', '/test2/test1/');
|
||||
|
||||
$this->assertDirectoryExists(TEST_REPOSITORY.'/test2/test1/');
|
||||
}
|
||||
}
|
61
tests/backend/Unit/MainTest.php
Normal file
61
tests/backend/Unit/MainTest.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\App;
|
||||
use Filegator\Config\Config;
|
||||
use Filegator\Container\Container;
|
||||
use Filegator\Kernel\Request;
|
||||
use Filegator\Kernel\Response;
|
||||
use Filegator\Services\View\Adapters\Vuejs;
|
||||
use Tests\FakeResponse;
|
||||
use Tests\FakeStreamedResponse;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class MainTest extends TestCase
|
||||
{
|
||||
public function testMainApp()
|
||||
{
|
||||
$config = new Config();
|
||||
$request = new Request();
|
||||
$response = new FakeResponse();
|
||||
$sresponse = new FakeStreamedResponse();
|
||||
$container = new Container();
|
||||
|
||||
$app = new App($config, $request, $response, $sresponse, $container);
|
||||
|
||||
$this->assertEquals($app->resolve(Config::class), $config);
|
||||
$this->assertEquals($app->resolve(Request::class), $request);
|
||||
$this->assertInstanceOf(Response::class, $app->resolve(Response::class));
|
||||
}
|
||||
|
||||
public function testServices()
|
||||
{
|
||||
$config = [
|
||||
'services' => [
|
||||
'Service1' => [
|
||||
'handler' => 'Filegator\Services\View\Adapters\Vuejs',
|
||||
],
|
||||
'Service2' => [
|
||||
'handler' => 'Filegator\Services\View\Adapters\Vuejs',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$app = new App(new Config($config), new Request(), new FakeResponse(), new FakeStreamedResponse(), new Container());
|
||||
|
||||
$this->assertEquals($app->resolve('Service1'), new Vuejs(new Config($config)));
|
||||
$this->assertEquals($app->resolve('Service2'), new Vuejs(new Config($config)));
|
||||
}
|
||||
}
|
141
tests/backend/Unit/RequestTest.php
Normal file
141
tests/backend/Unit/RequestTest.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Kernel\Request;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class RequestTest extends TestCase
|
||||
{
|
||||
public function testGetRequest()
|
||||
{
|
||||
$request = Request::create(
|
||||
'?r=/test&a=1&b=2',
|
||||
'GET'
|
||||
);
|
||||
|
||||
$this->assertEquals($request->all(), [
|
||||
'r' => '/test',
|
||||
'a' => '1',
|
||||
'b' => '2',
|
||||
]);
|
||||
|
||||
$this->assertEquals($request->input('r'), '/test');
|
||||
$this->assertEquals($request->input('a'), '1');
|
||||
$this->assertEquals($request->input('b'), '2');
|
||||
}
|
||||
|
||||
public function testPostRequest()
|
||||
{
|
||||
$request = Request::create(
|
||||
'/somewhere',
|
||||
'POST',
|
||||
['param1' => '1', 'param2' => '2']
|
||||
);
|
||||
|
||||
$this->assertEquals($request->all(), [
|
||||
'param1' => '1',
|
||||
'param2' => '2',
|
||||
]);
|
||||
|
||||
$this->assertEquals($request->input('param1'), '1');
|
||||
$this->assertEquals($request->input('param2'), '2');
|
||||
}
|
||||
|
||||
public function testJsonRequest()
|
||||
{
|
||||
$request = Request::create(
|
||||
'',
|
||||
'GET',
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
json_encode(['sample' => 'content'])
|
||||
);
|
||||
|
||||
$this->assertEquals($request->all(), [
|
||||
'sample' => 'content',
|
||||
]);
|
||||
|
||||
$this->assertEquals($request->input('sample'), 'content');
|
||||
}
|
||||
|
||||
public function testGetAndJsonParametersTogether()
|
||||
{
|
||||
$request = Request::create(
|
||||
'/test?priority=1',
|
||||
'POST',
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
json_encode(['sample' => 'content', 'more' => '1'])
|
||||
);
|
||||
|
||||
$this->assertEquals($request->all(), [
|
||||
'priority' => '1',
|
||||
'sample' => 'content',
|
||||
'more' => '1',
|
||||
]);
|
||||
|
||||
$this->assertEquals($request->input('priority'), '1');
|
||||
$this->assertEquals($request->input('sample'), 'content');
|
||||
$this->assertEquals($request->input('more'), '1');
|
||||
}
|
||||
|
||||
public function testGetPostParametersTogether()
|
||||
{
|
||||
$request = Request::create(
|
||||
'/test?priority=10&something=else',
|
||||
'POST',
|
||||
['param' => 'param1', 'priority' => 5],
|
||||
);
|
||||
|
||||
$this->assertEquals($request->all(), [
|
||||
'priority' => '10',
|
||||
'something' => 'else',
|
||||
'param' => 'param1',
|
||||
]);
|
||||
|
||||
$this->assertEquals($request->input('priority'), '10');
|
||||
$this->assertEquals($request->input('something'), 'else');
|
||||
$this->assertEquals($request->input('param'), 'param1');
|
||||
}
|
||||
|
||||
public function testGetPostAndJsonParametersTogether()
|
||||
{
|
||||
$request = Request::create(
|
||||
'/test?priority=10&something=else',
|
||||
'POST',
|
||||
['param' => 'param1', 'priority' => 5],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
json_encode(['sample' => 'content', 'priority' => '2'])
|
||||
);
|
||||
|
||||
$this->assertEquals($request->all(), [
|
||||
'priority' => '10',
|
||||
'something' => 'else',
|
||||
'param' => 'param1',
|
||||
'sample' => 'content',
|
||||
]);
|
||||
|
||||
$this->assertEquals($request->input('priority'), '10');
|
||||
$this->assertEquals($request->input('something'), 'else');
|
||||
$this->assertEquals($request->input('param'), 'param1');
|
||||
$this->assertEquals($request->input('sample'), 'content');
|
||||
}
|
||||
}
|
172
tests/backend/Unit/RouterTest.php
Normal file
172
tests/backend/Unit/RouterTest.php
Normal file
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Container\Container;
|
||||
use Filegator\Kernel\Request;
|
||||
use Filegator\Services\Auth\User;
|
||||
use Filegator\Services\Router\Router;
|
||||
use Tests\MockUsers;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class RouterTest extends TestCase
|
||||
{
|
||||
private $config_stub;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->config_stub = [
|
||||
'query_param' => 'r',
|
||||
'routes_file' => __DIR__.'/../testroutes.php',
|
||||
];
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testHome()
|
||||
{
|
||||
$request = Request::create('?r=/', 'GET');
|
||||
|
||||
$user = new User();
|
||||
|
||||
$container = $this->createMock(Container::class);
|
||||
$container->expects($this->once())
|
||||
->method('call')
|
||||
->with(['\Filegator\Controllers\ViewController', 'index'], [])
|
||||
;
|
||||
|
||||
$router = $this->getRouter($request, $user, $container);
|
||||
}
|
||||
|
||||
public function testPostToLogin()
|
||||
{
|
||||
$request = Request::create('?r=/login', 'POST');
|
||||
|
||||
$user = new User();
|
||||
|
||||
$container = $this->createMock(Container::class);
|
||||
$container->expects($this->once())
|
||||
->method('call')
|
||||
->with(['\Filegator\Controllers\AuthController', 'login'], [])
|
||||
;
|
||||
|
||||
$router = $this->getRouter($request, $user, $container);
|
||||
}
|
||||
|
||||
public function testRouteNotFound()
|
||||
{
|
||||
$request = Request::create('?r=/something', 'POST');
|
||||
|
||||
$user = new User();
|
||||
|
||||
$container = $this->createMock(Container::class);
|
||||
$container->expects($this->once())
|
||||
->method('call')
|
||||
->with(['\Filegator\Controllers\ErrorController', 'notFound'], [])
|
||||
;
|
||||
|
||||
$router = $this->getRouter($request, $user, $container);
|
||||
}
|
||||
|
||||
public function testMethodNotAllowed()
|
||||
{
|
||||
$request = Request::create('?r=/login', 'GET');
|
||||
|
||||
$user = new User();
|
||||
|
||||
$container = $this->createMock(Container::class);
|
||||
$container->expects($this->once())
|
||||
->method('call')
|
||||
->with(['\Filegator\Controllers\ErrorController', 'methodNotAllowed'], [])
|
||||
;
|
||||
|
||||
$router = $this->getRouter($request, $user, $container);
|
||||
}
|
||||
|
||||
public function testRouteIsProtectedFromGuests()
|
||||
{
|
||||
$request = Request::create('?r=/noguests', 'GET');
|
||||
|
||||
$user = new User();
|
||||
|
||||
$container = $this->createMock(Container::class);
|
||||
$container->expects($this->once())
|
||||
->method('call')
|
||||
->with(['\Filegator\Controllers\ErrorController', 'notFound'], [])
|
||||
;
|
||||
|
||||
$router = $this->getRouter($request, $user, $container);
|
||||
}
|
||||
|
||||
public function testRouteIsAllowedForUser()
|
||||
{
|
||||
$request = Request::create('?r=/noguests', 'GET');
|
||||
|
||||
$user = new User();
|
||||
$user->setRole('user');
|
||||
|
||||
$container = $this->createMock(Container::class);
|
||||
$container->expects($this->once())
|
||||
->method('call')
|
||||
->with(['ProtectedController', 'protectedMethod'], [])
|
||||
;
|
||||
|
||||
$router = $this->getRouter($request, $user, $container);
|
||||
}
|
||||
|
||||
public function testRouteIsProtectedFromUsers()
|
||||
{
|
||||
$request = Request::create('?r=/adminonly', 'GET');
|
||||
|
||||
$user = new User();
|
||||
$user->setRole('user');
|
||||
|
||||
$container = $this->createMock(Container::class);
|
||||
$container->expects($this->once())
|
||||
->method('call')
|
||||
->with(['\Filegator\Controllers\ErrorController', 'notFound'], [])
|
||||
;
|
||||
|
||||
$router = $this->getRouter($request, $user, $container);
|
||||
}
|
||||
|
||||
public function testRouteIsAllowedForAdmin()
|
||||
{
|
||||
$request = Request::create('?r=/adminonly', 'GET');
|
||||
|
||||
$user = new User();
|
||||
$user->setRole('admin');
|
||||
|
||||
$container = $this->createMock(Container::class);
|
||||
$container->expects($this->once())
|
||||
->method('call')
|
||||
->with(['AdminController', 'adminOnlyMethod'], [])
|
||||
;
|
||||
|
||||
$router = $this->getRouter($request, $user, $container);
|
||||
}
|
||||
|
||||
private function getRouter(Request $request, User $user, Container $container)
|
||||
{
|
||||
$auth_stub = $this->createMock(MockUsers::class);
|
||||
$auth_stub->method('user')
|
||||
->willReturn($user)
|
||||
;
|
||||
|
||||
$router = new Router($request, $auth_stub, $container);
|
||||
$router->init($this->config_stub);
|
||||
|
||||
return $router;
|
||||
}
|
||||
}
|
62
tests/backend/Unit/SessionStorageTest.php
Normal file
62
tests/backend/Unit/SessionStorageTest.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Kernel\Request;
|
||||
use Filegator\Services\Session\Adapters\SessionStorage;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class SessionStorageTest extends TestCase
|
||||
{
|
||||
protected $session_service;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->session_service = new SessionStorage(new Request());
|
||||
$this->session_service->init([
|
||||
'session_handler' => 'mockfilesession',
|
||||
'available' => [
|
||||
'mockfilesession' => function () {
|
||||
return new \Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testInvalidateSession()
|
||||
{
|
||||
$this->session_service->set('test1', 444);
|
||||
$this->session_service->invalidate();
|
||||
|
||||
$this->assertNull($this->session_service->get('test1'));
|
||||
}
|
||||
|
||||
public function testInvalidateSessionWhichIsNotStartedYet()
|
||||
{
|
||||
$this->session_service->invalidate();
|
||||
|
||||
$this->assertNull($this->session_service->get('something'));
|
||||
}
|
||||
|
||||
public function testUseSession()
|
||||
{
|
||||
$this->session_service->set('test2', 999);
|
||||
$this->session_service->save();
|
||||
|
||||
$this->assertEquals($this->session_service->get('test2'), 999);
|
||||
$this->assertNull($this->session_service->get('test1'));
|
||||
}
|
||||
}
|
146
tests/backend/Unit/TmpfsTest.php
Normal file
146
tests/backend/Unit/TmpfsTest.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Services\Tmpfs\Adapters\Tmpfs;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class TmpfsTest extends TestCase
|
||||
{
|
||||
protected $service;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->resetTempDir();
|
||||
rmdir(TEST_TMP_PATH);
|
||||
|
||||
$this->service = new Tmpfs();
|
||||
$this->service->init([
|
||||
'path' => TEST_TMP_PATH,
|
||||
'gc_probability_perc' => 100,
|
||||
'gc_older_than' => 60 * 60 * 24 * 2, // 2 days
|
||||
]);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testWriteContentToTmpFile()
|
||||
{
|
||||
$this->service->write('a.txt', 'lorem');
|
||||
|
||||
$this->assertFileExists(TEST_TMP_PATH.'/a.txt');
|
||||
}
|
||||
|
||||
public function testWriteContentToTmpFileUsingStream()
|
||||
{
|
||||
$stream = fopen(TEST_FILE, 'r');
|
||||
$this->service->write('a.txt', $stream);
|
||||
|
||||
$this->assertFileEquals(TEST_TMP_PATH.'/a.txt', TEST_FILE);
|
||||
}
|
||||
|
||||
public function testReadingTmpFileContents()
|
||||
{
|
||||
$this->service->write('a.txt', 'lorem');
|
||||
|
||||
$contents = $this->service->read('a.txt');
|
||||
|
||||
$this->assertEquals($contents, 'lorem');
|
||||
}
|
||||
|
||||
public function testReadingTmpFileContentsUsingStream()
|
||||
{
|
||||
$this->service->write('a.txt', 'lorem');
|
||||
|
||||
$ret = $this->service->readStream('a.txt');
|
||||
$this->assertEquals($ret['filename'], 'a.txt');
|
||||
|
||||
$contents = stream_get_contents($ret['stream']);
|
||||
$this->assertEquals($contents, 'lorem');
|
||||
}
|
||||
|
||||
public function testRemovingTmpFile()
|
||||
{
|
||||
$this->service->write('a.txt', 'lorem');
|
||||
|
||||
$this->assertFileExists(TEST_TMP_PATH.'a.txt');
|
||||
|
||||
$this->service->remove('a.txt');
|
||||
|
||||
$this->assertFileNotExists(TEST_TMP_PATH.'a.txt');
|
||||
}
|
||||
|
||||
public function testCheckExistingFile()
|
||||
{
|
||||
$this->service->write('a.txt', 'lorem');
|
||||
|
||||
$this->assertTrue($this->service->exists('a.txt'));
|
||||
$this->assertFalse($this->service->exists('nothere.txt'));
|
||||
}
|
||||
|
||||
public function testFindingAllFilesMatchingPatters()
|
||||
{
|
||||
$this->service->write('a.txt', 'lorem');
|
||||
$this->service->write('b.txt', 'lorem');
|
||||
$this->service->write('b.zip', 'lorem');
|
||||
|
||||
$this->assertCount(2, $this->service->findAll('*.txt'));
|
||||
$this->assertCount(2, $this->service->findAll('b*'));
|
||||
$this->assertCount(3, $this->service->findAll('*'));
|
||||
$this->assertCount(0, $this->service->findAll('1*2'));
|
||||
}
|
||||
|
||||
public function testCleaningFilesOlderThan()
|
||||
{
|
||||
$this->service->write('a.txt', 'lorem');
|
||||
$this->service->write('b.txt', 'lorem');
|
||||
$this->service->write('b.zip', 'lorem');
|
||||
|
||||
$this->service->clean(10000);
|
||||
|
||||
$this->assertCount(3, $this->service->findAll('*'));
|
||||
|
||||
$this->service->clean(0);
|
||||
|
||||
$this->assertCount(0, $this->service->findAll('*'));
|
||||
}
|
||||
|
||||
public function testDeleteOldFilesAutomaticaly()
|
||||
{
|
||||
touch(TEST_TMP_PATH.'fresh.txt', time());
|
||||
touch(TEST_TMP_PATH.'old.txt', time() - 60 * 60 * 24 * 10); // 10 days old
|
||||
|
||||
$this->service->init([
|
||||
'path' => TEST_TMP_PATH,
|
||||
'gc_probability_perc' => 100,
|
||||
'gc_older_than' => 60 * 60 * 24 * 2, // 2 days
|
||||
]);
|
||||
|
||||
$this->assertFileExists(TEST_TMP_PATH.'fresh.txt');
|
||||
$this->assertFileNotExists(TEST_TMP_PATH.'old.txt');
|
||||
}
|
||||
|
||||
public function testGarbageIsNotDeletedEveryTime()
|
||||
{
|
||||
touch(TEST_TMP_PATH.'old.txt', time() - 60 * 60 * 24 * 10); // 10 days old
|
||||
|
||||
$this->service->init([
|
||||
'path' => TEST_TMP_PATH,
|
||||
'gc_probability_perc' => 0,
|
||||
'gc_older_than' => 60 * 60 * 24 * 2, // 2 days
|
||||
]);
|
||||
|
||||
$this->assertFileExists(TEST_TMP_PATH.'old.txt');
|
||||
}
|
||||
}
|
173
tests/backend/Unit/UserTest.php
Normal file
173
tests/backend/Unit/UserTest.php
Normal file
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
use Filegator\Services\Auth\User;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class UserTest extends TestCase
|
||||
{
|
||||
public function testUser()
|
||||
{
|
||||
$name = 'John Doe';
|
||||
$username = 'john.doe@example.com';
|
||||
$homedir = '/';
|
||||
$role = 'user';
|
||||
$permissions = ['read'];
|
||||
|
||||
$user = new User($role, $homedir, $username, $name);
|
||||
$user->setRole($role);
|
||||
$user->setHomedir($homedir);
|
||||
$user->setUsername($username);
|
||||
$user->setName($name);
|
||||
$user->setPermissions($permissions);
|
||||
|
||||
$this->assertTrue($user->isUser());
|
||||
|
||||
$decoded = json_decode(json_encode($user));
|
||||
$this->assertEquals('user', $decoded->role);
|
||||
$this->assertEquals($username, $decoded->username);
|
||||
$this->assertEquals($name, $decoded->name);
|
||||
$this->assertEquals($permissions, $decoded->permissions);
|
||||
}
|
||||
|
||||
public function testAdmin()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setRole('admin');
|
||||
|
||||
$this->assertTrue($user->isAdmin());
|
||||
}
|
||||
|
||||
public function testGuest()
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$this->assertTrue($user->isGuest());
|
||||
|
||||
$decoded = json_decode(json_encode($user), true);
|
||||
$this->assertEquals([
|
||||
'role' => 'guest',
|
||||
'homedir' => '',
|
||||
'username' => '',
|
||||
'name' => '',
|
||||
'permissions' => [],
|
||||
], $decoded);
|
||||
}
|
||||
|
||||
public function testUserCannotGetNonExistingRole()
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$user->setRole('nonexistent');
|
||||
}
|
||||
|
||||
public function testUserCannotGetNonExistingPermision()
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$user->setPermissions(['read', 'write', 'nonexistent']);
|
||||
}
|
||||
|
||||
public function testUserHasRole()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setRole('user');
|
||||
|
||||
$this->assertTrue($user->hasRole('user'));
|
||||
$this->assertTrue($user->hasRole(['user']));
|
||||
$this->assertTrue($user->hasRole(['admin', 'guest', 'user']));
|
||||
|
||||
$this->assertFalse($user->hasRole(['admin', 'guest']));
|
||||
$this->assertFalse($user->hasRole('admin'));
|
||||
}
|
||||
|
||||
public function testDefaultUserHasNoPermissions()
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$this->assertTrue($user->hasPermissions([]));
|
||||
$this->assertFalse($user->hasPermissions('read'));
|
||||
$this->assertFalse($user->hasPermissions(['read']));
|
||||
$this->assertFalse($user->hasPermissions(['write', 'upload', 'read']));
|
||||
$this->assertFalse($user->hasPermissions(['write', 'upload']));
|
||||
$this->assertFalse($user->hasPermissions('upload'));
|
||||
}
|
||||
|
||||
public function testUserHasOnlyPermissionsSet()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setPermissions(['read', 'write']);
|
||||
|
||||
$this->assertTrue($user->hasPermissions('read'));
|
||||
$this->assertTrue($user->hasPermissions(['read']));
|
||||
$this->assertTrue($user->hasPermissions('write'));
|
||||
$this->assertTrue($user->hasPermissions(['write']));
|
||||
$this->assertTrue($user->hasPermissions(['read', 'write']));
|
||||
$this->assertTrue($user->hasPermissions(['write', 'read']));
|
||||
$this->assertFalse($user->hasPermissions(['write', 'upload', 'read']));
|
||||
$this->assertFalse($user->hasPermissions(['write', 'upload']));
|
||||
$this->assertFalse($user->hasPermissions('upload'));
|
||||
}
|
||||
|
||||
public function testUserCanHaveReadPermissions()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setPermissions(['read']);
|
||||
|
||||
$this->assertTrue($user->hasPermissions('read'));
|
||||
}
|
||||
|
||||
public function testUserCanHaveWritePermissions()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setPermissions(['write']);
|
||||
|
||||
$this->assertTrue($user->hasPermissions('write'));
|
||||
}
|
||||
|
||||
public function testUserCanHaveUploadPermissions()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setPermissions(['upload']);
|
||||
|
||||
$this->assertTrue($user->hasPermissions('upload'));
|
||||
}
|
||||
|
||||
public function testUserCanHaveDownloadPermissions()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setPermissions(['download']);
|
||||
|
||||
$this->assertTrue($user->hasPermissions('download'));
|
||||
}
|
||||
|
||||
public function testUserCanHaveBatchDownloadPermissions()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setPermissions(['batchdownload']);
|
||||
|
||||
$this->assertTrue($user->hasPermissions('batchdownload'));
|
||||
}
|
||||
|
||||
public function testUserCanHaveZipPermissions()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setPermissions(['zip']);
|
||||
|
||||
$this->assertTrue($user->hasPermissions('zip'));
|
||||
}
|
||||
}
|
31
tests/backend/Unit/ViewTest.php
Normal file
31
tests/backend/Unit/ViewTest.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Config\Config;
|
||||
use Filegator\Services\View\Adapters\Vuejs;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ViewTest extends TestCase
|
||||
{
|
||||
public function testViewService()
|
||||
{
|
||||
$config_mock = new Config(['frontend_config' => ['app_name' => 'testapp']]);
|
||||
|
||||
$service = new Vuejs($config_mock);
|
||||
$service->init();
|
||||
|
||||
$this->assertRegexp('/testapp/', $service->getIndexPage());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user