Skip to content

Error when creating a store view if there are CMS pages with the same URL key #28421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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