11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2011 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
88namespace Magento \Cms \Test \Unit \Block \Adminhtml \Block \Widget ;
99
1010use Magento \Backend \Block \Template \Context ;
11+ use Magento \Backend \Helper \Data ;
1112use Magento \Cms \Block \Adminhtml \Block \Widget \Chooser ;
1213use Magento \Cms \Model \Block ;
1314use Magento \Cms \Model \BlockFactory ;
15+ use Magento \Cms \Model \ResourceModel \Block \CollectionFactory ;
1416use Magento \Framework \Data \Form \Element \AbstractElement ;
1517use Magento \Framework \Escaper ;
1618use Magento \Framework \Math \Random ;
@@ -77,6 +79,16 @@ class ChooserTest extends TestCase
7779 */
7880 protected $ chooserMock ;
7981
82+ /**
83+ * @var Data|MockObject
84+ */
85+ protected $ backendHelperMock ;
86+
87+ /**
88+ * @var CollectionFactory|MockObject
89+ */
90+ protected $ collectionFactoryMock ;
91+
8092 protected function setUp (): void
8193 {
8294 $ this ->layoutMock = $ this ->getMockBuilder (LayoutInterface::class)
@@ -93,6 +105,7 @@ protected function setUp(): void
93105 ->onlyMethods (
94106 [
95107 'escapeHtml ' ,
108+ 'escapeJs '
96109 ]
97110 )
98111 ->getMock ();
@@ -138,6 +151,13 @@ protected function setUp(): void
138151 )
139152 ->onlyMethods (['toHtml ' ])
140153 ->getMockForAbstractClass ();
154+ $ this ->backendHelperMock = $ this ->getMockBuilder (Data::class)
155+ ->disableOriginalConstructor ()
156+ ->getMock ();
157+
158+ $ this ->collectionFactoryMock = $ this ->getMockBuilder (CollectionFactory::class)
159+ ->disableOriginalConstructor ()
160+ ->getMock ();
141161
142162 $ objectManager = new ObjectManager ($ this );
143163 $ objectManager ->prepareObjectManager ();
@@ -286,4 +306,33 @@ public function testGetGridUrl()
286306
287307 $ this ->assertEquals ($ url , $ this ->this ->getGridUrl ());
288308 }
309+
310+ /**
311+ * @covers \Magento\Cms\Block\Adminhtml\Block\Widget\Chooser::testGetRowClickCallback
312+ */
313+ public function testGetRowClickCallback (): void
314+ {
315+ $ chooserBlock = new Chooser (
316+ $ this ->context ,
317+ $ this ->backendHelperMock ,
318+ $ this ->blockFactoryMock ,
319+ $ this ->collectionFactoryMock
320+ );
321+ $ this ->escaper ->expects ($ this ->once ())
322+ ->method ('escapeJs ' )
323+ ->willReturnCallback (function ($ input ) {
324+ return $ input ;
325+ });
326+ $ jsCallback = $ chooserBlock ->getRowClickCallback ();
327+
328+ $ this ->assertStringContainsString (
329+ 'blockId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,"") ' ,
330+ $ jsCallback ,
331+ 'JavaScript callback should use first TD cell for block ID '
332+ );
333+
334+ $ this ->assertStringContainsString ('setElementValue(blockId) ' , $ jsCallback );
335+ $ this ->assertStringContainsString ('setElementLabel(blockTitle) ' , $ jsCallback );
336+ $ this ->assertStringContainsString ('close() ' , $ jsCallback );
337+ }
289338}
0 commit comments