DataTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GoogleAdwords\Test\Unit\Helper;
  7. class DataTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $_scopeConfigMock;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_registryMock;
  17. /**
  18. * @var \Magento\GoogleAdwords\Helper\Data
  19. */
  20. protected $_helper;
  21. protected function setUp()
  22. {
  23. $className = \Magento\GoogleAdwords\Helper\Data::class;
  24. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  25. $arguments = $objectManager->getConstructArguments($className);
  26. $this->_helper = $objectManager->getObject($className, $arguments);
  27. /** @var \Magento\Framework\App\Helper\Context $context */
  28. $context = $arguments['context'];
  29. $this->_scopeConfigMock = $context->getScopeConfig();
  30. $this->_registryMock = $arguments['registry'];
  31. }
  32. /**
  33. * @return array
  34. */
  35. public function dataProviderForTestIsActive()
  36. {
  37. return [
  38. [true, 1234, true],
  39. [true, 'conversionId', false],
  40. [true, '', false],
  41. [false, '', false]
  42. ];
  43. }
  44. /**
  45. * @param bool $isActive
  46. * @param string $returnConfigValue
  47. * @param bool $returnValue
  48. * @dataProvider dataProviderForTestIsActive
  49. */
  50. public function testIsGoogleAdwordsActive($isActive, $returnConfigValue, $returnValue)
  51. {
  52. $this->_scopeConfigMock->expects(
  53. $this->any()
  54. )->method(
  55. 'isSetFlag'
  56. )->with(
  57. \Magento\GoogleAdwords\Helper\Data::XML_PATH_ACTIVE
  58. )->will(
  59. $this->returnValue($isActive)
  60. );
  61. $this->_scopeConfigMock->expects($this->any())->method('getValue')->with($this->isType('string'))->will(
  62. $this->returnCallback(
  63. function () use ($returnConfigValue) {
  64. return $returnConfigValue;
  65. }
  66. )
  67. );
  68. $this->assertEquals($returnValue, $this->_helper->isGoogleAdwordsActive());
  69. }
  70. public function testGetLanguageCodes()
  71. {
  72. $languages = ['en', 'ru', 'uk'];
  73. $this->_scopeConfigMock->expects(
  74. $this->once()
  75. )->method(
  76. 'getValue'
  77. )->with(
  78. \Magento\GoogleAdwords\Helper\Data::XML_PATH_LANGUAGES,
  79. 'default'
  80. )->will(
  81. $this->returnValue($languages)
  82. );
  83. $this->assertEquals($languages, $this->_helper->getLanguageCodes());
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function dataProviderForTestConvertLanguage()
  89. {
  90. return [
  91. ['some-language', 'some-language'],
  92. ['zh_TW', 'zh_Hant'],
  93. ['zh_CN', 'zh_Hans'],
  94. ['iw', 'he']
  95. ];
  96. }
  97. /**
  98. * @param string $language
  99. * @param string $returnLanguage
  100. * @dataProvider dataProviderForTestConvertLanguage
  101. */
  102. public function testConvertLanguageCodeToLocaleCode($language, $returnLanguage)
  103. {
  104. $convertArray = ['zh_TW' => 'zh_Hant', 'iw' => 'he', 'zh_CN' => 'zh_Hans'];
  105. $this->_scopeConfigMock->expects(
  106. $this->once()
  107. )->method(
  108. 'getValue'
  109. )->with(
  110. \Magento\GoogleAdwords\Helper\Data::XML_PATH_LANGUAGE_CONVERT,
  111. 'default'
  112. )->will(
  113. $this->returnValue($convertArray)
  114. );
  115. $this->assertEquals($returnLanguage, $this->_helper->convertLanguageCodeToLocaleCode($language));
  116. }
  117. public function testGetConversionImgSrc()
  118. {
  119. $conversionId = 123;
  120. $label = 'LabEl';
  121. $imgSrc = sprintf(
  122. 'https://www.googleadservices.com/pagead/conversion/%s/?label=%s&amp;guid=ON&amp;script=0',
  123. $conversionId,
  124. $label
  125. );
  126. $this->_scopeConfigMock->expects(
  127. $this->at(0)
  128. )->method(
  129. 'getValue'
  130. )->with(
  131. \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_IMG_SRC,
  132. 'default'
  133. )->will(
  134. $this->returnValue($imgSrc)
  135. );
  136. $this->assertEquals($imgSrc, $this->_helper->getConversionImgSrc());
  137. }
  138. public function testGetConversionJsSrc()
  139. {
  140. $jsSrc = 'some-js-src';
  141. $this->_scopeConfigMock->expects(
  142. $this->once()
  143. )->method(
  144. 'getValue'
  145. )->with(
  146. \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_JS_SRC
  147. )->will(
  148. $this->returnValue($jsSrc)
  149. );
  150. $this->assertEquals($jsSrc, $this->_helper->getConversionJsSrc());
  151. }
  152. /**
  153. * @return array
  154. */
  155. public function dataProviderForTestStoreConfig()
  156. {
  157. return [
  158. ['getConversionId', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_ID, 123],
  159. ['getConversionLanguage', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_LANGUAGE, 'en'],
  160. ['getConversionFormat', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_FORMAT, '2'],
  161. ['getConversionColor', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_COLOR, 'ffffff'],
  162. ['getConversionLabel', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_LABEL, 'Label'],
  163. ['getConversionValueType', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE_TYPE, '1'],
  164. ['getConversionValueConstant', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE, '0'],
  165. ];
  166. }
  167. /**
  168. * @param string $method
  169. * @param string $xmlPath
  170. * @param string $returnValue
  171. * @dataProvider dataProviderForTestStoreConfig
  172. */
  173. public function testGetStoreConfigValue($method, $xmlPath, $returnValue)
  174. {
  175. $this->_scopeConfigMock->expects(
  176. $this->once()
  177. )->method(
  178. 'getValue'
  179. )->with(
  180. $xmlPath
  181. )->will(
  182. $this->returnValue($returnValue)
  183. );
  184. $this->assertEquals($returnValue, $this->_helper->{$method}());
  185. }
  186. public function testHasSendConversionValueCurrency()
  187. {
  188. $this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);
  189. $this->assertTrue($this->_helper->hasSendConversionValueCurrency());
  190. }
  191. public function testGetConversionValueDynamic()
  192. {
  193. $returnValue = 4.1;
  194. $this->_scopeConfigMock->expects(
  195. $this->any()
  196. )->method(
  197. 'getValue'
  198. )->with(
  199. \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE_TYPE
  200. )->will(
  201. $this->returnValue(\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_TYPE_DYNAMIC)
  202. );
  203. $this->_registryMock->expects(
  204. $this->once()
  205. )->method(
  206. 'registry'
  207. )->with(
  208. \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME
  209. )->will(
  210. $this->returnValue($returnValue)
  211. );
  212. $this->assertEquals($returnValue, $this->_helper->getConversionValue());
  213. }
  214. public function testGetConversionValueCurrency()
  215. {
  216. $returnValueCurrency = 'USD';
  217. $this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);
  218. $this->_registryMock->expects(
  219. $this->once()
  220. )->method(
  221. 'registry'
  222. )->with(
  223. \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME
  224. )->will(
  225. $this->returnValue($returnValueCurrency)
  226. );
  227. $this->assertEquals($returnValueCurrency, $this->_helper->getConversionValueCurrency());
  228. }
  229. /**
  230. * @return array
  231. */
  232. public function dataProviderForTestConversionValueConstant()
  233. {
  234. return [[1.4, 1.4], ['', \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_DEFAULT]];
  235. }
  236. /**
  237. * @param string $conversionValueConst
  238. * @param string $returnValue
  239. * @dataProvider dataProviderForTestConversionValueConstant
  240. */
  241. public function testGetConversionValueConstant($conversionValueConst, $returnValue)
  242. {
  243. $this->_scopeConfigMock->expects(
  244. $this->at(0)
  245. )->method(
  246. 'getValue'
  247. )->with(
  248. \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE_TYPE
  249. )->will(
  250. $this->returnValue(\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_TYPE_CONSTANT)
  251. );
  252. $this->_registryMock->expects($this->never())->method('registry');
  253. $this->_scopeConfigMock->expects(
  254. $this->at(1)
  255. )->method(
  256. 'getValue'
  257. )->with(
  258. \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE
  259. )->will(
  260. $this->returnValue($conversionValueConst)
  261. );
  262. $this->assertEquals($returnValue, $this->_helper->getConversionValue());
  263. }
  264. }