Skip to content

Commit

Permalink
[task] add robots controller, small corrections, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Hauri committed Jan 12, 2018
1 parent a26df3d commit dc12074
Show file tree
Hide file tree
Showing 24 changed files with 576 additions and 36 deletions.
21 changes: 18 additions & 3 deletions Block/HrefLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,30 @@

namespace Staempfli\Seo\Block;

use Magento\Framework\View\Element\Template;
use Magento\Store\Block\Switcher;

class HrefLang extends Switcher
class HrefLang extends Template
{
/**
* @var Switcher
*/
private $switcher;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Switcher $switcher,
array $data = []
) {
parent::__construct($context, $data);
$this->switcher = $switcher;
}

public function getAlternatives()
{
$data = [];
foreach ((array) $this->getStores() as $store) {
if ($store->getId() !== $this->getCurrentStoreId()) {
foreach ((array) $this->switcher->getStores() as $store) {
if ($store->getId() !== $this->switcher->getCurrentStoreId()) {
$data[substr($store->getLocaleCode(), 0, 2)] = $store->getCurrentUrl(false);
}
}
Expand Down
36 changes: 36 additions & 0 deletions Controller/Index/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare (strict_types=1);
/**
* Copyright © 2018 Stämpfli AG. All rights reserved.
* @author [email protected]
*/

namespace Staempfli\Seo\Controller\Index;

use Magento\Framework\App\Action\Context;
use Staempfli\Seo\Model\Robots;

class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var Robots
*/
private $robots;

public function __construct(
Robots $robots,
Context $context
) {
parent::__construct($context);
$this->robots = $robots;
}

public function execute()
{
$result = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
$result->setHeader('Content-Type', 'text/plain')->setContents(
$this->robots->getContent()
);
return $result;
}
}
9 changes: 5 additions & 4 deletions Model/Adapter/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public function __construct(
public function getProperty() : PropertyInterface
{
if ($this->page->getId()) {
$this->property
->setTitle((string) $this->page->getTitle())
->setDescription((string) $this->filterProvider->getBlockFilter()->filter($this->page->getContent()))
->setUrl((string) $this->url->getUrl($this->page->getIdentifier()));
$this->property->setTitle((string) $this->page->getTitle());
$this->property->setDescription(
(string) $this->filterProvider->getBlockFilter()->filter($this->page->getContent())
);
$this->property->setUrl((string) $this->url->getUrl($this->page->getIdentifier()));
}
return $this->property;
}
Expand Down
8 changes: 6 additions & 2 deletions Model/BlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public function __construct(

public function getBlockContentById(int $blockId) : string
{
$data = $this->blockRepository->getById($blockId)->toArray();
return html_entity_decode($data['content']) ?? ''; //@codingStandardsIgnoreLine
try {
$cmsBlock = $this->blockRepository->getById($blockId);
return html_entity_decode($cmsBlock->getData('content')) ?? ''; //@codingStandardsIgnoreLine
} catch (\Exception $e) {
return '';
}
}
}
6 changes: 6 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Config
const XML_PATH_SEO_TWITTER_DEFAULT_TYPE = 'seo/twitter_card/type';
const XML_PATH_SEO_TWITTER_DEFAULT_SITE = 'seo/twitter_card/site';
const XML_PATH_SEO_TWITTER_DEFAULT_CREATOR = 'seo/twitter_card/creator';
const XML_PATH_ROBOTS_CONTENT = 'seo/robots/content';
/**
* @var ScopeConfigInterface
*/
Expand Down Expand Up @@ -73,6 +74,11 @@ public function getDefaultTwitterCardCreator() : string
return $this->getConfigValue(self::XML_PATH_SEO_TWITTER_DEFAULT_CREATOR);
}

public function getRobotsContent() : string
{
return $this->getConfigValue(self::XML_PATH_ROBOTS_CONTENT);
}

private function getConfigValue(string $configPath) : string
{
$result = $this->scopeConfig->getValue(
Expand Down
93 changes: 93 additions & 0 deletions Model/Robots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
declare (strict_types=1);
/**
* Copyright © 2018 Stämpfli AG. All rights reserved.
* @author [email protected]
*/

namespace Staempfli\Seo\Model;

use Magento\Framework\UrlInterface;
use Magento\Sitemap\Model\Sitemap;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Api\StoreResolverInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;

/**
* Class Robots
* @package Staempfli\Seo\Model
*/
class Robots
{
/**
* @var Config
*/
private $config;
/**
* @var Sitemap
*/
private $sitemap;
/**
* @var WebsiteRepositoryInterface
*/
private $websiteRepository;
/**
* @var StoreResolverInterface
*/
private $storeResolver;

public function __construct(
Config $config,
Sitemap $sitemap,
WebsiteRepositoryInterface $websiteRepository,
StoreResolverInterface $storeResolver
) {
$this->config = $config;
$this->sitemap = $sitemap;
$this->websiteRepository = $websiteRepository;
$this->storeResolver = $storeResolver;
}

public function getContent() : string
{
$website = $this->getCurrentWebsite();
$content = $this->getSitemapsContent($website);
$content[] = $this->config->getRobotsContent();

return implode(PHP_EOL, $content);
}

private function getCurrentWebsite()
{
$websites = $this->websiteRepository->getList();
$currentStoreId = $this->storeResolver->getCurrentStoreId();
foreach ($websites as $website) {
if (in_array($currentStoreId, array_keys($website->getStores()))) {
return $website;
}
}
return $this->websiteRepository->getDefault();
}

protected function getSitemapsContent($website) : array
{
$data = [];
$sitemaps = $this->sitemap
->getCollection()
->addFieldToFilter('store_id', ['in' => array_keys($website->getStores())]);

if (!$sitemaps) {
return $data;
}

foreach ($sitemaps as $sitemap) {
$data[] = sprintf(
'Sitemap: %s%s%s',
rtrim($website->getDefaultStore()->getBaseUrl(UrlInterface::URL_TYPE_DIRECT_LINK), '/'),
$sitemap->getSitemapPath(),
$sitemap->getSitemapFilename()
);
}
return $data;
}
}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ By Adding:
<link rel="alternate" hreflang="fr" href="http://example.com/fr/content-pages/demo.html" />
<link rel="alternate" hreflang="en" href="http://example.com/en/content-pages/demo.html" />
```

## Webmaster Tools
Allowing you to set the verification code in the backend for:

- Google Webmaster Tools
Expand All @@ -97,6 +97,10 @@ Allowing you to set the verification code in the backend for:
<meta name="yandex-verification" content="YOUR_YANDEX_VERIFICATION_CODE" />
```

## Robots (robots.txt)

See: [Robots Configuration](docs/Robots.md)

## Requirements

- PHP: 7.0.x | 7.1.x
Expand Down
55 changes: 55 additions & 0 deletions Test/Unit/Block/AbstractBlockSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © 2018 Stämpfli AG. All rights reserved.
* @author [email protected]
*/
namespace Staempfli\Seo\Test\Unit\Block;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Staempfli\Seo\Model\AdapterInterface;
use Staempfli\Seo\Model\Config;
use Staempfli\Seo\Model\Property;

abstract class AbstractBlockSetup extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManager
*/
protected $objectManager;

/**
* @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
*/
protected $context;
/**
* @var \Staempfli\Seo\Model\Config|\PHPUnit_Framework_MockObject_MockObject
*/
protected $config;
/**
* @var \Staempfli\Seo\Model\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $adapterInterface;
/**
* @var \Staempfli\Seo\Model\PropertyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $propertyInterface;

public function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
->disableOriginalConstructor()
->getMock();

$this->config = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->getMock();

$this->propertyInterface = new Property();

$this->adapterInterface = $this->getMockBuilder(AdapterInterface::class)
->disableOriginalConstructor()
->getMock();
$this->adapterInterface->expects($this->any())->method('getProperty')->willReturn($this->propertyInterface);
}
}
19 changes: 13 additions & 6 deletions Test/Unit/Block/HrefLangTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
*/
namespace Staempfli\Seo\Test\Unit\Block;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Staempfli\Seo\Block\HrefLang;

/**
* @coversDefaultClass \Staempfli\Seo\Block\HrefLang
*/
final class HrefLangTest extends \PHPUnit\Framework\TestCase
final class HrefLangTest extends AbstractBlockSetup
{
/**
* @var HrefLang
Expand All @@ -21,14 +20,22 @@ final class HrefLangTest extends \PHPUnit\Framework\TestCase

public function setUp()
{
$objectManager = new ObjectManager($this);
$this->block = $objectManager->getObject(
HrefLang::class
parent::setUp();
$switcher = $this->getMockBuilder(\Magento\Store\Block\Switcher::class)
->disableOriginalConstructor()
->getMock();

$this->block = $this->objectManager->getObject(
HrefLang::class,
[
'context' => $this->context,
'switcher' => $switcher
]
);
}

public function testGetAlternatives()
{
$this->markTestSkipped();
$this->assertSame([], $this->block->getAlternatives());
}
}
31 changes: 31 additions & 0 deletions Test/Unit/Block/OpenGraphTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare (strict_types=1);
/**
* Copyright © 2018 Stämpfli AG. All rights reserved.
* @author [email protected]
*/
namespace Staempfli\Seo\Test\Unit\Block;

use Staempfli\Seo\Block\OpenGraph;

/**
* @coversDefaultClass \Staempfli\Seo\Block\OpenGraph
*/
final class OpenGraphTest extends AbstractBlockSetup
{
/**
* @var OpenGraph
*/
private $block;

public function setUp()
{
parent::setUp();
$this->block = new OpenGraph($this->context, $this->adapterInterface, []);
}

public function testGetMetaData()
{
$this->assertSame('', $this->block->getMetaData());
}
}
Loading

0 comments on commit dc12074

Please sign in to comment.