Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

### Fixed

- Correctly serialize and unserialize options in the SQLite database implementation.

## [4.5.0] 2025-04-23;

## Changed
### Changed

- Support `symfony/process` and `symfony/filesystem` version `>=7.0`.
- Set PHPUnit supported version to `<=12.0.0`.
Expand Down
4 changes: 2 additions & 2 deletions src/Extension/Symlinker.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function onModuleInit(SuiteEvent $event): void
*/
private function symlinkPlugin(string $plugin, string $pluginsDir): void
{
$link = $pluginsDir . basename($plugin);
$link = rtrim($pluginsDir, "\\/") .DIRECTORY_SEPARATOR. ltrim(basename($plugin), "\\/");

if (is_link($link)) {
$target = readlink($link);
Expand Down Expand Up @@ -151,7 +151,7 @@ private function symlinkPlugin(string $plugin, string $pluginsDir): void
private function symlinkTheme(string $theme, string $themesDir): void
{
$target = $theme;
$link = $themesDir . basename($theme);
$link = rtrim($themesDir, "\\/") . DIRECTORY_SEPARATOR . ltrim(basename($theme), "\\/");

if (is_link($link)) {
$target = readlink($link);
Expand Down
5 changes: 3 additions & 2 deletions src/WordPress/Database/SQLiteDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lucatume\WPBrowser\WordPress\Database;

use lucatume\WPBrowser\Utils\Serializer;
use lucatume\WPBrowser\WordPress\DbException;
use lucatume\WPBrowser\WordPress\WPConfigFile;
use PDO;
Expand Down Expand Up @@ -177,7 +178,7 @@ public function updateOption(string $name, mixed $value): int
);
}

$stmt->execute([':name' => $name, ':value' => $value, ':autoload' => 'yes']);
$stmt->execute([':name' => $name, ':value' => Serializer::maybeSerialize($value), ':autoload' => 'yes']);
return $stmt->rowCount();
}

Expand All @@ -201,7 +202,7 @@ public function getOption(string $name, mixed $default = null): mixed
if ($value === false) {
return $default;
}
return $value;
return Serializer::maybeUnserialize($value);
}

public function getDbDir(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,18 @@ public function should_run_queries_correctly(): void
$file = 'db.sqlite';
$db = new SQLiteDatabase($dir, $file);
$db->create();
$this->assertEquals(0,
$db->query('CREATE TABLE wp_options (option_id INTEGER PRIMARY KEY, option_name TEXT NOT NULL, option_value TEXT NOT NULL, autoload TEXT NOT NULL)'));
$this->assertEquals(1,
$db->query('INSERT INTO wp_options (option_name, option_value, autoload) VALUES ("siteurl", "http://localhost", "yes")'));
$this->assertEquals(1,
$db->query('INSERT INTO wp_options (option_name, option_value, autoload) VALUES ("home", "http://localhost", "yes")'));
$this->assertEquals(
0,
$db->query('CREATE TABLE wp_options (option_id INTEGER PRIMARY KEY, option_name TEXT NOT NULL, option_value TEXT NOT NULL, autoload TEXT NOT NULL)')
);
$this->assertEquals(
1,
$db->query('INSERT INTO wp_options (option_name, option_value, autoload) VALUES ("siteurl", "http://localhost", "yes")')
);
$this->assertEquals(
1,
$db->query('INSERT INTO wp_options (option_name, option_value, autoload) VALUES ("home", "http://localhost", "yes")')
);
$this->assertEquals(0, $db->query('SELECT * FROM wp_options'));
$this->assertEquals('http://localhost', $db->getoption('siteurl'));
$this->assertEquals('http://localhost', $db->getoption('home'));
Expand Down Expand Up @@ -342,4 +348,34 @@ public function should_throw_if_database_dump_file_cannot_be_written(): void
$dumpFile = tempnam(sys_get_temp_dir(), 'sqlite_');
$db->dump($dumpFile);
}

public static function optionsDataProvider(): array
{
return [
'string option' => ['http://example.com', 'http://example.com'],
'int option' => [23, '23'],
'boolean true option' => [true, '1'],
'boolean false option' => [false, ''],
'array option' => [[1, 2, 3], [1, 2, 3]],
'object option' => [(object) ['a' => 'b'], (object) ['a' => 'b']],
'null option' => [null, null],
];
}

/**
* @test
* @dataProvider optionsDataProvider
*/
public function should_read_and_write_options_correctly(mixed $optionValue, mixed $expectedOptionValue): void
{
$dump = codecept_data_dir('dump.sqlite');
$dir = FS::tmpDir('sqlite_');
$file = 'db.sqlite';
$db = new SQLiteDatabase($dir, $file);
$db->import($dump);

$db->updateOption('test', $optionValue);

$this->assertEquals($expectedOptionValue, $db->getOption('test'));
}
}