-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[task] add robots controller, small corrections, update tests
- Loading branch information
Marcel Hauri
committed
Jan 12, 2018
1 parent
a26df3d
commit dc12074
Showing
24 changed files
with
576 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.