ConfigTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /***
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Variable\Test\Unit\Model\Variable;
  7. use Magento\Backend\Model\UrlInterface;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Framework\View\Asset\Repository;
  10. use Magento\Variable\Model\ResourceModel\Variable\Collection;
  11. use Magento\Variable\Model\ResourceModel\Variable\CollectionFactory;
  12. use Magento\Variable\Model\Source\Variables;
  13. use Magento\Variable\Model\Variable\Config;
  14. class ConfigTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var Config
  18. */
  19. private $model;
  20. /**
  21. * @var Repository|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $assetRepoMock;
  24. /**
  25. * @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $urlMock;
  28. /**
  29. * @var string
  30. */
  31. private $actionUrl = 'action-url';
  32. /**
  33. * @var string
  34. */
  35. private $jsPluginSourceUrl = 'js-plugin-source';
  36. /**
  37. * @var Variables|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $storeVariablesMock;
  40. /**
  41. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $customVarsCollectionFactoryMock;
  44. /**
  45. * @var Collection|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $customVarsCollectionMock;
  48. /**
  49. * Set up before tests
  50. */
  51. protected function setUp()
  52. {
  53. $this->assetRepoMock = $this->getMockBuilder(Repository::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->urlMock = $this->getMockBuilder(UrlInterface::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->assetRepoMock->expects($this->any())
  60. ->method('getUrl')
  61. ->willReturn($this->jsPluginSourceUrl);
  62. $this->urlMock->expects($this->any())
  63. ->method('getUrl')
  64. ->willReturn($this->actionUrl);
  65. $this->storeVariablesMock = $this->getMockBuilder(Variables::class)
  66. ->disableOriginalConstructor()
  67. ->setMethods(['getData'])
  68. ->getMock();
  69. $this->customVarsCollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
  70. ->disableOriginalConstructor()
  71. ->setMethods(['create'])
  72. ->getMock();
  73. $this->customVarsCollectionMock = $this->getMockBuilder(Collection::class)
  74. ->disableOriginalConstructor()
  75. ->setMethods(['getData'])
  76. ->getMock();
  77. $this->customVarsCollectionFactoryMock->expects($this->any())
  78. ->method('create')
  79. ->willReturn($this->customVarsCollectionMock);
  80. // Set up SUT
  81. $args = [
  82. 'assetRepo' => $this->assetRepoMock,
  83. 'url' => $this->urlMock,
  84. 'collectionFactory' => $this->customVarsCollectionFactoryMock,
  85. 'storesVariables' => $this->storeVariablesMock,
  86. ];
  87. $this->model = (new ObjectManager($this))->getObject(Config::class, $args);
  88. }
  89. /**
  90. * Test method getWysiwygPluginSettings
  91. */
  92. public function testGetWysiwygPluginSettings()
  93. {
  94. $this->storeVariablesMock->expects($this->any())
  95. ->method('getData')
  96. ->willReturn([]);
  97. $this->customVarsCollectionMock->expects($this->any())
  98. ->method('getData')
  99. ->willReturn([]);
  100. $customKey = 'key';
  101. $customVal = 'val';
  102. $configObject = new \Magento\Framework\DataObject();
  103. $configObject->setPlugins([[$customKey => $customVal]]);
  104. $variablePluginConfig = $this->model->getWysiwygPluginSettings($configObject)['plugins'];
  105. $customPluginConfig = $variablePluginConfig[0];
  106. $addedPluginConfig = $variablePluginConfig[1];
  107. // Verify custom plugin config is present
  108. $this->assertSame($customVal, $customPluginConfig[$customKey]);
  109. // Verify added plugin config is present
  110. $this->assertContains($this->actionUrl, $addedPluginConfig['options']['onclick']['subject']);
  111. $this->assertContains($this->actionUrl, $addedPluginConfig['options']['url']);
  112. $this->assertContains($this->jsPluginSourceUrl, $addedPluginConfig['src']);
  113. }
  114. }