3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Cms \Test \Unit \Ui \Component \Listing \Column ;
7
9
8
10
use Magento \Cms \Ui \Component \Listing \Column \PageActions ;
11
+ use Magento \Cms \ViewModel \Page \Grid \UrlBuilder ;
9
12
use Magento \Framework \Escaper ;
13
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
+ use Magento \Framework \UrlInterface ;
15
+ use Magento \Framework \View \Element \UiComponent \ContextInterface ;
16
+ use Magento \Framework \View \Element \UiComponent \Processor ;
17
+ use PHPUnit \Framework \TestCase ;
18
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
10
19
11
20
/**
12
21
* Test for Magento\Cms\Ui\Component\Listing\Column\PageActions class.
13
22
*/
14
- class PageActionsTest extends \ PHPUnit \ Framework \ TestCase
23
+ class PageActionsTest extends TestCase
15
24
{
16
- public function testPrepareItemsByPageId ()
25
+
26
+ /**
27
+ * @var UrlInterface|MockObject
28
+ */
29
+ private $ urlBuilderMock ;
30
+
31
+ /**
32
+ * @var UrlBuilder|MockObject
33
+ */
34
+ private $ scopeUrlBuilderMock ;
35
+
36
+ /**
37
+ * @var ContextInterface|MockObject
38
+ */
39
+ private $ contextMock ;
40
+
41
+ /**
42
+ * @var Processor|MockObject
43
+ */
44
+ private $ processorMock ;
45
+
46
+ /**
47
+ * @var Escaper|MockObject
48
+ */
49
+ private $ escaperMock ;
50
+
51
+ /**
52
+ * @var PageActions
53
+ */
54
+ private $ model ;
55
+
56
+ /**
57
+ * @inheritDoc
58
+ */
59
+ public function setUp ()
17
60
{
18
- $ pageId = 1 ;
19
- // Create Mocks and SUT
20
- $ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
21
- /** @var \PHPUnit_Framework_MockObject_MockObject $urlBuilderMock */
22
- $ urlBuilderMock = $ this ->getMockBuilder (\Magento \Framework \UrlInterface::class)
23
- ->disableOriginalConstructor ()
24
- ->getMock ();
25
- $ contextMock = $ this ->getMockBuilder (\Magento \Framework \View \Element \UiComponent \ContextInterface::class)
61
+ $ this ->urlBuilderMock = $ this ->createMock (UrlInterface::class);
62
+ $ this ->scopeUrlBuilderMock = $ this ->createMock (UrlBuilder::class);
63
+ $ this ->processorMock = $ this ->createMock (Processor::class);
64
+ $ this ->contextMock = $ this ->getMockBuilder (ContextInterface::class)
26
65
->getMockForAbstractClass ();
27
- $ processor = $ this ->getMockBuilder (\ Magento \ Framework \ View \ Element \ UiComponent \Processor ::class)
66
+ $ this -> escaperMock = $ this ->getMockBuilder (Escaper ::class)
28
67
->disableOriginalConstructor ()
68
+ ->setMethods (['escapeHtml ' ])
29
69
->getMock ();
30
- $ contextMock ->expects ($ this ->never ())->method ('getProcessor ' )->willReturn ($ processor );
31
70
32
- /** @var \Magento\Cms\Ui\Component\Listing\Column\PageActions $model */
33
- $ model = $ objectManager ->getObject (
34
- \Magento \Cms \Ui \Component \Listing \Column \PageActions::class,
71
+ $ objectManager = new ObjectManager ($ this );
72
+
73
+ $ this ->model = $ objectManager ->getObject (
74
+ PageActions::class,
35
75
[
36
- 'urlBuilder ' => $ urlBuilderMock ,
37
- 'context ' => $ contextMock ,
76
+ 'urlBuilder ' => $ this ->urlBuilderMock ,
77
+ 'context ' => $ this ->contextMock ,
78
+ 'scopeUrlBuilder ' => $ this ->scopeUrlBuilderMock
38
79
]
39
80
);
40
81
41
- $ escaper = $ this ->getMockBuilder (Escaper::class)
42
- ->disableOriginalConstructor ()
43
- ->setMethods (['escapeHtml ' ])
44
- ->getMock ();
45
- $ objectManager ->setBackwardCompatibleProperty ($ model , 'escaper ' , $ escaper );
46
-
47
- // Define test input and expectations
48
- $ title = 'page title ' ;
49
- $ items = [
50
- 'data ' => [
51
- 'items ' => [
52
- [
53
- 'page_id ' => $ pageId ,
54
- 'title ' => $ title
55
- ]
56
- ]
57
- ]
58
- ];
59
- $ name = 'item_name ' ;
60
- $ expectedItems = [
61
- [
62
- 'page_id ' => $ pageId ,
63
- 'title ' => $ title ,
64
- $ name => [
65
- 'edit ' => [
66
- 'href ' => 'test/url/edit ' ,
67
- 'label ' => __ ('Edit ' ),
68
- '__disableTmpl ' => true ,
69
- ],
70
- 'delete ' => [
71
- 'href ' => 'test/url/delete ' ,
72
- 'label ' => __ ('Delete ' ),
73
- 'confirm ' => [
74
- 'title ' => __ ('Delete %1 ' , $ title ),
75
- 'message ' => __ ('Are you sure you want to delete a %1 record? ' , $ title ),
76
- '__disableTmpl ' => true ,
77
- ],
78
- 'post ' => true ,
79
- '__disableTmpl ' => true ,
80
- ],
81
- ],
82
- ],
83
- ];
82
+ $ objectManager ->setBackwardCompatibleProperty ($ this ->model , 'escaper ' , $ this ->escaperMock );
83
+ }
84
84
85
- $ escaper ->expects (static ::once ())
85
+ /**
86
+ * Verify Prepare Items by page Id.
87
+ *
88
+ * @dataProvider configDataProvider
89
+ * @param int $pageId
90
+ * @param string $title
91
+ * @param string $name
92
+ * @param array $items
93
+ * @param array $expectedItems
94
+ * @return void
95
+ */
96
+ public function testPrepareItemsByPageId (
97
+ int $ pageId ,
98
+ string $ title ,
99
+ string $ name ,
100
+ array $ items ,
101
+ array $ expectedItems
102
+ ):void {
103
+ $ this ->contextMock ->expects ($ this ->never ())
104
+ ->method ('getProcessor ' )
105
+ ->willReturn ($ this ->processorMock );
106
+ $ this ->escaperMock ->expects (static ::once ())
86
107
->method ('escapeHtml ' )
87
108
->with ($ title )
88
109
->willReturn ($ title );
89
110
// Configure mocks and object data
90
- $ urlBuilderMock ->expects ($ this ->any ())
111
+ $ this -> urlBuilderMock ->expects ($ this ->any ())
91
112
->method ('getUrl ' )
92
113
->willReturnMap (
93
114
[
@@ -107,9 +128,77 @@ public function testPrepareItemsByPageId()
107
128
],
108
129
]
109
130
);
110
- $ model ->setName ($ name );
111
- $ items = $ model ->prepareDataSource ($ items );
131
+
132
+ $ this ->scopeUrlBuilderMock ->expects ($ this ->any ())
133
+ ->method ('getUrl ' )
134
+ ->willReturn ('test/url/view ' );
135
+
136
+ $ this ->model ->setName ($ name );
137
+ $ items = $ this ->model ->prepareDataSource ($ items );
112
138
// Run test
113
139
$ this ->assertEquals ($ expectedItems , $ items ['data ' ]['items ' ]);
114
140
}
141
+
142
+ /**
143
+ * Data provider for testPrepareItemsByPageId
144
+ *
145
+ * @return array
146
+ */
147
+ public function configDataProvider ():array
148
+ {
149
+ $ pageId = 1 ;
150
+ $ title = 'page title ' ;
151
+ $ identifier = 'page_identifier ' ;
152
+ $ name = 'item_name ' ;
153
+
154
+ return [
155
+ [
156
+ 'pageId ' => $ pageId ,
157
+ 'title ' => $ title ,
158
+ 'name ' => $ name ,
159
+ 'items ' => [
160
+ 'data ' => [
161
+ 'items ' => [
162
+ [
163
+ 'page_id ' => $ pageId ,
164
+ 'title ' => $ title ,
165
+ 'identifier ' => $ identifier
166
+ ]
167
+ ]
168
+ ]
169
+ ],
170
+ 'expectedItems ' => [
171
+ [
172
+ 'page_id ' => $ pageId ,
173
+ 'title ' => $ title ,
174
+ 'identifier ' => $ identifier ,
175
+ $ name => [
176
+ 'edit ' => [
177
+ 'href ' => 'test/url/edit ' ,
178
+ 'label ' => __ ('Edit ' ),
179
+ '__disableTmpl ' => true ,
180
+ ],
181
+ 'delete ' => [
182
+ 'href ' => 'test/url/delete ' ,
183
+ 'label ' => __ ('Delete ' ),
184
+ 'confirm ' => [
185
+ 'title ' => __ ('Delete %1 ' , $ title ),
186
+ 'message ' => __ ('Are you sure you want to delete a %1 record? ' , $ title ),
187
+ '__disableTmpl ' => true ,
188
+ ],
189
+ 'post ' => true ,
190
+ '__disableTmpl ' => true ,
191
+ ],
192
+ 'preview ' => [
193
+ 'href ' => 'test/url/view ' ,
194
+ 'label ' => __ ('View ' ),
195
+ '__disableTmpl ' => true ,
196
+ 'target ' => '_blank '
197
+ ]
198
+ ],
199
+ ],
200
+ ]
201
+ ]
202
+ ];
203
+ }
115
204
}
0 commit comments