ContainerTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget;
  7. /**
  8. * @magentoAppArea adminhtml
  9. */
  10. class ContainerTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @magentoAppIsolation enabled
  14. */
  15. public function testPseudoConstruct()
  16. {
  17. /** @var $block \Magento\Backend\Block\Widget\Container */
  18. $block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  19. \Magento\Framework\View\LayoutInterface::class
  20. )->createBlock(
  21. \Magento\Backend\Block\Widget\Container::class,
  22. '',
  23. [
  24. 'data' => [
  25. \Magento\Backend\Block\Widget\Container::PARAM_CONTROLLER => 'one',
  26. \Magento\Backend\Block\Widget\Container::PARAM_HEADER_TEXT => 'two',
  27. ]
  28. ]
  29. );
  30. $this->assertStringEndsWith('one', $block->getHeaderCssClass());
  31. $this->assertContains('two', $block->getHeaderText());
  32. }
  33. /**
  34. * @magentoAppIsolation enabled
  35. */
  36. public function testGetButtonsHtml()
  37. {
  38. $titles = [1 => 'Title 1', 'Title 2', 'Title 3'];
  39. $block = $this->_buildBlock($titles);
  40. $html = $block->getButtonsHtml('header');
  41. $this->assertContains('<button', $html);
  42. foreach ($titles as $title) {
  43. $this->assertContains($title, $html);
  44. }
  45. }
  46. /**
  47. * @magentoAppIsolation enabled
  48. */
  49. public function testUpdateButton()
  50. {
  51. $originalTitles = [1 => 'Title 1', 'Title 2', 'Title 3'];
  52. $newTitles = [1 => 'Button A', 'Button B', 'Button C'];
  53. $block = $this->_buildBlock($originalTitles);
  54. foreach ($newTitles as $id => $newTitle) {
  55. $block->updateButton($id, 'title', $newTitle);
  56. }
  57. $html = $block->getButtonsHtml('header');
  58. foreach ($newTitles as $newTitle) {
  59. $this->assertContains($newTitle, $html);
  60. }
  61. }
  62. /**
  63. * Composes a container with several buttons in it
  64. *
  65. * @param array $titles
  66. * @param string $blockName
  67. * @return \Magento\Backend\Block\Widget\Container
  68. */
  69. protected function _buildBlock($titles, $blockName = 'block')
  70. {
  71. /** @var $layout \Magento\Framework\View\LayoutInterface */
  72. $layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  73. \Magento\Framework\View\LayoutInterface::class
  74. );
  75. /** @var $block \Magento\Backend\Block\Widget\Container */
  76. $block = $layout->createBlock(\Magento\Backend\Block\Widget\Container::class, $blockName);
  77. foreach ($titles as $id => $title) {
  78. $block->addButton($id, ['title' => $title], 0, 0, 'header');
  79. }
  80. $block->setLayout($layout);
  81. return $block;
  82. }
  83. }