33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Cms \Test \Unit \Ui \Component \Listing \Column ;
79
810use Magento \Cms \Ui \Component \Listing \Column \PageActions ;
11+ use Magento \Cms \ViewModel \Page \Grid \UrlBuilder ;
912use 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 ;
1019
1120/**
1221 * Test for Magento\Cms\Ui\Component\Listing\Column\PageActions class.
1322 */
14- class PageActionsTest extends \ PHPUnit \ Framework \ TestCase
23+ class PageActionsTest extends TestCase
1524{
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 ()
1760 {
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)
2665 ->getMockForAbstractClass ();
27- $ processor = $ this ->getMockBuilder (\ Magento \ Framework \ View \ Element \ UiComponent \Processor ::class)
66+ $ this -> escaperMock = $ this ->getMockBuilder (Escaper ::class)
2867 ->disableOriginalConstructor ()
68+ ->setMethods (['escapeHtml ' ])
2969 ->getMock ();
30- $ contextMock ->expects ($ this ->never ())->method ('getProcessor ' )->willReturn ($ processor );
3170
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,
3575 [
36- 'urlBuilder ' => $ urlBuilderMock ,
37- 'context ' => $ contextMock ,
76+ 'urlBuilder ' => $ this ->urlBuilderMock ,
77+ 'context ' => $ this ->contextMock ,
78+ 'scopeUrlBuilder ' => $ this ->scopeUrlBuilderMock
3879 ]
3980 );
4081
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+ }
8484
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 ())
86107 ->method ('escapeHtml ' )
87108 ->with ($ title )
88109 ->willReturn ($ title );
89110 // Configure mocks and object data
90- $ urlBuilderMock ->expects ($ this ->any ())
111+ $ this -> urlBuilderMock ->expects ($ this ->any ())
91112 ->method ('getUrl ' )
92113 ->willReturnMap (
93114 [
@@ -107,9 +128,77 @@ public function testPrepareItemsByPageId()
107128 ],
108129 ]
109130 );
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 );
112138 // Run test
113139 $ this ->assertEquals ($ expectedItems , $ items ['data ' ]['items ' ]);
114140 }
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+ }
115204}
0 commit comments