Skip to content

Commit ce75ca9

Browse files
committed
fix: corrected ErrorDocument handling
1 parent 4b2671b commit ce75ca9

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

phpmyfaq/.htaccess

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Header set Access-Control-Allow-Headers "Content-Type, Authorization"
101101
# Exclude assets from being handled by Symfony Router
102102
RewriteRule ^(admin/assets)($|/) - [L]
103103
# Error pages
104-
ErrorDocument 404 index.php?action=404
104+
ErrorDocument 404 /index.php?action=404
105105
# General pages
106106
RewriteRule ^add-faq.html$ index.php?action=add [L,QSA]
107107
RewriteRule ^add-question.html$ index.php?action=ask [L,QSA]

phpmyfaq/src/phpMyFAQ/Setup/EnvironmentConfigurator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,16 @@ public function adjustRewriteBaseHtaccess(): bool
102102
}
103103

104104
// Adjust ErrorDocument 404
105-
$errorDocument404 = $htaccess->search('ErrorDocument 404', TOKEN_DIRECTIVE);
105+
$errorDocument404 = $htaccess->search('ErrorDocument', TOKEN_DIRECTIVE);
106106
if ($errorDocument404) {
107+
// Get current arguments and clear them
108+
$currentArgs = $errorDocument404->getArguments();
109+
foreach ($currentArgs as $arg) {
110+
$errorDocument404->removeArgument($arg);
111+
}
112+
// Set new arguments: error code and path
107113
$new404Path = rtrim($this->getServerPath(), '/') . '/index.php?action=404';
108-
$errorDocument404->setArguments([(string) $new404Path]);
114+
$errorDocument404->setArguments(['404', $new404Path]);
109115
}
110116

111117
$output = (string) $htaccess;

tests/phpMyFAQ/Setup/EnvironmentConfiguratorTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,60 @@ public function testAdjustRewriteBaseHtaccess(): void
8787
$this->assertTrue($configurator->adjustRewriteBaseHtaccess());
8888
$this->assertEquals('/path/info', $configurator->getRewriteBase());
8989
}
90+
91+
/**
92+
* @throws Exception
93+
* @throws \PHPUnit\Framework\MockObject\Exception
94+
*/
95+
public function testAdjustRewriteBaseHtaccessUpdatesErrorDocumentWithRootPath(): void
96+
{
97+
// Set up a proper .htaccess file with ErrorDocument directive
98+
$htaccessPath = dirname(__DIR__, 2) . '/.htaccess';
99+
$htaccessContent = <<<'HTACCESS'
100+
<IfModule mod_rewrite.c>
101+
RewriteEngine On
102+
RewriteBase /phpmyfaq-test/
103+
ErrorDocument 404 /phpmyfaq-test/index.php?action=404
104+
</IfModule>
105+
HTACCESS;
106+
file_put_contents($htaccessPath, $htaccessContent);
107+
108+
$configuration = $this->createStub(Configuration::class);
109+
$configuration->method('getRootPath')->willReturn(dirname(__DIR__, 2));
110+
$configuration->method('getDefaultUrl')->willReturn('https://localhost/');
111+
$configurator = new EnvironmentConfigurator($configuration);
112+
$this->assertTrue($configurator->adjustRewriteBaseHtaccess());
113+
114+
// Read the .htaccess file and verify ErrorDocument 404 is set correctly
115+
$htaccessContent = file_get_contents($htaccessPath);
116+
$this->assertStringContainsString('ErrorDocument 404 /index.php?action=404', $htaccessContent);
117+
}
118+
119+
/**
120+
* @throws Exception
121+
* @throws \PHPUnit\Framework\MockObject\Exception
122+
*/
123+
public function testAdjustRewriteBaseHtaccessUpdatesErrorDocumentWithSubdirectoryPath(): void
124+
{
125+
// Set up a proper .htaccess file with ErrorDocument directive
126+
$htaccessPath = dirname(__DIR__, 2) . '/.htaccess';
127+
$htaccessContent = <<<'HTACCESS'
128+
<IfModule mod_rewrite.c>
129+
RewriteEngine On
130+
RewriteBase /
131+
ErrorDocument 404 /index.php?action=404
132+
</IfModule>
133+
HTACCESS;
134+
file_put_contents($htaccessPath, $htaccessContent);
135+
136+
$configuration = $this->createStub(Configuration::class);
137+
$configuration->method('getRootPath')->willReturn(dirname(__DIR__, 2));
138+
$configuration->method('getDefaultUrl')->willReturn('https://localhost/faq/');
139+
$configurator = new EnvironmentConfigurator($configuration);
140+
$this->assertTrue($configurator->adjustRewriteBaseHtaccess());
141+
142+
// Read the .htaccess file and verify ErrorDocument 404 is set correctly
143+
$htaccessContent = file_get_contents($htaccessPath);
144+
$this->assertStringContainsString('ErrorDocument 404 /faq/index.php?action=404', $htaccessContent);
145+
}
90146
}

0 commit comments

Comments
 (0)