ConfigTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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\Paypal\Model\Config;
  8. use Magento\Store\Model\ScopeInterface;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. class ConfigTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Config
  14. */
  15. private $model;
  16. /**
  17. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $scopeConfig;
  20. /**
  21. * @var \Magento\Directory\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $directoryHelper;
  24. /**
  25. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $storeManager;
  28. /**
  29. * @var \Magento\Payment\Model\Source\CctypeFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $ccTypeFactory;
  32. /**
  33. * @var \Magento\Paypal\Model\CertFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $certFactory;
  36. protected function setUp()
  37. {
  38. $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  39. $this->directoryHelper = $this->getMockBuilder(\Magento\Directory\Helper\Data::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  43. $this->ccTypeFactory = $this->getMockBuilder(\Magento\Payment\Model\Source\CctypeFactory::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->certFactory = $this->getMockBuilder(\Magento\Paypal\Model\CertFactory::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->model = new Config(
  50. $this->scopeConfig,
  51. $this->directoryHelper,
  52. $this->storeManager,
  53. $this->ccTypeFactory,
  54. $this->certFactory
  55. );
  56. }
  57. public function testGetCountryMethods()
  58. {
  59. $this->assertNotContains('payflow_direct', $this->model->getCountryMethods('GB'));
  60. $this->assertContains(Config::METHOD_WPP_PE_EXPRESS, $this->model->getCountryMethods('CA'));
  61. $this->assertNotContains(Config::METHOD_WPP_PE_EXPRESS, $this->model->getCountryMethods('GB'));
  62. $this->assertContains(Config::METHOD_WPP_PE_EXPRESS, $this->model->getCountryMethods('CA'));
  63. $this->assertContains(Config::METHOD_WPP_EXPRESS, $this->model->getCountryMethods('DE'));
  64. $this->assertContains(Config::METHOD_BILLING_AGREEMENT, $this->model->getCountryMethods('DE'));
  65. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('other'));
  66. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('other'));
  67. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('CA'));
  68. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('CA'));
  69. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('GB'));
  70. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('GB'));
  71. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('AU'));
  72. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('AU'));
  73. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('NZ'));
  74. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('NZ'));
  75. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('JP'));
  76. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('JP'));
  77. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('FR'));
  78. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('FR'));
  79. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('IT'));
  80. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('IT'));
  81. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('ES'));
  82. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('ES'));
  83. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('HK'));
  84. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('HK'));
  85. $this->assertNotContains(Config::METHOD_WPP_PE_BML, $this->model->getCountryMethods('DE'));
  86. $this->assertNotContains(Config::METHOD_WPP_BML, $this->model->getCountryMethods('DE'));
  87. }
  88. public function testIsMethodActive()
  89. {
  90. $this->assertFalse($this->model->isMethodActive('payflow_direct'));
  91. }
  92. /**
  93. * test for eliminating payflow_direct
  94. */
  95. public function testIsMethodAvailableWPPPE()
  96. {
  97. $this->assertFalse($this->model->isMethodAvailable('payflow_direct'));
  98. }
  99. /**
  100. * @dataProvider isMethodAvailableDataProvider
  101. */
  102. public function testIsMethodAvailableForIsMethodActive($methodName, $expected)
  103. {
  104. if ($methodName == Config::METHOD_WPP_BML) {
  105. $valueMap = [
  106. ['paypal/general/merchant_country', ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, 'US'],
  107. ['paypal/general/merchant_country', ScopeInterface::SCOPE_STORE, null, 'US'],
  108. ['payment/paypal_express/disable_funding_options', ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, []],
  109. ];
  110. $this->scopeConfig
  111. ->method('getValue')
  112. ->willReturnMap($valueMap);
  113. $this->scopeConfig->expects($this->exactly(1))
  114. ->method('isSetFlag')
  115. ->withAnyParameters()
  116. ->willReturn(true);
  117. } else {
  118. $this->scopeConfig
  119. ->method('getValue')
  120. ->with('paypal/general/merchant_country')
  121. ->willReturn('US');
  122. $this->scopeConfig->expects($this->exactly(2))
  123. ->method('isSetFlag')
  124. ->withAnyParameters()
  125. ->willReturn(true);
  126. }
  127. $this->model->setMethod($methodName);
  128. $this->assertEquals($expected, $this->model->isMethodAvailable($methodName));
  129. }
  130. public function testGetMerchantCountryPaypal()
  131. {
  132. $this->scopeConfig->expects(static::once())
  133. ->method('getValue')
  134. ->with(
  135. 'paypal/general/merchant_country',
  136. ScopeInterface::SCOPE_STORE,
  137. null
  138. )->willReturn('US');
  139. $this->directoryHelper->expects(static::never())
  140. ->method('getDefaultCountry');
  141. static::assertEquals('US', $this->model->getMerchantCountry());
  142. }
  143. public function testGetMerchantCountryGeneral()
  144. {
  145. $this->scopeConfig->expects(static::once())
  146. ->method('getValue')
  147. ->with(
  148. 'paypal/general/merchant_country',
  149. ScopeInterface::SCOPE_STORE,
  150. null
  151. )->willReturn(null);
  152. $this->directoryHelper->expects(static::once())
  153. ->method('getDefaultCountry')
  154. ->with(null)
  155. ->willReturn('US');
  156. static::assertEquals('US', $this->model->getMerchantCountry());
  157. }
  158. /**
  159. * @return array
  160. */
  161. public function isMethodAvailableDataProvider()
  162. {
  163. return [
  164. [Config::METHOD_WPP_EXPRESS, true],
  165. [Config::METHOD_WPP_BML, true],
  166. [Config::METHOD_WPP_PE_EXPRESS, true],
  167. [Config::METHOD_WPP_PE_BML, true],
  168. ];
  169. }
  170. public function testIsCreditCardMethod()
  171. {
  172. $this->assertFalse($this->model->getIsCreditCardMethod('payflow_direct'));
  173. }
  174. public function testGetSpecificConfigPath()
  175. {
  176. $this->model->setMethod('payflow_direct');
  177. $this->assertNull($this->model->getValue('useccv'));
  178. $this->assertNull($this->model->getValue('vendor'));
  179. // _mapBmlFieldset
  180. $this->model->setMethod(Config::METHOD_WPP_BML);
  181. $this->scopeConfig->expects($this->once())
  182. ->method('getValue')
  183. ->with('payment/' . Config::METHOD_WPP_EXPRESS . '/allow_ba_signup')
  184. ->will($this->returnValue(1));
  185. $this->assertEquals(1, $this->model->getValue('allow_ba_signup'));
  186. }
  187. public function testGetSpecificConfigPathPayflow()
  188. {
  189. // _mapBmlPayflowFieldset
  190. $this->model->setMethod(Config::METHOD_WPP_PE_BML);
  191. $this->scopeConfig->expects($this->once())
  192. ->method('getValue')
  193. ->with('payment/' . Config::METHOD_WPP_PE_EXPRESS . '/allow_ba_signup')
  194. ->will($this->returnValue(1));
  195. $this->assertEquals(1, $this->model->getValue('allow_ba_signup'));
  196. }
  197. public function testGetSpecificConfigPathPayflowAdvancedLink()
  198. {
  199. // _mapWpukFieldset
  200. $this->model->setMethod(Config::METHOD_PAYFLOWADVANCED);
  201. $this->scopeConfig->expects($this->once())
  202. ->method('getValue')
  203. ->with('payment/' . Config::METHOD_PAYFLOWADVANCED . '/payment_action')
  204. ->willReturn('Authorization');
  205. $this->assertEquals('Authorization', $this->model->getValue('payment_action'));
  206. }
  207. /**
  208. * @param string $name
  209. * @param string $expectedValue
  210. * @param string|null $expectedResult
  211. *
  212. * @dataProvider payPalStylesDataProvider
  213. */
  214. public function testGetSpecificConfigPathPayPalStyles($name, $expectedValue, $expectedResult)
  215. {
  216. // _mapGenericStyleFieldset
  217. $this->scopeConfig->method('getValue')
  218. ->with('paypal/style/' . $name)
  219. ->willReturn($expectedValue);
  220. $this->assertEquals($expectedResult, $this->model->getValue($name));
  221. }
  222. /**
  223. * @return array
  224. */
  225. public function payPalStylesDataProvider(): array
  226. {
  227. return [
  228. ['checkout_page_button_customize', 'value', 'value'],
  229. ['test', 'value', null],
  230. ];
  231. }
  232. /**
  233. * @dataProvider skipOrderReviewStepDataProvider
  234. */
  235. public function testGetPayPalBasicStartUrl($value, $url)
  236. {
  237. $this->scopeConfig->expects($this->once())
  238. ->method('getValue')
  239. ->with('payment/paypal_express/skip_order_review_step')
  240. ->will($this->returnValue($value));
  241. $this->assertEquals($url, $this->model->getPayPalBasicStartUrl('token'));
  242. }
  243. /**
  244. * @return array
  245. */
  246. public function skipOrderReviewStepDataProvider()
  247. {
  248. return [
  249. [true, 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=token&useraction=commit'],
  250. [false, 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=token']
  251. ];
  252. }
  253. public function testGetExpressCheckoutOrderUrl()
  254. {
  255. $url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&order_id=orderId';
  256. $this->assertEquals($url, $this->model->getExpressCheckoutOrderUrl('orderId'));
  257. }
  258. public function testGetBmlPublisherId()
  259. {
  260. $this->scopeConfig->expects($this->once())
  261. ->method('getValue')
  262. ->with('payment/' . Config::METHOD_WPP_BML . '/publisher_id')
  263. ->will($this->returnValue('12345'));
  264. $this->assertEquals('12345', $this->model->getBmlPublisherId());
  265. }
  266. /**
  267. * @dataProvider getBmlPositionDataProvider
  268. */
  269. public function testGetBmlPosition($section, $expected)
  270. {
  271. $this->scopeConfig->expects($this->once())
  272. ->method('getValue')
  273. ->with('payment/' . Config::METHOD_WPP_BML . '/' . $section . '_position')
  274. ->will($this->returnValue($expected));
  275. $this->assertEquals($expected, $this->model->getBmlPosition($section));
  276. }
  277. /**
  278. * @return array
  279. */
  280. public function getBmlPositionDataProvider()
  281. {
  282. return [
  283. ['head', 'left'],
  284. ['checkout', 'top']
  285. ];
  286. }
  287. /**
  288. * @dataProvider getBmlSizeDataProvider
  289. */
  290. public function testGetBmlSize($section, $expected)
  291. {
  292. $this->scopeConfig->expects($this->once())
  293. ->method('getValue')
  294. ->with('payment/' . Config::METHOD_WPP_BML . '/' . $section . '_size')
  295. ->will($this->returnValue($expected));
  296. $this->assertEquals($expected, $this->model->getBmlSize($section));
  297. }
  298. /**
  299. * @return array
  300. */
  301. public function getBmlSizeDataProvider()
  302. {
  303. return [
  304. ['head', '125x75'],
  305. ['checkout', ['50x50']]
  306. ];
  307. }
  308. /**
  309. * @dataProvider dataProviderGetBmlDisplay
  310. */
  311. public function testGetBmlDisplay($section, $expectedValue, $expectedFlag, $expected)
  312. {
  313. $this->model->setStoreId(1);
  314. $this->directoryHelper->expects($this->any())
  315. ->method('getDefaultCountry')
  316. ->with(1)
  317. ->will($this->returnValue('US'));
  318. $this->scopeConfig->expects($this->any())
  319. ->method('isSetFlag')
  320. ->will($this->returnValue($expectedFlag));
  321. $this->scopeConfig->expects($this->any())
  322. ->method('getValue')
  323. ->will($this->returnValueMap([
  324. ['payment/' . Config::METHOD_WPP_BML . '/' . $section . '_display', 'store', 1, $expectedValue],
  325. ['payment/' . Config::METHOD_WPP_BML . '/active', 'store', 1, $expectedValue],
  326. ['payment/' . Config::METHOD_WPP_PE_BML . '/active', 'store', 1, $expectedValue],
  327. ]));
  328. $this->assertEquals($expected, $this->model->getBmlDisplay($section));
  329. }
  330. /**
  331. * @return array
  332. */
  333. public function dataProviderGetBmlDisplay()
  334. {
  335. return [
  336. ['head', true, true, true],
  337. ['head', true, false, false],
  338. ['head', false, true, false],
  339. ['head', false, false, false],
  340. ];
  341. }
  342. /**
  343. * @param string $localeCode
  344. * @param float|null $orderTotal
  345. * @param string|null $pal
  346. * @param string $areButtonDynamic
  347. * @param bool $sandboxFlag
  348. * @param string $buttonType
  349. * @param string $result
  350. * @dataProvider dataProviderGetExpressCheckoutShortcutImageUrl
  351. */
  352. public function testGetExpressCheckoutShortcutImageUrl(
  353. $localeCode,
  354. $orderTotal,
  355. $pal,
  356. $areButtonDynamic,
  357. $sandboxFlag,
  358. $buttonType,
  359. $result
  360. ) {
  361. $this->model->setMethod(Config::METHOD_WPP_EXPRESS);
  362. $this->model->setStoreId(123);
  363. $this->scopeConfig->expects($this->any())
  364. ->method('getValue')
  365. ->willReturnMap([
  366. ['paypal/wpp/button_flavor', ScopeInterface::SCOPE_STORE, 123, $areButtonDynamic],
  367. ['paypal/wpp/sandbox_flag', ScopeInterface::SCOPE_STORE, 123, $sandboxFlag],
  368. ['paypal/wpp/button_type', ScopeInterface::SCOPE_STORE, 123, $buttonType],
  369. ]);
  370. $this->assertEquals(
  371. $result,
  372. $this->model->getExpressCheckoutShortcutImageUrl($localeCode, $orderTotal, $pal)
  373. );
  374. }
  375. /**
  376. * @return array
  377. */
  378. public function dataProviderGetExpressCheckoutShortcutImageUrl()
  379. {
  380. return [
  381. [
  382. 'en_US', null, null, Config::EC_FLAVOR_DYNAMIC, true, Config::EC_BUTTON_TYPE_SHORTCUT,
  383. 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-medium.png'
  384. ],
  385. [
  386. 'en_GB', null, null, Config::EC_FLAVOR_DYNAMIC, true, Config::EC_BUTTON_TYPE_SHORTCUT,
  387. 'https://fpdbs.sandbox.paypal.com/dynamicimageweb?cmd=_dynamic-image&buttontype=ecshortcut&locale=en_GB'
  388. ],
  389. [
  390. 'en_GB', null, null, Config::EC_FLAVOR_DYNAMIC, false, Config::EC_BUTTON_TYPE_SHORTCUT,
  391. 'https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image&buttontype=ecshortcut&locale=en_GB'
  392. ],
  393. [
  394. 'en_US', null, null, Config::EC_FLAVOR_STATIC, false, Config::EC_BUTTON_TYPE_MARK,
  395. 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-medium.png'
  396. ],
  397. [
  398. 'en_US', null, null, Config::EC_FLAVOR_STATIC, true, Config::EC_BUTTON_TYPE_SHORTCUT,
  399. 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-medium.png'],
  400. [
  401. 'en_GB', null, null, Config::EC_FLAVOR_STATIC, true, Config::EC_BUTTON_TYPE_SHORTCUT,
  402. 'https://www.paypal.com/en_GB/i/btn/btn_xpressCheckout.gif'
  403. ],
  404. ];
  405. }
  406. /**
  407. * @param string $localeCode
  408. * @param float|null $orderTotal
  409. * @param string|null $pal
  410. * @param string|null $staticSize
  411. * @param string $areButtonDynamic
  412. * @param bool $sandboxFlag
  413. * @param string $result
  414. * @dataProvider dataProviderGetPaymentMarkImageUrl
  415. */
  416. public function testGetPaymentMarkImageUrl(
  417. $localeCode,
  418. $orderTotal,
  419. $pal,
  420. $staticSize,
  421. $areButtonDynamic,
  422. $sandboxFlag,
  423. $result
  424. ) {
  425. $this->model->setMethod(Config::METHOD_WPP_EXPRESS);
  426. $this->model->setStoreId(123);
  427. $this->scopeConfig->expects($this->any())
  428. ->method('getValue')
  429. ->willReturnMap([
  430. ['paypal/wpp/button_flavor', ScopeInterface::SCOPE_STORE, 123, $areButtonDynamic],
  431. ['paypal/wpp/sandbox_flag', ScopeInterface::SCOPE_STORE, 123, $sandboxFlag],
  432. ]);
  433. $this->assertEquals(
  434. $result,
  435. $this->model->getPaymentMarkImageUrl($localeCode, $orderTotal, $pal, $staticSize)
  436. );
  437. }
  438. /**
  439. * @return array
  440. */
  441. public function dataProviderGetPaymentMarkImageUrl()
  442. {
  443. return [
  444. [
  445. 'en_US', null, null, 'small', Config::EC_FLAVOR_DYNAMIC, true,
  446. 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/ppcredit-logo-medium.png'
  447. ],
  448. [
  449. 'en_GB', null, null, 'small', Config::EC_FLAVOR_DYNAMIC, true,
  450. 'https://fpdbs.sandbox.paypal.com/dynamicimageweb?cmd=_dynamic-image&buttontype=ecmark&locale=en_GB'
  451. ],
  452. [
  453. 'en_GB', null, null, 'small', Config::EC_FLAVOR_DYNAMIC, false,
  454. 'https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image&buttontype=ecmark&locale=en_GB'
  455. ],
  456. [
  457. 'en_US', null, null, 'medium', Config::EC_FLAVOR_STATIC, true,
  458. 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-medium.png'
  459. ],
  460. [
  461. 'en_US', null, null, 'medium', Config::EC_FLAVOR_STATIC, true,
  462. 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-medium.png'
  463. ],
  464. [
  465. 'en_US', null, null, 'large', Config::EC_FLAVOR_STATIC, true,
  466. 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-large.png'
  467. ],
  468. [
  469. 'en_GB', null, null, 'affected', Config::EC_FLAVOR_STATIC, true,
  470. 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-medium.png'
  471. ],
  472. ];
  473. }
  474. }