|
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\Widget\Block\Adminhtml\Widget\Instance\Edit\Tab\Main;
|
7 | 9 |
|
| 10 | +use Magento\Framework\App\Area; |
| 11 | +use Magento\Framework\App\State; |
| 12 | +use Magento\Framework\Escaper; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\Framework\View\DesignInterface; |
| 15 | +use Magento\Framework\View\LayoutInterface; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use Magento\Widget\Model\Widget\Instance; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
8 | 20 | /**
|
9 | 21 | * @magentoAppArea adminhtml
|
10 | 22 | */
|
11 |
| -class LayoutTest extends \PHPUnit\Framework\TestCase |
| 23 | +class LayoutTest extends TestCase |
12 | 24 | {
|
13 | 25 | /**
|
14 |
| - * @var \Magento\Widget\Block\Adminhtml\Widget\Instance\Edit\Tab\Main\Layout |
| 26 | + * @var ObjectManagerInterface |
| 27 | + */ |
| 28 | + private $objectManager; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Layout |
15 | 32 | */
|
16 |
| - protected $_block; |
| 33 | + private $block; |
17 | 34 |
|
| 35 | + /** |
| 36 | + * @inheritDoc |
| 37 | + */ |
18 | 38 | protected function setUp(): void
|
19 | 39 | {
|
20 |
| - parent::setUp(); |
| 40 | + $this->objectManager = Bootstrap::getObjectManager(); |
21 | 41 |
|
22 |
| - $this->_block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
23 |
| - \Magento\Framework\View\LayoutInterface::class |
24 |
| - )->createBlock( |
25 |
| - \Magento\Widget\Block\Adminhtml\Widget\Instance\Edit\Tab\Main\Layout::class, |
26 |
| - '', |
27 |
| - [ |
28 |
| - 'data' => [ |
29 |
| - 'widget_instance' => \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
30 |
| - \Magento\Widget\Model\Widget\Instance::class |
31 |
| - ), |
| 42 | + $this->block = $this->objectManager->get(LayoutInterface::class) |
| 43 | + ->createBlock( |
| 44 | + Layout::class, |
| 45 | + '', |
| 46 | + [ |
| 47 | + 'data' => [ |
| 48 | + 'widget_instance' => $this->objectManager->create(Instance::class), |
| 49 | + ], |
32 | 50 | ]
|
33 |
| - ] |
34 |
| - ); |
35 |
| - $this->_block->setLayout( |
36 |
| - \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
37 |
| - \Magento\Framework\View\LayoutInterface::class |
38 |
| - ) |
39 |
| - ); |
| 51 | + ); |
| 52 | + $this->block->setLayout($this->objectManager->get(LayoutInterface::class)); |
40 | 53 | }
|
41 | 54 |
|
42 | 55 | /**
|
43 | 56 | * @magentoAppIsolation enabled
|
44 | 57 | */
|
45 | 58 | public function testGetLayoutsChooser()
|
46 | 59 | {
|
47 |
| - \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
48 |
| - \Magento\Framework\App\State::class |
49 |
| - )->setAreaCode( |
50 |
| - \Magento\Framework\App\Area::AREA_FRONTEND |
51 |
| - ); |
52 |
| - \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
53 |
| - \Magento\Framework\View\DesignInterface::class |
54 |
| - )->setDefaultDesignTheme(); |
| 60 | + $this->objectManager->get(State::class) |
| 61 | + ->setAreaCode(Area::AREA_FRONTEND); |
| 62 | + $this->objectManager->get(DesignInterface::class) |
| 63 | + ->setDefaultDesignTheme(); |
55 | 64 |
|
56 |
| - $actualHtml = $this->_block->getLayoutsChooser(); |
| 65 | + $actualHtml = $this->block->getLayoutsChooser(); |
57 | 66 | $this->assertStringStartsWith('<select ', $actualHtml);
|
58 | 67 | $this->assertStringEndsWith('</select>', $actualHtml);
|
59 | 68 | $this->assertStringContainsString('id="layout_handle"', $actualHtml);
|
60 | 69 | $optionCount = substr_count($actualHtml, '<option ');
|
61 | 70 | $this->assertGreaterThan(1, $optionCount, 'HTML select tag must provide options to choose from.');
|
62 | 71 | $this->assertEquals($optionCount, substr_count($actualHtml, '</option>'));
|
63 | 72 | }
|
| 73 | + |
| 74 | + /** |
| 75 | + * Check that escapeUrl called from template |
| 76 | + * |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + public function testToHtml(): void |
| 80 | + { |
| 81 | + $escaperMock = $this->createMock(Escaper::class); |
| 82 | + $this->objectManager->addSharedInstance($escaperMock, Escaper::class); |
| 83 | + |
| 84 | + $escaperMock->expects($this->atLeast(6)) |
| 85 | + ->method('escapeUrl'); |
| 86 | + |
| 87 | + $this->block->toHtml(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @inheritDoc |
| 92 | + */ |
| 93 | + protected function tearDown(): void |
| 94 | + { |
| 95 | + $this->objectManager->removeSharedInstance(Escaper::class); |
| 96 | + } |
64 | 97 | }
|
0 commit comments