Skip to content
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

Drop page id from result #38667

Open
wants to merge 16 commits into
base: 2.4-develop
Choose a base branch
from
7 changes: 6 additions & 1 deletion app/code/Magento/Cms/Model/GetUtilityPageIdentifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function __construct(

/**
* Get List Page Identifiers.
* When two CMS pages have the same URL key (in different stores),
* the value stored in configuration is 'url-key|ID'.
* This method strips the trailing '|ID' so that this method returns
* only configured url-key mathcing the current scope store ID.
* See https://github.com/magento/magento2/issues/35001
*
* @return array
*/
Expand All @@ -50,6 +55,6 @@ public function execute()
ScopeInterface::SCOPE_STORE
);

return [$homePageIdentifier, $noRouteIdentifier, $noCookieIdentifier];
return [strtok($homePageIdentifier, '|'), strtok($noRouteIdentifier, '|'), strtok($noCookieIdentifier, '|')];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Magento\Cms\Model\GetUtilityPageIdentifiers;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -24,52 +23,53 @@ class GetUtilityPageIdentifiersTest extends TestCase
*
* @var GetUtilityPageIdentifiers
*/
private $model;
private GetUtilityPageIdentifiers $model;

/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfig;
private ScopeConfigInterface|MockObject $scopeConfig;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
->setMethods(['getValue'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->model = $objectManager->getObject(
GetUtilityPageIdentifiers::class,
[
'scopeConfig' => $this->scopeConfig,
]
);
$this->model = new GetUtilityPageIdentifiers($this->scopeConfig);
}

/**
* Test GetUtilityPageIdentifiers::execute() will read config for getting correct routes.
*
* @return void
* @dataProvider webDefaultPages
*/
public function testExecute()
public function testExecute(array $configValues, array $identifiers): void
{
$cmsHomePage = 'testCmsHomePage';
$cmsNoRoute = 'testCmsNoRoute';
$cmsNoCookies = 'testCmsNoCookies';
$this->scopeConfig->expects($this->exactly(3))
->method('getValue')
->withConsecutive(
[$this->identicalTo('web/default/cms_home_page'), $this->identicalTo(ScopeInterface::SCOPE_STORE)],
[$this->identicalTo('web/default/cms_no_route'), $this->identicalTo(ScopeInterface::SCOPE_STORE)],
[$this->identicalTo('web/default/cms_no_cookies'), $this->identicalTo(ScopeInterface::SCOPE_STORE)]
)->willReturnOnConsecutiveCalls(
$cmsHomePage,
$cmsNoRoute,
$cmsNoCookies
);
$this->assertSame([$cmsHomePage, $cmsNoRoute, $cmsNoCookies], $this->model->execute());
)->willReturnOnConsecutiveCalls(...$configValues);
$this->assertSame($identifiers, $this->model->execute());
}

public function webDefaultPages(): array
{
return [
[
['testCmsHomePage', 'testCmsNoRoute', 'testCmsNoCookies'],
['testCmsHomePage', 'testCmsNoRoute', 'testCmsNoCookies']
],
[
['testCmsHomePage|1', 'testCmsNoRoute|1', 'testCmsNoCookies|1'],
['testCmsHomePage', 'testCmsNoRoute', 'testCmsNoCookies']
]
];
}
}
14 changes: 2 additions & 12 deletions app/code/Magento/Sitemap/Model/ResourceModel/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,9 @@ public function getCollection($storeId)
'main_table.is_active = 1'
)->where(
'main_table.identifier NOT IN (?)',
array_map(
// When two CMS pages have the same URL key (in different
// stores), the value stored in configuration is 'url-key|ID'.
// This function strips the trailing '|ID' so that this where()
// matches the url-key configured.
// See https://github.com/magento/magento2/issues/35001
static function ($urlKey) {
return explode('|', $urlKey, 2)[0];
},
$this->getUtilityPageIdentifiers->execute()
)
$this->getUtilityPageIdentifiers->execute()
)->where(
'store_table.store_id IN(?)',
'store_table.store_id IN (?)',
[0, $storeId]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Sitemap\Test\Unit\Model\ResourceModel\Cms;

use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Model\GetUtilityPageIdentifiers;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DataObject;
use Magento\Framework\DB\Adapter\AdapterInterface;
Expand All @@ -21,6 +20,7 @@
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Sitemap\Model\ResourceModel\Cms\Page;
use Magento\Store\Model\ScopeInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -37,32 +37,37 @@ class PageTest extends TestCase
*
* @var Page
*/
private $model;
private Page $model;

/**
* @var Context|MockObject
*/
private $context;
private Context|MockObject $context;

/**
* @var MetadataPool|MockObject
*/
private $metadataPool;
private MetadataPool|MockObject $metadataPool;

/**
* @var EntityManager|MockObject
*/
private $entityManager;
private EntityManager|MockObject $entityManager;

/**
* @var GetUtilityPageIdentifiers|MockObject
* @var GetUtilityPageIdentifiers
*/
private $getUtilityPageIdentifiers;
private GetUtilityPageIdentifiers $getUtilityPageIdentifiers;

/**
* @var ResourceConnection|MockObject
*/
private $resource;
private ResourceConnection|MockObject $resource;

/**
* @var ScopeConfigInterface|MockObject
*/
private ScopeConfigInterface|MockObject $scopeConfig;

/**
* @inheritdoc
Expand All @@ -83,36 +88,30 @@ protected function setUp(): void
$this->entityManager = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->getUtilityPageIdentifiers = $this->getMockBuilder(GetUtilityPageIdentifiers::class)
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
->setMethods(['getValue'])
->disableOriginalConstructor()
->getMock();
$this->model = $objectManager->getObject(
Page::class,
[
'context' => $this->context,
'metadataPool' => $this->metadataPool,
'entityManager' => $this->entityManager,
'connectionName' => 'default',
'getUtilityPageIdentifiers' => $this->getUtilityPageIdentifiers,
]
->getMockForAbstractClass();
$this->getUtilityPageIdentifiers = new GetUtilityPageIdentifiers($this->scopeConfig);
$this->model = new Page(
$this->context,
$this->metadataPool,
$this->entityManager,
'default',
$this->getUtilityPageIdentifiers
);
}

/**
* Test Page::getCollection() build correct query to get cms page collection array.
*
* @return void
* @dataProvider webDefaultPages
*/
public function testGetCollection()
public function testGetCollection(array $pageIdentifiers): void
{
$pageId = 'testPageId';
$url = 'testUrl';
$updatedAt = 'testUpdatedAt';
$pageIdentifiers = [
'testCmsHomePage|ID' => 'testCmsHomePage',
'testCmsNoRoute' => 'testCmsNoRoute',
'testCmsNoCookies' => 'testCmsNoCookies',
];
$storeId = 1;
$linkField = 'testLinkField';
$expectedPage = new DataObject();
Expand Down Expand Up @@ -159,7 +158,7 @@ public function testGetCollection()
$this->identicalTo('main_table.identifier NOT IN (?)'),
$this->identicalTo(array_values($pageIdentifiers))
],
[$this->identicalTo('store_table.store_id IN(?)'), $this->identicalTo([0, $storeId])]
[$this->identicalTo('store_table.store_id IN (?)'), $this->identicalTo([0, $storeId])]
)->willReturnSelf();

$connection = $this->getMockBuilder(AdapterInterface::class)
Expand All @@ -185,9 +184,13 @@ public function testGetCollection()
->method('getEntityConnection')
->willReturn($connection);

$this->getUtilityPageIdentifiers->expects($this->once())
->method('execute')
->willReturn(array_keys($pageIdentifiers));
$this->scopeConfig->expects($this->exactly(3))
->method('getValue')
->withConsecutive(
[$this->identicalTo('web/default/cms_home_page'), $this->identicalTo(ScopeInterface::SCOPE_STORE)],
[$this->identicalTo('web/default/cms_no_route'), $this->identicalTo(ScopeInterface::SCOPE_STORE)],
[$this->identicalTo('web/default/cms_no_cookies'), $this->identicalTo(ScopeInterface::SCOPE_STORE)]
)->willReturnOnConsecutiveCalls(...array_keys($pageIdentifiers));

$this->resource->expects($this->exactly(2))
->method('getTableName')
Expand All @@ -208,4 +211,31 @@ public function testGetCollection()
$resultPage = array_shift($result);
$this->assertEquals($expectedPage, $resultPage);
}

public function webDefaultPages(): array
{
return [
[
[
'testCmsHomePage' => 'testCmsHomePage',
'testCmsNoRoute' => 'testCmsNoRoute',
'testCmsNoCookies' => 'testCmsNoCookies',
]
],
[
[
'testCmsHomePage|ID' => 'testCmsHomePage',
'testCmsNoRoute|ID' => 'testCmsNoRoute',
'testCmsNoCookies|ID' => 'testCmsNoCookies',
]
],
[
[
'testCmsHomePage|ID' => 'testCmsHomePage',
'testCmsNoRoute' => 'testCmsNoRoute',
'testCmsNoCookies' => 'testCmsNoCookies',
]
]
];
}
}