Files
Gnuboard7/modules/_bundled/sirsoft-ecommerce/tests/Unit/Seo/EcommerceSitemapContributorTest.php
T
HeuJung 1a6fca0d6b feat(seo): 사이트맵 기여자 지연 스트리밍 + hreflang 계산 선형화
AbstractSitemapContributor 브리지 base 로 인터페이스를 바꾸지 않고
board/ecommerce/page 기여자를 getUrlsLazy 지연 스트리밍으로 전환해,
게시글이 수백만 건인 사이트에서도 URL 배열을 통째로 적재하지 않는다.
SitemapGenerator 는 capability 를 감지해 지연 경로를 쓰고, base 를
상속하지 않은 제3자 구현체는 기존 getUrls 경로로 그대로 동작한다.

hreflang alternate 집합을 base URL 당 한 번만 계산해 로케일별 <url>
에 재사용하도록 바꿔 계산 비용을 로케일 수의 제곱에서 선형으로 줄였다
(정상 구성 출력은 불변). 다국어 대체 링크 사용 여부 토글과 로케일 수
상한을 추가하고 설정/검증/관리자 UI/다국어(ko·en·ja) 표면을 동반했다.
2026-07-18 15:05:56 +09:00

235 lines
8.6 KiB
PHP

<?php
namespace Modules\Sirsoft\Ecommerce\Tests\Unit\Seo;
require_once __DIR__.'/../../ModuleTestCase.php';
use App\Seo\AbstractSitemapContributor;
use App\Seo\Contracts\SitemapContributorInterface;
use Illuminate\Support\Facades\Config;
use Modules\Sirsoft\Ecommerce\Models\Category;
use Modules\Sirsoft\Ecommerce\Models\Product;
use Modules\Sirsoft\Ecommerce\Seo\EcommerceSitemapContributor;
use Modules\Sirsoft\Ecommerce\Tests\ModuleTestCase;
/**
* EcommerceSitemapContributor 단위 테스트
*
* 검증 목적:
* - getIdentifier: 'sirsoft-ecommerce' 반환
* - getUrls: 기본(토글 ON) 상태에서 목록/카테고리/상품 URL 포함
* - getUrls: SEO 제공 페이지 토글 OFF 시 해당 URL 유형 제외 (회귀)
*
* @group ecommerce
* @group unit
* @group seo
*/
class EcommerceSitemapContributorTest extends ModuleTestCase
{
private EcommerceSitemapContributor $contributor;
private string $routePath = 'shop';
protected function setUp(): void
{
parent::setUp();
// Repository 주입이 필요하므로 컨테이너로 해석합니다.
$this->contributor = $this->app->make(EcommerceSitemapContributor::class);
$this->routePath = g7_module_settings('sirsoft-ecommerce', 'basic_info.route_path') ?? 'shop';
}
/**
* 테스트용 활성 카테고리 1건을 생성합니다.
*/
private function createTestCategory(): Category
{
return Category::create([
'name' => ['ko' => '사이트맵 카테고리', 'en' => 'Sitemap Category'],
'slug' => 'sitemap-cat',
'is_active' => true,
'path' => '/',
'depth' => 0,
'sort_order' => 0,
]);
}
/**
* SitemapContributorInterface 를 구현한다
*/
public function test_implements_sitemap_contributor_interface(): void
{
$this->assertInstanceOf(SitemapContributorInterface::class, $this->contributor);
}
/**
* getIdentifier: 'sirsoft-ecommerce' 반환
*/
public function test_get_identifier_returns_sirsoft_ecommerce(): void
{
$this->assertSame('sirsoft-ecommerce', $this->contributor->getIdentifier());
}
/**
* getUrls: route_path 미설정 시 기본값 'shop' 을 사용한다
*/
public function test_get_urls_uses_default_route_path_when_not_configured(): void
{
Config::set('g7_settings.modules.sirsoft-ecommerce.basic_info.route_path', null);
$contributor = $this->app->make(EcommerceSitemapContributor::class);
$urlPaths = array_column($contributor->getUrls(), 'url');
$this->assertContains('/shop/products', $urlPaths);
}
/**
* getUrls: 정적/카테고리/상품 URL 항목이 각각 올바른 키 구조를 가진다
*/
public function test_url_entries_have_correct_structure(): void
{
$category = $this->createTestCategory();
$product = Product::factory()->create();
$urls = collect($this->contributor->getUrls());
// 정적 목록 URL — lastmod 없음
$index = $urls->firstWhere('url', "/{$this->routePath}/products");
$this->assertNotNull($index);
$this->assertSame('daily', $index['changefreq']);
$this->assertSame(0.7, $index['priority']);
$this->assertArrayNotHasKey('lastmod', $index);
// 카테고리 URL — lastmod 있음
$categoryEntry = $urls->firstWhere('url', "/{$this->routePath}/category/{$category->slug}");
$this->assertNotNull($categoryEntry);
$this->assertArrayHasKey('lastmod', $categoryEntry);
$this->assertSame('weekly', $categoryEntry['changefreq']);
$this->assertSame(0.6, $categoryEntry['priority']);
// 상품 URL — lastmod 있음
$productEntry = $urls->firstWhere('url', "/{$this->routePath}/products/{$product->id}");
$this->assertNotNull($productEntry);
$this->assertArrayHasKey('lastmod', $productEntry);
$this->assertSame('weekly', $productEntry['changefreq']);
$this->assertSame(0.8, $productEntry['priority']);
}
/**
* getUrls: 기여자당 URL 안전 상한을 초과하면 상품 URL이 잘린다
*/
public function test_get_urls_truncates_at_max_urls_per_contributor(): void
{
Product::factory()->count(3)->create();
// 정적 목록 URL 1건이 이미 담기므로 상품은 1건까지만 담긴다.
// g7_core_settings 는 Config 파사드 기반이므로 Config::set 으로 주입합니다.
Config::set('g7_settings.core.seo.sitemap_max_urls_per_contributor', 2);
$this->assertCount(2, $this->contributor->getUrls());
}
/**
* getUrls: 상한이 카테고리 루프에도 적용된다 (회귀)
*
* 상한 검사가 상품 루프에만 있으면, 카테고리만으로 상한을 초과해도 잘리지 않는다.
*/
public function test_get_urls_truncates_within_category_loop(): void
{
$this->createTestCategory();
Category::create([
'name' => ['ko' => '사이트맵 카테고리2', 'en' => 'Sitemap Category 2'],
'slug' => 'sitemap-cat-2',
'is_active' => true,
'path' => '/',
'depth' => 0,
'sort_order' => 1,
]);
Product::factory()->count(2)->create();
// 정적 목록 1건 + 카테고리 2건 = 3건이지만 상한은 2건.
Config::set('g7_settings.core.seo.sitemap_max_urls_per_contributor', 2);
$urls = $this->contributor->getUrls();
$this->assertCount(2, $urls, '카테고리 루프에서도 상한이 지켜져야 합니다.');
// 상한 도달 후에는 상품 URL 이 하나도 담기지 않아야 한다
$this->assertEmpty(
array_filter($urls, fn (array $u): bool => str_contains($u['url'], '/products/')),
'상한 도달 후 상품 URL 이 추가되면 안 됩니다.'
);
}
/**
* getUrls: 기본(토글 ON) 상태에서 목록/카테고리/상품 URL이 모두 포함된다 (비파괴 회귀)
*/
public function test_get_urls_includes_all_when_toggles_default_on(): void
{
$category = $this->createTestCategory();
$product = Product::factory()->create();
$urls = $this->contributor->getUrls();
$urlPaths = array_column($urls, 'url');
$this->assertContains("/{$this->routePath}/products", $urlPaths);
$this->assertContains("/{$this->routePath}/category/{$category->slug}", $urlPaths);
$this->assertContains("/{$this->routePath}/products/{$product->id}", $urlPaths);
}
/**
* getUrls: seo_shop_index 토글 OFF 시 상품 목록 URL이 제외된다 (회귀)
*/
public function test_get_urls_excludes_shop_index_when_toggle_off(): void
{
Config::set('g7_settings.modules.sirsoft-ecommerce.seo.seo_shop_index', false);
$urls = $this->contributor->getUrls();
$urlPaths = array_column($urls, 'url');
$this->assertNotContains("/{$this->routePath}/products", $urlPaths);
}
/**
* getUrls: seo_category 토글 OFF 시 카테고리 URL이 제외된다 (회귀)
*/
public function test_get_urls_excludes_category_when_toggle_off(): void
{
$category = $this->createTestCategory();
Config::set('g7_settings.modules.sirsoft-ecommerce.seo.seo_category', false);
$urls = $this->contributor->getUrls();
$urlPaths = array_column($urls, 'url');
$this->assertNotContains("/{$this->routePath}/category/{$category->slug}", $urlPaths);
}
/**
* getUrls: seo_product_detail 토글 OFF 시 상품 상세 URL이 제외된다 (회귀)
*/
public function test_get_urls_excludes_product_detail_when_toggle_off(): void
{
$product = Product::factory()->create();
Config::set('g7_settings.modules.sirsoft-ecommerce.seo.seo_product_detail', false);
$urls = $this->contributor->getUrls();
$urlPaths = array_column($urls, 'url');
$this->assertNotContains("/{$this->routePath}/products/{$product->id}", $urlPaths);
}
/**
* getUrlsLazy: 배열을 실체화하지 않는 지연 제너레이터로 URL 을 흘려보낸다 (⑭ 스트리밍)
*/
public function test_get_urls_lazy_streams_entries(): void
{
$product = Product::factory()->create();
$this->assertInstanceOf(AbstractSitemapContributor::class, $this->contributor);
$lazy = $this->contributor->getUrlsLazy();
$this->assertInstanceOf(\Traversable::class, $lazy);
$urlPaths = array_column(iterator_to_array($lazy, false), 'url');
$this->assertContains("/{$this->routePath}/products/{$product->id}", $urlPaths);
}
}