DocumentTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Ui\Component\DataProvider;
  7. use Magento\Customer\Api\CustomerMetadataInterface;
  8. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  9. use Magento\Customer\Api\Data\GroupInterface;
  10. use Magento\Customer\Api\Data\OptionInterface;
  11. use Magento\Customer\Api\GroupRepositoryInterface;
  12. use Magento\Customer\Ui\Component\DataProvider\Document;
  13. use Magento\Framework\Api\AttributeValue;
  14. use Magento\Framework\Api\AttributeValueFactory;
  15. use Magento\Framework\App\Config\ScopeConfigInterface;
  16. use Magento\Framework\Phrase;
  17. use Magento\Store\Api\Data\WebsiteInterface;
  18. use Magento\Store\Model\StoreManagerInterface;
  19. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  20. /**
  21. * Class DocumentTest
  22. *
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. */
  25. class DocumentTest extends \PHPUnit\Framework\TestCase
  26. {
  27. /**
  28. * @var GroupRepositoryInterface|MockObject
  29. */
  30. private $groupRepository;
  31. /**
  32. * @var AttributeValueFactory|MockObject
  33. */
  34. private $attributeValueFactory;
  35. /**
  36. * @var CustomerMetadataInterface|MockObject
  37. */
  38. private $customerMetadata;
  39. /**
  40. * @var StoreManagerInterface|MockObject
  41. */
  42. private $storeManager;
  43. /**
  44. * @var ScopeConfigInterface|MockObject
  45. */
  46. private $scopeConfig;
  47. /**
  48. * @var Document
  49. */
  50. private $document;
  51. protected function setUp()
  52. {
  53. $this->initAttributeValueFactoryMock();
  54. $this->groupRepository = $this->getMockForAbstractClass(GroupRepositoryInterface::class);
  55. $this->customerMetadata = $this->getMockForAbstractClass(CustomerMetadataInterface::class);
  56. $this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
  57. $this->scopeConfig = $this->getMockForAbstractClass(ScopeConfigInterface::class);
  58. $this->document = new Document(
  59. $this->attributeValueFactory,
  60. $this->groupRepository,
  61. $this->customerMetadata,
  62. $this->storeManager,
  63. $this->scopeConfig
  64. );
  65. }
  66. /**
  67. * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
  68. */
  69. public function testGetGenderAttribute()
  70. {
  71. $genderId = 1;
  72. $this->document->setData('gender', $genderId);
  73. $this->groupRepository->expects(static::never())
  74. ->method('getById');
  75. $this->storeManager->expects(static::never())
  76. ->method('getWebsites');
  77. $metadata = $this->getMockForAbstractClass(AttributeMetadataInterface::class);
  78. $this->customerMetadata->expects(static::once())
  79. ->method('getAttributeMetadata')
  80. ->willReturn($metadata);
  81. $option = $this->getMockForAbstractClass(OptionInterface::class);
  82. $metadata->expects(static::once())
  83. ->method('getOptions')
  84. ->willReturn([$genderId => $option]);
  85. $option->expects(static::once())
  86. ->method('getLabel')
  87. ->willReturn('Male');
  88. $attribute = $this->document->getCustomAttribute('gender');
  89. static::assertEquals('Male', $attribute->getValue());
  90. }
  91. /**
  92. * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
  93. */
  94. public function testGetGroupAttribute()
  95. {
  96. $this->document->setData('group_id', 1);
  97. $this->customerMetadata->expects(static::never())
  98. ->method('getAttributeMetadata');
  99. $this->storeManager->expects(static::never())
  100. ->method('getWebsites');
  101. $group = $this->getMockForAbstractClass(GroupInterface::class);
  102. $this->groupRepository->expects(static::once())
  103. ->method('getById')
  104. ->willReturn($group);
  105. $group->expects(static::once())
  106. ->method('getCode')
  107. ->willReturn('General');
  108. $attribute = $this->document->getCustomAttribute('group_id');
  109. static::assertEquals('General', $attribute->getValue());
  110. }
  111. /**
  112. * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
  113. */
  114. public function testGetWebsiteAttribute()
  115. {
  116. $websiteId = 1;
  117. $this->document->setData('website_id', $websiteId);
  118. $this->groupRepository->expects(static::never())
  119. ->method('getById');
  120. $this->customerMetadata->expects(static::never())
  121. ->method('getAttributeMetadata');
  122. $website = $this->getMockForAbstractClass(WebsiteInterface::class);
  123. $this->storeManager->expects(static::once())
  124. ->method('getWebsites')
  125. ->willReturn([$websiteId => $website]);
  126. $website->expects(static::once())
  127. ->method('getName')
  128. ->willReturn('Main Website');
  129. $attribute = $this->document->getCustomAttribute('website_id');
  130. static::assertEquals('Main Website', $attribute->getValue());
  131. }
  132. /**
  133. * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
  134. */
  135. public function testGetConfirmationAttribute()
  136. {
  137. $websiteId = 1;
  138. $this->document->setData('original_website_id', $websiteId);
  139. $this->scopeConfig->expects(static::once())
  140. ->method('isSetFlag')
  141. ->with()
  142. ->willReturn(true);
  143. $this->document->setData('confirmation', null);
  144. $attribute = $this->document->getCustomAttribute('confirmation');
  145. $value = $attribute->getValue();
  146. static::assertInstanceOf(Phrase::class, $value);
  147. static::assertEquals('Confirmed', (string)$value);
  148. }
  149. /**
  150. * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
  151. */
  152. public function testGetAccountLockValue()
  153. {
  154. $this->document->setData('lock_expires', null);
  155. $attribute = $this->document->getCustomAttribute('lock_expires');
  156. $value = $attribute->getValue();
  157. static::assertInstanceOf(Phrase::class, $value);
  158. static::assertEquals('Unlocked', (string)$value);
  159. }
  160. /**
  161. * Create mock for attribute value factory
  162. * @return void
  163. */
  164. private function initAttributeValueFactoryMock()
  165. {
  166. $this->attributeValueFactory = $this->getMockBuilder(AttributeValueFactory::class)
  167. ->disableOriginalConstructor()
  168. ->setMethods(['create'])
  169. ->getMock();
  170. $attributeValue = new AttributeValue();
  171. $this->attributeValueFactory->expects(static::once())
  172. ->method('create')
  173. ->willReturn($attributeValue);
  174. }
  175. }