Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BlockActionsTest.php
Go to the documentation of this file.
1 <?php
7 
14 use PHPUnit_Framework_MockObject_MockObject as MockObject;
15 
19 class BlockActionsTest extends \PHPUnit\Framework\TestCase
20 {
24  private $blockActions;
25 
29  private $escaper;
30 
34  private $urlBuilder;
35 
36  protected function setUp()
37  {
38  $objectManager = new ObjectManager($this);
39 
40  $context = $this->createMock(ContextInterface::class);
41 
42  $processor = $this->getMockBuilder(Processor::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $context->expects(static::never())
46  ->method('getProcessor')
47  ->willReturn($processor);
48 
49  $this->urlBuilder = $this->createMock(UrlInterface::class);
50 
51  $this->escaper = $this->getMockBuilder(Escaper::class)
52  ->disableOriginalConstructor()
53  ->setMethods(['escapeHtml'])
54  ->getMock();
55 
56  $this->blockActions = $objectManager->getObject(BlockActions::class, [
57  'context' => $context,
58  'urlBuilder' => $this->urlBuilder
59  ]);
60 
61  $objectManager->setBackwardCompatibleProperty($this->blockActions, 'escaper', $this->escaper);
62  }
63 
67  public function testPrepareDataSource()
68  {
69  $blockId = 1;
70  $title = 'block title';
71  $items = [
72  'data' => [
73  'items' => [
74  [
75  'block_id' => $blockId,
76  'title' => $title
77  ]
78  ]
79  ]
80  ];
81  $name = 'item_name';
82  $expectedItems = [
83  [
84  'block_id' => $blockId,
85  'title' => $title,
86  $name => [
87  'edit' => [
88  'href' => 'test/url/edit',
89  'label' => __('Edit'),
90  ],
91  'delete' => [
92  'href' => 'test/url/delete',
93  'label' => __('Delete'),
94  'confirm' => [
95  'title' => __('Delete %1', $title),
96  'message' => __('Are you sure you want to delete a %1 record?', $title)
97  ],
98  'post' => true
99  ]
100  ],
101  ]
102  ];
103 
104  $this->escaper->expects(static::once())
105  ->method('escapeHtml')
106  ->with($title)
107  ->willReturn($title);
108 
109  $this->urlBuilder->expects(static::exactly(2))
110  ->method('getUrl')
111  ->willReturnMap(
112  [
113  [
115  [
116  'block_id' => $blockId
117  ],
118  'test/url/edit',
119  ],
120  [
122  [
123  'block_id' => $blockId
124  ],
125  'test/url/delete',
126  ],
127  ]
128  );
129 
130  $this->blockActions->setData('name', $name);
131 
132  $actual = $this->blockActions->prepareDataSource($items);
133  static::assertEquals($expectedItems, $actual['data']['items']);
134  }
135 }
$title
Definition: default.phtml:14
$objectManager
Definition: bootstrap.php:17
$processor
Definition: 404.php:10
__()
Definition: __.php:13
$items
if(!isset($_GET['name'])) $name
Definition: log.php:14