Skip to content

Commit 3e6bb4b

Browse files
committed
Final adjustments to the implementation
1 parent 4656d9e commit 3e6bb4b

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Cms\Api\Data\BlockInterface;
1111
use Magento\Cms\Api\GetBlockByIdentifierInterface;
12+
use Magento\Cms\Model\Block as BlockModel;
1213
use Magento\Cms\Model\Template\FilterProvider;
1314
use Magento\Framework\DataObject\IdentityInterface;
1415
use Magento\Framework\Exception\NoSuchEntityException;
@@ -45,6 +46,13 @@ class BlockByIdentifier extends AbstractBlock implements IdentityInterface
4546
*/
4647
private $cmsBlock;
4748

49+
/**
50+
* @param GetBlockByIdentifierInterface $blockByIdentifier
51+
* @param StoreManagerInterface $storeManager
52+
* @param FilterProvider $filterProvider
53+
* @param Context $context
54+
* @param array $data
55+
*/
4856
public function __construct(
4957
GetBlockByIdentifierInterface $blockByIdentifier,
5058
StoreManagerInterface $storeManager,
@@ -89,7 +97,7 @@ private function filterOutput(string $content): string
8997
/**
9098
* Loads the CMS block by `identifier` provided as an argument
9199
*
92-
* @return BlockInterface
100+
* @return BlockInterface|BlockModel
93101
* @throws NoSuchEntityException
94102
*/
95103
private function getCmsBlock(): BlockInterface
@@ -142,17 +150,13 @@ public function getIdentities(): array
142150

143151
try {
144152
$cmsBlock = $this->getCmsBlock();
145-
146-
$identities[] = self::CACHE_KEY_PREFIX . '_' . $cmsBlock->getId();
147-
148-
if (method_exists($this->getCmsBlock(), 'getStores')) {
149-
foreach ($cmsBlock->getStores() as $storeId) {
150-
$identities[] = self::CACHE_KEY_PREFIX . '_' . $this->getIdentifier() . '_' . $storeId;
151-
}
153+
if ($cmsBlock instanceof IdentityInterface) {
154+
$identities = array_merge($identities, $cmsBlock->getIdentities());
152155
}
153156
// phpcs:disable Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
154157
} catch (NoSuchEntityException $e) {
155158
}
159+
156160
return $identities;
157161
}
158162
}

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Cms\Api\Data\BlockInterface;
1111
use Magento\Cms\Api\GetBlockByIdentifierInterface;
1212
use Magento\Cms\Block\BlockByIdentifier;
13+
use Magento\Cms\Model\Block;
1314
use Magento\Cms\Model\Template\FilterProvider;
1415
use Magento\Framework\App\Config\ScopeConfigInterface;
1516
use Magento\Framework\Event\ManagerInterface;
@@ -21,6 +22,9 @@
2122
use PHPUnit\Framework\MockObject\MockObject;
2223
use PHPUnit\Framework\TestCase;
2324

25+
/**
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
*/
2428
class BlockByIdentifierTest extends TestCase
2529
{
2630
private const STUB_MODULE_OUTPUT_DISABLED = false;
@@ -37,6 +41,8 @@ class BlockByIdentifierTest extends TestCase
3741
BlockByIdentifier::CACHE_KEY_PREFIX . '_' . self::STUB_UNAVAILABLE_IDENTIFIER,
3842
BlockByIdentifier::CACHE_KEY_PREFIX . '_' . self::STUB_UNAVAILABLE_IDENTIFIER . '_' . self::STUB_DEFAULT_STORE
3943
];
44+
private const STUB_CMS_BLOCK_IDENTITY_BY_ID = 'CMS_BLOCK_' . self::STUB_CMS_BLOCK_ID;
45+
private const STUB_CMS_BLOCK_IDENTITY_BY_IDENTIFIER = 'CMS_BLOCK_' . self::STUB_EXISTING_IDENTIFIER;
4046

4147
/** @var MockObject|GetBlockByIdentifierInterface */
4248
private $getBlockByIdentifierMock;
@@ -108,14 +114,19 @@ public function testBlockReturnsCmsContentsWhenIdentifierFound(): void
108114
$this->assertSame(self::ASSERT_CONTENT_HTML, $block->toHtml());
109115
}
110116

111-
public function testBlockCacheIdentitiesContainExplicitScopeInformation(): void
117+
public function testBlockCacheIdentitiesContainCmsBlockIdentities(): void
112118
{
113119
// Given
114-
$cmsBlockMock = $this->getCmsBlockMock(
115-
self::STUB_CMS_BLOCK_ID,
116-
self::STUB_EXISTING_IDENTIFIER,
117-
self::STUB_CONTENT
120+
$cmsBlockMock = $this->createMock(Block::class);
121+
$cmsBlockMock->method('getId')->willReturn(self::STUB_CMS_BLOCK_ID);
122+
$cmsBlockMock->method('getIdentifier')->willReturn(self::STUB_EXISTING_IDENTIFIER);
123+
$cmsBlockMock->method('getIdentities')->willReturn(
124+
[
125+
self::STUB_CMS_BLOCK_IDENTITY_BY_ID,
126+
self::STUB_CMS_BLOCK_IDENTITY_BY_IDENTIFIER
127+
]
118128
);
129+
119130
$this->storeMock->method('getId')->willReturn(self::STUB_DEFAULT_STORE);
120131
$this->getBlockByIdentifierMock->method('execute')
121132
->with(self::STUB_EXISTING_IDENTIFIER, self::STUB_DEFAULT_STORE)
@@ -127,10 +138,8 @@ public function testBlockCacheIdentitiesContainExplicitScopeInformation(): void
127138

128139
// Then
129140
$this->assertContains($this->getIdentityStubById(self::STUB_CMS_BLOCK_ID), $identities);
130-
$this->assertContains(
131-
$this->getIdentityStubByIdentifier(self::STUB_EXISTING_IDENTIFIER, self::STUB_DEFAULT_STORE),
132-
$identities
133-
);
141+
$this->assertContains(self::STUB_CMS_BLOCK_IDENTITY_BY_ID, $identities);
142+
$this->assertContains(self::STUB_CMS_BLOCK_IDENTITY_BY_IDENTIFIER, $identities);
134143
}
135144

136145
/**

0 commit comments

Comments
 (0)