AbstractElementTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\Model\Config\Structure;
  7. use Magento\Config\Model\Config\Structure\ElementVisibilityInterface;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. class AbstractElementTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Config\Model\Config\Structure\AbstractElement
  14. */
  15. protected $_model;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $storeManagerMock;
  20. /**
  21. * @var \Magento\Config\Model\Config\Structure\AbstractElement | \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $moduleManagerMock;
  24. /**
  25. * @var ElementVisibilityInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $elementVisibilityMock;
  28. protected function setUp()
  29. {
  30. $this->elementVisibilityMock = $this->getMockBuilder(ElementVisibilityInterface::class)
  31. ->getMockForAbstractClass();
  32. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
  33. $this->moduleManagerMock = $this->createPartialMock(
  34. \Magento\Framework\Module\Manager::class,
  35. ['isOutputEnabled']
  36. );
  37. $this->_model = $this->getMockForAbstractClass(
  38. \Magento\Config\Model\Config\Structure\AbstractElement::class,
  39. [
  40. 'storeManager' => $this->storeManagerMock,
  41. 'moduleManager' => $this->moduleManagerMock,
  42. ]
  43. );
  44. $objectManagerHelper = new ObjectManagerHelper($this);
  45. $objectManagerHelper->setBackwardCompatibleProperty(
  46. $this->_model,
  47. 'elementVisibility',
  48. $this->elementVisibilityMock,
  49. \Magento\Config\Model\Config\Structure\AbstractElement::class
  50. );
  51. }
  52. public function testGetId()
  53. {
  54. $this->assertEquals('', $this->_model->getId());
  55. $this->_model->setData(['id' => 'someId'], 'someScope');
  56. $this->assertEquals('someId', $this->_model->getId());
  57. }
  58. public function testGetLabelTranslatesLabel()
  59. {
  60. $this->assertEquals('', $this->_model->getLabel());
  61. $this->_model->setData(['label' => 'some_label'], 'someScope');
  62. $this->assertEquals(__('some_label'), $this->_model->getLabel());
  63. }
  64. public function testGetCommentTranslatesComment()
  65. {
  66. $this->assertEquals('', $this->_model->getComment());
  67. $this->_model->setData(['comment' => 'some_comment'], 'someScope');
  68. $this->assertEquals(__('some_comment'), $this->_model->getComment());
  69. }
  70. public function testGetFrontEndModel()
  71. {
  72. $this->_model->setData(['frontend_model' => 'frontend_model_name'], 'store');
  73. $this->assertEquals('frontend_model_name', $this->_model->getFrontendModel());
  74. }
  75. public function testGetAttribute()
  76. {
  77. $this->_model->setData(
  78. ['id' => 'elementId', 'label' => 'Element Label', 'someAttribute' => 'Some attribute value'],
  79. 'someScope'
  80. );
  81. $this->assertEquals('elementId', $this->_model->getAttribute('id'));
  82. $this->assertEquals('Element Label', $this->_model->getAttribute('label'));
  83. $this->assertEquals('Some attribute value', $this->_model->getAttribute('someAttribute'));
  84. $this->assertNull($this->_model->getAttribute('nonexistingAttribute'));
  85. }
  86. public function testIsVisibleReturnsTrueInSingleStoreModeForNonHiddenElements()
  87. {
  88. $this->storeManagerMock->expects($this->once())->method('isSingleStoreMode')->will($this->returnValue(true));
  89. $this->_model->setData(
  90. ['showInDefault' => 1, 'showInStore' => 0, 'showInWebsite' => 0],
  91. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  92. );
  93. $this->assertTrue($this->_model->isVisible());
  94. }
  95. public function testIsVisibleReturnsFalseInSingleStoreModeForHiddenElements()
  96. {
  97. $this->storeManagerMock->expects($this->once())->method('isSingleStoreMode')->will($this->returnValue(true));
  98. $this->_model->setData(
  99. ['hide_in_single_store_mode' => 1, 'showInDefault' => 1, 'showInStore' => 0, 'showInWebsite' => 0],
  100. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  101. );
  102. $this->assertFalse($this->_model->isVisible());
  103. }
  104. /**
  105. * Invisible elements is contains showInDefault="0" showInWebsite="0" showInStore="0"
  106. */
  107. public function testIsVisibleReturnsFalseInSingleStoreModeForInvisibleElements()
  108. {
  109. $this->storeManagerMock->expects($this->once())->method('isSingleStoreMode')->will($this->returnValue(true));
  110. $this->_model->setData(
  111. ['showInDefault' => 0, 'showInStore' => 0, 'showInWebsite' => 0],
  112. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  113. );
  114. $this->assertFalse($this->_model->isVisible());
  115. }
  116. /**
  117. * @param array $settings
  118. * @param string $scope
  119. * @dataProvider isVisibleReturnsTrueForProperScopesDataProvider
  120. */
  121. public function testIsVisibleReturnsTrueForProperScopes($settings, $scope)
  122. {
  123. $this->_model->setData($settings, $scope);
  124. $this->assertTrue($this->_model->isVisible());
  125. }
  126. /**
  127. * @return array
  128. */
  129. public function isVisibleReturnsTrueForProperScopesDataProvider()
  130. {
  131. return [
  132. [
  133. ['showInDefault' => 1, 'showInStore' => 0, 'showInWebsite' => 0],
  134. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  135. ],
  136. [
  137. ['showInDefault' => 0, 'showInStore' => 1, 'showInWebsite' => 0],
  138. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  139. ],
  140. [
  141. ['showInDefault' => 0, 'showInStore' => 0, 'showInWebsite' => 1],
  142. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE
  143. ]
  144. ];
  145. }
  146. /**
  147. * @param array $settings
  148. * @param string $scope
  149. * @dataProvider isVisibleReturnsFalseForNonProperScopesDataProvider
  150. */
  151. public function testIsVisibleReturnsFalseForNonProperScopes($settings, $scope)
  152. {
  153. $this->_model->setData($settings, $scope);
  154. $this->assertFalse($this->_model->isVisible());
  155. }
  156. /**
  157. * @return array
  158. */
  159. public function isVisibleReturnsFalseForNonProperScopesDataProvider()
  160. {
  161. return [
  162. [
  163. ['showInDefault' => 0, 'showInStore' => 1, 'showInWebsite' => 1],
  164. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  165. ],
  166. [
  167. ['showInDefault' => 1, 'showInStore' => 0, 'showInWebsite' => 1],
  168. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  169. ],
  170. [
  171. ['showInDefault' => 1, 'showInStore' => 1, 'showInWebsite' => 0],
  172. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE
  173. ]
  174. ];
  175. }
  176. public function testIsVisibleReturnFalseIfModuleNotEnabled()
  177. {
  178. $this->moduleManagerMock->expects($this->once())
  179. ->method('isOutputEnabled')
  180. ->with('test_module')
  181. ->willReturn(false);
  182. $this->_model->setData(
  183. [
  184. 'showInDefault' => 1,
  185. 'showInStore' => 0,
  186. 'showInWebsite' => 0,
  187. 'if_module_enabled' => 'test_module',
  188. ],
  189. 'default'
  190. );
  191. $this->assertFalse($this->_model->isVisible());
  192. }
  193. public function testIsVisibleVisibilityIsHiddenTrue()
  194. {
  195. $this->elementVisibilityMock->expects($this->once())
  196. ->method('isHidden')
  197. ->willReturn(true);
  198. $this->assertFalse($this->_model->isVisible());
  199. }
  200. public function testGetClass()
  201. {
  202. $this->assertEquals('', $this->_model->getClass());
  203. $this->_model->setData(['class' => 'some_class'], 'store');
  204. $this->assertEquals('some_class', $this->_model->getClass());
  205. }
  206. public function testGetPathBuildsFullPath()
  207. {
  208. $this->_model->setData(['path' => 'section/group', 'id' => 'fieldId'], 'scope');
  209. $this->assertEquals('section/group/prefix_fieldId', $this->_model->getPath('prefix_'));
  210. }
  211. }