Skip to content

Commit 465c1fa

Browse files
authored
Merge pull request #776 from lucatume/v4-fix-775
Fix #775
2 parents def1ed8 + 7706930 commit 465c1fa

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [unreleased] Unreleased
66

7+
### Fixed
8+
9+
- Correctly serialize and unserialize options in the SQLite database implementation.
10+
711
## [4.5.0] 2025-04-23;
812

9-
## Changed
13+
### Changed
1014

1115
- Support `symfony/process` and `symfony/filesystem` version `>=7.0`.
1216
- Set PHPUnit supported version to `<=12.0.0`.

src/Extension/Symlinker.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function onModuleInit(SuiteEvent $event): void
118118
*/
119119
private function symlinkPlugin(string $plugin, string $pluginsDir): void
120120
{
121-
$link = $pluginsDir . basename($plugin);
121+
$link = rtrim($pluginsDir, "\\/") .DIRECTORY_SEPARATOR. ltrim(basename($plugin), "\\/");
122122

123123
if (is_link($link)) {
124124
$target = readlink($link);
@@ -151,7 +151,7 @@ private function symlinkPlugin(string $plugin, string $pluginsDir): void
151151
private function symlinkTheme(string $theme, string $themesDir): void
152152
{
153153
$target = $theme;
154-
$link = $themesDir . basename($theme);
154+
$link = rtrim($themesDir, "\\/") . DIRECTORY_SEPARATOR . ltrim(basename($theme), "\\/");
155155

156156
if (is_link($link)) {
157157
$target = readlink($link);

src/WordPress/Database/SQLiteDatabase.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace lucatume\WPBrowser\WordPress\Database;
44

5+
use lucatume\WPBrowser\Utils\Serializer;
56
use lucatume\WPBrowser\WordPress\DbException;
67
use lucatume\WPBrowser\WordPress\WPConfigFile;
78
use PDO;
@@ -177,7 +178,7 @@ public function updateOption(string $name, mixed $value): int
177178
);
178179
}
179180

180-
$stmt->execute([':name' => $name, ':value' => $value, ':autoload' => 'yes']);
181+
$stmt->execute([':name' => $name, ':value' => Serializer::maybeSerialize($value), ':autoload' => 'yes']);
181182
return $stmt->rowCount();
182183
}
183184

@@ -201,7 +202,7 @@ public function getOption(string $name, mixed $default = null): mixed
201202
if ($value === false) {
202203
return $default;
203204
}
204-
return $value;
205+
return Serializer::maybeUnserialize($value);
205206
}
206207

207208
public function getDbDir(): string

tests/unit/lucatume/WPBrowser/WordPress/Database/SqliteDatabaseTest.php

+42-6
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,18 @@ public function should_run_queries_correctly(): void
185185
$file = 'db.sqlite';
186186
$db = new SQLiteDatabase($dir, $file);
187187
$db->create();
188-
$this->assertEquals(0,
189-
$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)'));
190-
$this->assertEquals(1,
191-
$db->query('INSERT INTO wp_options (option_name, option_value, autoload) VALUES ("siteurl", "http://localhost", "yes")'));
192-
$this->assertEquals(1,
193-
$db->query('INSERT INTO wp_options (option_name, option_value, autoload) VALUES ("home", "http://localhost", "yes")'));
188+
$this->assertEquals(
189+
0,
190+
$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)')
191+
);
192+
$this->assertEquals(
193+
1,
194+
$db->query('INSERT INTO wp_options (option_name, option_value, autoload) VALUES ("siteurl", "http://localhost", "yes")')
195+
);
196+
$this->assertEquals(
197+
1,
198+
$db->query('INSERT INTO wp_options (option_name, option_value, autoload) VALUES ("home", "http://localhost", "yes")')
199+
);
194200
$this->assertEquals(0, $db->query('SELECT * FROM wp_options'));
195201
$this->assertEquals('http://localhost', $db->getoption('siteurl'));
196202
$this->assertEquals('http://localhost', $db->getoption('home'));
@@ -342,4 +348,34 @@ public function should_throw_if_database_dump_file_cannot_be_written(): void
342348
$dumpFile = tempnam(sys_get_temp_dir(), 'sqlite_');
343349
$db->dump($dumpFile);
344350
}
351+
352+
public static function optionsDataProvider(): array
353+
{
354+
return [
355+
'string option' => ['http://example.com', 'http://example.com'],
356+
'int option' => [23, '23'],
357+
'boolean true option' => [true, '1'],
358+
'boolean false option' => [false, ''],
359+
'array option' => [[1, 2, 3], [1, 2, 3]],
360+
'object option' => [(object) ['a' => 'b'], (object) ['a' => 'b']],
361+
'null option' => [null, null],
362+
];
363+
}
364+
365+
/**
366+
* @test
367+
* @dataProvider optionsDataProvider
368+
*/
369+
public function should_read_and_write_options_correctly(mixed $optionValue, mixed $expectedOptionValue): void
370+
{
371+
$dump = codecept_data_dir('dump.sqlite');
372+
$dir = FS::tmpDir('sqlite_');
373+
$file = 'db.sqlite';
374+
$db = new SQLiteDatabase($dir, $file);
375+
$db->import($dump);
376+
377+
$db->updateOption('test', $optionValue);
378+
379+
$this->assertEquals($expectedOptionValue, $db->getOption('test'));
380+
}
345381
}

0 commit comments

Comments
 (0)