Skip to content

Commit

Permalink
modify test (mb_substr)
Browse files Browse the repository at this point in the history
  • Loading branch information
h.matsuo committed Mar 29, 2021
1 parent fcc5fcd commit b0f8d15
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
13 changes: 7 additions & 6 deletions src/Eccube/Resource/template/default/meta.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
<meta property="product:price:currency" content="{{ eccube_config.currency }}"/>
<meta property="product:product_link" content="{{ url('product_detail', {'id': Product.id}) }}"/>
<meta property="product:retailer_title" content="{{ BaseInfo.shop_name }}"/>
{% elseif Category.id is defined %}
{% elseif Category is defined %}
{% set meta_og_type = 'article' %}
{% set meta_description = Page.description %}
{% set meta_canonical = url('product_list', {'category_id': Category.id}) %}
{% set meta_canonical = url('product_list', {'category_id': Category.id|default(null)}) %}
{% elseif Page is defined %}
{% set meta_og_type = (app.request.get('_route') == 'homepage') ? 'website' : 'article' %}
{% set meta_description = Page.description %}
{% set meta_canonical = url(Page.url) %}
{% endif %}
<meta property="og:type" content="{{ meta_og_type }}"/>
<meta property="og:site_name" content="{{ BaseInfo.shop_name }}"/>
<meta property="og:url" content="{{ meta_canonical }}"/>
<link rel="canonical" href="{{ meta_canonical }}"/>
{% if meta_description is not empty %}
{% if meta_description|default() %}
<meta name="description" content="{{ meta_description|striptags|slice(0,120) }}">
<meta property="og:description" content="{{ meta_description|striptags|slice(0,120) }}"/>
{% endif %}
{% if meta_canonical|default() %}
<meta property="og:url" content="{{ meta_canonical }}"/>
<link rel="canonical" href="{{ meta_canonical }}"/>
{% endif %}
21 changes: 13 additions & 8 deletions tests/Eccube/Tests/Web/ProductControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,25 @@ public function testProductFavoriteAddThroughLogin()
*/
public function testMetaTagsInListPage()
{
// カテゴリ指定なし
$url = $this->generateUrl('product_list', [], UrlGeneratorInterface::ABSOLUTE_URL);
$crawler = $this->client->request('GET', $this->generateUrl('product_list'));
$crawler = $this->client->request('GET', $url);
$this->assertEquals('article', $crawler->filter('meta[property="og:type"]')->attr('content'));
$this->assertEquals($url, $crawler->filter('link[rel="canonical"]')->attr('href'));
$this->assertEquals($url, $crawler->filter('meta[property="og:url"]')->attr('content'));

// カテゴリ指定あり
$url = $this->generateUrl('product_list', ['category_id' => 1], UrlGeneratorInterface::ABSOLUTE_URL);
$crawler = $this->client->request('GET', $url);
$this->assertEquals($url, $crawler->filter('link[rel="canonical"]')->attr('href'));
}

/**
* 詳細ページ metaタグのテスト
*/
public function testMetaTagsInDetailPage()
{
$id = 2;
$product = $this->productRepository->find($id); /** @var Product $product */
$product = $this->productRepository->find(2); /** @var Product $product */
$description_detail = 'またそのなかでいっしょになったたくさんのひとたち、ファゼーロとロザーロ、羊飼のミーロや、顔の赤いこどもたち、地主のテーモ、山猫博士のボーガント・デストゥパーゴなど、いまこの暗い巨きな石の建物のなかで考えていると、みんなむかし風のなつかしい青い幻燈のように思われます。';
$description_list = 'では、わたくしはいつかの小さなみだしをつけながら、しずかにあの年のイーハトーヴォの五月から十月までを書きつけましょう。';

Expand All @@ -285,12 +290,12 @@ public function testMetaTagsInDetailPage()
$product->setDescriptionList($description_list);
$product->setDescriptionDetail($description_detail);
$this->entityManager->flush();
$expected_desc = mb_substr($description_list, 0, 120);
$expected_desc = mb_substr($description_list, 0, 120, 'utf-8');

$url = $this->generateUrl('product_detail', ['id' => $id], UrlGeneratorInterface::ABSOLUTE_URL);
$url = $this->generateUrl('product_detail', ['id' => 2], UrlGeneratorInterface::ABSOLUTE_URL);
$imgPath = $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL) . 'html/upload/save_image/' . $product->getMainListImage()->getFileName();

$crawler = $this->client->request('GET', $this->generateUrl('product_detail', ['id' => $id]));
$crawler = $this->client->request('GET', $url);

$this->assertEquals($expected_desc, $crawler->filter('meta[name="description"]')->attr('content'));
$this->assertEquals($expected_desc, $crawler->filter('meta[property="og:description"]')->attr('content'));
Expand All @@ -303,9 +308,9 @@ public function testMetaTagsInDetailPage()
// → meta description には description_detail が設定される
$product->setDescriptionList(null);
$this->entityManager->flush();
$expected_desc = mb_substr($description_detail, 0, 120);
$expected_desc = mb_substr($description_detail, 0, 120, 'utf-8');

$crawler = $this->client->request('GET', $this->generateUrl('product_detail', ['id' => $id]));
$crawler = $this->client->request('GET', $url);

$this->assertEquals($expected_desc, $crawler->filter('meta[name="description"]')->attr('content'));
$this->assertEquals($expected_desc, $crawler->filter('meta[property="og:description"]')->attr('content'));
Expand Down
5 changes: 1 addition & 4 deletions tests/Eccube/Tests/Web/TopControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,13 @@ public function testMetaTags()
$this->entityManager->flush();

$shopName = $this->container->get(BaseInfoRepository::class)->get()->getShopName();
$url = $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL);
$expected_desc = mb_substr($description, 0, 120);
$expected_desc = mb_substr($description, 0, 120, 'utf-8');

$crawler = $this->client->request('GET', $this->generateUrl('homepage'));

$this->assertEquals($shopName, $crawler->filter('meta[property="og:site_name"]')->attr('content'));
$this->assertEquals('website', $crawler->filter('meta[property="og:type"]')->attr('content'));
$this->assertEquals($expected_desc, $crawler->filter('meta[name="description"]')->attr('content'));
$this->assertEquals($expected_desc, $crawler->filter('meta[property="og:description"]')->attr('content'));
$this->assertEquals($url, $crawler->filter('link[rel="canonical"]')->attr('href'));
$this->assertEquals($url, $crawler->filter('meta[property="og:url"]')->attr('content'));
}
}

0 comments on commit b0f8d15

Please sign in to comment.