2010-05-10 23:32:02 +01:00
|
|
|
<?php
|
2019-07-12 16:31:14 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2010-05-10 23:32:02 +01:00
|
|
|
|
2019-07-12 16:31:14 +01:00
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
if (!defined('INSTALLDIR')) {
|
|
|
|
define('INSTALLDIR', dirname(dirname(__DIR__)));
|
|
|
|
}
|
2019-07-25 01:29:20 +01:00
|
|
|
if (!defined('PUBLICDIR')) {
|
|
|
|
define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
|
|
|
|
}
|
2019-07-12 16:31:14 +01:00
|
|
|
if (!defined('GNUSOCIAL')) {
|
|
|
|
define('GNUSOCIAL', true);
|
|
|
|
}
|
|
|
|
if (!defined('STATUSNET')) { // Compatibility
|
|
|
|
define('STATUSNET', true);
|
2010-05-10 23:32:02 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:31:14 +01:00
|
|
|
use ClientException;
|
|
|
|
use Exception;
|
|
|
|
use MediaFile;
|
2020-09-04 11:15:23 +01:00
|
|
|
use TemporaryFile;
|
2019-07-12 16:31:14 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use ServerException;
|
2010-05-10 23:32:02 +01:00
|
|
|
|
2019-08-23 13:36:02 +01:00
|
|
|
require_once INSTALLDIR . '/lib/util/common.php';
|
2010-05-10 23:32:02 +01:00
|
|
|
|
2019-07-12 16:31:14 +01:00
|
|
|
final class MediaFileTest extends TestCase
|
2010-05-10 23:32:02 +01:00
|
|
|
{
|
2020-06-24 13:08:11 +01:00
|
|
|
protected function setup(): void
|
2010-05-10 23:32:02 +01:00
|
|
|
{
|
|
|
|
$this->old_attachments_supported = common_config('attachments', 'supported');
|
|
|
|
$GLOBALS['config']['attachments']['supported'] = true;
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:08:11 +01:00
|
|
|
protected function tearDown(): void
|
2010-05-10 23:32:02 +01:00
|
|
|
{
|
|
|
|
$GLOBALS['config']['attachments']['supported'] = $this->old_attachments_supported;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider fileTypeCases
|
2020-06-24 13:08:11 +01:00
|
|
|
*
|
2019-07-12 16:31:14 +01:00
|
|
|
* @param $filename
|
|
|
|
* @param $expectedType
|
2020-06-24 13:08:11 +01:00
|
|
|
*
|
2019-07-12 16:31:14 +01:00
|
|
|
* @throws ClientException
|
|
|
|
* @throws ServerException
|
2010-05-10 23:32:02 +01:00
|
|
|
*/
|
2014-03-08 02:03:04 +00:00
|
|
|
public function testMimeType($filename, $expectedType)
|
2010-05-10 23:32:02 +01:00
|
|
|
{
|
|
|
|
if (!file_exists($filename)) {
|
2020-06-24 13:08:11 +01:00
|
|
|
throw new Exception("Test file {$filename} missing");
|
2010-05-10 23:32:02 +01:00
|
|
|
}
|
2010-05-11 00:18:29 +01:00
|
|
|
|
2014-03-08 02:03:04 +00:00
|
|
|
$type = MediaFile::getUploadedMimeType($filename, basename($filename));
|
2020-06-24 13:08:11 +01:00
|
|
|
static::assertSame($expectedType, $type);
|
2010-05-11 00:18:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider fileTypeCases
|
2020-06-24 13:08:11 +01:00
|
|
|
*
|
2019-07-12 16:31:14 +01:00
|
|
|
* @param $filename
|
|
|
|
* @param $expectedType
|
2020-06-24 13:08:11 +01:00
|
|
|
*
|
2019-07-12 16:31:14 +01:00
|
|
|
* @throws ClientException
|
|
|
|
* @throws ServerException
|
2010-05-11 00:18:29 +01:00
|
|
|
*/
|
2014-03-08 02:03:04 +00:00
|
|
|
public function testUploadedMimeType($filename, $expectedType)
|
2010-05-11 00:18:29 +01:00
|
|
|
{
|
|
|
|
if (!file_exists($filename)) {
|
2020-06-24 13:08:11 +01:00
|
|
|
throw new Exception("WTF? {$filename} test file missing");
|
2010-05-11 00:18:29 +01:00
|
|
|
}
|
2020-09-04 11:15:23 +01:00
|
|
|
$tempfile = new TemporaryFile('gs-mediafiletest');
|
|
|
|
fwrite($tempfile->getResource(), file_get_contents($filename));
|
|
|
|
fflush($tempfile->getResource());
|
2010-05-11 00:18:29 +01:00
|
|
|
|
2020-09-04 11:15:23 +01:00
|
|
|
$type = MediaFile::getUploadedMimeType($tempfile->getRealPath(), basename($filename));
|
2020-06-24 13:08:11 +01:00
|
|
|
static::assertSame($expectedType, $type);
|
2010-05-10 23:32:02 +01:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:08:11 +01:00
|
|
|
public static function fileTypeCases()
|
2010-05-10 23:32:02 +01:00
|
|
|
{
|
2020-06-24 13:08:11 +01:00
|
|
|
$base = __DIR__;
|
|
|
|
$dir = "{$base}/sample-uploads";
|
|
|
|
$files = [
|
|
|
|
'image.png' => 'image/png',
|
|
|
|
'image.gif' => 'image/gif',
|
|
|
|
'image.jpg' => 'image/jpeg',
|
|
|
|
'image.jpeg' => 'image/jpeg',
|
|
|
|
'office.pdf' => 'application/pdf',
|
|
|
|
'wordproc.odt' => 'application/vnd.oasis.opendocument.text',
|
|
|
|
'wordproc.ott' => 'application/vnd.oasis.opendocument.text-template',
|
|
|
|
'wordproc.doc' => 'application/msword',
|
|
|
|
'wordproc.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
|
|
'wordproc.rtf' => 'text/rtf',
|
|
|
|
'spreadsheet.ods' => 'application/vnd.oasis.opendocument.spreadsheet',
|
|
|
|
'spreadsheet.ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
|
|
|
|
'spreadsheet.xls' => 'application/vnd.ms-excel',
|
|
|
|
'spreadsheet.xlt' => 'application/vnd.ms-excel',
|
|
|
|
'spreadsheet.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
|
|
'presentation.odp' => 'application/vnd.oasis.opendocument.presentation',
|
|
|
|
'presentation.otp' => 'application/vnd.oasis.opendocument.presentation-template',
|
|
|
|
'presentation.ppt' => 'application/vnd.ms-powerpoint',
|
|
|
|
'presentation.pptx' => 'application/zip', //"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
|
|
];
|
2010-05-11 00:18:29 +01:00
|
|
|
|
2020-06-24 13:08:11 +01:00
|
|
|
$dataset = [];
|
2010-05-11 00:18:29 +01:00
|
|
|
foreach ($files as $file => $type) {
|
2020-06-24 13:08:11 +01:00
|
|
|
$dataset[] = ["{$dir}/{$file}", $type];
|
2010-05-11 00:18:29 +01:00
|
|
|
}
|
|
|
|
return $dataset;
|
2010-05-10 23:32:02 +01:00
|
|
|
}
|
|
|
|
}
|