Skip to content

Commit b84edec

Browse files
committed
Merge remote-tracking branch 'origin/MC-37545' into 2.4-develop-sidecar-pr6
2 parents f0739e0 + 8f2a8be commit b84edec

File tree

6 files changed

+441
-46
lines changed

6 files changed

+441
-46
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Block\Category;
9+
10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Api\Data\CategoryInterface;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\Registry;
14+
use Magento\Framework\View\Result\PageFactory;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\Store\ExecuteInStoreContext;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Category title check
22+
*
23+
* @magentoAppArea frontend
24+
* @see \Magento\Theme\Block\Html\Title
25+
*/
26+
class TitleTest extends TestCase
27+
{
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var CategoryRepositoryInterface */
32+
private $categoryRepository;
33+
34+
/** @var StoreManagerInterface */
35+
private $storeManager;
36+
37+
/** @var Registry */
38+
private $registry;
39+
40+
/** @var PageFactory */
41+
private $pageFactory;
42+
43+
/** @var ExecuteInStoreContext */
44+
private $executeInStoreContext;
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
protected function setUp(): void
50+
{
51+
parent::setUp();
52+
53+
$this->objectManager = Bootstrap::getObjectManager();
54+
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
55+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
56+
$this->pageFactory = $this->objectManager->get(PageFactory::class);
57+
$this->registry = $this->objectManager->get(Registry::class);
58+
$this->executeInStoreContext = $this->objectManager->get(ExecuteInStoreContext::class);
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
protected function tearDown(): void
65+
{
66+
$this->registry->unregister('current_category');
67+
68+
parent::tearDown();
69+
}
70+
71+
/**
72+
* @magentoDbIsolation enabled
73+
* @magentoDataFixture Magento/Catalog/_files/category.php
74+
* @magentoDataFixture Magento/Store/_files/store.php
75+
* @return void
76+
*/
77+
public function testCategoryNameOnStoreView(): void
78+
{
79+
$id = 333;
80+
$categoryNameForSecondStore = 'Category Name For Second Store';
81+
$this->executeInStoreContext->execute(
82+
'test',
83+
[$this, 'updateCategoryName'],
84+
$this->categoryRepository->get($id),
85+
$categoryNameForSecondStore
86+
);
87+
$this->registerCategory($this->categoryRepository->get($id));
88+
$this->assertStringContainsString('Category 1', $this->getBlockTitle(), 'Wrong category name');
89+
$this->registerCategory($this->categoryRepository->get($id, $this->storeManager->getStore('test')->getId()));
90+
$this->assertStringContainsString($categoryNameForSecondStore, $this->getBlockTitle(), 'Wrong category name');
91+
}
92+
93+
/**
94+
* Update category name
95+
*
96+
* @param CategoryInterface $category
97+
* @param string $categoryName
98+
* @return void
99+
*/
100+
public function updateCategoryName(CategoryInterface $category, string $categoryName): void
101+
{
102+
$category->setName($categoryName);
103+
$this->categoryRepository->save($category);
104+
}
105+
106+
/**
107+
* Get title block
108+
*
109+
* @return string
110+
*/
111+
private function getBlockTitle(): string
112+
{
113+
$page = $this->pageFactory->create();
114+
$page->addHandle([
115+
'default',
116+
'catalog_category_view',
117+
]);
118+
$page->getLayout()->generateXml();
119+
$block = $page->getLayout()->getBlock('page.main.title');
120+
$this->assertNotFalse($block);
121+
122+
return $block->stripTags($block->toHtml());
123+
}
124+
125+
/**
126+
* Register category in registry
127+
*
128+
* @param CategoryInterface $category
129+
* @return void
130+
*/
131+
private function registerCategory(CategoryInterface $category): void
132+
{
133+
$this->registry->unregister('current_category');
134+
$this->registry->register('current_category', $category);
135+
}
136+
}

dev/tests/integration/testsuite/Magento/Catalog/Controller/Category/CategoryUrlRewriteTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@
77

88
namespace Magento\Catalog\Controller\Category;
99

10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Model\Layer\Category;
12+
use Magento\Catalog\Model\Layer\Resolver;
13+
use Magento\Catalog\Model\Session as CatalogSession;
1014
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1115
use Magento\Framework\App\Config\ScopeConfigInterface;
1216
use Magento\Framework\App\Response\Http;
1317
use Magento\Framework\Registry;
1418
use Magento\Store\Model\ScopeInterface;
19+
use Magento\Store\Model\StoreManagerInterface;
20+
use Magento\TestFramework\Request;
21+
use Magento\TestFramework\Response;
22+
use Magento\TestFramework\Store\ExecuteInStoreContext;
1523
use Magento\TestFramework\TestCase\AbstractController;
1624

1725
/**
1826
* Checks category availability on storefront by url rewrite
1927
*
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2029
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
2130
* @magentoDbIsolation enabled
2231
*/
@@ -31,6 +40,18 @@ class CategoryUrlRewriteTest extends AbstractController
3140
/** @var string */
3241
private $categoryUrlSuffix;
3342

43+
/** @var StoreManagerInterface */
44+
private $storeManager;
45+
46+
/** @var CategoryRepositoryInterface */
47+
private $categoryRepository;
48+
49+
/** @var CatalogSession */
50+
private $catalogSession;
51+
52+
/** @var ExecuteInStoreContext */
53+
private $executeInStoreContext;
54+
3455
/**
3556
* @inheritdoc
3657
*/
@@ -44,6 +65,10 @@ protected function setUp(): void
4465
CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX,
4566
ScopeInterface::SCOPE_STORE
4667
);
68+
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
69+
$this->categoryRepository = $this->_objectManager->get(CategoryRepositoryInterface::class);
70+
$this->catalogSession = $this->_objectManager->get(CatalogSession::class);
71+
$this->executeInStoreContext = $this->_objectManager->get(ExecuteInStoreContext::class);
4772
}
4873

4974
/**
@@ -87,4 +112,87 @@ public function categoryRewriteProvider(): array
87112
],
88113
];
89114
}
115+
116+
/**
117+
* Test category url on different store view
118+
*
119+
* @magentoDataFixture Magento/Catalog/_files/category.php
120+
* @magentoDataFixture Magento/Store/_files/store.php
121+
* @return void
122+
*/
123+
public function testCategoryUrlOnStoreView(): void
124+
{
125+
$id = 333;
126+
$secondStoreUrlKey = 'category-1-second';
127+
$currentStore = $this->storeManager->getStore();
128+
$secondStore = $this->storeManager->getStore('test');
129+
$this->executeInStoreContext->execute(
130+
$secondStore,
131+
[$this, 'updateCategoryUrlKey'],
132+
$id,
133+
(int)$secondStore->getId(),
134+
$secondStoreUrlKey
135+
);
136+
$url = sprintf('/' . $secondStoreUrlKey . '%s', $this->categoryUrlSuffix);
137+
$this->executeInStoreContext->execute($secondStore, [$this, 'dispatch'], $url);
138+
$this->assertCategoryIsVisible();
139+
$this->assertEquals(
140+
$secondStoreUrlKey,
141+
$this->categoryRepository->get($id, (int)$secondStore->getId())->getUrlKey(),
142+
'Wrong category is registered'
143+
);
144+
$this->cleanUpCachedObjects();
145+
$defaultStoreUrlKey = $this->categoryRepository->get($id, $currentStore->getId())->getUrlKey();
146+
$this->dispatch(sprintf($defaultStoreUrlKey . '%s', $this->categoryUrlSuffix));
147+
$this->assertCategoryIsVisible();
148+
}
149+
150+
/**
151+
* Assert that category is available in storefront
152+
*
153+
* @return void
154+
*/
155+
private function assertCategoryIsVisible(): void
156+
{
157+
$this->assertEquals(
158+
Response::STATUS_CODE_200,
159+
$this->getResponse()->getHttpResponseCode(),
160+
'Wrong response code is returned'
161+
);
162+
$this->assertNotNull((int)$this->catalogSession->getData('last_viewed_category_id'));
163+
}
164+
165+
/**
166+
* Clean up cached objects
167+
*
168+
* @return void
169+
*/
170+
private function cleanUpCachedObjects(): void
171+
{
172+
$this->catalogSession->clearStorage();
173+
$this->registry->unregister('current_category');
174+
$this->registry->unregister('category');
175+
$this->_objectManager->removeSharedInstance(Request::class);
176+
$this->_objectManager->removeSharedInstance(Response::class);
177+
$this->_objectManager->removeSharedInstance(Resolver::class);
178+
$this->_objectManager->removeSharedInstance(Category::class);
179+
$this->_objectManager->removeSharedInstance('categoryFilterList');
180+
$this->_response = null;
181+
$this->_request = null;
182+
}
183+
184+
/**
185+
* Update category url key
186+
*
187+
* @param int $id
188+
* @param int $storeId
189+
* @param string $categoryUrlKey
190+
* @return void
191+
*/
192+
public function updateCategoryUrlKey(int $id, int $storeId, string $categoryUrlKey): void
193+
{
194+
$category = $this->categoryRepository->get($id, $storeId);
195+
$category->setUrlKey($categoryUrlKey);
196+
$this->categoryRepository->save($category);
197+
}
90198
}

0 commit comments

Comments
 (0)