Skip to content

Commit

Permalink
fix RenameUpload filter, removing declare(strict_types=1);
Browse files Browse the repository at this point in the history
  • Loading branch information
armenio committed Oct 5, 2021
1 parent 6ee5424 commit bcfb685
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 62 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"laminas/laminas-mvc-form": "^1.1"
},
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"laminas/laminas-coding-standard": "^2.3",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.3"
},
Expand Down
24 changes: 22 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
<?xml version="1.0"?>
<ruleset name="Laminas coding standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>
<ruleset name="Laminas Coding Standard">
<description>Laminas Coding Standard</description>

<!-- display progress -->
<arg value="p"/>
<arg name="colors"/>
<arg name="extensions" value="php,dist,phtml"/>

<!-- inherit rules from: -->
<rule ref="PSR12"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Generic.Formatting.SpaceAfterNot"/>
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
<properties>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="false"/>
</properties>
</rule>

<!-- Paths to check -->
<file>src</file>
Expand Down
2 changes: 0 additions & 2 deletions src/Filter/ArrayKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\Filter;

use Laminas\Filter\AbstractFilter;
Expand Down
33 changes: 14 additions & 19 deletions src/Filter/File/RenameUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\Filter\File;

use Laminas\Filter\Exception;
Expand Down Expand Up @@ -68,27 +66,24 @@ public function __construct($targetOrOptions = [])
protected function getFinalTarget($source, $clientFileName)
{
$target = $this->getTarget();
if ($target === null || $target === '*') {
$target = $source;
}

if (null !== $target && '*' !== $target) {
$last = $target[strlen($target) - 1];
if (! is_dir($target)
&& ! file_exists($target)
&& ($last === '/' || $last === '\\')
) {
ErrorHandler::start();
mkdir($target, 0755, true);
$warningException = ErrorHandler::stop();
if (! is_dir($target)) {
throw new Exception\RuntimeException(
sprintf('Could not create target directory: %s', $target),
0,
$warningException
);
}
if (! is_dir($target)) {
ErrorHandler::start();
mkdir($target, 0755, true);
$warningException = ErrorHandler::stop();
if (! is_dir($target)) {
throw new Exception\RuntimeException(
sprintf('Could not create target directory: %s', $target),
0,
$warningException
);
}
}

if ($this->getSanitize() && $this->getUseUploadName()) {
if ($this->getUseUploadName() && $this->getSanitize()) {
$clientFileName = $this->sanitizeFilename($clientFileName);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Filter/StringReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\Filter;

use Laminas\Filter\AbstractFilter;
Expand Down
2 changes: 0 additions & 2 deletions src/I18n/Filter/DateTimeParse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\I18n\Filter;

use Laminas\Filter\AbstractFilter;
Expand Down
2 changes: 0 additions & 2 deletions src/I18n/Form/Element/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\I18n\Form\Element;

use Laminas\Form\Element\Date as VendorDate;
Expand Down
2 changes: 0 additions & 2 deletions src/I18n/Form/Element/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\I18n\Form\Element;

use Laminas\Form\Element\DateTime as VendorDateTime;
Expand Down
2 changes: 0 additions & 2 deletions src/I18n/Form/Element/DateTimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\I18n\Form\Element;

use Laminas\I18n\Validator\DateTime as DateTimeI18nValidator;
Expand Down
2 changes: 0 additions & 2 deletions src/I18n/Form/Element/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\I18n\Form\Element;

use Laminas\Form\Element\Number as VendorNumber;
Expand Down
2 changes: 0 additions & 2 deletions src/I18n/Form/Element/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace Armenio\I18n\Form\Element;

use Laminas\Form\Element\Time as VendorTime;
Expand Down
2 changes: 0 additions & 2 deletions test/Filter/ArrayKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace ArmenioTest\Filter;

use Armenio\Filter\ArrayKey as ArrayKeyFilter;
Expand Down
2 changes: 0 additions & 2 deletions test/Filter/File/RenameUploadMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace ArmenioTest\Filter\File;

use Armenio\Filter\File\RenameUpload;
Expand Down
78 changes: 60 additions & 18 deletions test/Filter/File/RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace ArmenioTest\Filter\File;

use Armenio\Filter\File\RenameUpload as RenameUploadFilter;
Expand All @@ -28,6 +26,11 @@ class RenameUploadTest extends TestCase
*/
protected $filesPath;

/**
* @var string
*/
protected $sourceFilename = 'tést file Not^Sanitizêd@.txt';

/**
* @var string
*/
Expand All @@ -43,15 +46,26 @@ class RenameUploadTest extends TestCase
*/
protected $targetPathFile;

/**
* @var string
*/
protected $targetFilenameSanitized = 'tést_file_notsanitizêd.txt';

/**
* @var string
*/
protected $targetPathFileSanitized;

public function setUp(): void
{
$this->filesPath = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, uniqid('laminasilter'));
$this->targetPath = sprintf('%s%s%s', $this->filesPath, DIRECTORY_SEPARATOR, 'targetPath');

mkdir($this->targetPath, 0775, true);

$this->sourceFile = $this->filesPath . DIRECTORY_SEPARATOR . 'tést file Not^Sanitizêd@.txt';
$this->targetPathFile = $this->targetPath . DIRECTORY_SEPARATOR . 'tést_file_notsanitizêd.txt';
$this->sourceFile = $this->filesPath . DIRECTORY_SEPARATOR . $this->sourceFilename;
$this->targetPathFile = $this->targetPath . DIRECTORY_SEPARATOR . $this->sourceFilename;
$this->targetPathFileSanitized = $this->targetPath . DIRECTORY_SEPARATOR . $this->targetFilenameSanitized;

touch($this->sourceFile);
}
Expand Down Expand Up @@ -106,37 +120,65 @@ public function testGetSetOptions()
/**
* @return void
*/
public function testTargetDirectoryWillNotBeCreated()
public function testTargetDirectoryWillBeCreatedWithoutUseUploadName()
{
$filter = new RenameUploadMock($this->targetPath . '/testTargetDirectoryWillBeCreatedWithoutUseUploadName');
$filter->setUseUploadName(false);
$filter->filter($this->sourceFile);
$this->assertFalse($filter->getUseUploadName());
$this->assertTrue(is_dir($this->targetPath . '/testTargetDirectoryWillBeCreatedWithoutUseUploadName'));
$this->assertTrue(file_exists($this->targetPath . '/testTargetDirectoryWillBeCreatedWithoutUseUploadName/' . $this->sourceFilename));
}

/**
* @return void
*/
public function testTargetDirectoryWillBeCreatedWithUseUploadName()
{
$filter = new RenameUploadFilter($this->targetPath . '/targetDirectoryWillNotExists');
$filter->filter('falsefile');
$this->assertFalse(is_dir($this->targetPath . '/targetDirectoryWillNotExists'));
$filter = new RenameUploadMock($this->targetPath . '/testTargetDirectoryWillBeCreatedWithUseUploadName');
$filter->setUseUploadName(true);
$filter->filter($this->sourceFile);
$this->assertTrue($filter->getUseUploadName());
$this->assertTrue(is_dir($this->targetPath . '/testTargetDirectoryWillBeCreatedWithUseUploadName'));
$this->assertTrue(file_exists($this->targetPath . '/testTargetDirectoryWillBeCreatedWithUseUploadName/' . $this->sourceFilename));
}

/**
* @return void
*/
public function testTargetDirectoryWillBeCreated()
public function testTargetDirectoryWillBeCreatedWithUseUploadNameAndSanitize()
{
$filter = new RenameUploadFilter($this->targetPath . '/targetDirectoryWillExists/');
$filter->filter('falsefile');
$this->assertTrue(is_dir($this->targetPath . '/targetDirectoryWillExists/'));
$filter = new RenameUploadMock($this->targetPath . '/testTargetDirectoryWillBeCreatedWithUseUploadNameWithoutBar');
$filter->setUseUploadName(true);
$filter->setSanitize(true);
$filter->filter($this->sourceFile);
$this->assertTrue($filter->getUseUploadName());
$this->assertTrue($filter->getSanitize());
$this->assertTrue(is_dir($this->targetPath . '/testTargetDirectoryWillBeCreatedWithUseUploadNameWithoutBar'));
$this->assertTrue(file_exists($this->targetPath . '/testTargetDirectoryWillBeCreatedWithUseUploadNameWithoutBar/' . $this->targetFilenameSanitized));
}

/**
* @return void
*/
public function testTargetTargetDirectoryWillNotBeCreatedWithBackSlash()
public function testTargetPathFileWillBeCreated()
{
$filter = new RenameUploadFilter($this->targetPath . '/targetDirectoryWillExistsWithBackslash\\');
$filter->filter('falsefile');
$this->assertTrue(is_dir($this->targetPath . '/targetDirectoryWillExistsWithBackslash\\'));
$filter = new RenameUploadMock([
'target' => $this->targetPath,
'use_upload_name' => true,
]);
$this->assertEquals($this->targetPath, $filter->getTarget());
$this->assertTrue($filter->getUseUploadName());

$filered = $filter->filter($this->sourceFile);
$this->assertTrue(file_exists($filered));
$this->assertEquals($this->targetPathFile, $filered);
}

/**
* @return void
*/
public function testGetSanitizedFile()
public function testTargetPathFileSanitizedWillBeCreated()
{
$filter = new RenameUploadMock([
'target' => $this->targetPath,
Expand All @@ -149,6 +191,6 @@ public function testGetSanitizedFile()

$filered = $filter->filter($this->sourceFile);
$this->assertTrue(file_exists($filered));
$this->assertEquals($this->targetPathFile, $filered);
$this->assertEquals($this->targetPathFileSanitized, $filered);
}
}
2 changes: 0 additions & 2 deletions test/Filter/StringReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @link http://github.com/armenio
*/

declare(strict_types=1);

namespace ArmenioTest\Filter;

use Armenio\Filter\StringReplace as StringReplaceFilter;
Expand Down

0 comments on commit bcfb685

Please sign in to comment.