1
0
Files
breadcrumb-the-shire/tests/Architecture/FileStorageContractTest.php
fs 0e86925464 refactor: rename lib/ to core/ for clearer core-module separation
Rename the top-level lib/ directory to core/ so the project root
immediately communicates which code is core platform and which lives
in modules/. PHP namespaces (MintyPHP\*) are unchanged — only the
Composer PSR-4 path mapping moves from lib/ to core/.

Module-internal lib/ directories (modules/*/lib/) are untouched.

Updated across the full stack:
- composer.json PSR-4 mapping
- bootstrap entry points (web/index.php, bin/*, tests/bootstrap.php)
- tooling configs (phpstan.neon, phpunit.xml, php-cs-fixer)
- 26 architecture contract tests
- enforcement-policy, guard-catalog, quality-gates
- all documentation (CLAUDE.md, README, 25 docs/, .agents/skills/)
- bin/qa-extended.sh search paths
- .claude/settings.local.json permission paths

Workflow: .agents/runs/CORE-LIB-RENAME-001/ (Analyst → Planner →
Executor → Code Review (4 findings fixed) → Security Review →
Acceptance Test → Finalizer)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 23:20:42 +02:00

86 lines
2.6 KiB
PHP

<?php
namespace MintyPHP\Tests\Architecture;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/**
* GR-SEC-006: User-uploaded files MUST go to storage/, never web/.
* Exception: Favicon services write derived images to web/favicon/ because
* favicons must be publicly accessible by browsers.
*/
class FileStorageContractTest extends TestCase
{
use ProjectFileAssertionSupport;
/** Services that legitimately write to web/ for favicon delivery. */
private const WEB_WRITE_EXEMPT_FILES = [
'core/Service/Branding/BrandingFaviconService.php',
'core/Service/Tenant/TenantFaviconService.php',
];
public function testNoServiceWritesFilesToWebDirectory(): void
{
$root = $this->projectRootPath();
$serviceDir = $root . '/core/Service';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($serviceDir));
$violations = [];
/** @var \SplFileInfo $file */
foreach ($iterator as $file) {
if (!$file->isFile() || $file->getExtension() !== 'php') {
continue;
}
$relativePath = str_replace($root . '/', '', $file->getPathname());
if (in_array($relativePath, self::WEB_WRITE_EXEMPT_FILES, true)) {
continue;
}
$content = (string) file_get_contents($file->getPathname());
$hasFileWrite = preg_match('/\bmove_uploaded_file\s*\(/', $content)
|| preg_match('/\bfile_put_contents\s*\(/', $content);
if (!$hasFileWrite) {
continue;
}
$hasWebPath = preg_match('/[\'"]web\//', $content)
|| preg_match('/\/web\/[a-z]/', $content);
if ($hasWebPath) {
$violations[] = $relativePath;
}
}
sort($violations);
$this->assertSame(
[],
$violations,
"Service files writing to web/ directory (GR-SEC-006):\n" . implode("\n", $violations)
);
}
public function testNoPagesWriteFilesToFilesystem(): void
{
$violations = array_merge(
$this->findPatternMatchesInPhpFiles('pages', '/\bmove_uploaded_file\s*\(/'),
$this->findPatternMatchesInPhpFiles('pages', '/\bfile_put_contents\s*\(/')
);
$violations = array_values(array_unique($violations));
sort($violations);
$this->assertSame(
[],
$violations,
"Pages must not write files directly — delegate to Service layer (GR-SEC-006):\n" . implode("\n", $violations)
);
}
}