Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions app/code/Magento/Cms/Model/Config/Source/Block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Cms\Model\Config\Source;

use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;

/**
* Class Block
*/
class Block implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var array
*/
private $options;

/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* @param CollectionFactory $collectionFactory
*/
public function __construct(
CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}

/**
* To option array
*
* @return array
*/
public function toOptionArray()
{
if (!$this->options) {
$this->options = $this->collectionFactory->create()->toOptionIdArray();
}
return $this->options;
}
}
28 changes: 28 additions & 0 deletions app/code/Magento/Cms/Model/ResourceModel/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,32 @@ public function getSelectCountSql()

return $countSelect;
}

/**
* Returns pairs identifier - title for unique identifiers
* and pairs identifier|entity_id - title for non-unique after first
*
* @return array
*/
public function toOptionIdArray()
{
$res = [];
$existingIdentifiers = [];
foreach ($this as $item) {
$identifier = $item->getData('identifier');

$data['value'] = $identifier;
$data['label'] = $item->getData('title');

if (in_array($identifier, $existingIdentifiers)) {
$data['value'] .= '|' . $item->getData($this->getIdFieldName());
} else {
$existingIdentifiers[] = $identifier;
}

$res[] = $data;
}

return $res;
}
}
28 changes: 0 additions & 28 deletions app/code/Magento/Cms/Model/ResourceModel/Page/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,6 @@ protected function _construct()
$this->_map['fields']['store'] = 'store_table.store_id';
}

/**
* Returns pairs identifier - title for unique identifiers
* and pairs identifier|page_id - title for non-unique after first
*
* @return array
*/
public function toOptionIdArray()
{
$res = [];
$existingIdentifiers = [];
foreach ($this as $item) {
$identifier = $item->getData('identifier');

$data['value'] = $identifier;
$data['label'] = $item->getData('title');

if (in_array($identifier, $existingIdentifiers)) {
$data['value'] .= '|' . $item->getData('page_id');
} else {
$existingIdentifiers[] = $identifier;
}

$res[] = $data;
}

return $res;
}

/**
* Set first store flag
*
Expand Down
64 changes: 64 additions & 0 deletions app/code/Magento/Cms/Test/Unit/Model/Config/Source/BlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Cms\Test\Unit\Model\Config\Source;

/**
* Class BlockTest
*/
class BlockTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Cms\Model\ResourceModel\Block\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $collectionFactory;

/**
* @var \Magento\Cms\Model\Config\Source\Block
*/
protected $block;

/**
* Set up
*
* @return void
*/
protected function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->collectionFactory = $this->createPartialMock(
\Magento\Cms\Model\ResourceModel\Block\CollectionFactory::class,
['create']
);

$this->block = $objectManager->getObject(
\Magento\Cms\Model\Config\Source\Block::class,
[
'collectionFactory' => $this->collectionFactory,
]
);
}

/**
* Run test toOptionArray method
*
* @return void
*/
public function testToOptionArray()
{
$blockCollectionMock = $this->createMock(\Magento\Cms\Model\ResourceModel\Block\Collection::class);

$this->collectionFactory->expects($this->once())
->method('create')
->will($this->returnValue($blockCollectionMock));

$blockCollectionMock->expects($this->once())
->method('toOptionIdArray')
->will($this->returnValue('return-value'));

$this->assertEquals('return-value', $this->block->toOptionArray());
}
}