ConfigProviderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Test\Unit\Model\Checkout;
  7. class ConfigProviderTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $storeManagerMock;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $captchaHelperMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $captchaMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $storeMock;
  25. /**
  26. * @var integer
  27. */
  28. protected $formId = 1;
  29. /**
  30. * @var \Magento\Captcha\Model\Checkout\ConfigProvider
  31. */
  32. protected $model;
  33. protected function setUp()
  34. {
  35. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  36. $this->captchaHelperMock = $this->createMock(\Magento\Captcha\Helper\Data::class);
  37. $this->captchaMock = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
  38. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  39. $formIds = [$this->formId];
  40. $this->model = new \Magento\Captcha\Model\Checkout\ConfigProvider(
  41. $this->storeManagerMock,
  42. $this->captchaHelperMock,
  43. $formIds
  44. );
  45. }
  46. /**
  47. * @dataProvider getConfigDataProvider
  48. * @param bool $isRequired
  49. * @param integer $captchaGenerations
  50. * @param array $expectedConfig
  51. */
  52. public function testGetConfig($isRequired, $captchaGenerations, $expectedConfig)
  53. {
  54. $this->captchaHelperMock->expects($this->any())->method('getCaptcha')->with($this->formId)
  55. ->will($this->returnValue($this->captchaMock));
  56. $this->captchaMock->expects($this->any())->method('isCaseSensitive')->will($this->returnValue(1));
  57. $this->captchaMock->expects($this->any())->method('getHeight')->will($this->returnValue('12px'));
  58. $this->captchaMock->expects($this->any())->method('isRequired')->will($this->returnValue($isRequired));
  59. $this->captchaMock->expects($this->exactly($captchaGenerations))->method('generate');
  60. $this->captchaMock->expects($this->exactly($captchaGenerations))->method('getImgSrc')
  61. ->will($this->returnValue('source'));
  62. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($this->storeMock));
  63. $this->storeMock->expects($this->once())->method('isCurrentlySecure')->will($this->returnValue(true));
  64. $this->storeMock->expects($this->once())->method('getUrl')->with('captcha/refresh', ['_secure' => true])
  65. ->will($this->returnValue('https://magento.com/captcha'));
  66. $config = $this->model->getConfig();
  67. unset($config['captcha'][$this->formId]['timestamp']);
  68. $this->assertEquals($config, $expectedConfig);
  69. }
  70. /**
  71. * @return array
  72. */
  73. public function getConfigDataProvider()
  74. {
  75. return [
  76. [
  77. 'isRequired' => true,
  78. 'captchaGenerations' => 1,
  79. 'expectedConfig' => [
  80. 'captcha' => [
  81. $this->formId => [
  82. 'isCaseSensitive' => true,
  83. 'imageHeight' => '12px',
  84. 'imageSrc' => 'source',
  85. 'refreshUrl' => 'https://magento.com/captcha',
  86. 'isRequired' => true
  87. ],
  88. ],
  89. ],
  90. ],
  91. [
  92. 'isRequired' => false,
  93. 'captchaGenerations' => 0,
  94. 'expectedConfig' => [
  95. 'captcha' => [
  96. $this->formId => [
  97. 'isCaseSensitive' => true,
  98. 'imageHeight' => '12px',
  99. 'imageSrc' => '',
  100. 'refreshUrl' => 'https://magento.com/captcha',
  101. 'isRequired' => false
  102. ],
  103. ],
  104. ],
  105. ],
  106. ];
  107. }
  108. }