Skip to content

Commit

Permalink
fix: corrected path to backup file
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 29, 2024
1 parent 861d54d commit 0e129ee
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion phpmyfaq/setup/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
'currentYear' => date('Y'),
'currentStep' => $step,
'documentationUrl' => System::getDocumentationUrl(),
'configTableNotAvailable' => $update->isConfigTableAvailable($faqConfig->getDb()),
'configTableNotAvailable' => $update->isConfigTableNotAvailable($faqConfig->getDb()),
];

echo $template->render($templateVars);
16 changes: 7 additions & 9 deletions phpmyfaq/src/phpMyFAQ/Controller/Setup/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ public function check(Request $request): JsonResponse
return $this->json(['message' => 'No version given.'], Response::HTTP_BAD_REQUEST);
}

$configuration = Configuration::getConfigurationInstance();

$installedVersion = Filter::filterVar($request->getContent(), FILTER_SANITIZE_SPECIAL_CHARS);

$update = new Update(new System(), $configuration);
$update = new Update(new System(), $this->configuration);
$update->setVersion($installedVersion);

if (!$update->checkMaintenanceMode()) {
Expand Down Expand Up @@ -75,14 +73,14 @@ public function backup(Request $request): JsonResponse
return $this->json(['message' => 'No version given.'], Response::HTTP_BAD_REQUEST);
}

$update = new Update(new System(), Configuration::getConfigurationInstance());
$update = new Update(new System(), $this->configuration);
$update->setVersion(System::getVersion());

$installedVersion = Filter::filterVar($request->getContent(), FILTER_SANITIZE_SPECIAL_CHARS);

$configPath = PMF_ROOT_DIR . DIRECTORY_SEPARATOR . 'content';
if (!version_compare($installedVersion, '4.0.0-dev') < 0) {
$configPath = PMF_ROOT_DIR . DIRECTORY_SEPARATOR . 'config';
$configPath = PMF_ROOT_DIR . '/content/core/config';
if (!version_compare($installedVersion, '4.0.0-alpha') < 0) {
$configPath = PMF_ROOT_DIR . '/config';
}

try {
Expand All @@ -96,7 +94,6 @@ public function backup(Request $request): JsonResponse

public function updateDatabase(Request $request): StreamedResponse|JsonResponse
{
$configuration = Configuration::getConfigurationInstance();

if (empty($request->getContent())) {
$response = new JsonResponse();
Expand All @@ -107,10 +104,11 @@ public function updateDatabase(Request $request): StreamedResponse|JsonResponse

$installedVersion = Filter::filterVar($request->getContent(), FILTER_SANITIZE_SPECIAL_CHARS);

$update = new Update(new System(), $configuration);
$update = new Update(new System(), $this->configuration);
$update->setVersion($installedVersion);

$response = new StreamedResponse();
$configuration = $this->configuration;
$response->setCallback(static function () use ($update, $configuration) {
$progressCallback = static function ($progress) {
echo json_encode(['progress' => $progress]) . "\n";
Expand Down
6 changes: 3 additions & 3 deletions phpmyfaq/src/phpMyFAQ/Setup/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function setVersion(string $version): void
/**
* Checks if the "faqconfig" table is available
*/
public function isConfigTableAvailable(DatabaseDriver $databaseDriver): bool
public function isConfigTableNotAvailable(DatabaseDriver $databaseDriver): bool
{
$query = sprintf('SELECT * FROM %s%s', Database::getTablePrefix(), 'faqconfig');
$result = $databaseDriver->query($query);
Expand All @@ -81,7 +81,7 @@ public function createConfigBackup(string $configDir): string

foreach ($files as $file) {
$file = realpath($file);
if (!str_contains($file, $configDir . DIRECTORY_SEPARATOR)) {
if (str_contains($file, $configDir . DIRECTORY_SEPARATOR)) {
if (is_dir($file)) {
$zipArchive->addEmptyDir(
str_replace($configDir . DIRECTORY_SEPARATOR, '', $file . DIRECTORY_SEPARATOR)
Expand All @@ -98,7 +98,7 @@ public function createConfigBackup(string $configDir): string
throw new Exception('Cannot store config backup file.');
}

return $this->configuration->getDefaultUrl() . 'content/' . $this->getBackupFilename();
return $this->configuration->getDefaultUrl() . 'content/core/config/' . $this->getBackupFilename();
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/phpMyFAQ/Setup/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace phpMyFAQ\Setup;

use phpMyFAQ\Configuration;
use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\System;
use PHPUnit\Framework\TestCase;

class UpdateTest extends TestCase
{
private Configuration $configuration;
private Sqlite3 $dbHandle;
private Update $update;
protected function setUp(): void
{
parent::setUp();

$this->dbHandle = new Sqlite3();
$this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$this->configuration = new Configuration($this->dbHandle);
$this->configuration->set('main.currentVersion', '4.0.0');

$this->update = new Update(new System(), Configuration::getConfigurationInstance());
}

public function testCreateConfigBackup(): void
{
$this->update->setVersion('4.0.0');
$configPath = PMF_TEST_DIR . '/content/core/config';

$this->update->createConfigBackup($configPath);

$this->assertFileExists(
PMF_TEST_DIR . '/content/core/config/phpmyfaq-config-backup.' . date('Y-m-d') . '.zip'
);

unlink(PMF_TEST_DIR . '/content/core/config/phpmyfaq-config-backup.' . date('Y-m-d') . '.zip');
}

public function testIsConfigTableNotAvailable(): void
{
$this->update->setVersion('4.0.0');
$this->assertFalse($this->update->isConfigTableNotAvailable($this->dbHandle));
}
}

0 comments on commit 0e129ee

Please sign in to comment.