Skip to content

Commit

Permalink
added test and edited fileUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Feb 14, 2018
1 parent b9edb05 commit d565e09
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 19 deletions.
30 changes: 16 additions & 14 deletions src/File/FileUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(ContaoFrameworkInterface $framework)
*/
public function getUniqueFileNameWithinTarget($target, $prefix = null, $i = 0)
{
$file = new File($target, true);
$file = new File($target);

$target = ltrim(str_replace(TL_ROOT, '', $target), '/');
$path = str_replace('.'.$file->extension, '', $target);
Expand Down Expand Up @@ -108,8 +108,7 @@ public function getFileList($dir, $baseUrl, $protectedBaseUrl = null)
}

$fileArray['path'] = str_replace($fileArray['filename'], '', $fileArray['absUrl']);
$fileArray['filesize'] =
$this->formatSizeUnits(filesize(str_replace('\\', '/', str_replace('//', '', $dir.'/'.$file))), true);
$fileArray['filesize'] = $this->formatSizeUnits(filesize(str_replace('\\', '/', str_replace('//', '', $dir.'/'.$file))), true);

$results[] = $fileArray;
}
Expand All @@ -123,7 +122,7 @@ public function getFileList($dir, $baseUrl, $protectedBaseUrl = null)
return $results;
}

public function formatSizeUnits($bytes, $keepTogether = false)
public function formatSizeUnits(int $bytes, $keepTogether = false)
{
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2).($keepTogether ? ' ' : ' ').'GB';
Expand All @@ -146,6 +145,10 @@ public function getPathWithoutFilename($pathToFile)
{
$path = pathinfo($pathToFile);

if (!isset($path['dirname'])) {
return '';
}

return $path['dirname'];
}

Expand Down Expand Up @@ -181,14 +184,14 @@ public function getPathFromUuid($uuid, $checkIfExists = true)
*
* @return File|null Return the file object
*/
public function getFileFromUuid($uuid, $doNotCreate = true)
public function getFileFromUuid($uuid)
{
if ($path = $this->getPathFromUuid($uuid)) {
if (is_dir(TL_ROOT.DIRECTORY_SEPARATOR.$path)) {
return null;
}

return new File($path, $doNotCreate);
return new File($path);
}
}

Expand All @@ -198,10 +201,10 @@ public function getFileFromUuid($uuid, $doNotCreate = true)
*
* @return bool|Folder Return the folder object
*/
public function getFolderFromUuid($uuid, $doNotCreate = true)
public function getFolderFromUuid($uuid)
{
if ($path = $this->getPathFromUuid($uuid)) {
return new Folder($path, $doNotCreate);
return new Folder($path);
}

return false;
Expand All @@ -219,12 +222,11 @@ public function getFolderFromUuid($uuid, $doNotCreate = true)
*/
public function addUniqueIdToFilename($fileName, $prefix = null, $moreEntropy = true)
{
$file = new File($fileName, true);
$file = new File($fileName);

$directory = ltrim(str_replace(TL_ROOT, '', $file->dirname), '/');

return ($directory ? $directory.'/' : '').$file->filename.uniqid($prefix, $moreEntropy).
($file->extension ? '.'.$file->extension : '');
return ($directory ? $directory.'/' : '').$file->filename.uniqid($prefix, $moreEntropy).($file->extension ? '.'.$file->extension : '');
}

/**
Expand All @@ -238,7 +240,7 @@ public function addUniqueIdToFilename($fileName, $prefix = null, $moreEntropy =
*/
public function sanitizeFileName($fileName, $maxCount = 0, $preserveUppercase = false)
{
$file = new \File($fileName, true);
$file = new File($fileName);

$name = $file->filename;

Expand Down Expand Up @@ -276,7 +278,7 @@ public function sendTextAsFileToBrowser($content, $fileName)
*
* @return mixed|null The folder path or null
*/
public function getFolderFromDca($folder, DataContainer $dc = null, $doNotCreate = true)
public function getFolderFromDca($folder, DataContainer $dc = null)
{
// upload folder
if (is_array($folder) && null !== $dc) {
Expand All @@ -300,7 +302,7 @@ public function getFolderFromDca($folder, DataContainer $dc = null, $doNotCreate
}

if (Validator::isUuid($folder)) {
$folderObj = $this->getFolderFromUuid($folder, $doNotCreate);
$folderObj = $this->getFolderFromUuid($folder);
$folder = $folderObj->value;
}

Expand Down
24 changes: 19 additions & 5 deletions src/Url/UrlUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Contao\Controller;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\Environment;
use Contao\Model;
use Contao\PageModel;
use Contao\System;

Expand Down Expand Up @@ -48,10 +47,17 @@ public function getCurrentUrl(array $options)
*/
public function addQueryString($query, $url = null)
{
$queryString = '';
$url = static::prepareUrl($url);
$query = trim(ampersand($query, false), '&');

list($script, $queryString) = explode('?', $url, 2);
$explodedUrl = explode('?', $url, 2);

if (2 === count($explodedUrl)) {
list($script, $queryString) = $explodedUrl;
} else {
list($script) = $explodedUrl;
}

parse_str($queryString, $queries);

Expand Down Expand Up @@ -86,7 +92,15 @@ public function removeQueryString(array $params, $url = null)
return $strUrl;
}

list($script, $queryString) = explode('?', $strUrl, 2);
$explodedUrl = explode('?', $url, 2);

if (2 === count($explodedUrl)) {
list($script, $queryString) = $explodedUrl;
} else {
list($script) = $explodedUrl;

return $script;
}

parse_str($queryString, $queries);

Expand All @@ -112,8 +126,8 @@ public function getJumpToPageObject(int $jumpTo, bool $fallbackToObjPage = true)
{
global $objPage;

if ($jumpTo && $jumpTo != $objPage->id && null !== ($jumpToPage =
System::getContainer()->get('huh.utils.model')->findModelInstanceByPk('tl_page', $jumpTo))) {
if ($jumpTo && $jumpTo != $objPage->id
&& null !== ($jumpToPage = System::getContainer()->get('huh.utils.model')->findModelInstanceByPk('tl_page', $jumpTo))) {
return $jumpToPage;
}

Expand Down
149 changes: 149 additions & 0 deletions tests/File/FileUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\UtilsBundle\Tests\File;

use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\Arrays\ArrayUtil;
use HeimrichHannot\UtilsBundle\File\FileUtil;
use Symfony\Component\Filesystem\Filesystem;

class FileUtilTest extends ContaoTestCase
{
public static function tearDownAfterClass(): void
{
// The temporary directory would not be removed without this call!
parent::tearDownAfterClass();
}

public function setUp()
{
parent::setUp();

if (!defined('TL_ROOT')) {
\define('TL_ROOT', __DIR__);
}

$fs = new Filesystem();
$fs->mkdir($this->getTempDir().'/files/');

$arrayUtils = new ArrayUtil($this->mockContaoFramework());
$container = $this->mockContainer();
$container->set('huh.utils.array', $arrayUtils);
System::setContainer($container);
}

public function testGetFileList()
{
file_put_contents($this->getTempDir().'/files/testfile1', 'test');
file_put_contents($this->getTempDir().'/files/testfile2', 'test');
file_put_contents($this->getTempDir().'/files/testfile3', 'test');

$framework = $this->mockContaoFramework();
$fileUtil = new FileUtil($framework);
$fileList = $fileUtil->getFileList($this->getTempDir().'/files', __DIR__);

$this->assertCount(3, $fileList);
$this->assertArrayHasKey(0, $fileList);
$this->assertArrayHasKey('filename', $fileList[0]);
$this->assertSame('testfile3', $fileList[0]['filename']);
$this->assertArrayHasKey(1, $fileList);
$this->assertArrayHasKey('filename', $fileList[1]);
$this->assertSame('testfile2', $fileList[1]['filename']);
$this->assertArrayHasKey(2, $fileList);
$this->assertArrayHasKey('filename', $fileList[2]);
$this->assertSame('testfile1', $fileList[2]['filename']);

$fileList = $fileUtil->getFileList($this->getTempDir().'/fileList', __DIR__);

$this->assertCount(0, $fileList);
}

public function testGetUniqueFileNameWithinTarget()
{
$framework = $this->mockContaoFramework();
$fileUtil = new FileUtil($framework);
$fileName = $fileUtil->getUniqueFileNameWithinTarget($this->getTempDir().'/files/testfile.txt');

$this->assertFalse($fileName);
}

public function testFormatSizeUnits()
{
$framework = $this->mockContaoFramework();
$fileUtil = new FileUtil($framework);

$bytes = $fileUtil->formatSizeUnits(1073741824);
$this->assertSame('1.00 GB', $bytes);
$bytes = $fileUtil->formatSizeUnits(1048576);
$this->assertSame('1.00 MB', $bytes);
$bytes = $fileUtil->formatSizeUnits(1024);
$this->assertSame('1.00 KB', $bytes);
$bytes = $fileUtil->formatSizeUnits(3);
$this->assertSame('3 Bytes', $bytes);
$bytes = $fileUtil->formatSizeUnits(1);
$this->assertSame('1 Byte', $bytes);
$bytes = $fileUtil->formatSizeUnits(10737.41824);
$this->assertSame('10.49 KB', $bytes);
try {
$bytes = $fileUtil->formatSizeUnits('107374,1824');
} catch (\Exception $exception) {
$this->assertSame('A non well formed numeric value encountered', $exception->getMessage());
}
$bytes = $fileUtil->formatSizeUnits(1073741894, true);
$this->assertSame('1.00&nbsp;GB', $bytes);
$bytes = $fileUtil->formatSizeUnits(0.1073741894, true);
$this->assertSame('0&nbsp;Bytes', $bytes);
}

public function testGetPathWithoutFilename()
{
$framework = $this->mockContaoFramework();
$fileUtil = new FileUtil($framework);
$path = $fileUtil->getPathWithoutFilename($this->getTempDir().'/file/testfile1');
$this->assertSame($this->getTempDir().'/file', $path);

$path = $fileUtil->getPathWithoutFilename('');
$this->assertSame('', $path);

$path = $fileUtil->getPathWithoutFilename(1234);
$this->assertSame('.', $path);
}

public function testGetFileExtension()
{
$framework = $this->mockContaoFramework();
$fileUtil = new FileUtil($framework);
$fileExtension = $fileUtil->getFileExtension($this->getTempDir().'/file/testfile1');
$this->assertSame('', $fileExtension);
$fileExtension = $fileUtil->getFileExtension($this->getTempDir().'/file/testfile1.txt');
$this->assertSame('txt', $fileExtension);
$fileExtension = $fileUtil->getFileExtension($this->getTempDir().'/file/testfile1.xml');
$this->assertSame('xml', $fileExtension);
$fileExtension = $fileUtil->getFileExtension($this->getTempDir().'/file/testfile1...xml');
$this->assertSame('xml', $fileExtension);
$fileExtension = $fileUtil->getFileExtension($this->getTempDir().'');
$this->assertSame('', $fileExtension);
$fileExtension = $fileUtil->getFileExtension($this->getTempDir().'.xml');
$this->assertSame('xml', $fileExtension);
$fileExtension = $fileUtil->getFileExtension('');
$this->assertSame('', $fileExtension);
$fileExtension = $fileUtil->getFileExtension(1234);
$this->assertSame('', $fileExtension);
}

public function testAddUniqueIdToFilename()
{
$framework = $this->mockContaoFramework();
$fileUtil = new FileUtil($framework);

$file = $fileUtil->addUniqueIdToFilename('testFile');
$this->assertNotSame('testFile', $file);
}
}
Loading

0 comments on commit d565e09

Please sign in to comment.