Skip to content

Commit ae11a0c

Browse files
committed
Improve the logic behind getIdentifiers and Unit Tests coverage
1 parent 4501271 commit ae11a0c

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

app/code/Magento/Cms/Block/BlockByIdentifier.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,28 @@ private function getCurrentStoreId(): int
131131
*/
132132
public function getIdentities(): array
133133
{
134+
if (!$this->getIdentifier()) {
135+
return [];
136+
}
137+
138+
$identities = [
139+
self::CACHE_KEY_PREFIX . '_' . $this->getIdentifier(),
140+
self::CACHE_KEY_PREFIX . '_' . $this->getIdentifier() . '_' . $this->getCurrentStoreId()
141+
];
142+
134143
try {
135144
$cmsBlock = $this->getCmsBlock();
136145

137-
$identities = [self::CACHE_KEY_PREFIX . '_' . $cmsBlock->getId()];
146+
$identities[] = self::CACHE_KEY_PREFIX . '_' . $cmsBlock->getId();
138147

139148
if (method_exists($this->getCmsBlock(), 'getStores')) {
140-
foreach ($cmsBlock->getStores() as $store) {
141-
$identities[] = self::CACHE_KEY_PREFIX . '_' . $this->getIdentifier() . '_' . $store;
149+
foreach ($cmsBlock->getStores() as $storeId) {
150+
$identities[] = self::CACHE_KEY_PREFIX . '_' . $this->getIdentifier() . '_' . $storeId;
142151
}
143152
}
144-
145-
$identities[] = self::CACHE_KEY_PREFIX . '_' . $this->getIdentifier() . '_' . $this->getCurrentStoreId();
146-
147-
return $identities;
153+
// phpcs:disable Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
148154
} catch (NoSuchEntityException $e) {
149-
// If CMS Block does not exist, it should not be cached
150-
return [];
151155
}
156+
return $identities;
152157
}
153158
}

app/code/Magento/Cms/Test/Unit/Block/BlockByIdentifierTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Cms\Model\Template\FilterProvider;
1515
use Magento\Framework\App\Config\ScopeConfigInterface;
1616
use Magento\Framework\Event\ManagerInterface;
17+
use Magento\Framework\Exception\NoSuchEntityException;
1718
use Magento\Framework\Filter\Template;
1819
use Magento\Framework\View\Element\Context;
1920
use Magento\Store\Api\Data\StoreInterface;
@@ -25,13 +26,18 @@ class BlockByIdentifierTest extends TestCase
2526
{
2627
private const STUB_MODULE_OUTPUT_DISABLED = false;
2728
private const STUB_EXISTING_IDENTIFIER = 'existingOne';
29+
private const STUB_UNAVAILABLE_IDENTIFIER = 'notExists';
2830
private const STUB_DEFAULT_STORE = 1;
2931
private const STUB_CMS_BLOCK_ID = 1;
3032
private const STUB_CONTENT = 'Content';
3133

3234
private const ASSERT_EMPTY_BLOCK_HTML = '';
3335
private const ASSERT_CONTENT_HTML = self::STUB_CONTENT;
3436
private const ASSERT_NO_CACHE_IDENTITIES = [];
37+
private const ASSERT_UNAVAILABLE_IDENTIFIER_BASED_IDENTITIES = [
38+
BlockByIdentifier::CACHE_KEY_PREFIX . '_' . self::STUB_UNAVAILABLE_IDENTIFIER,
39+
BlockByIdentifier::CACHE_KEY_PREFIX . '_' . self::STUB_UNAVAILABLE_IDENTIFIER . '_' . self::STUB_DEFAULT_STORE
40+
];
3541

3642
/** @var MockObject|GetBlockByIdentifierInterface */
3743
private $getBlockByIdentifierMock;
@@ -61,12 +67,30 @@ public function testBlockReturnsEmptyStringWhenNoIdentifierProvided(): void
6167
{
6268
// Given
6369
$missingIdentifierBlock = $this->getTestedBlockUsingIdentifier(null);
70+
$this->storeMock->method('getId')->willReturn(self::STUB_DEFAULT_STORE);
6471

6572
// Expect
6673
$this->assertSame(self::ASSERT_EMPTY_BLOCK_HTML, $missingIdentifierBlock->toHtml());
6774
$this->assertSame(self::ASSERT_NO_CACHE_IDENTITIES, $missingIdentifierBlock->getIdentities());
6875
}
6976

77+
public function testBlockReturnsEmptyStringWhenIdentifierProvidedNotFound(): void
78+
{
79+
// Given
80+
$this->getBlockByIdentifierMock->method('execute')->willThrowException(
81+
new NoSuchEntityException(__('NoSuchEntityException'))
82+
);
83+
$missingIdentifierBlock = $this->getTestedBlockUsingIdentifier(self::STUB_UNAVAILABLE_IDENTIFIER);
84+
$this->storeMock->method('getId')->willReturn(self::STUB_DEFAULT_STORE);
85+
86+
// Expect
87+
$this->assertSame(self::ASSERT_EMPTY_BLOCK_HTML, $missingIdentifierBlock->toHtml());
88+
$this->assertSame(
89+
self::ASSERT_UNAVAILABLE_IDENTIFIER_BASED_IDENTITIES,
90+
$missingIdentifierBlock->getIdentities()
91+
);
92+
}
93+
7094
public function testBlockReturnsCmsContentsWhenIdentifierFound(): void
7195
{
7296
// Given

0 commit comments

Comments
 (0)