Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Correct media image link from seo facebook.twitter. Improve seo servi…
Browse files Browse the repository at this point in the history
…ce unit test
  • Loading branch information
salamichel authored and OskarStark committed Nov 14, 2019
1 parent a6e0af2 commit 981e452
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/ProductBundle/Seo/Services/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function alterPage(SeoPageInterface $seoPage, ProductInterface $product):
'slug' => $product->getSlug(),
'productId' => $product->getId(),
], UrlGeneratorInterface::ABSOLUTE_URL))
->addMeta('property', 'product:price:amount', $this->numberHelper->formatDecimal($product->getPrice()))
->addMeta('property', 'product:price:amount', (string) $this->numberHelper->formatDecimal($product->getPrice()))
->addMeta('property', 'product:price:currency', $this->currencyDetector->getCurrency()->getLabel());

// If a media is available, we add the opengraph image data
Expand All @@ -97,9 +97,9 @@ protected function addImageInfo(MediaInterface $image, SeoPageInterface $seoPage
{
$provider = $this->mediaPool->getProvider($image->getProviderName());

$seoPage->addMeta('property', 'og:image', $provider->generatePublicUrl($image, $this->mediaFormat))
->addMeta('property', 'og:image:width', $image->getWidth())
->addMeta('property', 'og:image:height', $image->getHeight())
$seoPage->addMeta('property', 'og:image', $this->domain.$provider->generatePublicUrl($image, $this->mediaFormat))
->addMeta('property', 'og:image:width', (string) $image->getWidth())
->addMeta('property', 'og:image:height', (string) $image->getHeight())
->addMeta('property', 'og:image:type', $image->getContentType());
}

Expand Down
4 changes: 2 additions & 2 deletions src/ProductBundle/Seo/Services/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function alterPage(SeoPageInterface $seoPage, ProductInterface $product):
->addMeta('name', 'twitter:title', $product->getName())
->addMeta('name', 'twitter:description', substr((string) $product->getDescription(), 0, 200))
->addMeta('name', 'twitter:label1', 'Price')
->addMeta('name', 'twitter:data1', $this->numberHelper->formatCurrency($product->getPrice(), $this->currencyDetector->getCurrency()->getLabel()))
->addMeta('name', 'twitter:data1', (string) $this->numberHelper->formatCurrency($product->getPrice(), $this->currencyDetector->getCurrency()->getLabel()))
->addMeta('name', 'twitter:label2', 'SKU')
->addMeta('name', 'twitter:data2', $product->getSku())
->addMeta('name', 'twitter:site', $this->site)
Expand All @@ -96,7 +96,7 @@ public function alterPage(SeoPageInterface $seoPage, ProductInterface $product):

if ($image = $product->getImage()) {
$provider = $this->mediaPool->getProvider($image->getProviderName());
$seoPage->addMeta('property', 'twitter:image:src', $provider->generatePublicUrl($image, $this->mediaFormat));
$seoPage->addMeta('property', 'twitter:image:src', $this->domain.$provider->generatePublicUrl($image, $this->mediaFormat));
}
}
}
84 changes: 83 additions & 1 deletion tests/ProductBundle/Seo/Services/FacebookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Sonata\Component\Currency\Currency;
use Sonata\Component\Currency\CurrencyDetectorInterface;
use Sonata\IntlBundle\Templating\Helper\NumberHelper;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\ImageProvider;
use Sonata\MediaBundle\Provider\Pool;
use Sonata\ProductBundle\Entity\BaseProduct;
use Sonata\ProductBundle\Seo\Services\Facebook;
Expand All @@ -39,6 +41,21 @@ public function getDescription()
{
return 'O-some product';
}

public function getName()
{
return 'Product1';
}

public function getSku()
{
return 'Sku1';
}

public function getPrice($vat = false)
{
return 123.56;
}
}

class FacebookTest extends TestCase
Expand All @@ -60,6 +77,10 @@ public function testAlterPage()
->method('getCurrency')
->willReturn($currency);

$numberHelper->expects($this->any())
->method('formatDecimal')
->willReturn($product->getPrice());

// Check if the header data are correctly registered
$fbService = new Facebook($router, $mediaPool, $numberHelper, $currencyDetector, 'test', 'test', 'reference');
$fbService->alterPage($seoPage, $product);
Expand All @@ -69,6 +90,67 @@ public function testAlterPage()

$content = $extension->getMetadatas();

$this->assertContains('O-some product', $content);
$this->assertContains('property="og:description" content="O-some product"', $content);
$this->assertContains('property="og:title" content="Product1"', $content);
$this->assertContains('property="product:price:amount" content="123.56"', $content);
}

public function testAlterPageImage()
{
$mediaPool = $this->createMock(Pool::class);
$seoPage = new SeoPage('test');
$extension = new SeoExtension($seoPage, 'UTF-8');
$numberHelper = $this->createMock(NumberHelper::class);
$currencyDetector = $this->createMock(CurrencyDetectorInterface::class);
$product = new ProductFbMock();
$router = $this->createMock(RouterInterface::class);

//Prepare currency
$currency = new Currency();
$currency->setLabel('EUR');
$currencyDetector->expects($this->any())
->method('getCurrency')
->willReturn($currency);

// Test getImage
$image = $this->createMock(MediaInterface::class);

$imageProvider = $this->createMock(ImageProvider::class);
$imageProvider->expects($this->any())
->method('generatePublicUrl')->willReturn('/upload/dummy.png');

$image->expects($this->any())
->method('getName')->willReturn('correctMedia');
$image->expects($this->any())
->method('getWidth')->willReturn(1111);
$image->expects($this->any())
->method('getHeight')->willReturn(2222);
$image->expects($this->any())
->method('getProviderName')->willReturn($imageProvider);
$image->expects($this->any())
->method('getContentType')->willReturn('image/png');

$mediaPool->expects($this->any())
->method('getProvider')->willReturn($imageProvider);

$product->setImage($image);

// Check if the header data are correctly registered
$fbService = new Facebook($router, $mediaPool, $numberHelper, $currencyDetector, 'http://my-domain.ltd', 'test', 'reference');
$fbService->alterPage($seoPage, $product);
$content = $extension->getHeadAttributes();

$this->assertContains('fb: http://ogp.me/ns/fb#', $content);

$content = $extension->getMetadatas();

// image link
$this->assertContains('http://my-domain.ltd/upload/dummy.png', $content);
//image width
$this->assertContains('meta property="og:image:width" content="1111"', $content);
//image height
$this->assertContains('meta property="og:image:height" content="2222"', $content);
//image content type
$this->assertContains('meta property="og:image:type" content="image/png"', $content);
}
}
71 changes: 69 additions & 2 deletions tests/ProductBundle/Seo/Services/TwitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Sonata\Component\Currency\Currency;
use Sonata\Component\Currency\CurrencyDetectorInterface;
use Sonata\IntlBundle\Templating\Helper\NumberHelper;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\ImageProvider;
use Sonata\MediaBundle\Provider\Pool;
use Sonata\ProductBundle\Entity\BaseProduct;
use Sonata\ProductBundle\Seo\Services\Twitter;
Expand All @@ -33,6 +35,26 @@ class ProductTwitterMock extends BaseProduct
public function getId()
{
}

public function getName()
{
return 'Product1';
}

public function getSku()
{
return 'Sku1';
}

public function getPrice($vat = false)
{
return 123.56;
}

public function getDescription()
{
return 'O-some product';
}
}

class TwitterTest extends TestCase
Expand All @@ -46,18 +68,63 @@ public function testAlterPage(): void
$currencyDetector = $this->createMock(CurrencyDetectorInterface::class);
$product = new ProductTwitterMock();

$numberHelper->expects($this->any())
->method('formatCurrency')
->willReturn($product->getPrice());

//Prepare currency
$currency = new Currency();
$currency->setLabel('EUR');
$currencyDetector->expects($this->any())
->method('getCurrency')
->willReturn($currency);

$twitterService = new Twitter($mediaPool, $numberHelper, $currencyDetector, 'test', 'test', 'test', 'test', 'reference');
$twitterService = new Twitter($mediaPool, $numberHelper, $currencyDetector, 'mySiteName', 'Sonata', 'test', 'test', 'reference');
$twitterService->alterPage($seoPage, $product);
$content = $extension->getMetadatas();

$this->assertContains('twitter:label1', $content);
$this->assertContains('name="twitter:label1" content="Price"', $content);
$this->assertNotContains('twitter:image:src', $content);
$this->assertContains('name="twitter:site" content="mySiteName"', $content);
$this->assertContains('name="twitter:creator" content="Sonata"', $content);
$this->assertContains('name="twitter:data2" content="Sku1"', $content);
$this->assertContains('name="twitter:title" content="Product1"', $content);
$this->assertContains('name="twitter:description" content="O-some product"', $content);
$this->assertContains('name="twitter:data1" content="123.56"', $content);
}

public function testAlterPageImage(): void
{
$mediaPool = $this->createMock(Pool::class);
$seoPage = new SeoPage('test');
$extension = new SeoExtension($seoPage, 'UTF-8');
$numberHelper = $this->createMock(NumberHelper::class);
$currencyDetector = $this->createMock(CurrencyDetectorInterface::class);
$product = new ProductTwitterMock();

//Prepare currency
$currency = new Currency();
$currency->setLabel('EUR');
$currencyDetector->expects($this->any())
->method('getCurrency')
->willReturn($currency);

// Test getImage
$image = $this->createMock(MediaInterface::class);

$imageProvider = $this->createMock(ImageProvider::class);
$imageProvider->expects($this->any())
->method('generatePublicUrl')->willReturn('/upload/dummy.png');

$mediaPool->expects($this->any())
->method('getProvider')->willReturn($imageProvider);

$product->setImage($image);

$twitterService = new Twitter($mediaPool, $numberHelper, $currencyDetector, 'mySiteName', 'Sonata', 'http://my-domain.ltd', 'reference');
$twitterService->alterPage($seoPage, $product);
$content = $extension->getMetadatas();

$this->assertContains('property="twitter:image:src" content="http://my-domain.ltd/upload/dummy.png"', $content);
}
}

0 comments on commit 981e452

Please sign in to comment.