Skip to content

Commit

Permalink
fix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed May 29, 2020
1 parent ac3aa70 commit 035aaf0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
22 changes: 19 additions & 3 deletions app/code/Magento/CmsUrlRewrite/Plugin/Cms/Model/Store/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\CmsUrlRewrite\Plugin\Cms\Model\Store;

use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\CmsUrlRewrite\Model\CmsPageUrlRewriteGenerator;
use Magento\Framework\Api\SearchCriteriaBuilder;
Expand All @@ -21,6 +22,8 @@
*/
class View
{
private const ALL_STORE_VIEWS = '0';

/**
* @var UrlPersistInterface
*/
Expand Down Expand Up @@ -89,14 +92,27 @@ private function generateCmsPagesUrls(int $storeId): array
{
$rewrites = [];
$urls = [];
$searchCriteria = $this->searchCriteriaBuilder->create();
$cmsPagesCollection = $this->pageRepository->getList($searchCriteria)->getItems();
foreach ($cmsPagesCollection as $page) {

foreach ($this->getCmsPageItems() as $page) {
$page->setStoreId($storeId);
$rewrites[] = $this->cmsPageUrlRewriteGenerator->generate($page);
}
$urls = array_merge($urls, ...$rewrites);

return $urls;
}

/**
* Return cms page items for all store view
*
* @return PageInterface[]
*/
private function getCmsPageItems(): array
{
$searchCriteria = $this->searchCriteriaBuilder->addFilter('store_id', self::ALL_STORE_VIEWS)
->create();
$list = $this->pageRepository->getList($searchCriteria);

return $list->getItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\CmsUrlRewrite\Plugin\Cms\Model\Store;
Expand All @@ -13,13 +14,14 @@
use Magento\TestFramework\Helper\Bootstrap;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use PHPUnit\Framework\TestCase;

/**
* Test for plugin which is listening store resource model and on save replace cms page url rewrites
* Test for plugin which is listening store resource model and on save replace cms page url rewrites.
*
* @magentoAppArea adminhtml
*/
class ViewTest extends \PHPUnit\Framework\TestCase
class ViewTest extends TestCase
{
/**
* @var UrlFinderInterface
Expand Down Expand Up @@ -49,26 +51,32 @@ protected function setUp()
/**
* Test of replacing cms page url rewrites on create and delete store
*
* @magentoDataFixture Magento/Cms/_files/two_cms_page_with_same_url_for_different_stores.php
* @magentoDataFixture Magento/Cms/_files/pages.php
*
* @return void
*/
public function testUrlRewritesChangesAfterStoreSave()
public function testUrlRewritesChangesAfterStoreSave(): void
{
$storeId = $this->createStore();
$this->assertUrlRewritesCount($storeId, 1);
$this->assertUrlRewritesCount($storeId, 'page100', 1);
$this->assertUrlRewritesCount($storeId, 'page1', 0);
$this->deleteStore($storeId);
$this->assertUrlRewritesCount($storeId, 0);
$this->assertUrlRewritesCount($storeId, 'page100', 0);
}

/**
* Assert url rewrites count by store id
* Assert url rewrites count by store id and request path
*
* @param int $storeId
* @param string $requestPath
* @param int $expectedCount
* @return void
*/
private function assertUrlRewritesCount(int $storeId, int $expectedCount): void
private function assertUrlRewritesCount(int $storeId, string $requestPath, int $expectedCount): void
{
$data = [
UrlRewrite::REQUEST_PATH => 'page100',
UrlRewrite::REQUEST_PATH => $requestPath,
UrlRewrite::STORE_ID => $storeId
];
$urlRewrites = $this->urlFinder->findAllByData($data);
Expand Down

0 comments on commit 035aaf0

Please sign in to comment.