Skip to content

Commit 3586810

Browse files
author
Stanislav Idolov
authored
ENGCOM-2800: [Forwardport] Introduce Block Config Source #17598
2 parents 1ad5bcb + 6cad837 commit 3586810

File tree

4 files changed

+142
-28
lines changed

4 files changed

+142
-28
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Config\Source;
9+
10+
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
11+
use Magento\Framework\Data\OptionSourceInterface;
12+
13+
/**
14+
* Class Block
15+
*/
16+
class Block implements OptionSourceInterface
17+
{
18+
/**
19+
* @var array
20+
*/
21+
private $options;
22+
23+
/**
24+
* @var \Magento\Cms\Model\ResourceModel\Block\CollectionFactory
25+
*/
26+
private $collectionFactory;
27+
28+
/**
29+
* @param \Magento\Cms\Model\ResourceModel\Block\CollectionFactory $collectionFactory
30+
*/
31+
public function __construct(
32+
CollectionFactory $collectionFactory
33+
) {
34+
$this->collectionFactory = $collectionFactory;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function toOptionArray()
41+
{
42+
if (!$this->options) {
43+
$this->options = $this->collectionFactory->create()->toOptionIdArray();
44+
}
45+
46+
return $this->options;
47+
}
48+
}

app/code/Magento/Cms/Model/ResourceModel/AbstractCollection.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,32 @@ public function getSelectCountSql()
176176

177177
return $countSelect;
178178
}
179+
180+
/**
181+
* Returns pairs identifier - title for unique identifiers
182+
* and pairs identifier|entity_id - title for non-unique after first
183+
*
184+
* @return array
185+
*/
186+
public function toOptionIdArray()
187+
{
188+
$res = [];
189+
$existingIdentifiers = [];
190+
foreach ($this as $item) {
191+
$identifier = $item->getData('identifier');
192+
193+
$data['value'] = $identifier;
194+
$data['label'] = $item->getData('title');
195+
196+
if (in_array($identifier, $existingIdentifiers)) {
197+
$data['value'] .= '|' . $item->getData($this->getIdFieldName());
198+
} else {
199+
$existingIdentifiers[] = $identifier;
200+
}
201+
202+
$res[] = $data;
203+
}
204+
205+
return $res;
206+
}
179207
}

app/code/Magento/Cms/Model/ResourceModel/Page/Collection.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,6 @@ protected function _construct()
5151
$this->_map['fields']['store'] = 'store_table.store_id';
5252
}
5353

54-
/**
55-
* Returns pairs identifier - title for unique identifiers
56-
* and pairs identifier|page_id - title for non-unique after first
57-
*
58-
* @return array
59-
*/
60-
public function toOptionIdArray()
61-
{
62-
$res = [];
63-
$existingIdentifiers = [];
64-
foreach ($this as $item) {
65-
$identifier = $item->getData('identifier');
66-
67-
$data['value'] = $identifier;
68-
$data['label'] = $item->getData('title');
69-
70-
if (in_array($identifier, $existingIdentifiers)) {
71-
$data['value'] .= '|' . $item->getData('page_id');
72-
} else {
73-
$existingIdentifiers[] = $identifier;
74-
}
75-
76-
$res[] = $data;
77-
}
78-
79-
return $res;
80-
}
81-
8254
/**
8355
* Set first store flag
8456
*
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Config\Source;
9+
10+
/**
11+
* Class BlockTest
12+
*/
13+
class BlockTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var \Magento\Cms\Model\ResourceModel\Block\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
protected $collectionFactory;
19+
20+
/**
21+
* @var \Magento\Cms\Model\Config\Source\Block
22+
*/
23+
protected $block;
24+
25+
/**
26+
* Set up
27+
*
28+
* @return void
29+
*/
30+
protected function setUp()
31+
{
32+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33+
34+
$this->collectionFactory = $this->createPartialMock(
35+
\Magento\Cms\Model\ResourceModel\Block\CollectionFactory::class,
36+
['create']
37+
);
38+
39+
$this->block = $objectManager->getObject(
40+
\Magento\Cms\Model\Config\Source\Block::class,
41+
[
42+
'collectionFactory' => $this->collectionFactory,
43+
]
44+
);
45+
}
46+
47+
/**
48+
* Run test toOptionArray method
49+
*
50+
* @return void
51+
*/
52+
public function testToOptionArray()
53+
{
54+
$blockCollectionMock = $this->createMock(\Magento\Cms\Model\ResourceModel\Block\Collection::class);
55+
56+
$this->collectionFactory->expects($this->once())
57+
->method('create')
58+
->will($this->returnValue($blockCollectionMock));
59+
60+
$blockCollectionMock->expects($this->once())
61+
->method('toOptionIdArray')
62+
->will($this->returnValue('return-value'));
63+
64+
$this->assertEquals('return-value', $this->block->toOptionArray());
65+
}
66+
}

0 commit comments

Comments
 (0)