|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace { |
| 6 | + // include/manual-lookup.inc defines global functions and depends on the global |
| 7 | + // get_manual_search_sections() (normally from include/site.inc). Provide the same |
| 8 | + // section list here so the file can be exercised without pulling in all of site.inc. |
| 9 | + if (!function_exists('get_manual_search_sections')) { |
| 10 | + function get_manual_search_sections(): array |
| 11 | + { |
| 12 | + return ['', 'book.', 'ref.', 'function.', 'class.', 'enum.', 'features.', 'control-structures.', 'language.', 'about.', 'faq.']; |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + require_once __DIR__ . '/../../../include/manual-lookup.inc'; |
| 17 | +} |
| 18 | + |
| 19 | +namespace phpweb\Test\Unit\ManualLookup { |
| 20 | + |
| 21 | + use PHPUnit\Framework; |
| 22 | + |
| 23 | + #[Framework\Attributes\CoversFunction('find_manual_page')] |
| 24 | + #[Framework\Attributes\CoversFunction('find_manual_page_slow')] |
| 25 | + final class FindManualPageTest extends Framework\TestCase |
| 26 | + { |
| 27 | + private string $root; |
| 28 | + |
| 29 | + private ?string $originalDocumentRoot; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->root = sys_get_temp_dir() . '/phpweb-ml-' . uniqid('', true); |
| 34 | + mkdir($this->root . '/backend', 0777, true); |
| 35 | + mkdir($this->root . '/manual/en', 0777, true); |
| 36 | + // Filesystem (slow-path) target for the keyword "echo". |
| 37 | + file_put_contents($this->root . '/manual/en/function.echo.php', '<?php'); |
| 38 | + |
| 39 | + $this->originalDocumentRoot = $_SERVER['DOCUMENT_ROOT'] ?? null; |
| 40 | + $_SERVER['DOCUMENT_ROOT'] = $this->root; |
| 41 | + } |
| 42 | + |
| 43 | + protected function tearDown(): void |
| 44 | + { |
| 45 | + if ($this->originalDocumentRoot === null) { |
| 46 | + unset($_SERVER['DOCUMENT_ROOT']); |
| 47 | + } else { |
| 48 | + $_SERVER['DOCUMENT_ROOT'] = $this->originalDocumentRoot; |
| 49 | + } |
| 50 | + |
| 51 | + array_map('unlink', glob($this->root . '/backend/*') ?: []); |
| 52 | + array_map('unlink', glob($this->root . '/manual/en/*') ?: []); |
| 53 | + @rmdir($this->root . '/backend'); |
| 54 | + @rmdir($this->root . '/manual/en'); |
| 55 | + @rmdir($this->root . '/manual'); |
| 56 | + @rmdir($this->root); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Regression test for the production fatal: |
| 61 | + * Uncaught PDOException: SQLSTATE[HY000]: General error: 8 |
| 62 | + * attempt to write a readonly database in include/manual-lookup.inc |
| 63 | + * |
| 64 | + * When the sqlite fast-path fails for ANY reason (a read-only/locked database, |
| 65 | + * or a corrupt/truncated one from an interrupted rsync), find_manual_page() must |
| 66 | + * fall back to the filesystem search instead of throwing an uncaught exception. |
| 67 | + */ |
| 68 | + public function testFallsBackToSlowSearchWhenSqliteQueryFails(): void |
| 69 | + { |
| 70 | + file_put_contents($this->root . '/backend/manual-lookup.sqlite', 'this is not a sqlite database'); |
| 71 | + |
| 72 | + $result = find_manual_page('en', 'echo'); |
| 73 | + |
| 74 | + self::assertSame('/manual/en/function.echo.php', $result); |
| 75 | + } |
| 76 | + |
| 77 | + public function testFallsBackToSlowSearchForDottedKeywordWhenSqliteQueryFails(): void |
| 78 | + { |
| 79 | + // A dotted keyword takes the other SQL branch in find_manual_page(); it must |
| 80 | + // fall back to the filesystem search on a broken database too. |
| 81 | + file_put_contents($this->root . '/backend/manual-lookup.sqlite', 'this is not a sqlite database'); |
| 82 | + |
| 83 | + $result = find_manual_page('en', 'function.echo'); |
| 84 | + |
| 85 | + self::assertSame('/manual/en/function.echo.php', $result); |
| 86 | + } |
| 87 | + |
| 88 | + #[Framework\Attributes\RequiresPhpExtension('pdo_sqlite')] |
| 89 | + public function testUsesSqliteFastPathWhenDatabaseIsValid(): void |
| 90 | + { |
| 91 | + $this->buildValidDatabase(); |
| 92 | + |
| 93 | + $result = find_manual_page('en', 'function.echo'); |
| 94 | + |
| 95 | + self::assertSame('/manual/en/function.echo.php', $result); |
| 96 | + } |
| 97 | + |
| 98 | + public function testFallsBackToSlowSearchWhenNoDatabasePresent(): void |
| 99 | + { |
| 100 | + // No backend/manual-lookup.sqlite at all -> slow (filesystem) search only. |
| 101 | + $result = find_manual_page('en', 'echo'); |
| 102 | + |
| 103 | + self::assertSame('/manual/en/function.echo.php', $result); |
| 104 | + } |
| 105 | + |
| 106 | + private function buildValidDatabase(): void |
| 107 | + { |
| 108 | + $dbh = new \PDO('sqlite:' . $this->root . '/backend/manual-lookup.sqlite'); |
| 109 | + $dbh->exec('CREATE TABLE fs (lang TEXT, prefix TEXT, keyword TEXT, name TEXT, prio INT)'); |
| 110 | + $dbh->exec("INSERT INTO fs (lang, prefix, keyword, name, prio) VALUES ('en', 'function.', 'echo', '/manual/en/function.echo.php', 3)"); |
| 111 | + $dbh = null; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments