Booking, QI Content, Trees, Media

This commit is contained in:
Kevin Adametz 2019-10-02 20:03:55 +02:00
parent 1f340e96fa
commit 7fbac395a9
260 changed files with 27160 additions and 3773 deletions

View file

@ -0,0 +1,386 @@
<?php
namespace Tests;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
class ApiTest extends TestCase
{
/**
* Upload file.
*
* @var UploadedFile
*/
protected $file;
/**
* Upload file name.
*
* @var string
*/
protected $filename;
/**
* Upload thumble file name.
*
* @var string
*/
protected $filename_s;
/**
* Upload directory name.
*
* @var string
*/
protected $dir_name;
/**
* Root directory.
*
* @var string
*/
protected $root_dir = '/1';
public function setUp()
{
parent::setUp();
$uniq = uniqid();
$this->filename = $uniq . '.jpg';
$this->filename_s = $uniq . '_S.jpg';
$this->file = UploadedFile::fake()->image($this->filename);
$this->dir_name = uniqid();
}
public function tearDown()
{
$storage_path = implode(DIRECTORY_SEPARATOR, [
config('lfm.base_directory'),
config('lfm.files_folder_name'),
(new TestConfigHandler)->userField(),
]);
Storage::deleteDirectory($storage_path);
parent::tearDown();
}
/**
* test directory api.
*
* @group directory
*/
public function testFolder()
{
// auth()->loginUsingId(1);
$create = $this->getResponseByRouteName('getAddfolder', [
'name' => 'testcase',
]);
$create_duplicate = $this->getResponseByRouteName('getAddfolder', [
'name' => 'testcase',
]);
$create_empty = $this->getResponseByRouteName('getAddfolder', [
'name' => '',
]);
Config::set('lfm.alphanumeric_directory', true);
$create_alphanumeric = $this->getResponseByRouteName('getAddfolder', [
'name' => '測試資料夾',
]);
$rename = $this->getResponseByRouteName('getRename', [
'file' => 'testcase',
'new_name' => 'testcase2',
]);
$delete = $this->getResponseByRouteName('getDelete', [
'items' => 'testcase2',
]);
$this->assertEquals('OK', $create);
$this->assertEquals(trans('laravel-filemanager::lfm.error-folder-exist'), $create_duplicate);
$this->assertEquals(trans('laravel-filemanager::lfm.error-folder-name'), $create_empty);
$this->assertEquals(trans('laravel-filemanager::lfm.error-folder-alnum'), $create_alphanumeric);
$this->assertEquals('OK', $rename);
$this->assertEquals('OK', $delete);
}
/**
* upload a file.
*
* @group image
*/
public function testUploadImage()
{
$response = $this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $this->root_dir,
]);
$response->assertStatus(200);
$files_path = $this->getStoragedFilePathWithThumb($this->filename, $this->filename_s, $this->root_dir);
$this->assertFileExists($files_path['file']);
$this->assertFileExists($files_path['file_s']);
}
/**
* delete a file.
*
* @group image
* @group delete
*/
public function testDeleteImage()
{
$this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $this->root_dir,
]);
$response = $this->json('GET', route('unisharp.lfm.getDelete'), [
'items' => $this->filename,
'working_dir' => $this->root_dir,
]);
$response->assertStatus(200);
$files_path = $this->getStoragedFilePathWithThumb($this->filename, $this->filename_s, $this->root_dir);
$this->assertFileNotExists($files_path['file']);
$this->assertFileNotExists($files_path['file_s']);
}
/**
* upload file which exists already.
*
* @group image
* @group doubleUpload
*/
public function testDoubleUpload()
{
$this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $this->root_dir,
]);
$response = $this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $this->root_dir,
]);
$response->assertStatus(200);
$this->assertEquals($response->getContent(), '["A file with this name already exists!"]');
}
/**
* change file name.
*
* @group image
* @group rename
*/
public function testRenameImage()
{
$this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $this->root_dir,
]);
$uniq = uniqid();
$new_name = $uniq . '.jpg';
$new_name_s = $uniq . '_S.jpg';
$response = $this->json('GET', route('unisharp.lfm.getRename'), [
'file' => $this->filename,
'new_name' => $new_name,
'working_dir' => $this->root_dir,
]);
$response->assertStatus(200);
$files_path = $this->getStoragedFilePathWithThumb($new_name, $new_name_s, $this->root_dir);
$this->assertFileExists($files_path['file']);
$this->assertFileExists($files_path['file_s']);
}
/**
* add directory.
*
* @group directory
*/
public function testAddDirectory()
{
$response = $this->json('GET', route('unisharp.lfm.getAddfolder'), [
'name' => $this->dir_name,
'working_dir' => $this->root_dir,
]);
$response->assertStatus(200);
$dir_path = $this->getStoragedFilePath($this->dir_name, $this->root_dir);
$this->assertFileExists($dir_path);
}
/**
* delete directory.
*
* @group directory
* @group delete
*/
public function testDeleteDirectory()
{
$this->json('GET', route('unisharp.lfm.getAddfolder'), [
'name' => $this->dir_name,
'working_dir' => $this->root_dir,
]);
$reponse = $this->json('GET', route('unisharp.lfm.getDelete'), [
'items' => $this->dir_name,
'working_dir' => $this->root_dir,
]);
$reponse->assertStatus(200);
$dir_path = $this->getStoragedFilePath($this->dir_name, $this->root_dir);
$this->assertFileNotExists($dir_path);
}
/**
* rename directory.
*
* @group directory
* @group rename
*/
public function testRenameDirectory()
{
$this->json('GET', route('unisharp.lfm.getAddfolder'), [
'name' => $this->dir_name,
'working_dir' => $this->root_dir,
]);
$new_dir_name = uniqid();
$response = $this->json('GET', route('unisharp.lfm.getRename'), [
'file' => $this->dir_name,
'new_name' => $new_dir_name,
'working_dir' => $this->root_dir,
]);
$response->assertStatus(200);
$new_dir_path = $this->getStoragedFilePath($new_dir_name, $this->root_dir);
$this->assertFileExists($new_dir_path);
}
/**
* upload file in a directory.
*
* @group image
* @group directory
*/
public function testUploadFileInDirectory()
{
$this->json('GET', route('unisharp.lfm.getAddfolder'), [
'name' => $this->dir_name,
'working_dir' => $this->root_dir,
]);
$working_dir = $this->root_dir . DIRECTORY_SEPARATOR . $this->dir_name;
$response = $this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $working_dir,
]);
$response->assertStatus(200);
$files_path = $this->getStoragedFilePathWithThumb($this->filename, $this->filename_s, $working_dir);
$this->assertFileExists($files_path['file']);
$this->assertFileExists($files_path['file_s']);
}
/**
* delete file in a directory.
*
* @group image
* @group directory
*/
public function testDeleteFileInDirectory()
{
$this->json('GET', route('unisharp.lfm.getAddfolder'), [
'name' => $this->dir_name,
'working_dir' => $this->root_dir,
]);
$working_dir = $this->root_dir . DIRECTORY_SEPARATOR . $this->dir_name;
$response = $this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $working_dir,
]);
$reponse = $this->json('GET', route('unisharp.lfm.getDelete'), [
'items' => $this->filename,
'working_dir' => $working_dir,
]);
$reponse->assertStatus(200);
$files_path = $this->getStoragedFilePathWithThumb($this->filename, $this->filename_s, $working_dir);
$this->assertFileNotExists($files_path['file']);
$this->assertFileNotExists($files_path['file_s']);
}
/**
* rename file in directory.
*
* @group image
* @group directory
*/
public function testRenameFileInDirectory()
{
$this->json('GET', route('unisharp.lfm.getAddfolder'), [
'name' => $this->dir_name,
'working_dir' => $this->root_dir,
]);
$working_dir = $this->root_dir . DIRECTORY_SEPARATOR . $this->dir_name;
$response = $this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $working_dir,
]);
$uniq = uniqid();
$new_name = $uniq . '.jpg';
$new_name_s = $uniq . '_S.jpg';
$response = $this->json('GET', route('unisharp.lfm.getRename'), [
'file' => $this->filename,
'new_name' => $new_name,
'working_dir' => $working_dir,
]);
$response->assertStatus(200);
$files_path = $this->getStoragedFilePathWithThumb($new_name, $new_name_s, $working_dir);
$this->assertFileExists($files_path['file']);
$this->assertFileExists($files_path['file_s']);
}
/**
* upload file with lfm.rename_file = true.
*
* @group image
*/
public function testUploadImageWithRename()
{
config(['lfm.rename_file' => true]);
$response = $this->json('GET', route('unisharp.lfm.upload'), [
'upload' => [$this->file],
'working_dir' => $this->root_dir,
]);
$response->assertStatus(200);
$files_path = $this->getStoragedFilePathWithThumb($this->filename, $this->filename_s, $this->root_dir);
$this->assertFileNotExists($files_path['file']);
$this->assertFileNotExists($files_path['file_s']);
}
/*
* upload file with lfm.alphanumeric_filename = true
*
* @group image
* @group
*/
}

View file

@ -0,0 +1,206 @@
<?php
namespace Tests;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use UniSharp\LaravelFilemanager\Lfm;
use UniSharp\LaravelFilemanager\LfmItem;
use UniSharp\LaravelFilemanager\LfmPath;
class LfmItemTest extends TestCase
{
private $lfm_path;
private $lfm;
public function setUp()
{
$this->lfm = m::mock(Lfm::class);
$this->lfm_path = m::mock(LfmPath::class);
$this->lfm_path->shouldReceive('thumb')->andReturn($this->lfm_path);
}
public function tearDown()
{
m::close();
parent::tearDown();
}
public function testMagicGet()
{
$this->lfm_item = new LfmItem($this->lfm_path, m::mock(Lfm::class));
$this->lfm_item->attributes['foo'] = 'bar';
$this->assertEquals('bar', $this->lfm_item->foo);
}
public function testName()
{
$this->lfm_path->shouldReceive('getName')->andReturn('bar');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('bar', $item->name());
}
public function testAbsolutePath()
{
$this->lfm_path->shouldReceive('path')->with('absolute')->andReturn('foo/bar.baz');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('foo/bar.baz', $item->path());
}
public function testIsDirectory()
{
$this->lfm_path->shouldReceive('isDirectory')->andReturn(false);
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertFalse($item->isDirectory());
}
public function testIsFile()
{
$this->lfm_path->shouldReceive('isDirectory')->andReturn(false);
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertTrue($item->isFile());
}
public function testIsImage()
{
$this->lfm_path->shouldReceive('mimeType')->andReturn('application/plain');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertFalse($item->isImage());
}
public function testMimeType()
{
$this->lfm_path->shouldReceive('mimeType')->andReturn('application/plain');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('application/plain', $item->mimeType());
}
public function testType()
{
$this->lfm_path->shouldReceive('isDirectory')->andReturn(false);
$this->lfm_path->shouldReceive('mimeType')->andReturn('application/plain');
$this->lfm_path->shouldReceive('path')->with('absolute')->andReturn('foo/bar.baz');
$this->lfm_path->shouldReceive('extension')->andReturn('baz');
$this->lfm->shouldReceive('getFileType')->with('baz')->andReturn('File');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('File', $item->type());
}
public function testExtension()
{
$this->lfm_path->shouldReceive('path')->with('absolute')->andReturn('foo/bar.baz');
$this->lfm_path->shouldReceive('extension')->andReturn('baz');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('baz', $item->extension());
}
public function testThumbUrl()
{
$this->lfm_path->shouldReceive('isDirectory')->andReturn(false);
$this->lfm_path->shouldReceive('mimeType')->andReturn('application/plain');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertNull($item->thumbUrl());
}
// TODO: refactor
public function testUrl()
{
$this->lfm_path->shouldReceive('isDirectory')->andReturn(false);
$this->lfm_path->shouldReceive('getName')->andReturn('bar');
$this->lfm_path->shouldReceive('setName')->andReturn($this->lfm_path);
$this->lfm_path->shouldReceive('url')->andReturn('foo/bar');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('foo/bar', $item->url());
}
public function testSize()
{
$this->lfm_path->shouldReceive('size')->andReturn(1024);
$this->lfm_path->shouldReceive('isDirectory')->andReturn(false);
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('1.00 kB', $item->size());
}
public function testTime()
{
$this->lfm_path->shouldReceive('lastModified')->andReturn(0);
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals(0, $item->time());
}
public function testIcon()
{
$this->lfm_path->shouldReceive('isDirectory')->andReturn(false);
$this->lfm_path->shouldReceive('mimeType')->andReturn('application/plain');
$this->lfm_path->shouldReceive('path')->with('absolute')->andReturn('foo/bar.baz');
$this->lfm_path->shouldReceive('extension')->andReturn('baz');
$this->lfm->shouldReceive('getFileIcon')->with('baz')->andReturn('fa-file');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('baz', $item->icon());
// $path1 = m::mock(LfmPath::class);
// $path1->shouldReceive('path')->with('absolute')->andReturn('foo/bar');
// $path1->shouldReceive('isDirectory')->andReturn(false);
// $path1->shouldReceive('mimeType')->andReturn('image/png');
// $path3 = m::mock(LfmPath::class);
// $path3->shouldReceive('path')->with('absolute')->andReturn('foo/biz');
// $path3->shouldReceive('isDirectory')->andReturn(true);
// $this->assertEquals('fa-image', (new LfmItem($path1))->icon());
// $this->assertEquals('fa-folder-o', (new LfmItem($path3))->icon());
}
public function testHasThumb()
{
$this->lfm_path->shouldReceive('mimeType')->andReturn('application/plain');
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertFalse($item->hasThumb());
}
public function testHumanFilesize()
{
$item = new LfmItem($this->lfm_path, $this->lfm);
$this->assertEquals('1.00 kB', $item->humanFilesize(1024));
$this->assertEquals('1.00 MB', $item->humanFilesize(1024 ** 2));
$this->assertEquals('1.00 GB', $item->humanFilesize(1024 ** 3));
$this->assertEquals('1.00 TB', $item->humanFilesize(1024 ** 4));
$this->assertEquals('1.00 PB', $item->humanFilesize(1024 ** 5));
$this->assertEquals('1.00 EB', $item->humanFilesize(1024 ** 6));
}
}

View file

@ -0,0 +1,201 @@
<?php
namespace Tests;
use Illuminate\Http\Request;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use UniSharp\LaravelFilemanager\Lfm;
use UniSharp\LaravelFilemanager\LfmItem;
use UniSharp\LaravelFilemanager\LfmPath;
class LfmPathTest extends TestCase
{
public function tearDown()
{
m::close();
parent::tearDown();
}
public function testMagicGet()
{
$storage = m::mock(LfmStorage::class);
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getStorage')->with('files/bar')->andReturn($storage);
$helper->shouldReceive('getCategoryName')->andReturn('files');
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$helper->shouldReceive('ds')->andReturn('/');
$path = new LfmPath($helper);
$this->assertEquals($storage, $path->storage);
}
public function testMagicCall()
{
$storage = m::mock(LfmStorage::class);
$storage->shouldReceive('foo')->andReturn('bar');
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getStorage')->with('files/bar')->andReturn($storage);
$helper->shouldReceive('getCategoryName')->andReturn('files');
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$helper->shouldReceive('ds')->andReturn('/');
$path = new LfmPath($helper);
$this->assertEquals('bar', $path->foo());
}
public function testDirAndNormalizeWorkingDir()
{
$helper = m::mock(Lfm::class);
$helper->shouldReceive('input')->with('working_dir')->once()->andReturn('foo');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$path = new LfmPath($helper);
$this->assertEquals('foo', $path->normalizeWorkingDir());
$this->assertEquals('bar', $path->dir('bar')->normalizeWorkingDir());
}
public function testSetNameAndGetName()
{
$path = new LfmPath(m::mock(Lfm::class));
$path->setName('bar');
$this->assertEquals('bar', $path->getName());
}
public function testPath()
{
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getRootFolder')->andReturn('/foo');
$helper->shouldReceive('basePath')->andReturn(realpath(__DIR__ . '/../'));
$helper->shouldReceive('input')->with('working_dir')->andReturnNull();
$helper->shouldReceive('getCategoryName')->andReturn('files');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$helper->shouldReceive('ds')->andReturn('/');
$storage = m::mock(LfmStorage::class);
$storage->shouldReceive('rootPath')->andReturn(realpath(__DIR__ . '/../') . '/storage/app');
$helper->shouldReceive('getStorage')->andReturn($storage);
$path = new LfmPath($helper);
$this->assertEquals('files/foo', $path->path());
$this->assertEquals('files/foo/bar', $path->setName('bar')->path('storage'));
}
public function testUrl()
{
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getRootFolder')->andReturn('/foo');
$helper->shouldReceive('input')->with('working_dir')->andReturnNull();
$helper->shouldReceive('getCategoryName')->andReturn('files');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$helper->shouldReceive('ds')->andReturn('/');
$storage = m::mock(LfmStorage::class);
$storage->shouldReceive('url')->andReturn('/files/foo/foo');
$helper->shouldReceive('getStorage')->andReturn($storage);
$path = new LfmPath($helper);
$this->assertEquals('/files/foo/foo', $path->setName('foo')->url());
}
public function testFolders()
{
$storage = m::mock(LfmStorage::class);
$storage->shouldReceive('directories')->andReturn(['foo/bar']);
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getCategoryName')->andReturn('files');
$helper->shouldReceive('input')->with('working_dir')->andReturn('/shares');
$helper->shouldReceive('input')->with('sort_type')->andReturn('alphabetic');
$helper->shouldReceive('getStorage')->andReturn($storage);
$helper->shouldReceive('getNameFromPath')->andReturn('bar');
$helper->shouldReceive('getThumbFolderName')->andReturn('thumbs');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$helper->shouldReceive('ds')->andReturn('/');
$path = new LfmPath($helper);
$this->assertInstanceOf(LfmItem::class, $path->folders()[0]);
}
public function testFiles()
{
$storage = m::mock(LfmStorage::class);
$storage->shouldReceive('files')->andReturn(['foo/bar']);
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getCategoryName')->andReturn('files');
$helper->shouldReceive('input')->with('working_dir')->andReturn('/shares');
$helper->shouldReceive('input')->with('sort_type')->andReturn('alphabetic');
$helper->shouldReceive('getStorage')->andReturn($storage);
$helper->shouldReceive('getNameFromPath')->andReturn('bar');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$helper->shouldReceive('ds')->andReturn('/');
$path = new LfmPath($helper);
$this->assertInstanceOf(LfmItem::class, $path->files()[0]);
}
public function testPretty()
{
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getNameFromPath')->andReturn('bar');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$path = new LfmPath($helper);
$this->assertInstanceOf(LfmItem::class, $path->pretty('foo'));
}
public function testCreateFolder()
{
$storage = m::mock(LfmStorage::class);
$storage->shouldReceive('rootPath')->andReturn(realpath(__DIR__ . '/../') . '/storage/app');
$storage->shouldReceive('exists')->andReturn(false);
$storage->shouldReceive('makeDirectory')->andReturn(true);
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getStorage')->with('files/bar')->andReturn($storage);
$helper->shouldReceive('getCategoryName')->andReturn('files');
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$helper->shouldReceive('ds')->andReturn('/');
$path = new LfmPath($helper);
$this->assertNull($path->createFolder('bar'));
}
public function testCreateFolderButFolderAlreadyExists()
{
$storage = m::mock(LfmStorage::class);
$storage->shouldReceive('exists')->andReturn(true);
$storage->shouldReceive('makeDirectory')->andReturn(true);
$helper = m::mock(Lfm::class);
$helper->shouldReceive('getStorage')->with('files/bar')->andReturn($storage);
$helper->shouldReceive('getCategoryName')->andReturn('files');
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
$helper->shouldReceive('ds')->andReturn('/');
$path = new LfmPath($helper);
$this->assertFalse($path->createFolder('foo'));
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Tests;
use Illuminate\Support\Facades\Storage;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use UniSharp\LaravelFilemanager\Lfm;
use UniSharp\LaravelFilemanager\LfmPath;
use UniSharp\LaravelFilemanager\LfmStorageRepository;
class LfmStorageRepositoryTest extends TestCase
{
private $storage;
public function setUp()
{
parent::setUp();
$disk = m::mock('disk');
$disk->shouldReceive('getDriver')->andReturn($disk);
$disk->shouldReceive('getAdapter')->andReturn($disk);
$disk->shouldReceive('getPathPrefix')->andReturn('foo/bar');
$disk->shouldReceive('functionToCall')->with('foo/bar')->andReturn('baz');
$disk->shouldReceive('directories')->with('foo')->andReturn(['foo/bar']);
$disk->shouldReceive('move')->with('foo/bar', 'foo/bar/baz')->andReturn(true);
$helper = m::mock(Lfm::class);
$helper->shouldReceive('config')->with('disk')->andReturn('local');
Storage::shouldReceive('disk')->with('local')->andReturn($disk);
$this->storage = new LfmStorageRepository('foo/bar', $helper);
}
public function tearDown()
{
m::close();
}
public function testMagicCall()
{
$this->assertEquals('baz', $this->storage->functionToCall());
}
public function testRootPath()
{
$this->assertEquals('foo/bar', $this->storage->rootPath());
}
public function testMove()
{
$new_lfm_path = m::mock(LfmPath::class);
$new_lfm_path->shouldReceive('path')->with('storage')->andReturn('foo/bar/baz');
$this->assertTrue($this->storage->move($new_lfm_path));
}
}

View file

@ -0,0 +1,195 @@
<?php
namespace Tests;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Http\Request;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use UniSharp\LaravelFilemanager\Lfm;
use UniSharp\LaravelFilemanager\LfmFileRepository;
use UniSharp\LaravelFilemanager\LfmStorageRepository;
class LfmTest extends TestCase
{
public function tearDown()
{
m::close();
parent::tearDown();
}
public function testGetStorage()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.disk')->once()->andReturn('local');
$lfm = new Lfm($config);
$this->assertInstanceOf(LfmStorageRepository::class, $lfm->getStorage('foo/bar'));
}
public function testInput()
{
$request = m::mock(Request::class);
$request->shouldReceive('input')->with('foo')->andReturn('bar');
$lfm = new Lfm(m::mock(Config::class), $request);
$this->assertEquals('bar', $lfm->input('foo'));
}
public function testGetNameFromPath()
{
$this->assertEquals('bar', (new Lfm)->getNameFromPath('foo/bar'));
}
public function testAllowFolderType()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.allow_multi_user')->once()->andReturn(true);
$config->shouldReceive('get')->with('lfm.allow_multi_user')->once()->andReturn(false);
$config->shouldReceive('get')->with('lfm.allow_multi_user')->once()->andReturn(true);
$config->shouldReceive('get')->with('lfm.allow_share_folder')->once()->andReturn(false);
$lfm = new Lfm($config);
$this->assertTrue($lfm->allowFolderType('user'));
$this->assertTrue($lfm->allowFolderType('shared'));
$this->assertFalse($lfm->allowFolderType('shared'));
}
public function testGetCategoryName()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')
->with('lfm.folder_categories.file.folder_name', m::type('string'))
->once()
->andReturn('files');
$config->shouldReceive('get')
->with('lfm.folder_categories.image.folder_name', m::type('string'))
->once()
->andReturn('photos');
$config->shouldReceive('get')
->with('lfm.folder_categories')
->andReturn(['file' => [], 'image' => []]);
$request = m::mock(Request::class);
$request->shouldReceive('input')->with('type')->once()->andReturn('file');
$request->shouldReceive('input')->with('type')->once()->andReturn('image');
$lfm = new Lfm($config, $request);
$this->assertEquals('files', $lfm->getCategoryName('file'));
$this->assertEquals('photos', $lfm->getCategoryName('image'));
}
public function testCurrentLfmType()
{
$request = m::mock(Request::class);
$request->shouldReceive('input')->with('type')->once()->andReturn('file');
$request->shouldReceive('input')->with('type')->once()->andReturn('image');
$request->shouldReceive('input')->with('type')->once()->andReturn('foo');
$config = m::mock(Config::class);
$config->shouldReceive('get')
->with('lfm.folder_categories')
->andReturn(['file' => [], 'image' => []]);
$lfm = new Lfm($config, $request);
$this->assertEquals('file', $lfm->currentLfmType());
$this->assertEquals('image', $lfm->currentLfmType());
$this->assertEquals('file', $lfm->currentLfmType());
}
public function testGetUserSlug()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.user_folder_name')->once()->andReturn(function () {
return 'foo';
});
$lfm = new Lfm($config);
$this->assertEquals('foo', $lfm->getUserSlug());
}
public function testGetRootFolder()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.allow_multi_user')->andReturn(true);
$config->shouldReceive('get')->with('lfm.user_folder_name')->once()->andReturn(function () {
return 'foo';
});
$config->shouldReceive('get')->with('lfm.shared_folder_name')->once()->andReturn('bar');
$lfm = new Lfm($config);
$this->assertEquals('/foo', $lfm->getRootFolder('user'));
$this->assertEquals('/bar', $lfm->getRootFolder('shared'));
}
public function testGetThumbFolderName()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.thumb_folder_name')->once()->andReturn('foo');
$lfm = new Lfm($config);
$this->assertEquals('foo', $lfm->getThumbFolderName());
}
public function testGetFileIcon()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.file_icon_array.foo', m::type('string'))->once()->andReturn('fa-foo');
$config->shouldReceive('get')->with(m::type('string'), m::type('string'))->once()->andReturn('fa-file');
$lfm = new Lfm($config);
$this->assertEquals('fa-foo', $lfm->getFileIcon('foo'));
$this->assertEquals('fa-file', $lfm->getFileIcon('bar'));
}
public function testGetFileType()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.file_type_array.foo', m::type('string'))->once()->andReturn('foo');
$config->shouldReceive('get')->with(m::type('string'), m::type('string'))->once()->andReturn('File');
$lfm = new Lfm($config);
$this->assertEquals('foo', $lfm->getFileType('foo'));
$this->assertEquals('File', $lfm->getFileType('bar'));
}
public function testAllowMultiUser()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.allow_multi_user')->once()->andReturn(true);
$lfm = new Lfm($config);
$this->assertTrue($lfm->allowMultiUser());
}
public function testAllowShareFolder()
{
$config = m::mock(Config::class);
$config->shouldReceive('get')->with('lfm.allow_multi_user')->once()->andReturn(false);
$config->shouldReceive('get')->with('lfm.allow_multi_user')->once()->andReturn(true);
$config->shouldReceive('get')->with('lfm.allow_share_folder')->once()->andReturn(false);
$lfm = new Lfm($config);
$this->assertTrue($lfm->allowShareFolder());
$this->assertFalse($lfm->allowShareFolder());
}
public function testTranslateFromUtf8()
{
$input = 'test/測試';
$this->assertEquals($input, (new Lfm)->translateFromUtf8($input));
}
}

View file

@ -0,0 +1,169 @@
<?php
namespace Tests;
class TestCase extends \Orchestra\Testbench\TestCase
{
public function getPackageProviders($app)
{
return [
'Unisharp\Laravelfilemanager\LaravelFilemanagerServiceProvider',
'Unisharp\FileApi\FileApiServiceProvider',
];
}
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('lfm.use_package_routes', true);
$app['config']->set('lfm.middlewares', []);
$app['config']->set('lfm.prefix', 'laravel-filemanager');
$app['config']->set('lfm.urls_prefix', '');
$app['config']->set('lfm.allow_multi_user', true);
$app['config']->set('lfm.allow_share_folder', true);
$app['config']->set('lfm.user_field', TestConfigHandler::class);
$app['config']->set('lfm.base_directory', 'public');
$app['config']->set('lfm.images_folder_name', 'photos');
$app['config']->set('lfm.files_folder_name', 'files');
$app['config']->set('lfm.shared_folder_name', 'shares');
$app['config']->set('lfm.thumb_folder_name', 'thumbs');
$app['config']->set('lfm.images_startup_view', 'grid');
$app['config']->set('lfm.files_startup_view', 'list');
$app['config']->set('lfm.rename_file', false);
$app['config']->set('lfm.alphanumeric_filename', true);
$app['config']->set('lfm.alphanumeric_directory', false);
$app['config']->set('lfm.should_validate_size', false);
$app['config']->set('lfm.max_image_size', 50000);
$app['config']->set('lfm.max_file_size', 50000);
$app['config']->set('lfm.should_validate_mime', false);
$app['config']->set('lfm.valid_image_mimetypes', [
'image/jpeg',
'image/pjpeg',
'image/png',
'image/gif',
'image/svg+xml',
]);
$app['config']->set('lfm.valid_file_mimetypes', [
'image/jpeg',
'image/pjpeg',
'image/png',
'image/gif',
'image/svg+xml',
'application/pdf',
'text/plain',
]);
$app['config']->set('lfm.thumb_img_width', 200);
$app['config']->set('lfm.thumb_img_height', 200);
$app['config']->set('lfm.file_type_array', [
'pdf' => 'Adobe Acrobat',
'doc' => 'Microsoft Word',
'docx' => 'Microsoft Word',
'xls' => 'Microsoft Excel',
'xlsx' => 'Microsoft Excel',
'zip' => 'Archive',
'gif' => 'GIF Image',
'jpg' => 'JPEG Image',
'jpeg' => 'JPEG Image',
'png' => 'PNG Image',
'ppt' => 'Microsoft PowerPoint',
'pptx' => 'Microsoft PowerPoint',
]);
$app['config']->set('lfm.file_icon_array', [
'pdf' => 'fa-file-pdf-o',
'doc' => 'fa-file-word-o',
'docx' => 'fa-file-word-o',
'xls' => 'fa-file-excel-o',
'xlsx' => 'fa-file-excel-o',
'zip' => 'fa-file-archive-o',
'gif' => 'fa-file-image-o',
'jpg' => 'fa-file-image-o',
'jpeg' => 'fa-file-image-o',
'png' => 'fa-file-image-o',
'ppt' => 'fa-file-powerpoint-o',
'pptx' => 'fa-file-powerpoint-o',
]);
$app['config']->set('lfm.php_ini_overrides', [
'memory_limit' => '256M',
]);
$app['config']->set('fileapi.path', ['/images/event/']);
$app['config']->set('fileapi.watermark', 'public/img/watermark.png');
$app['config']->set('fileapi.default_thumbs', ['S' => '96x96', 'M' => '256x256', 'L' => '480x480']);
$app['config']->set('fileapi.compress_quality', 90);
}
public function getResponseByRouteName($route_name, $input = [], $file = [])
{
$response = $this->call('GET', route('unisharp.lfm.' . $route_name), $input, $file);
$data = json_encode($response);
return $response->getContent();
}
protected function getPackageAliases($app)
{
return [
'Image' => 'Intervention\Image\Facades\Image',
];
}
public function getStoragedFilePathWithThumb($filename, $filename_s, $working_dir)
{
$files_path['file'] = $this->getStoragedFilePath($filename, $working_dir);
$files_path['file_s'] = $this->getStoragedFilePath($filename_s, $working_dir);
return $files_path;
}
public function getStoragedFilePath($filename, $working_dir)
{
return storage_path(implode(DIRECTORY_SEPARATOR, [
'app',
config('lfm.base_directory'),
config('lfm.files_folder_name'),
$working_dir,
$filename,
]));
}
public function unlinkFiles(array $files_path)
{
foreach ($files_path as $file_path) {
@unlink($file_path);
}
}
public function test()
{
$this->assertEquals(1, 1);
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace Tests;
class TestConfigHandler
{
public function userField()
{
return '1';
}
}