/
AttachedFileTest.php
56 lines (47 loc) · 2.07 KB
/
AttachedFileTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2022 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Test\Core\Model;
use FacturaScripts\Core\Base\ToolBox;
use FacturaScripts\Core\Model\AttachedFile;
use PHPUnit\Framework\TestCase;
final class AttachedFileTest extends TestCase
{
public function testSaveFile()
{
$name = 'xss"\'><img src=x onerror=alert(123)>.jpeg';
$filePath = FS_FOLDER . '/Test/__files/' . $name;
$this->assertTrue(file_exists($filePath), 'File not found: ' . $filePath);
// copiamos el archivo a MyFiles
$this->assertTrue(copy($filePath, FS_FOLDER . '/MyFiles/' . $name), 'File not copied');
$model = new AttachedFile();
$model->path = $name;
$this->assertTrue($model->save(), 'can-not-save-file');
// filename no puede contener html
$fileNameNoHtml = ToolBox::utils()::noHtml($name);
$this->assertEquals($fileNameNoHtml, $model->filename);
// si forzamos el html en el filename, debe quitar el html
$model->filename = $name;
$this->assertTrue($model->save(), 'can-not-update-file');
$this->assertEquals($fileNameNoHtml, $model->filename);
// podemos eliminar
$this->assertTrue($model->delete(), 'can-not-delete-file');
// el archivo ya no está en el path
$this->assertFalse(file_exists($model->path));
}
}