AbstractConfigTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Store\Model\ScopeInterface as ModelScopeInterface;
  9. use Magento\Payment\Model\MethodInterface;
  10. use Magento\Framework\App\ProductMetadataInterface;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  12. /**
  13. * Class AbstractConfigTest
  14. * @package Magento\Paypal\Test\Unit\Model
  15. */
  16. class AbstractConfigTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $scopeConfigMock;
  22. /**
  23. * @var AbstractConfigTesting|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $config;
  26. protected function setUp()
  27. {
  28. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  29. ->setMethods(['getValue', 'isSetFlag'])
  30. ->getMockForAbstractClass();
  31. $this->config = new AbstractConfigTesting($this->scopeConfigMock);
  32. }
  33. /**
  34. * @param string|MethodInterface $method
  35. * @param $expected
  36. * @dataProvider setMethodDataProvider
  37. */
  38. public function testSetMethod($method, $expected)
  39. {
  40. $this->assertSame($this->config, $this->config->setMethod($method));
  41. $this->assertEquals($expected, $this->config->getMethodCode());
  42. }
  43. public function testSetMethodInstance()
  44. {
  45. /** @var $methodInterfaceMock MethodInterface */
  46. $methodInterfaceMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  47. ->getMockForAbstractClass();
  48. $this->assertSame($this->config, $this->config->setMethodInstance($methodInterfaceMock));
  49. }
  50. /**
  51. * @case #1 The method value is string - we expected same string value
  52. * @case #2 The method value is instance of MethodInterface - we expect result MethodInterface::getCode
  53. * @case #3 The method value is not string and not instance of MethodInterface - we expect null
  54. *
  55. * @return array
  56. */
  57. public function setMethodDataProvider()
  58. {
  59. /** @var $methodInterfaceMock MethodInterface */
  60. $methodInterfaceMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  61. ->getMockForAbstractClass();
  62. $methodInterfaceMock->expects($this->once())
  63. ->method('getCode')
  64. ->willReturn('payment_code');
  65. return [
  66. ['payment_code', 'payment_code'],
  67. [$methodInterfaceMock, 'payment_code'],
  68. [['array'], null]
  69. ];
  70. }
  71. public function testGetMethod()
  72. {
  73. $this->config->setMethod('method');
  74. $this->assertEquals('method', $this->config->getMethodCode());
  75. }
  76. public function testSetStoreId()
  77. {
  78. $this->assertSame($this->config, $this->config->setStoreId(1));
  79. }
  80. /**
  81. * @param string $key
  82. * @param string $method
  83. * @param array $returnMap
  84. * @param string $expectedValue
  85. *
  86. * @dataProvider getValueDataProvider
  87. */
  88. public function testGetValue($key, $method, $returnMap, $expectedValue)
  89. {
  90. $this->config->setMethod($method);
  91. $this->scopeConfigMock->expects($this->any())
  92. ->method('getValue')
  93. ->willReturnMap($returnMap);
  94. $this->assertEquals($expectedValue, $this->config->getValue($key));
  95. }
  96. /**
  97. *
  98. * @case #1 This conf parameters must return AbstractConfig::PAYMENT_ACTION_SALE (isWppApiAvailabe == false)
  99. * @case #2 This conf parameters must return configValue (isWppApiAvailabe == true)
  100. * @case #3 This conf parameters must return configValue ($key != 'payment_action')
  101. * @case #4 This conf parameters must return configValue (configValue == 'Sale')
  102. * @case #5 This conf parameters must return configValue (shouldUseUnilateralPayments == false)
  103. * @case #6 This conf parameters must return configValue (method != METHOD_WPP_EXPRESS)
  104. *
  105. * @return array
  106. */
  107. public function getValueDataProvider()
  108. {
  109. return [
  110. [
  111. 'payment_action',
  112. AbstractConfigTesting::METHOD_WPP_EXPRESS,
  113. [
  114. ['payment/paypal_express/payment_action', ModelScopeInterface::SCOPE_STORE, null, 'notSaleValue'],
  115. ['payment/paypal_express/business_account', ModelScopeInterface::SCOPE_STORE, null, 1],
  116. ['payment/paypal_express/api_username', ModelScopeInterface::SCOPE_STORE, null, 1],
  117. ['payment/paypal_express/api_password', ModelScopeInterface::SCOPE_STORE, null, 1],
  118. ['payment/paypal_express/api_signature', ModelScopeInterface::SCOPE_STORE, null, 0],
  119. ['payment/paypal_express/api_cert', ModelScopeInterface::SCOPE_STORE, null, 0],
  120. ],
  121. AbstractConfigTesting::PAYMENT_ACTION_SALE
  122. ],
  123. [
  124. 'payment_action',
  125. AbstractConfigTesting::METHOD_WPP_EXPRESS,
  126. [
  127. ['payment/paypal_express/payment_action', ModelScopeInterface::SCOPE_STORE, null, 'configValue'],
  128. ['payment/paypal_express/business_account', ModelScopeInterface::SCOPE_STORE, null, 1],
  129. ['payment/paypal_express/api_username', ModelScopeInterface::SCOPE_STORE, null, 1],
  130. ['payment/paypal_express/api_password', ModelScopeInterface::SCOPE_STORE, null, 1],
  131. ['payment/paypal_express/api_signature', ModelScopeInterface::SCOPE_STORE, null, 1],
  132. ['payment/paypal_express/api_cert', ModelScopeInterface::SCOPE_STORE, null, 0],
  133. ],
  134. 'configValue'
  135. ],
  136. [
  137. 'payment_other',
  138. AbstractConfigTesting::METHOD_WPP_EXPRESS,
  139. [
  140. ['payment/paypal_express/payment_other', ModelScopeInterface::SCOPE_STORE, null, 'configValue'],
  141. ],
  142. 'configValue'
  143. ],
  144. [
  145. 'payment_action',
  146. AbstractConfigTesting::METHOD_WPP_EXPRESS,
  147. [
  148. ['payment/paypal_express/payment_action', ModelScopeInterface::SCOPE_STORE, null, 'Sale'],
  149. ],
  150. 'Sale'
  151. ],
  152. [
  153. 'payment_action',
  154. AbstractConfigTesting::METHOD_WPP_EXPRESS,
  155. [
  156. ['payment/paypal_express/payment_action', ModelScopeInterface::SCOPE_STORE, null, 'configValue'],
  157. ['payment/paypal_express/business_account', ModelScopeInterface::SCOPE_STORE, null, 0],
  158. ],
  159. 'configValue'
  160. ],
  161. [
  162. 'payment_action',
  163. 'method_other',
  164. [
  165. ['payment/method_other/payment_action', ModelScopeInterface::SCOPE_STORE, null, 'configValue'],
  166. ],
  167. 'configValue'
  168. ],
  169. ];
  170. }
  171. /**
  172. * @param array $returnMap
  173. * @param bool $expectedValue
  174. *
  175. * @dataProvider isWppApiAvailabeDataProvider
  176. */
  177. public function testIsWppApiAvailable($returnMap, $expectedValue)
  178. {
  179. $this->config->setMethod('paypal_express');
  180. $this->scopeConfigMock->expects($this->any())
  181. ->method('getValue')
  182. ->willReturnMap($returnMap);
  183. $this->assertEquals($expectedValue, $this->config->isWppApiAvailable());
  184. }
  185. /**
  186. * @return array
  187. */
  188. public function isWppApiAvailabeDataProvider()
  189. {
  190. return [
  191. [
  192. [
  193. ['payment/paypal_express/api_username', ModelScopeInterface::SCOPE_STORE, null, 1],
  194. ['payment/paypal_express/api_password', ModelScopeInterface::SCOPE_STORE, null, 1],
  195. ['payment/paypal_express/api_signature', ModelScopeInterface::SCOPE_STORE, null, 0],
  196. ['payment/paypal_express/api_cert', ModelScopeInterface::SCOPE_STORE, null, 0],
  197. ],
  198. false
  199. ],
  200. [
  201. [
  202. ['payment/paypal_express/api_username', ModelScopeInterface::SCOPE_STORE, null, 0],
  203. ],
  204. false
  205. ],
  206. [
  207. [
  208. ['payment/paypal_express/api_username', ModelScopeInterface::SCOPE_STORE, null, 1],
  209. ['payment/paypal_express/api_password', ModelScopeInterface::SCOPE_STORE, null, 0],
  210. ],
  211. false
  212. ],
  213. [
  214. [
  215. ['payment/paypal_express/api_username', ModelScopeInterface::SCOPE_STORE, null, 1],
  216. ['payment/paypal_express/api_password', ModelScopeInterface::SCOPE_STORE, null, 1],
  217. ['payment/paypal_express/api_signature', ModelScopeInterface::SCOPE_STORE, null, 1],
  218. ['payment/paypal_express/api_cert', ModelScopeInterface::SCOPE_STORE, null, 0],
  219. ],
  220. true
  221. ],
  222. [
  223. [
  224. ['payment/paypal_express/api_username', ModelScopeInterface::SCOPE_STORE, null, 1],
  225. ['payment/paypal_express/api_password', ModelScopeInterface::SCOPE_STORE, null, 1],
  226. ['payment/paypal_express/api_signature', ModelScopeInterface::SCOPE_STORE, null, 0],
  227. ['payment/paypal_express/api_cert', ModelScopeInterface::SCOPE_STORE, null, 1],
  228. ],
  229. true
  230. ],
  231. [
  232. [
  233. ['payment/paypal_express/api_username', ModelScopeInterface::SCOPE_STORE, null, 1],
  234. ['payment/paypal_express/api_password', ModelScopeInterface::SCOPE_STORE, null, 1],
  235. ['payment/paypal_express/api_signature', ModelScopeInterface::SCOPE_STORE, null, 1],
  236. ['payment/paypal_express/api_cert', ModelScopeInterface::SCOPE_STORE, null, 1],
  237. ],
  238. true
  239. ],
  240. ];
  241. }
  242. /**
  243. * @param string|null $methodCode
  244. * @param bool $expectedFlag
  245. *
  246. * @dataProvider isMethodAvailableDataProvider
  247. */
  248. public function testIsMethodAvailable($methodCode, $expectedFlag)
  249. {
  250. $this->config->setMethod('settedMethod');
  251. $this->scopeConfigMock->expects($this->once())
  252. ->method('isSetFlag')
  253. ->with($expectedFlag);
  254. $this->config->isMethodAvailable($methodCode);
  255. }
  256. /**
  257. * @return array
  258. */
  259. public function isMethodAvailableDataProvider()
  260. {
  261. return [
  262. [null, 'payment/settedMethod/active'],
  263. ['newMethod', 'payment/newMethod/active'],
  264. ];
  265. }
  266. public function testIsMethodActive()
  267. {
  268. $this->scopeConfigMock->expects($this->once())
  269. ->method('isSetFlag')
  270. ->with('payment/method/active');
  271. $this->config->isMethodActive('method');
  272. }
  273. /**
  274. * Check bill me later active setting uses disable funding options
  275. *
  276. * @param string|null $disableFundingOptions
  277. * @param int $expectedFlag
  278. * @param bool $expectedValue
  279. *
  280. * @dataProvider isMethodActiveBmlDataProvider
  281. */
  282. public function testIsMethodActiveBml($disableFundingOptions, $expectedFlag, $expectedValue)
  283. {
  284. $this->scopeConfigMock->method('getValue')
  285. ->with(
  286. self::equalTo('payment/paypal_express/disable_funding_options'),
  287. self::equalTo('store')
  288. )
  289. ->willReturn($disableFundingOptions);
  290. $this->scopeConfigMock->method('isSetFlag')
  291. ->with('payment/paypal_express_bml/active')
  292. ->willReturn($expectedFlag);
  293. self::assertEquals($expectedValue, $this->config->isMethodActive('paypal_express_bml'));
  294. }
  295. /**
  296. * @return array
  297. */
  298. public function isMethodActiveBmlDataProvider()
  299. {
  300. return [
  301. ['CREDIT,CARD,ELV', 0, false],
  302. ['CREDIT,CARD,ELV', 1, true],
  303. ['CREDIT', 0, false],
  304. ['CREDIT', 1, true],
  305. ['CARD', 0, true],
  306. ['CARD', 1, true],
  307. [null, 0, true],
  308. [null, 1, true]
  309. ];
  310. }
  311. /**
  312. * Checks a case, when notation code based on Magento edition.
  313. */
  314. public function testGetBuildNotationCode()
  315. {
  316. $productMetadata = $this->getMockBuilder(ProductMetadataInterface::class)
  317. ->disableOriginalConstructor()
  318. ->getMock();
  319. $productMetadata->method('getEdition')
  320. ->willReturn('SomeEdition');
  321. $objectManagerHelper = new ObjectManagerHelper($this);
  322. $objectManagerHelper->setBackwardCompatibleProperty(
  323. $this->config,
  324. 'productMetadata',
  325. $productMetadata
  326. );
  327. self::assertEquals('Magento_Cart_SomeEdition', $this->config->getBuildNotationCode());
  328. }
  329. /**
  330. * Checks a case, when notation code should be provided from configuration.
  331. */
  332. public function testBuildNotationCodeFromConfig()
  333. {
  334. $notationCode = 'Magento_Cart_EditionFromConfig';
  335. $this->scopeConfigMock->method('getValue')
  336. ->with(self::equalTo('paypal/notation_code'), self::equalTo('stores'))
  337. ->willReturn($notationCode);
  338. self::assertEquals($notationCode, $this->config->getBuildNotationCode());
  339. }
  340. }