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

/vendor/magento/module-customer/Test/Unit/Block/Widget/DobTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 429 lines | 279 code | 51 blank | 99 comment | 0 complexity | 538ba61ce147cb4a9b40c8ffef2d632a MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\Customer\Test\Unit\Block\Widget;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Customer\Block\Widget\Dob;
  10. use Magento\Framework\Locale\Resolver;
  11. class DobTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /** Constants used in the unit tests */
  14. const MIN_DATE = '01/01/2010';
  15. const MAX_DATE = '01/01/2020';
  16. const DATE = '01/01/2014';
  17. const DAY = '01';
  18. // Value of date('d', strtotime(self::DATE))
  19. const MONTH = '01';
  20. // Value of date('m', strtotime(self::DATE))
  21. const YEAR = '2014';
  22. // Value of date('Y', strtotime(self::DATE))
  23. const DATE_FORMAT = 'M/d/yy';
  24. /** Constants used by Dob::setDateInput($code, $html) */
  25. const DAY_HTML =
  26. '<div><label for="day"><span>d</span></label><input type="text" id="day" name="Day" value="1"></div>';
  27. const MONTH_HTML =
  28. '<div><label for="month"><span>M</span></label><input type="text" id="month" name="Month" value="jan"></div>';
  29. const YEAR_HTML =
  30. '<div><label for="year"><span>yy</span></label><input type="text" id="year" name="Year" value="14"></div>';
  31. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Api\Data\AttributeMetadataInterface */
  32. private $attribute;
  33. /** @var Dob */
  34. private $_block;
  35. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Api\CustomerMetadataInterface */
  36. private $customerMetadata;
  37. public function setUp()
  38. {
  39. $zendCacheCore = new \Zend_Cache_Core();
  40. $zendCacheCore->setBackend(new \Zend_Cache_Backend_BlackHole());
  41. $frontendCache = $this->getMockForAbstractClass(
  42. 'Magento\Framework\Cache\FrontendInterface',
  43. [],
  44. '',
  45. false
  46. );
  47. $frontendCache->expects($this->any())->method('getLowLevelFrontend')->will($this->returnValue($zendCacheCore));
  48. $cache = $this->getMock('Magento\Framework\App\CacheInterface');
  49. $cache->expects($this->any())->method('getFrontend')->will($this->returnValue($frontendCache));
  50. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  51. $localeResolver = $this->getMock('\Magento\Framework\Locale\ResolverInterface');
  52. $localeResolver->expects($this->any())
  53. ->method('getLocale')
  54. ->willReturn(Resolver::DEFAULT_LOCALE);
  55. $timezone = $objectManager->getObject(
  56. 'Magento\Framework\Stdlib\DateTime\Timezone',
  57. ['localeResolver' => $localeResolver]
  58. );
  59. $context = $this->getMock('Magento\Framework\View\Element\Template\Context', [], [], '', false);
  60. $context->expects($this->any())->method('getLocaleDate')->will($this->returnValue($timezone));
  61. $this->attribute = $this->getMockBuilder('\Magento\Customer\Api\Data\AttributeMetadataInterface')
  62. ->getMockForAbstractClass();
  63. $this->customerMetadata = $this->getMockBuilder('\Magento\Customer\Api\CustomerMetadataInterface')
  64. ->getMockForAbstractClass();
  65. $this->customerMetadata->expects($this->any())
  66. ->method('getAttributeMetadata')
  67. ->will($this->returnValue($this->attribute));
  68. date_default_timezone_set('America/Los_Angeles');
  69. $this->_block = new \Magento\Customer\Block\Widget\Dob(
  70. $context,
  71. $this->getMock('Magento\Customer\Helper\Address', [], [], '', false),
  72. $this->customerMetadata,
  73. $this->getMock('Magento\Framework\View\Element\Html\Date', [], [], '', false)
  74. );
  75. }
  76. /**
  77. * @param bool $isVisible Determines whether the 'dob' attribute is visible or enabled
  78. * @param bool $expectedValue The value we expect from Dob::isEnabled()
  79. *
  80. * @dataProvider isEnabledDataProvider
  81. */
  82. public function testIsEnabled($isVisible, $expectedValue)
  83. {
  84. $this->attribute->expects($this->once())->method('isVisible')->will($this->returnValue($isVisible));
  85. $this->assertSame($expectedValue, $this->_block->isEnabled());
  86. }
  87. /**
  88. * @return array
  89. */
  90. public function isEnabledDataProvider()
  91. {
  92. return [[true, true], [false, false]];
  93. }
  94. public function testIsEnabledWithException()
  95. {
  96. $this->customerMetadata->expects($this->any())
  97. ->method('getAttributeMetadata')
  98. ->will(
  99. $this->throwException(new NoSuchEntityException(
  100. __(
  101. NoSuchEntityException::MESSAGE_SINGLE_FIELD,
  102. ['fieldName' => 'field', 'fieldValue' => 'value']
  103. )
  104. ))
  105. );
  106. $this->assertSame(false, $this->_block->isEnabled());
  107. }
  108. /**
  109. * @param bool $isRequired Determines whether the 'dob' attribute is required
  110. * @param bool $expectedValue The value we expect from Dob::isRequired()
  111. *
  112. * @dataProvider isRequiredDataProvider
  113. */
  114. public function testIsRequired($isRequired, $expectedValue)
  115. {
  116. $this->attribute->expects($this->once())->method('isRequired')->will($this->returnValue($isRequired));
  117. $this->assertSame($expectedValue, $this->_block->isRequired());
  118. }
  119. public function testIsRequiredWithException()
  120. {
  121. $this->customerMetadata->expects($this->any())
  122. ->method('getAttributeMetadata')
  123. ->will(
  124. $this->throwException(new NoSuchEntityException(
  125. __(
  126. NoSuchEntityException::MESSAGE_SINGLE_FIELD,
  127. ['fieldName' => 'field', 'fieldValue' => 'value']
  128. )
  129. ))
  130. );
  131. $this->assertSame(false, $this->_block->isRequired());
  132. }
  133. /**
  134. * @return array
  135. */
  136. public function isRequiredDataProvider()
  137. {
  138. return [[true, true], [false, false]];
  139. }
  140. /**
  141. * @param string|bool $date Date (e.g. '01/01/2020' or false for no date)
  142. * @param int|bool $expectedTime The value we expect from Dob::getTime()
  143. * @param string|bool $expectedDate The value we expect from Dob::getData('date')
  144. *
  145. * @dataProvider setDateDataProvider
  146. */
  147. public function testSetDate($date, $expectedTime, $expectedDate)
  148. {
  149. $this->assertSame($this->_block, $this->_block->setDate($date));
  150. $this->assertEquals($expectedTime, $this->_block->getTime());
  151. $this->assertEquals($expectedDate, $this->_block->getData('date'));
  152. }
  153. /**
  154. * @return array
  155. */
  156. public function setDateDataProvider()
  157. {
  158. return [[self::DATE, strtotime(self::DATE), self::DATE], [false, false, false]];
  159. }
  160. /**
  161. * @param string|bool $date The date (e.g. '01/01/2020' or false for no date)
  162. * @param string $expectedDay The value we expect from Dob::getDay()
  163. *
  164. * @dataProvider getDayDataProvider
  165. */
  166. public function testGetDay($date, $expectedDay)
  167. {
  168. $this->_block->setDate($date);
  169. $this->assertEquals($expectedDay, $this->_block->getDay());
  170. }
  171. /**
  172. * @return array
  173. */
  174. public function getDayDataProvider()
  175. {
  176. return [[self::DATE, self::DAY], [false, '']];
  177. }
  178. /**
  179. * @param string|bool $date The date (e.g. '01/01/2020' or false for no date)
  180. * @param string $expectedMonth The value we expect from Dob::getMonth()
  181. *
  182. * @dataProvider getMonthDataProvider
  183. */
  184. public function testGetMonth($date, $expectedMonth)
  185. {
  186. $this->_block->setDate($date);
  187. $this->assertEquals($expectedMonth, $this->_block->getMonth());
  188. }
  189. /**
  190. * @return array
  191. */
  192. public function getMonthDataProvider()
  193. {
  194. return [[self::DATE, self::MONTH], [false, '']];
  195. }
  196. /**
  197. * @param string|bool $date The date (e.g. '01/01/2020' or false for no date)
  198. * @param string $expectedYear The value we expect from Dob::getYear()
  199. *
  200. * @dataProvider getYearDataProvider
  201. */
  202. public function testGetYear($date, $expectedYear)
  203. {
  204. $this->_block->setDate($date);
  205. $this->assertEquals($expectedYear, $this->_block->getYear());
  206. }
  207. /**
  208. * @return array
  209. */
  210. public function getYearDataProvider()
  211. {
  212. return [[self::DATE, self::YEAR], [false, '']];
  213. }
  214. /**
  215. * The \Magento\Framework\Locale\ResolverInterface::DEFAULT_LOCALE
  216. * is used to derive the Locale that is used to determine the
  217. * value of Dob::getDateFormat() for that Locale.
  218. */
  219. public function testGetDateFormat()
  220. {
  221. $this->assertEquals(self::DATE_FORMAT, $this->_block->getDateFormat());
  222. }
  223. /**
  224. * This tests the Dob::setDateInput() method. The Dob::getSortedDateInputs() uses the value of
  225. * Dob::getDateFormat() to derive the return value, which is equivalent to self::DATE_FORMAT.
  226. */
  227. public function testGetSortedDateInputs()
  228. {
  229. $this->_block->setDateInput('d', self::DAY_HTML);
  230. $this->_block->setDateInput('m', self::MONTH_HTML);
  231. $this->_block->setDateInput('y', self::YEAR_HTML);
  232. $this->assertEquals(self::MONTH_HTML . self::DAY_HTML . self::YEAR_HTML, $this->_block->getSortedDateInputs());
  233. }
  234. /**
  235. * This tests the Dob::setDateInput() method. The Dob::getSortedDateInputs() uses the value of
  236. * Dob::getDateFormat() to derive the return value, which is equivalent to self::DATE_FORMAT.
  237. */
  238. public function testGetSortedDateInputsWithoutStrippingNonInputChars()
  239. {
  240. $this->_block->setDateInput('d', self::DAY_HTML);
  241. $this->_block->setDateInput('m', self::MONTH_HTML);
  242. $this->_block->setDateInput('y', self::YEAR_HTML);
  243. $this->assertEquals(
  244. self::MONTH_HTML . '/' . self::DAY_HTML . '/' . self::YEAR_HTML,
  245. $this->_block->getSortedDateInputs(false)
  246. );
  247. }
  248. /**
  249. * @param array $validationRules The date Min/Max validation rules
  250. * @param int $expectedValue The value we expect from Dob::getMinDateRange()
  251. *
  252. * @dataProvider getMinDateRangeDataProvider
  253. */
  254. public function testGetMinDateRange($validationRules, $expectedValue)
  255. {
  256. $this->attribute->expects(
  257. $this->once()
  258. )->method(
  259. 'getValidationRules'
  260. )->will(
  261. $this->returnValue($validationRules)
  262. );
  263. $this->assertEquals($expectedValue, $this->_block->getMinDateRange());
  264. }
  265. /**
  266. * @return array
  267. */
  268. public function getMinDateRangeDataProvider()
  269. {
  270. $emptyValidationRule = $this->getMockBuilder('Magento\Customer\Api\Data\ValidationRuleInterface')
  271. ->disableOriginalConstructor()
  272. ->setMethods(['getName', 'getValue'])
  273. ->getMockForAbstractClass();
  274. $validationRule = $this->getMockBuilder('Magento\Customer\Api\Data\ValidationRuleInterface')
  275. ->disableOriginalConstructor()
  276. ->setMethods(['getName', 'getValue'])
  277. ->getMockForAbstractClass();
  278. $validationRule->expects($this->any())
  279. ->method('getName')
  280. ->will($this->returnValue(Dob::MIN_DATE_RANGE_KEY));
  281. $validationRule->expects($this->any())
  282. ->method('getValue')
  283. ->will($this->returnValue(strtotime(self::MIN_DATE)));
  284. return [
  285. [
  286. [
  287. $validationRule,
  288. ],
  289. date('Y/m/d', strtotime(self::MIN_DATE)),
  290. ],
  291. [
  292. [
  293. $emptyValidationRule,
  294. ],
  295. null
  296. ]
  297. ];
  298. }
  299. public function testGetMinDateRangeWithException()
  300. {
  301. $this->customerMetadata->expects($this->any())
  302. ->method('getAttributeMetadata')
  303. ->will(
  304. $this->throwException(new NoSuchEntityException(
  305. __(
  306. NoSuchEntityException::MESSAGE_SINGLE_FIELD,
  307. ['fieldName' => 'field', 'fieldValue' => 'value']
  308. )
  309. ))
  310. );
  311. $this->assertNull($this->_block->getMinDateRange());
  312. }
  313. /**
  314. * @param array $validationRules The date Min/Max validation rules
  315. * @param int $expectedValue The value we expect from Dob::getMaxDateRange()
  316. *
  317. * @dataProvider getMaxDateRangeDataProvider
  318. */
  319. public function testGetMaxDateRange($validationRules, $expectedValue)
  320. {
  321. $this->attribute->expects(
  322. $this->once()
  323. )->method(
  324. 'getValidationRules'
  325. )->will(
  326. $this->returnValue($validationRules)
  327. );
  328. $this->assertEquals($expectedValue, $this->_block->getMaxDateRange());
  329. }
  330. /**
  331. * @return array
  332. */
  333. public function getMaxDateRangeDataProvider()
  334. {
  335. $emptyValidationRule = $this->getMockBuilder('Magento\Customer\Api\Data\ValidationRuleInterface')
  336. ->disableOriginalConstructor()
  337. ->setMethods(['getName', 'getValue'])
  338. ->getMockForAbstractClass();
  339. $validationRule = $this->getMockBuilder('Magento\Customer\Api\Data\ValidationRuleInterface')
  340. ->disableOriginalConstructor()
  341. ->setMethods(['getName', 'getValue'])
  342. ->getMockForAbstractClass();
  343. $validationRule->expects($this->any())
  344. ->method('getName')
  345. ->will($this->returnValue(Dob::MAX_DATE_RANGE_KEY));
  346. $validationRule->expects($this->any())
  347. ->method('getValue')
  348. ->will($this->returnValue(strtotime(self::MAX_DATE)));
  349. return [
  350. [
  351. [
  352. $validationRule,
  353. ],
  354. date('Y/m/d', strtotime(self::MAX_DATE)),
  355. ],
  356. [
  357. [
  358. $emptyValidationRule,
  359. ],
  360. null
  361. ]
  362. ];
  363. }
  364. public function testGetMaxDateRangeWithException()
  365. {
  366. $this->customerMetadata->expects($this->any())
  367. ->method('getAttributeMetadata')
  368. ->will(
  369. $this->throwException(new NoSuchEntityException(
  370. __(
  371. NoSuchEntityException::MESSAGE_SINGLE_FIELD,
  372. ['fieldName' => 'field', 'fieldValue' => 'value']
  373. )
  374. ))
  375. );
  376. $this->assertNull($this->_block->getMaxDateRange());
  377. }
  378. }