DataTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Helper;
  7. use Magento\Store\Model\ScopeInterface;
  8. class DataTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Eav\Helper\Data
  12. */
  13. protected $helper;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $eavConfig;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $attributeConfig;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $context;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $scopeConfigMock;
  30. /**
  31. * Initialize helper
  32. */
  33. protected function setUp()
  34. {
  35. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  36. $this->attributeConfig = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Config::class);
  37. $this->eavConfig = $this->createMock(\Magento\Eav\Model\Config::class);
  38. $this->context = $this->createPartialMock(\Magento\Framework\App\Helper\Context::class, ['getScopeConfig']);
  39. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  40. $this->context->expects($this->once())->method('getScopeConfig')->willReturn($this->scopeConfigMock);
  41. $this->helper = $objectManager->getObject(
  42. \Magento\Eav\Helper\Data::class,
  43. [
  44. 'attributeConfig' => $this->attributeConfig,
  45. 'eavConfig' => $this->eavConfig,
  46. 'context' => $this->context,
  47. ]
  48. );
  49. }
  50. public function testGetAttributeMetadata()
  51. {
  52. $attribute = new \Magento\Framework\DataObject([
  53. 'entity_type_id' => '1',
  54. 'attribute_id' => '2',
  55. 'backend' => new \Magento\Framework\DataObject(['table' => 'customer_entity_varchar']),
  56. 'backend_type' => 'varchar',
  57. ]);
  58. $this->eavConfig->expects($this->once())
  59. ->method('getAttribute')
  60. ->will($this->returnValue($attribute));
  61. $result = $this->helper->getAttributeMetadata('customer', 'lastname');
  62. $expected = [
  63. 'entity_type_id' => '1',
  64. 'attribute_id' => '2',
  65. 'attribute_table' => 'customer_entity_varchar',
  66. 'backend_type' => 'varchar',
  67. ];
  68. foreach ($result as $key => $value) {
  69. $this->assertArrayHasKey($key, $expected, 'Attribute metadata with key "' . $key . '" not found.');
  70. $this->assertEquals(
  71. $expected[$key],
  72. $value,
  73. 'Attribute metadata with key "' . $key . '" has invalid value.'
  74. );
  75. }
  76. }
  77. /**
  78. * @covers \Magento\Eav\Helper\Data::getFrontendClasses
  79. * @covers \Magento\Eav\Helper\Data::_getDefaultFrontendClasses
  80. */
  81. public function testGetFrontendClasses()
  82. {
  83. $result = $this->helper->getFrontendClasses('someNonExistedClass');
  84. $this->assertTrue(count($result) > 1);
  85. $this->assertContains(['value' => '', 'label' => 'None'], $result);
  86. $this->assertContains(['value' => 'validate-number', 'label' => 'Decimal Number'], $result);
  87. }
  88. /**
  89. * @covers \Magento\Eav\Helper\Data::getAttributeLockedFields
  90. */
  91. public function testGetAttributeLockedFieldsNoEntityCode()
  92. {
  93. $this->attributeConfig->expects($this->never())->method('getEntityAttributesLockedFields');
  94. $this->assertEquals([], $this->helper->getAttributeLockedFields(''));
  95. }
  96. /**
  97. * @covers \Magento\Eav\Helper\Data::getAttributeLockedFields
  98. */
  99. public function testGetAttributeLockedFieldsNonCachedLockedFiled()
  100. {
  101. $lockedFields = ['lockedField1', 'lockedField2'];
  102. $this->attributeConfig->expects($this->once())->method('getEntityAttributesLockedFields')
  103. ->with('entityTypeCode')->will($this->returnValue($lockedFields));
  104. $this->assertEquals($lockedFields, $this->helper->getAttributeLockedFields('entityTypeCode'));
  105. }
  106. /**
  107. * @covers \Magento\Eav\Helper\Data::getAttributeLockedFields
  108. */
  109. public function testGetAttributeLockedFieldsCachedLockedFiled()
  110. {
  111. $lockedFields = ['lockedField1', 'lockedField2'];
  112. $this->attributeConfig->expects($this->once())->method('getEntityAttributesLockedFields')
  113. ->with('entityTypeCode')->will($this->returnValue($lockedFields));
  114. $this->helper->getAttributeLockedFields('entityTypeCode');
  115. $this->assertEquals($lockedFields, $this->helper->getAttributeLockedFields('entityTypeCode'));
  116. }
  117. /**
  118. * @covers \Magento\Eav\Helper\Data::getAttributeLockedFields
  119. */
  120. public function testGetAttributeLockedFieldsNoLockedFields()
  121. {
  122. $this->attributeConfig->expects($this->once())->method('getEntityAttributesLockedFields')
  123. ->with('entityTypeCode')->will($this->returnValue([]));
  124. $this->assertEquals([], $this->helper->getAttributeLockedFields('entityTypeCode'));
  125. }
  126. public function testGetInputTypesValidatorData()
  127. {
  128. $configValue = 'config_value';
  129. $this->scopeConfigMock->expects($this->once())
  130. ->method('getValue')
  131. ->with(\Magento\Eav\Helper\Data::XML_PATH_VALIDATOR_DATA_INPUT_TYPES, ScopeInterface::SCOPE_STORE)
  132. ->willReturn($configValue);
  133. $this->assertEquals($configValue, $this->helper->getInputTypesValidatorData());
  134. }
  135. }