AddressTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Helper;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\CustomerMetadataInterface;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class AddressTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Customer\Helper\Address|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $helper;
  16. /** @var \Magento\Framework\App\Helper\Context */
  17. protected $context;
  18. /** @var \Magento\Framework\View\Element\BlockFactory|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $blockFactory;
  20. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $storeManager;
  22. /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $scopeConfig;
  24. /** @var CustomerMetadataInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $customerMetadataService;
  26. /** @var \Magento\Customer\Model\Address\Config|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $addressConfig;
  28. /** @var \PHPUnit_Framework_MockObject_MockObject|AddressMetadataInterface */
  29. private $addressMetadataService;
  30. protected function setUp()
  31. {
  32. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  33. $className = \Magento\Customer\Helper\Address::class;
  34. $arguments = $objectManagerHelper->getConstructArguments($className);
  35. /** @var \Magento\Framework\App\Helper\Context $context */
  36. $this->context = $arguments['context'];
  37. $this->blockFactory = $arguments['blockFactory'];
  38. $this->storeManager = $arguments['storeManager'];
  39. $this->scopeConfig = $this->context->getScopeConfig();
  40. $this->customerMetadataService = $arguments['customerMetadataService'];
  41. $this->addressConfig = $arguments['addressConfig'];
  42. $this->addressMetadataService = $arguments['addressMetadataService'];
  43. $this->helper = $objectManagerHelper->getObject($className, $arguments);
  44. }
  45. /**
  46. * @param int $numLines
  47. * @param int $expectedNumLines
  48. * @dataProvider providerGetStreetLines
  49. */
  50. public function testGetStreetLines($numLines, $expectedNumLines)
  51. {
  52. $attributeMock = $this->getMockBuilder(
  53. \Magento\Customer\Api\Data\AttributeMetadataInterface::class
  54. )->getMock();
  55. $attributeMock->expects($this->any())->method('getMultilineCount')->will($this->returnValue($numLines));
  56. $this->addressMetadataService
  57. ->expects($this->any())
  58. ->method('getAttributeMetadata')
  59. ->will($this->returnValue($attributeMock));
  60. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  61. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
  62. $this->assertEquals($expectedNumLines, $this->helper->getStreetLines());
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function providerGetStreetLines()
  68. {
  69. return [
  70. [-1, 2],
  71. [0, 2],
  72. [1, 1],
  73. [2, 2],
  74. [3, 3],
  75. [4, 4],
  76. [5, 5],
  77. [10, 10],
  78. [15, 15],
  79. [20, 20],
  80. [21, 20],
  81. ];
  82. }
  83. /**
  84. * @dataProvider getRendererDataProvider
  85. */
  86. public function testGetRenderer($renderer, $blockFactory, $result)
  87. {
  88. $this->helper = new \Magento\Customer\Helper\Address(
  89. $this->context,
  90. $blockFactory,
  91. $this->storeManager,
  92. $this->customerMetadataService,
  93. $this->addressMetadataService,
  94. $this->addressConfig
  95. );
  96. $this->assertEquals($result, $this->helper->getRenderer($renderer));
  97. }
  98. /**
  99. * @return array
  100. */
  101. public function getRendererDataProvider()
  102. {
  103. $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)->getMock();
  104. $blockFactory = $this->getMockBuilder(
  105. \Magento\Framework\View\Element\BlockFactory::class
  106. )->disableOriginalConstructor()->getMock();
  107. $blockFactory->expects($this->once())
  108. ->method('createBlock')
  109. ->with('some_test_block', [])
  110. ->will($this->returnValue($blockMock));
  111. return [
  112. ['some_test_block', $blockFactory, $blockMock],
  113. [$blockMock, $blockFactory, $blockMock],
  114. ];
  115. }
  116. public function testGetConfigCanShowConfig()
  117. {
  118. $result = ['key1' => 'value1', 'key2' => 'value2'];
  119. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  120. $store->expects($this->any())
  121. ->method('getWebsiteId')
  122. ->will($this->returnValue('1'));
  123. $this->scopeConfig->expects($this->once())//test method cache
  124. ->method('getValue')
  125. ->with('customer/address', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store)
  126. ->will($this->returnValue($result));
  127. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
  128. $this->assertNull($this->helper->getConfig('unavailable_key'));
  129. $this->assertFalse($this->helper->canShowConfig('unavailable_key'));
  130. $this->assertEquals($result['key1'], $this->helper->getConfig('key1'));
  131. $this->assertEquals($result['key2'], $this->helper->getConfig('key2'));
  132. $this->assertTrue($this->helper->canShowConfig('key1'));
  133. $this->assertTrue($this->helper->canShowConfig('key2'));
  134. }
  135. public function testGetAttributeValidationClass()
  136. {
  137. $attributeCode = 'attr_code';
  138. $attributeClass = 'Attribute_Class';
  139. $attributeMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  140. ->getMockForAbstractClass();
  141. $attributeMock->expects($this->once())
  142. ->method('getFrontendClass')
  143. ->willReturn($attributeClass);
  144. $this->addressMetadataService->expects($this->any())
  145. ->method('getAttributeMetadata')
  146. ->willReturn($attributeMock);
  147. $this->assertEquals($attributeClass, $this->helper->getAttributeValidationClass($attributeCode));
  148. }
  149. public function testGetAttributeValidationClassWithNoAttribute()
  150. {
  151. $attrCode = 'attr_code';
  152. $this->addressMetadataService->expects($this->any())
  153. ->method('getAttributeMetadata')
  154. ->willReturn(null);
  155. $this->assertEquals('', $this->helper->getAttributeValidationClass($attrCode));
  156. }
  157. /**
  158. * @param $origStreets
  159. * @param $toCount
  160. * @param $result
  161. * @dataProvider getConvertStreetLinesDataProvider
  162. */
  163. public function testConvertStreetLines($origStreets, $toCount, $result)
  164. {
  165. $this->assertEquals($result, $this->helper->convertStreetLines($origStreets, $toCount));
  166. }
  167. /**
  168. * @return array
  169. */
  170. public function getConvertStreetLinesDataProvider()
  171. {
  172. return [
  173. [['street1', 'street2', 'street3', 'street4'], 3, ['street1 street2', 'street3', 'street4']],
  174. [['street1', 'street2', 'street3', 'street4'], 2, ['street1 street2', 'street3 street4']],
  175. ];
  176. }
  177. /**
  178. * @param $store
  179. * @param $result
  180. * @dataProvider getVatValidationEnabledDataProvider
  181. */
  182. public function testIsVatValidationEnabled($store, $result)
  183. {
  184. $this->scopeConfig->expects($this->once())
  185. ->method('isSetFlag')
  186. ->with(
  187. \Magento\Customer\Helper\Address::XML_PATH_VAT_VALIDATION_ENABLED,
  188. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  189. $store
  190. )
  191. ->will($this->returnValue($result));
  192. $this->assertEquals($result, $this->helper->isVatValidationEnabled($store));
  193. }
  194. /**
  195. * @return array
  196. */
  197. public function getVatValidationEnabledDataProvider()
  198. {
  199. return [
  200. [0, true],
  201. [1, false],
  202. [2, true],
  203. ];
  204. }
  205. /**
  206. * @param $store
  207. * @param $result
  208. * @dataProvider getValidateOnEachTransactionDataProvider
  209. */
  210. public function testHasValidateOnEachTransaction($store, $result)
  211. {
  212. $this->scopeConfig->expects($this->once())
  213. ->method('isSetFlag')
  214. ->with(
  215. \Magento\Customer\Helper\Address::XML_PATH_VIV_ON_EACH_TRANSACTION,
  216. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  217. $store
  218. )
  219. ->will($this->returnValue($result));
  220. $this->assertEquals($result, $this->helper->hasValidateOnEachTransaction($store));
  221. }
  222. /**
  223. * @return array
  224. */
  225. public function getValidateOnEachTransactionDataProvider()
  226. {
  227. return [
  228. [0, true],
  229. [1, false],
  230. [2, true],
  231. ];
  232. }
  233. /**
  234. * @param $store
  235. * @param $result
  236. * @dataProvider getTaxCalculationAddressTypeDataProvider
  237. */
  238. public function testGetTaxCalculationAddressType($store, $result)
  239. {
  240. $this->scopeConfig->expects($this->once())
  241. ->method('getValue')
  242. ->with(
  243. \Magento\Customer\Helper\Address::XML_PATH_VIV_TAX_CALCULATION_ADDRESS_TYPE,
  244. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  245. $store
  246. )
  247. ->will($this->returnValue($result));
  248. $this->assertEquals($result, $this->helper->getTaxCalculationAddressType($store));
  249. }
  250. /**
  251. * @return array
  252. */
  253. public function getTaxCalculationAddressTypeDataProvider()
  254. {
  255. return [
  256. [0, 'address_type_store_0'],
  257. [1, 'address_type_store_1'],
  258. [2, 'address_type_store_2'],
  259. ];
  260. }
  261. public function testIsDisableAutoGroupAssignDefaultValue()
  262. {
  263. $this->scopeConfig->expects($this->once())
  264. ->method('isSetFlag')
  265. ->with(
  266. \Magento\Customer\Helper\Address::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT,
  267. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  268. )
  269. ->will($this->returnValue(true));
  270. $this->assertTrue($this->helper->isDisableAutoGroupAssignDefaultValue());
  271. }
  272. public function testIsVatAttributeVisible()
  273. {
  274. $this->scopeConfig->expects($this->once())
  275. ->method('isSetFlag')
  276. ->with(
  277. \Magento\Customer\Helper\Address::XML_PATH_VAT_FRONTEND_VISIBILITY,
  278. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  279. )
  280. ->will($this->returnValue(true));
  281. $this->assertTrue($this->helper->isVatAttributeVisible());
  282. }
  283. /**
  284. * @param string $code
  285. * @param \Magento\Customer\Block\Address\Renderer\RendererInterface|null $result
  286. * @dataProvider getFormatTypeRendererDataProvider
  287. */
  288. public function testGetFormatTypeRenderer($code, $result)
  289. {
  290. $this->addressConfig->expects($this->once())
  291. ->method('getFormatByCode')
  292. ->with($code)
  293. ->will($this->returnValue(
  294. new \Magento\Framework\DataObject($result !== null ? ['renderer' => $result] : [])
  295. ));
  296. $this->assertEquals($result, $this->helper->getFormatTypeRenderer($code));
  297. }
  298. /**
  299. * @return array
  300. */
  301. public function getFormatTypeRendererDataProvider()
  302. {
  303. $renderer = $this->getMockBuilder(\Magento\Customer\Block\Address\Renderer\RendererInterface::class)
  304. ->disableOriginalConstructor()->getMock();
  305. return [
  306. ['valid_code', $renderer],
  307. ['invalid_code', null]
  308. ];
  309. }
  310. /**
  311. * @param string $code
  312. * @param array $result
  313. * @dataProvider getFormatDataProvider
  314. */
  315. public function testGetFormat($code, $result)
  316. {
  317. if ($result) {
  318. $renderer = $this->getMockBuilder(\Magento\Customer\Block\Address\Renderer\RendererInterface::class)
  319. ->disableOriginalConstructor()->getMock();
  320. $renderer->expects($this->once())
  321. ->method('getFormatArray')
  322. ->will($this->returnValue(['key' => 'value']));
  323. }
  324. $this->addressConfig->expects($this->once())
  325. ->method('getFormatByCode')
  326. ->with($code)
  327. ->will($this->returnValue(
  328. new \Magento\Framework\DataObject(!empty($result) ? ['renderer' => $renderer] : [])
  329. ));
  330. $this->assertEquals($result, $this->helper->getFormat($code));
  331. }
  332. /**
  333. * @return array
  334. */
  335. public function getFormatDataProvider()
  336. {
  337. return [
  338. ['valid_code', ['key' => 'value']],
  339. ['invalid_code', '']
  340. ];
  341. }
  342. /**
  343. * @param string $attributeCode
  344. * @param bool $isMetadataExists
  345. * @dataProvider isAttributeVisibleDataProvider
  346. */
  347. public function testIsAttributeVisible($attributeCode, $isMetadataExists)
  348. {
  349. $attributeMetadata = null;
  350. if ($isMetadataExists) {
  351. $attributeMetadata = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  352. ->getMockForAbstractClass();
  353. $attributeMetadata->expects($this->once())
  354. ->method('isVisible')
  355. ->willReturn(true);
  356. }
  357. $this->addressMetadataService->expects($this->once())
  358. ->method('getAttributeMetadata')
  359. ->with($attributeCode)
  360. ->willReturn($attributeMetadata);
  361. $this->assertEquals($isMetadataExists, $this->helper->isAttributeVisible($attributeCode));
  362. }
  363. /**
  364. * @return array
  365. */
  366. public function isAttributeVisibleDataProvider()
  367. {
  368. return [
  369. ['fax', true],
  370. ['invalid_code', false]
  371. ];
  372. }
  373. /**
  374. * Data provider for test testIsAttributeRequire
  375. *
  376. * @return array
  377. */
  378. public function isAttributeRequiredDataProvider()
  379. {
  380. return [
  381. ['fax', true],
  382. ['invalid_code', false]
  383. ];
  384. }
  385. }