PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/AbstractDataTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 373 lines | 271 code | 32 blank | 70 comment | 6 complexity | 89021d0ad92f82ea7dafb7be78adc10e MD5 | raw file
  1. <?php
  2. /**
  3. * test Magento\Customer\Model\Metadata\Form\AbstractData
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Test\Unit\Model\Metadata\Form;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class AbstractDataTest extends \PHPUnit_Framework_TestCase
  13. {
  14. const MODEL = 'MODEL';
  15. /** @var \Magento\Customer\Test\Unit\Model\Metadata\Form\ExtendsAbstractData */
  16. protected $_model;
  17. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Stdlib\DateTime\TimezoneInterface */
  18. protected $_localeMock;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Locale\ResolverInterface */
  20. protected $_localeResolverMock;
  21. /** @var \PHPUnit_Framework_MockObject_MockObject | \Psr\Log\LoggerInterface */
  22. protected $_loggerMock;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Api\Data\AttributeMetadataInterface */
  24. protected $_attributeMock;
  25. /** @var string */
  26. protected $_value;
  27. /** @var string */
  28. protected $_entityTypeCode;
  29. /** @var string */
  30. protected $_isAjax;
  31. protected function setUp()
  32. {
  33. $this->_localeMock = $this->getMockBuilder(
  34. \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class
  35. )->disableOriginalConstructor()->getMock();
  36. $this->_localeResolverMock = $this->getMockBuilder(
  37. \Magento\Framework\Locale\ResolverInterface::class
  38. )->disableOriginalConstructor()->getMock();
  39. $this->_loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
  40. $this->_attributeMock = $this->getMock(\Magento\Customer\Api\Data\AttributeMetadataInterface::class);
  41. $this->_value = 'VALUE';
  42. $this->_entityTypeCode = 'ENTITY_TYPE_CODE';
  43. $this->_isAjax = false;
  44. $this->_model = new ExtendsAbstractData(
  45. $this->_localeMock,
  46. $this->_loggerMock,
  47. $this->_attributeMock,
  48. $this->_localeResolverMock,
  49. $this->_value,
  50. $this->_entityTypeCode,
  51. $this->_isAjax
  52. );
  53. }
  54. public function testGetAttribute()
  55. {
  56. $this->assertSame($this->_attributeMock, $this->_model->getAttribute());
  57. }
  58. /**
  59. * @expectedException \Magento\Framework\Exception\LocalizedException
  60. * @expectedExceptionMessage Attribute object is undefined
  61. */
  62. public function testGetAttributeException()
  63. {
  64. $this->_model->setAttribute(false);
  65. $this->_model->getAttribute();
  66. }
  67. public function testSetRequestScope()
  68. {
  69. $this->assertSame($this->_model, $this->_model->setRequestScope('REQUEST_SCOPE'));
  70. $this->assertSame('REQUEST_SCOPE', $this->_model->getRequestScope());
  71. }
  72. /**
  73. * @param bool $bool
  74. * @dataProvider trueFalseDataProvider
  75. */
  76. public function testSetRequestScopeOnly($bool)
  77. {
  78. $this->assertSame($this->_model, $this->_model->setRequestScopeOnly($bool));
  79. $this->assertSame($bool, $this->_model->isRequestScopeOnly());
  80. }
  81. public function trueFalseDataProvider()
  82. {
  83. return [[true], [false]];
  84. }
  85. public function testGetSetExtractedData()
  86. {
  87. $data = ['KEY' => 'VALUE'];
  88. $this->assertSame($this->_model, $this->_model->setExtractedData($data));
  89. $this->assertSame($data, $this->_model->getExtractedData());
  90. $this->assertSame('VALUE', $this->_model->getExtractedData('KEY'));
  91. $this->assertSame(null, $this->_model->getExtractedData('BAD_KEY'));
  92. }
  93. /**
  94. * @param bool|string $input
  95. * @param bool|string $output
  96. * @param bool|string $filter
  97. * @dataProvider applyInputFilterProvider
  98. */
  99. public function testApplyInputFilter($input, $output, $filter)
  100. {
  101. if ($input) {
  102. $this->_attributeMock->expects($this->once())->method('getInputFilter')->will($this->returnValue($filter));
  103. }
  104. $this->assertEquals($output, $this->_model->applyInputFilter($input));
  105. }
  106. public function applyInputFilterProvider()
  107. {
  108. return [
  109. [false, false, false],
  110. [true, true, false],
  111. ['string', 'string', false],
  112. ['2014/01/23', '2014-01-23', 'date'],
  113. ['<tag>internal text</tag>', 'internal text', 'striptags']
  114. ];
  115. }
  116. /**
  117. * @param null|bool|string $format
  118. * @param string $output
  119. * @dataProvider dateFilterFormatProvider
  120. */
  121. public function testDateFilterFormat($format, $output)
  122. {
  123. // Since model is instantiated in setup, if I use it directly in the dataProvider, it will be null.
  124. // I use this value to indicate the model is to be used for output
  125. if (self::MODEL == $output) {
  126. $output = $this->_model;
  127. }
  128. if ($format === null) {
  129. $this->_localeMock->expects(
  130. $this->once()
  131. )->method(
  132. 'getDateFormat'
  133. )->with(
  134. $this->equalTo(\IntlDateFormatter::SHORT)
  135. )->will(
  136. $this->returnValue($output)
  137. );
  138. }
  139. $actual = $this->_model->dateFilterFormat($format);
  140. $this->assertEquals($output, $actual);
  141. }
  142. public function dateFilterFormatProvider()
  143. {
  144. return [[null, 'Whatever I put'], [false, self::MODEL], ['something else', self::MODEL]];
  145. }
  146. /**
  147. * @param bool|string $input
  148. * @param bool|string $output
  149. * @param bool|string $filter
  150. * @dataProvider applyOutputFilterDataProvider
  151. */
  152. public function testApplyOutputFilter($input, $output, $filter)
  153. {
  154. if ($input) {
  155. $this->_attributeMock->expects($this->once())->method('getInputFilter')->will($this->returnValue($filter));
  156. }
  157. $this->assertEquals($output, $this->_model->applyOutputFilter($input));
  158. }
  159. /**
  160. * This is similar to applyInputFilterProvider except for striptags
  161. *
  162. * @return array
  163. */
  164. public function applyOutputFilterDataProvider()
  165. {
  166. return [
  167. [false, false, false],
  168. [true, true, false],
  169. ['string', 'string', false],
  170. ['2014/01/23', '2014-01-23', 'date'],
  171. ['internal text', 'internal text', 'striptags']
  172. ];
  173. }
  174. /**
  175. * @param null|string $value
  176. * @param null|string $label
  177. * @param null|string $inputValidation
  178. * @param bool|array $expectedOutput
  179. * @dataProvider validateInputRuleDataProvider
  180. */
  181. public function testValidateInputRule($value, $label, $inputValidation, $expectedOutput)
  182. {
  183. $validationRule = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
  184. ->disableOriginalConstructor()
  185. ->setMethods(['getName', 'getValue'])
  186. ->getMockForAbstractClass();
  187. $validationRule->expects($this->any())
  188. ->method('getName')
  189. ->will($this->returnValue('input_validation'));
  190. $validationRule->expects($this->any())
  191. ->method('getValue')
  192. ->will($this->returnValue($inputValidation));
  193. $this->_attributeMock->expects($this->any())->method('getStoreLabel')->will($this->returnValue($label));
  194. $this->_attributeMock->expects(
  195. $this->any()
  196. )->method(
  197. 'getValidationRules'
  198. )->will(
  199. $this->returnValue(
  200. [
  201. $validationRule,
  202. ]
  203. )
  204. );
  205. $this->assertEquals($expectedOutput, $this->_model->validateInputRule($value));
  206. }
  207. public function validateInputRuleDataProvider()
  208. {
  209. return [
  210. [null, null, null, true],
  211. ['value', null, null, true],
  212. [
  213. '!@#$',
  214. 'mylabel',
  215. 'alphanumeric',
  216. [
  217. \Zend_Validate_Alnum::NOT_ALNUM => '"mylabel" contains non-alphabetic or non-numeric characters.'
  218. ]
  219. ],
  220. [
  221. '!@#$',
  222. 'mylabel',
  223. 'numeric',
  224. [\Zend_Validate_Digits::NOT_DIGITS => '"mylabel" contains non-numeric characters.']
  225. ],
  226. [
  227. '1234',
  228. 'mylabel',
  229. 'alpha',
  230. [\Zend_Validate_Alpha::NOT_ALPHA => '"mylabel" contains non-alphabetic characters.']
  231. ],
  232. [
  233. '!@#$',
  234. 'mylabel',
  235. 'email',
  236. [
  237. // @codingStandardsIgnoreStart
  238. \Zend_Validate_EmailAddress::INVALID_HOSTNAME => '"mylabel" is not a valid hostname.',
  239. \Zend_Validate_Hostname::INVALID_HOSTNAME => "'#\$' does not match the expected structure for a DNS hostname",
  240. \Zend_Validate_Hostname::INVALID_LOCAL_NAME => "'#\$' does not look like a valid local network name."
  241. // @codingStandardsIgnoreEnd
  242. ]
  243. ],
  244. ['1234', 'mylabel', 'url', ['"mylabel" is not a valid URL.']],
  245. ['http://.com', 'mylabel', 'url', ['"mylabel" is not a valid URL.']],
  246. [
  247. '1234',
  248. 'mylabel',
  249. 'date',
  250. [\Zend_Validate_Date::INVALID_DATE => '"mylabel" is not a valid date.']
  251. ]
  252. ];
  253. }
  254. /**
  255. * @param bool $ajaxRequest
  256. * @dataProvider trueFalseDataProvider
  257. */
  258. public function testGetIsAjaxRequest($ajaxRequest)
  259. {
  260. $this->_model = new ExtendsAbstractData(
  261. $this->_localeMock,
  262. $this->_loggerMock,
  263. $this->_attributeMock,
  264. $this->_localeResolverMock,
  265. $this->_value,
  266. $this->_entityTypeCode,
  267. $ajaxRequest
  268. );
  269. $this->assertSame($ajaxRequest, $this->_model->getIsAjaxRequest());
  270. }
  271. /**
  272. * @param \Magento\Framework\App\RequestInterface $request
  273. * @param string $attributeCode
  274. * @param bool|string $requestScope
  275. * @param bool $requestScopeOnly
  276. * @param string $expectedValue
  277. * @dataProvider getRequestValueDataProvider
  278. */
  279. public function testGetRequestValue($request, $attributeCode, $requestScope, $requestScopeOnly, $expectedValue)
  280. {
  281. $this->_attributeMock->expects(
  282. $this->once()
  283. )->method(
  284. 'getAttributeCode'
  285. )->will(
  286. $this->returnValue($attributeCode)
  287. );
  288. $this->_model->setRequestScope($requestScope);
  289. $this->_model->setRequestScopeOnly($requestScopeOnly);
  290. $this->assertEquals($expectedValue, $this->_model->getRequestValue($request));
  291. }
  292. public function getRequestValueDataProvider()
  293. {
  294. $expectedValue = 'EXPECTED_VALUE';
  295. $requestMockOne = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)->getMock();
  296. $requestMockOne->expects(
  297. $this->any()
  298. )->method(
  299. 'getParam'
  300. )->with(
  301. 'ATTR_CODE'
  302. )->will(
  303. $this->returnValue($expectedValue)
  304. );
  305. $requestMockTwo = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)->getMock();
  306. $requestMockTwo->expects(
  307. $this->at(0)
  308. )->method(
  309. 'getParam'
  310. )->with(
  311. 'REQUEST_SCOPE'
  312. )->will(
  313. $this->returnValue(['ATTR_CODE' => $expectedValue])
  314. );
  315. $requestMockTwo->expects(
  316. $this->at(1)
  317. )->method(
  318. 'getParam'
  319. )->with(
  320. 'REQUEST_SCOPE'
  321. )->will(
  322. $this->returnValue([])
  323. );
  324. $requestMockThree = $this->getMockBuilder(
  325. \Magento\Framework\App\Request\Http::class
  326. )->disableOriginalConstructor()->getMock();
  327. $requestMockThree->expects(
  328. $this->once()
  329. )->method(
  330. 'getParams'
  331. )->will(
  332. $this->returnValue(['REQUEST' => ['SCOPE' => ['ATTR_CODE' => $expectedValue]]])
  333. );
  334. return [
  335. [$requestMockOne, 'ATTR_CODE', false, false, $expectedValue],
  336. [$requestMockTwo, 'ATTR_CODE', 'REQUEST_SCOPE', false, $expectedValue],
  337. [$requestMockTwo, 'ATTR_CODE', 'REQUEST_SCOPE', false, false],
  338. [$requestMockThree, 'ATTR_CODE', 'REQUEST/SCOPE', false, $expectedValue]
  339. ];
  340. }
  341. }