DobTest.php 17 KB

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