53 lines
2.6 KiB
PHP
53 lines
2.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
// Exercise SEO locally without controllers that call production services.
|
|
try {
|
|
$app = new think\App(dirname(__DIR__) . '/');
|
|
$app->initialize();
|
|
$config = require dirname(__DIR__) . '/config/seo.php';
|
|
$config['base_url'] = 'https://example.test';
|
|
$app->config->set($config, 'seo');
|
|
$meta = new app\service\SeoMetaService($config);
|
|
$check = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
|
|
foreach ($config['pages'] as $route => $page) {
|
|
[$application, $controller, $action] = explode('/', $route);
|
|
$result = $meta->forRoute($application, $controller, $action);
|
|
$check($result['robots'] === $page['robots'], 'Public page robots changed: ' . $route);
|
|
$check($result['canonical'] === $config['base_url'] . $page['canonical_path'], 'Canonical changed: ' . $route);
|
|
}
|
|
foreach ($config['noindex_controllers'] as $controller) {
|
|
$result = $meta->forRoute('home', $controller, 'index');
|
|
$check($result['robots'] === 'noindex,follow', 'Private page indexing changed: ' . $controller);
|
|
}
|
|
foreach ($config['sensitive_query_keys'] as $key) {
|
|
$result = $meta->forRoute('home', 'index', 'index', [$key => 'test']);
|
|
$check($result['robots'] === 'noindex,nofollow', 'Sensitive query indexing changed: ' . $key);
|
|
}
|
|
|
|
$sitemap = new app\service\SitemapService($config['base_url']);
|
|
$entries = [['path' => '/'], ['path' => '/home/news/detail', 'query' => ['id' => 7]]];
|
|
foreach ($config['noindex_controllers'] as $controller) {
|
|
$entries[] = ['path' => '/home/' . $controller . '/index'];
|
|
}
|
|
$xml = $sitemap->render($entries);
|
|
$check(substr_count($xml, '<loc>') === 2, 'Sitemap must exclude all private controllers');
|
|
$check(str_contains($xml, '<loc>https://example.test/</loc>'), 'Homepage missing from sitemap');
|
|
$check(str_contains($xml, '<loc>https://example.test/home/news/detail?id=7</loc>'), 'News missing from sitemap');
|
|
|
|
$disallowed = array_map(static fn (string $name): string => '/home/' . $name . '/', $config['noindex_controllers']);
|
|
$robots = (new app\home\controller\Seo())->robots()->getContent();
|
|
$check($robots === $sitemap->renderRobots($disallowed), 'Robots must use only current private-page exclusions');
|
|
echo "PASS: public metadata, sensitive/private indexing, sitemap and robots\n";
|
|
} catch (Throwable $error) {
|
|
fwrite(STDERR, 'FAIL: ' . $error->getMessage() . "\n");
|
|
exit(1);
|
|
}
|