-
Notifications
You must be signed in to change notification settings - Fork 7
Añadir límite de copias de seguridad y mensajes de registro #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9809496
Añadir límite de copias de seguridad y mensajes de registro
EstebanSM85 0088889
Añadir opción para establecer el número máximo de copias de seguridad
EstebanSM85 e62ef0b
Merge remote-tracking branch 'origin/master' into Tarea-4772-limite-c…
EstebanSM85 976676a
Soporte para PostgreSQL en BackupSQL
EstebanSM85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| <?php | ||
| /** | ||
| * This file is part of Backup plugin for FacturaScripts | ||
| * Copyright (C) 2026 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\Plugins; | ||
|
|
||
| use FacturaScripts\Core\Tools; | ||
| use FacturaScripts\Plugins\Backup\Cron; | ||
| use FacturaScripts\Test\Traits\LogErrorsTrait; | ||
| use PHPUnit\Framework\TestCase; | ||
| use ReflectionMethod; | ||
|
|
||
| /** | ||
| * @author Esteban Sanchez <esteban@factura.city> | ||
| */ | ||
| final class CronApplyBackupLimitTest extends TestCase | ||
| { | ||
| use LogErrorsTrait; | ||
|
|
||
| private string $folder = ''; | ||
| private int $sqlPrevios = 0; | ||
| private int $zipPrevios = 0; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->folder = Tools::folder('MyFiles', 'Backups'); | ||
| Tools::folderCheckOrCreate($this->folder); | ||
| $this->sqlPrevios = count(glob($this->folder . DIRECTORY_SEPARATOR . '*.sql') ?: []); | ||
| $this->zipPrevios = count(glob($this->folder . DIRECTORY_SEPARATOR . '*.zip') ?: []); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| foreach (glob($this->folder . DIRECTORY_SEPARATOR . '2000-01-01_*') ?: [] as $archivo) { | ||
| if (is_file($archivo)) { | ||
| unlink($archivo); | ||
| } | ||
| } | ||
|
|
||
| $this->logErrors(); | ||
| } | ||
|
|
||
| public function testSinLimiteNoElimina(): void | ||
| { | ||
| $archivos = $this->crearArchivos(['sql'], 5); | ||
| Tools::settingsSet('backup', 'max_backups', 0); | ||
|
|
||
| $this->ejecutarLimite(); | ||
|
|
||
| foreach ($archivos as $archivo) { | ||
| $this->assertFileExists($archivo, 'Con limite 0 no debe eliminar ningun archivo'); | ||
| } | ||
| } | ||
|
|
||
| public function testEliminaLosMasAntiguosSql(): void | ||
| { | ||
| $archivos = $this->crearArchivos(['sql'], 5); | ||
| Tools::settingsSet('backup', 'max_backups', $this->sqlPrevios + 3); | ||
|
|
||
| $this->ejecutarLimite(); | ||
|
|
||
| $this->assertFileDoesNotExist($archivos[0], 'El sql mas antiguo debe eliminarse'); | ||
| $this->assertFileDoesNotExist($archivos[1], 'El segundo sql mas antiguo debe eliminarse'); | ||
| $this->assertFileExists($archivos[2], 'El tercer sql debe conservarse'); | ||
| $this->assertFileExists($archivos[3], 'El cuarto sql debe conservarse'); | ||
| $this->assertFileExists($archivos[4], 'El quinto sql debe conservarse'); | ||
| } | ||
|
|
||
| public function testEliminaLosMasAntiguosZip(): void | ||
| { | ||
| $archivos = $this->crearArchivos(['zip'], 5); | ||
| Tools::settingsSet('backup', 'max_backups', $this->zipPrevios + 3); | ||
|
|
||
| $this->ejecutarLimite(); | ||
|
|
||
| $this->assertFileDoesNotExist($archivos[0], 'El zip mas antiguo debe eliminarse'); | ||
| $this->assertFileDoesNotExist($archivos[1], 'El segundo zip mas antiguo debe eliminarse'); | ||
| $this->assertFileExists($archivos[2], 'El tercer zip debe conservarse'); | ||
| $this->assertFileExists($archivos[3], 'El cuarto zip debe conservarse'); | ||
| $this->assertFileExists($archivos[4], 'El quinto zip debe conservarse'); | ||
| } | ||
|
|
||
| public function testLimiteSeAplicaASqlYZipPorSeparado(): void | ||
| { | ||
| $archivosSql = $this->crearArchivos(['sql'], 5); | ||
| $archivosZip = $this->crearArchivos(['zip'], 5); | ||
| $limite = max($this->sqlPrevios, $this->zipPrevios) + 3; | ||
| Tools::settingsSet('backup', 'max_backups', $limite); | ||
|
|
||
| $this->ejecutarLimite(); | ||
|
|
||
| $restantesSql = array_filter($archivosSql, 'file_exists'); | ||
| $restantesZip = array_filter($archivosZip, 'file_exists'); | ||
|
|
||
| $this->assertLessThanOrEqual(3, count($restantesSql), 'No deben quedar mas de 3 sql de test'); | ||
| $this->assertLessThanOrEqual(3, count($restantesZip), 'No deben quedar mas de 3 zip de test'); | ||
| } | ||
|
|
||
| /** @return array<string> */ | ||
| private function crearArchivos(array $extensiones, int $cantidad): array | ||
| { | ||
| $archivos = []; | ||
| for ($i = 1; $i <= $cantidad; $i++) { | ||
| foreach ($extensiones as $ext) { | ||
| $nombre = sprintf('2000-01-01_00-00-%02d.%s', $i, $ext); | ||
| $ruta = $this->folder . DIRECTORY_SEPARATOR . $nombre; | ||
| file_put_contents($ruta, ''); | ||
| $archivos[] = $ruta; | ||
| } | ||
| } | ||
| return $archivos; | ||
| } | ||
|
|
||
| private function ejecutarLimite(): void | ||
| { | ||
| $cron = new Cron('Backup'); | ||
| $metodo = new ReflectionMethod(Cron::class, 'applyBackupLimit'); | ||
| $metodo->setAccessible(true); | ||
| $metodo->invoke($cron); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.