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 all commits
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 @@ -25,7 +25,7 @@
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
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -84,9 +84,10 @@ protected function setUp(): void
/**
* 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
*/
public function testUrlRewritesChangesAfterStoreSave()
public function testUrlRewritesChangesAfterStoreSave(): void
{
$storeId = $this->createStore();
$this->assertUrlRewritesCount($storeId, 'page100', 1);
Expand All @@ -98,16 +99,16 @@ public function testUrlRewritesChangesAfterStoreSave()
}

/**
* Assert url rewrites count by store id
* Assert url rewrites count by store id and request path
*
* @param int $storeId
* @param string $pageIdentifier
* @param string $requestPath
* @param int $expectedCount
*/
private function assertUrlRewritesCount(int $storeId, string $pageIdentifier, int $expectedCount): void
private function assertUrlRewritesCount(int $storeId, string $requestPath, int $expectedCount): void
{
$data = [
UrlRewrite::REQUEST_PATH => $pageIdentifier,
UrlRewrite::REQUEST_PATH => $requestPath,
UrlRewrite::STORE_ID => $storeId
];
$urlRewrites = $this->urlFinder->findAllByData($data);
Expand All @@ -116,8 +117,6 @@ private function assertUrlRewritesCount(int $storeId, string $pageIdentifier, in

/**
* Create test store
*
* @return int
*/
private function createStore(): int
{
Expand All @@ -134,7 +133,6 @@ private function createStore(): int
* Delete test store
*
* @param int $storeId
* @return void
*/
private function deleteStore(int $storeId): void
{
Expand Down