1
0
Files
breadcrumb-the-shire/tests/Architecture/NoAddressBookHardcodingTest.php
fs c364e2b46d feat: introduce module system and extract address book as first module (MODULAR-MONOLITH-V1-001)
Add a module kernel (ModuleManifest, ModuleRegistry) that allows modules to
contribute routes, UI slots, search providers, layout context, session
lifecycle, and permissions. Modules are activated via config/modules.php or
APP_ENABLED_MODULES env variable. Conflicts (duplicate routes, permissions,
slot keys) cause a fail-fast with a clear error message.

Extract the address book from hardcoded Core integration points into the
first module (modules/addressbook/). The module provides:
- Aside icon-bar tab + People panel via UI slot system
- Global search resource via AddressBookSearchProvider
- Layout context data via AddressBookLayoutProvider
- Session lifecycle via AddressBookSessionProvider

Core cleanup removes address-book hardcodings from SearchSqlResourceProvider,
SearchUiMetaProvider, SearchItemMapperProvider, appBuildLayoutNavContext(),
and the aside templates. Permissions (ADDRESS_BOOK_VIEW) and business logic
(AddressBookService) remain in Core as they gate general user visibility.

Includes 38 new tests (894 total), PHPStan level 5 clean, and architecture
tests verifying zero hardcoded address-book references in search/templates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 15:43:25 +01:00

164 lines
6.1 KiB
PHP

<?php
namespace MintyPHP\Tests\Architecture;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use FilesystemIterator;
/**
* Architecture test: verifies that no address-book-specific hardcodings remain
* in the search infrastructure, layout context builder, or aside templates.
*
* Intentionally allowed in Core (not flagged):
* - PermissionService::ADDRESS_BOOK_VIEW (general people-visibility gate)
* - OperationsAuthorizationPolicy / UiCapabilityMap / UserAuthorizationPolicy (authorization)
* - AddressBookService / AddressBookServicesFactory (business logic, V1 shared-db)
* - pages/address-book/* (page files, not hardcoded integrations)
* - config/assets.php (asset pipeline not yet modular)
* - i18n translation files
* - Module directory (lib/Module/, modules/)
* - Test files
*/
final class NoAddressBookHardcodingTest extends TestCase
{
/**
* Search infrastructure must not contain hardcoded address-book references.
* The module's AddressBookSearchProvider is the sole source now.
*/
public function testSearchInfrastructureHasNoAddressBookHardcoding(): void
{
$searchDir = dirname(__DIR__, 2) . '/lib/Support/Search';
$hits = $this->grepDir($searchDir, '/address.book/i', ['*.php']);
self::assertSame([], $hits, sprintf(
"Search infrastructure still contains address-book hardcodings:\n%s",
implode("\n", $hits)
));
}
/**
* SearchConfig facade must not contain hardcoded address-book references
* (module provider bridge handles it dynamically).
*/
public function testSearchConfigHasNoAddressBookHardcoding(): void
{
$file = dirname(__DIR__, 2) . '/lib/Support/SearchConfig.php';
$content = file_get_contents($file);
self::assertIsString($content);
self::assertStringNotContainsString('address-book', $content);
self::assertStringNotContainsString('address_book', $content);
self::assertStringNotContainsString('addressBook', $content);
}
/**
* Layout context builder (appBuildLayoutNavContext) must not contain
* hardcoded addressBook data assembly.
*/
public function testLayoutContextHasNoHardcodedAddressBookBlock(): void
{
$file = dirname(__DIR__, 2) . '/lib/Support/helpers/app.php';
$content = file_get_contents($file);
self::assertIsString($content);
// The function should not contain 'addressBook' as an array key in the $layoutNav builder
// (the module LayoutContextProvider provides it dynamically)
self::assertDoesNotMatchRegularExpression(
"/'\s*addressBook\s*'\s*=>\s*\[/",
$content,
'appBuildLayoutNavContext() still has hardcoded addressBook array block'
);
}
/**
* Aside icon-bar template must not have hardcoded People tab.
*/
public function testIconBarHasNoHardcodedPeopleTab(): void
{
$file = dirname(__DIR__, 2) . '/templates/partials/app-main-aside-icon-bar.phtml';
$content = file_get_contents($file);
self::assertIsString($content);
self::assertStringNotContainsString('aside-tab-people', $content,
'Icon-bar still has hardcoded People tab (should come from module slot)');
self::assertStringNotContainsString('$canViewAddressBook', $content,
'Icon-bar still references $canViewAddressBook variable');
self::assertStringNotContainsString('$addressBookUrl', $content,
'Icon-bar still references $addressBookUrl variable');
}
/**
* Aside panel template must not have hardcoded People panel.
*
* Note: The search panel legitimately contains data-search-key="address-book"
* for the global search results — that is NOT a hardcoded People panel.
*/
public function testAsideHasNoHardcodedPeoplePanel(): void
{
$file = dirname(__DIR__, 2) . '/templates/partials/app-main-aside.phtml';
$content = file_get_contents($file);
self::assertIsString($content);
// The hardcoded People panel used id="aside-panel-people" with data-aside-panel="people"
self::assertStringNotContainsString('id="aside-panel-people"', $content,
'Aside still has hardcoded People panel element');
// The hardcoded People panel extracted $canViewAddressBook as a standalone variable
self::assertDoesNotMatchRegularExpression(
'/^\$canViewAddressBook\s*=/m',
$content,
'Aside still defines $canViewAddressBook as a standalone variable'
);
// The hardcoded People panel extracted $peopleGroups as a standalone variable
self::assertDoesNotMatchRegularExpression(
'/^\$peopleGroups\s*=/m',
$content,
'Aside still defines $peopleGroups as a standalone variable'
);
}
/**
* @return list<string> Lines matching the pattern
*/
private function grepDir(string $dir, string $pattern, array $extensions): array
{
$hits = [];
if (!is_dir($dir)) {
return $hits;
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iterator as $file) {
$ext = $file->getExtension();
$matchesExt = false;
foreach ($extensions as $allowedExt) {
if ('*.' . $ext === $allowedExt) {
$matchesExt = true;
break;
}
}
if (!$matchesExt) {
continue;
}
$lines = file($file->getPathname(), FILE_IGNORE_NEW_LINES);
if ($lines === false) {
continue;
}
foreach ($lines as $lineNum => $line) {
if (preg_match($pattern, $line)) {
$relative = str_replace(dirname(__DIR__, 2) . '/', '', $file->getPathname());
$hits[] = sprintf('%s:%d: %s', $relative, $lineNum + 1, trim($line));
}
}
}
return $hits;
}
}