Skip to content

Commit 2d21590

Browse files
committed
Merge remote-tracking branch 'local/ACP2E-1840' into PR_Jun_06_2023
2 parents 1189680 + 2d9cb81 commit 2d21590

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\Cms\Model\ResourceModel\Page\Query;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
13+
class PageIdsList
14+
{
15+
/**
16+
* @var ResourceConnection
17+
*/
18+
private $resourceConnection;
19+
20+
/**
21+
* @var AdapterInterface
22+
*/
23+
private $connection;
24+
25+
/**
26+
* @param ResourceConnection $resourceConnection
27+
*/
28+
public function __construct(ResourceConnection $resourceConnection)
29+
{
30+
$this->resourceConnection = $resourceConnection;
31+
}
32+
33+
/**
34+
* Returns connection.
35+
*
36+
* @return AdapterInterface
37+
*/
38+
private function getConnection(): AdapterInterface
39+
{
40+
if (!$this->connection) {
41+
$this->connection = $this->resourceConnection->getConnection();
42+
}
43+
44+
return $this->connection;
45+
}
46+
47+
/**
48+
* Get all pages that contain blocks identified by ids or identifiers
49+
*
50+
* @param array $ids
51+
* @return array
52+
*/
53+
public function execute(array $ids = []): array
54+
{
55+
$select = $this->getConnection()->select()
56+
->from(
57+
['main_table' => $this->resourceConnection->getTableName('cms_page')],
58+
['main_table.page_id']
59+
);
60+
if (count($ids)) {
61+
foreach ($ids as $id) {
62+
$select->orWhere(
63+
"MATCH (title, meta_keywords, meta_description, identifier, content)
64+
AGAINST ('block_id=\"$id\"')"
65+
);
66+
}
67+
$identifiers = $this->getBlockIdentifiersByIds($ids);
68+
foreach ($identifiers as $identifier) {
69+
$select->orWhere(
70+
"MATCH (title, meta_keywords, meta_description, identifier, content)
71+
AGAINST ('block_id=\"$identifier\"')"
72+
);
73+
}
74+
} else {
75+
$select->where("MATCH (title, meta_keywords, meta_description, identifier, content)
76+
AGAINST ('block_id=')");
77+
}
78+
79+
return $this->connection->fetchCol($select);
80+
}
81+
82+
/**
83+
* Get blocks identifiers based on ids
84+
*
85+
* @param array $ids
86+
* @return array
87+
*/
88+
private function getBlockIdentifiersByIds(array $ids): array
89+
{
90+
$select = $this->getConnection()->select()
91+
->from(
92+
['main_table' => $this->resourceConnection->getTableName('cms_block')],
93+
['main_table.identifier']
94+
)->where('block_id IN (?)', $ids, \Zend_Db::INT_TYPE);
95+
96+
return $this->connection->fetchCol($select);
97+
}
98+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\Cms\Test\Unit\Model\ResourceModel\Page\Query;
9+
10+
use Magento\Cms\Model\ResourceModel\Page\Query\PageIdsList;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\DB\Select;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class PageIdsListTest extends TestCase
18+
{
19+
20+
/**
21+
* @var ResourceConnection|MockObject
22+
*/
23+
private $resourceMock;
24+
25+
/**
26+
* @var AdapterInterface|MockObject
27+
*/
28+
private $connectionMock;
29+
30+
/**
31+
* @var Select|MockObject
32+
*/
33+
private $selectMock;
34+
35+
protected function setUp(): void
36+
{
37+
$this->resourceMock = $this->createMock(ResourceConnection::class);
38+
$this->selectMock = $this->createMock(Select::class);
39+
$this->connectionMock = $this->createMock(AdapterInterface::class);
40+
}
41+
42+
/**
43+
* @param $blockEntityIds
44+
* @param $pageEntityIds
45+
* @param $blockIdentifiers
46+
* @dataProvider getDataProvider
47+
*/
48+
public function testExecute($blockEntityIds, $pageEntityIds, $blockIdentifiers)
49+
{
50+
$this->selectMock->expects($this->any())
51+
->method('from')
52+
->willReturnSelf();
53+
if (count($blockEntityIds)) {
54+
$this->resourceMock->expects($this->any())
55+
->method('getTableName')
56+
->willReturnOnConsecutiveCalls('cms_page', 'cms_block');
57+
$this->selectMock->expects($this->any())
58+
->method('orWhere')
59+
->willReturnSelf();
60+
61+
$this->connectionMock->expects($this->exactly(2))
62+
->method('fetchCol')
63+
->willReturnOnConsecutiveCalls($blockIdentifiers, $pageEntityIds);
64+
65+
$this->connectionMock->expects($this->exactly(2))
66+
->method('select')
67+
->willReturn($this->selectMock);
68+
} else {
69+
$this->connectionMock->expects($this->once())
70+
->method('select')
71+
->willReturn($this->selectMock);
72+
$this->selectMock->expects($this->any())
73+
->method('where')
74+
->willReturnSelf();
75+
$this->connectionMock->expects($this->once())
76+
->method('fetchCol')
77+
->willReturn($pageEntityIds);
78+
}
79+
$this->resourceMock->expects($this->any())
80+
->method('getConnection')
81+
->with()
82+
->willReturn($this->connectionMock);
83+
84+
$pageIdsList = new PageIdsList(
85+
$this->resourceMock
86+
);
87+
88+
$this->assertSame($pageEntityIds, $pageIdsList->execute($blockEntityIds));
89+
}
90+
91+
/**
92+
* Execute data provider
93+
*
94+
* @return array
95+
*/
96+
public function getDataProvider(): array
97+
{
98+
return [
99+
[[1, 2, 3], [1], ['test1', 'test2', 'test3']],
100+
[[1, 2, 3], [], []],
101+
[[], [], []]
102+
];
103+
}
104+
}

0 commit comments

Comments
 (0)