|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sbpp\Tests\Integration; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sbpp\Theme\ThemeConf; |
| 9 | + |
| 10 | +/** |
| 11 | + * Issue #1466: the admin Themes picker regex-reads theme.conf.php |
| 12 | + * without executing it. The shipped default manifest uses |
| 13 | + * single-quoted define() values; the pre-fix parser only matched |
| 14 | + * double-quoted literals, so cards showed "by Unknown · v?". |
| 15 | + */ |
| 16 | +final class ThemeConfParseTest extends TestCase |
| 17 | +{ |
| 18 | + public function testDefaultThemeConfParsesSingleQuotedDefines(): void |
| 19 | + { |
| 20 | + $src = (string) file_get_contents(ROOT . 'themes/default/theme.conf.php'); |
| 21 | + |
| 22 | + $this->assertSame('SourceBans++ Default', ThemeConf::parseDefine($src, 'theme_name', '')); |
| 23 | + $this->assertSame('SourceBans++ Dev Team', ThemeConf::parseDefine($src, 'theme_author', 'Unknown')); |
| 24 | + $this->assertSame('2.0.0', ThemeConf::parseDefine($src, 'theme_version', '?')); |
| 25 | + $this->assertSame('https://github.com/sbpp/sourcebans-pp', ThemeConf::parseDefine($src, 'theme_link', '')); |
| 26 | + $this->assertSame('screenshot.jpg', ThemeConf::parseDefine($src, 'theme_screenshot', '')); |
| 27 | + } |
| 28 | + |
| 29 | + public function testParseDefineStillAcceptsDoubleQuotedManifests(): void |
| 30 | + { |
| 31 | + $src = <<<'PHP' |
| 32 | +<?php |
| 33 | +define('theme_name', "Fork Theme"); |
| 34 | +define('theme_author', "Example Author"); |
| 35 | +define('theme_version', "1.2.3"); |
| 36 | +define('theme_link', "https://example.com/theme"); |
| 37 | +define('theme_screenshot', "preview.png"); |
| 38 | +PHP; |
| 39 | + |
| 40 | + $this->assertSame('Fork Theme', ThemeConf::parseDefine($src, 'theme_name', '')); |
| 41 | + $this->assertSame('Example Author', ThemeConf::parseDefine($src, 'theme_author', 'Unknown')); |
| 42 | + $this->assertSame('1.2.3', ThemeConf::parseDefine($src, 'theme_version', '?')); |
| 43 | + $this->assertSame('https://example.com/theme', ThemeConf::parseDefine($src, 'theme_link', 'missing')); |
| 44 | + $this->assertSame('preview.png', ThemeConf::parseDefine($src, 'theme_screenshot', '')); |
| 45 | + } |
| 46 | + |
| 47 | + public function testEmptyDoubleQuotedValueDoesNotFatal(): void |
| 48 | + { |
| 49 | + $src = "<?php\ndefine('theme_link', \"\");\n"; |
| 50 | + |
| 51 | + $this->assertSame('', ThemeConf::parseDefine($src, 'theme_link', 'fallback')); |
| 52 | + } |
| 53 | + |
| 54 | + public function testEmptySingleQuotedValue(): void |
| 55 | + { |
| 56 | + $src = "<?php\ndefine('theme_link', '');\n"; |
| 57 | + |
| 58 | + $this->assertSame('', ThemeConf::parseDefine($src, 'theme_link', 'fallback')); |
| 59 | + } |
| 60 | + |
| 61 | + public function testEscapedApostropheInSingleQuotedValue(): void |
| 62 | + { |
| 63 | + $src = "<?php\ndefine('theme_author', 'Bob\\'s Fork');\n"; |
| 64 | + |
| 65 | + $this->assertSame("Bob's Fork", ThemeConf::parseDefine($src, 'theme_author', 'Unknown')); |
| 66 | + } |
| 67 | + |
| 68 | + public function testDoubleQuotedKeyIsNotMatched(): void |
| 69 | + { |
| 70 | + $src = 'define("theme_name", "Wrong key shape");'; |
| 71 | + |
| 72 | + $this->assertSame('fallback', ThemeConf::parseDefine($src, 'theme_name', 'fallback')); |
| 73 | + } |
| 74 | + |
| 75 | + public function testSanitizeLinkAllowsEmptyAndHttpUrls(): void |
| 76 | + { |
| 77 | + $this->assertSame('', ThemeConf::sanitizeLink('')); |
| 78 | + $this->assertSame('https://example.com', ThemeConf::sanitizeLink('https://example.com')); |
| 79 | + $this->assertSame('http://example.com/path', ThemeConf::sanitizeLink('http://example.com/path')); |
| 80 | + } |
| 81 | + |
| 82 | + public function testSanitizeLinkRejectsNonHttpSchemes(): void |
| 83 | + { |
| 84 | + $this->assertSame('', ThemeConf::sanitizeLink('javascript:alert(1)')); |
| 85 | + $this->assertSame('', ThemeConf::sanitizeLink('file:///etc/passwd')); |
| 86 | + } |
| 87 | + |
| 88 | + public function testSanitizeScreenshotFilenameStripsPaths(): void |
| 89 | + { |
| 90 | + $this->assertSame('shot.jpg', ThemeConf::sanitizeScreenshotFilename('shot.jpg', 'screenshot.jpg')); |
| 91 | + $this->assertSame('shot.jpg', ThemeConf::sanitizeScreenshotFilename('../../../shot.jpg', 'screenshot.jpg')); |
| 92 | + $this->assertSame('screenshot.jpg', ThemeConf::sanitizeScreenshotFilename('', 'screenshot.jpg')); |
| 93 | + $this->assertSame('screenshot.jpg', ThemeConf::sanitizeScreenshotFilename('..', 'screenshot.jpg')); |
| 94 | + } |
| 95 | + |
| 96 | + public function testDefaultThemeDiscoveryRowMatchesManifest(): void |
| 97 | + { |
| 98 | + $filename = 'default'; |
| 99 | + $confSrc = (string) file_get_contents(ROOT . 'themes/default/theme.conf.php'); |
| 100 | + |
| 101 | + $row = [ |
| 102 | + 'author' => ThemeConf::parseDefine($confSrc, 'theme_author', 'Unknown'), |
| 103 | + 'version' => ThemeConf::parseDefine($confSrc, 'theme_version', '?'), |
| 104 | + 'link' => ThemeConf::sanitizeLink(ThemeConf::parseDefine($confSrc, 'theme_link', '')), |
| 105 | + ]; |
| 106 | + |
| 107 | + $this->assertSame('SourceBans++ Dev Team', $row['author']); |
| 108 | + $this->assertSame('2.0.0', $row['version']); |
| 109 | + $this->assertSame('https://github.com/sbpp/sourcebans-pp', $row['link']); |
| 110 | + $this->assertStringContainsString( |
| 111 | + 'themes/' . $filename . '/', |
| 112 | + 'themes/' . $filename . '/' . ThemeConf::sanitizeScreenshotFilename( |
| 113 | + ThemeConf::parseDefine($confSrc, 'theme_screenshot', 'screenshot.jpg'), |
| 114 | + 'screenshot.jpg', |
| 115 | + ), |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments