DataTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Test\Unit\Helper;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class DataTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_filesystem;
  17. /**
  18. * @var \Magento\Captcha\Helper\Data
  19. */
  20. protected $helper;
  21. /**
  22. * @var \Magento\Framework\App\Config\ScopeConfigInterface | \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $configMock;
  25. /**
  26. * @var \Magento\Captcha\Model\CaptchaFactory | \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $factoryMock;
  29. protected function setUp()
  30. {
  31. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  32. $className = \Magento\Captcha\Helper\Data::class;
  33. $arguments = $objectManagerHelper->getConstructArguments($className);
  34. /** @var \Magento\Framework\App\Helper\Context $context */
  35. $context = $arguments['context'];
  36. $this->configMock = $context->getScopeConfig();
  37. $this->_filesystem = $arguments['filesystem'];
  38. $storeManager = $arguments['storeManager'];
  39. $storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($this->_getWebsiteStub()));
  40. $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->_getStoreStub()));
  41. $this->factoryMock = $arguments['factory'];
  42. $this->helper = $objectManagerHelper->getObject($className, $arguments);
  43. }
  44. /**
  45. * @covers \Magento\Captcha\Helper\Data::getCaptcha
  46. */
  47. public function testGetCaptcha()
  48. {
  49. $this->configMock->expects(
  50. $this->once()
  51. )->method(
  52. 'getValue'
  53. )->with(
  54. 'customer/captcha/type'
  55. )->will(
  56. $this->returnValue('zend')
  57. );
  58. $this->factoryMock->expects(
  59. $this->once()
  60. )->method(
  61. 'create'
  62. )->with(
  63. $this->equalTo('Zend')
  64. )->will(
  65. $this->returnValue(
  66. new \Magento\Captcha\Model\DefaultModel(
  67. $this->createMock(\Magento\Framework\Session\SessionManager::class),
  68. $this->createMock(\Magento\Captcha\Helper\Data::class),
  69. $this->createPartialMock(\Magento\Captcha\Model\ResourceModel\LogFactory::class, ['create']),
  70. 'user_create'
  71. )
  72. )
  73. );
  74. $this->assertInstanceOf(\Magento\Captcha\Model\DefaultModel::class, $this->helper->getCaptcha('user_create'));
  75. }
  76. /**
  77. * @covers \Magento\Captcha\Helper\Data::getConfig
  78. */
  79. public function testGetConfigNode()
  80. {
  81. $this->configMock->expects(
  82. $this->once()
  83. )->method(
  84. 'getValue'
  85. )->with(
  86. 'customer/captcha/enable',
  87. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  88. )->will(
  89. $this->returnValue('1')
  90. );
  91. $this->helper->getConfig('enable');
  92. }
  93. public function testGetFonts()
  94. {
  95. $fontPath = 'path/to/fixture.ttf';
  96. $expectedFontPath = 'lib/' . $fontPath;
  97. $libDirMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
  98. $libDirMock->expects($this->once())
  99. ->method('getAbsolutePath')
  100. ->with($fontPath)
  101. ->will($this->returnValue($expectedFontPath));
  102. $this->_filesystem->expects($this->once())
  103. ->method('getDirectoryRead')
  104. ->with(DirectoryList::LIB_INTERNAL)
  105. ->will($this->returnValue($libDirMock));
  106. $configData = ['font_code' => ['label' => 'Label', 'path' => $fontPath]];
  107. $this->configMock->expects(
  108. $this->any()
  109. )->method(
  110. 'getValue'
  111. )->with(
  112. 'captcha/fonts',
  113. 'default'
  114. )->will(
  115. $this->returnValue($configData)
  116. );
  117. $fonts = $this->helper->getFonts();
  118. $this->assertArrayHasKey('font_code', $fonts);
  119. // fixture
  120. $this->assertArrayHasKey('label', $fonts['font_code']);
  121. $this->assertArrayHasKey('path', $fonts['font_code']);
  122. $this->assertEquals('Label', $fonts['font_code']['label']);
  123. $this->assertEquals($expectedFontPath, $fonts['font_code']['path']);
  124. }
  125. /**
  126. * @covers \Magento\Captcha\Model\DefaultModel::getImgDir
  127. * @covers \Magento\Captcha\Helper\Data::getImgDir
  128. */
  129. public function testGetImgDir()
  130. {
  131. $dirWriteMock = $this->createPartialMock(
  132. \Magento\Framework\Filesystem\Directory\Write::class,
  133. ['changePermissions', 'create', 'getAbsolutePath']
  134. );
  135. $this->_filesystem->expects(
  136. $this->once()
  137. )->method(
  138. 'getDirectoryWrite'
  139. )->with(
  140. DirectoryList::MEDIA
  141. )->will(
  142. $this->returnValue($dirWriteMock)
  143. );
  144. $dirWriteMock->expects(
  145. $this->once()
  146. )->method(
  147. 'getAbsolutePath'
  148. )->with(
  149. '/captcha/base'
  150. )->will(
  151. $this->returnValue(TESTS_TEMP_DIR . '/captcha/base')
  152. );
  153. $this->assertFileNotExists(TESTS_TEMP_DIR . '/captcha');
  154. $result = $this->helper->getImgDir();
  155. $this->assertStringStartsWith(TESTS_TEMP_DIR, $result);
  156. $this->assertStringEndsWith('captcha/base/', $result);
  157. }
  158. /**
  159. * @covers \Magento\Captcha\Model\DefaultModel::getImgUrl
  160. * @covers \Magento\Captcha\Helper\Data::getImgUrl
  161. */
  162. public function testGetImgUrl()
  163. {
  164. $this->assertEquals($this->helper->getImgUrl(), 'http://localhost/pub/media/captcha/base/');
  165. }
  166. /**
  167. * Create Website Stub
  168. *
  169. * @return \Magento\Store\Model\Website
  170. */
  171. protected function _getWebsiteStub()
  172. {
  173. $website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getCode', '__wakeup']);
  174. $website->expects($this->any())->method('getCode')->will($this->returnValue('base'));
  175. return $website;
  176. }
  177. /**
  178. * Create store stub
  179. *
  180. * @return \Magento\Store\Model\Store
  181. */
  182. protected function _getStoreStub()
  183. {
  184. $store = $this->createMock(\Magento\Store\Model\Store::class);
  185. $store->expects($this->any())->method('getBaseUrl')->will($this->returnValue('http://localhost/pub/media/'));
  186. return $store;
  187. }
  188. }