2015-12-06 18:02:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-10-09 05:16:18 +00:00
|
|
|
use System\Classes\MediaLibrary;
|
2015-12-06 18:02:51 +00:00
|
|
|
|
|
|
|
|
class MediaLibraryTest extends TestCase // @codingStandardsIgnoreLine
|
|
|
|
|
{
|
2020-05-26 09:20:41 +00:00
|
|
|
protected function tearDown()
|
|
|
|
|
{
|
|
|
|
|
$this->removeMedia();
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-06 18:02:51 +00:00
|
|
|
public function invalidPathsProvider()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
['./file'],
|
|
|
|
|
['../secret'],
|
|
|
|
|
['.../secret'],
|
|
|
|
|
['/../secret'],
|
|
|
|
|
['/.../secret'],
|
|
|
|
|
['/secret/..'],
|
|
|
|
|
['file/../secret'],
|
|
|
|
|
['file/..'],
|
|
|
|
|
['......./secret'],
|
|
|
|
|
['./file'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validPathsProvider()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
['file'],
|
|
|
|
|
['folder/file'],
|
|
|
|
|
['/file'],
|
|
|
|
|
['/folder/file'],
|
|
|
|
|
['/.file'],
|
|
|
|
|
['/..file'],
|
|
|
|
|
['/...file'],
|
|
|
|
|
['file.ext'],
|
|
|
|
|
['file..ext'],
|
|
|
|
|
['file...ext'],
|
2018-09-12 17:37:21 +00:00
|
|
|
['one,two.ext'],
|
|
|
|
|
['one(two)[].ext'],
|
|
|
|
|
['one=(two)[].ext'],
|
|
|
|
|
['one_(two)[].ext'],
|
2019-12-05 08:44:04 +00:00
|
|
|
/*
|
|
|
|
|
Example of a unicode-based filename with a single quote
|
|
|
|
|
@see: https://github.com/octobercms/october/pull/4564
|
|
|
|
|
*/
|
|
|
|
|
['BG中国通讯期刊(Blend\'r)创刊号.pdf'],
|
2015-12-06 18:02:51 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider invalidPathsProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testInvalidPathsOnValidatePath($path)
|
|
|
|
|
{
|
2018-06-04 22:48:45 +00:00
|
|
|
$this->expectException('ApplicationException');
|
2015-12-06 18:02:51 +00:00
|
|
|
MediaLibrary::validatePath($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider validPathsProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testValidPathsOnValidatePath($path)
|
|
|
|
|
{
|
2018-06-04 22:48:45 +00:00
|
|
|
$result = MediaLibrary::validatePath($path);
|
|
|
|
|
$this->assertInternalType('string', $result);
|
2015-12-06 18:02:51 +00:00
|
|
|
}
|
2020-05-26 09:20:41 +00:00
|
|
|
|
|
|
|
|
public function testListFolderContents()
|
|
|
|
|
{
|
|
|
|
|
$this->setUpStorage();
|
|
|
|
|
$this->copyMedia();
|
|
|
|
|
|
|
|
|
|
$contents = MediaLibrary::instance()->listFolderContents();
|
|
|
|
|
$this->assertNotEmpty($contents, 'Media library item is not discovered');
|
|
|
|
|
|
|
|
|
|
$item = reset($contents);
|
|
|
|
|
$this->assertAttributeEquals('file', 'type', $item, 'Media library item does not have the right type');
|
|
|
|
|
$this->assertAttributeEquals('/text.txt', 'path', $item, 'Media library item does not have the right path');
|
|
|
|
|
$this->assertAttributeNotEmpty('lastModified', $item, 'Media library item last modified is empty');
|
|
|
|
|
$this->assertAttributeNotEmpty('size', $item, 'Media library item size is empty');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function setUpStorage()
|
|
|
|
|
{
|
|
|
|
|
$this->app->useStoragePath(base_path('storage/temp'));
|
|
|
|
|
|
|
|
|
|
config(['filesystems.disks.test_local' => [
|
|
|
|
|
'driver' => 'local',
|
|
|
|
|
'root' => storage_path('app'),
|
|
|
|
|
]]);
|
|
|
|
|
|
|
|
|
|
config(['cms.storage.media' => [
|
|
|
|
|
'disk' => 'test_local',
|
|
|
|
|
'folder' => 'media',
|
|
|
|
|
'path' => '/storage/app/media',
|
|
|
|
|
]]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function copyMedia()
|
|
|
|
|
{
|
|
|
|
|
$mediaPath = storage_path('app/media');
|
|
|
|
|
|
|
|
|
|
if (!is_dir($mediaPath)) {
|
|
|
|
|
mkdir($mediaPath, 0777, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (glob(base_path('tests/fixtures/media/*')) as $file) {
|
|
|
|
|
$path = pathinfo($file);
|
|
|
|
|
copy($file, $mediaPath . DIRECTORY_SEPARATOR . $path['basename']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function removeMedia()
|
|
|
|
|
{
|
|
|
|
|
if ($this->app->storagePath() !== base_path('storage/temp')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (glob(storage_path('app/media/*')) as $file) {
|
|
|
|
|
unlink($file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rmdir(storage_path('app/media'));
|
|
|
|
|
rmdir(storage_path('app'));
|
|
|
|
|
}
|
2015-12-06 18:02:51 +00:00
|
|
|
}
|