PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Customer/Test/Unit/Block/Adminhtml/Edit/Tab/View/PersonalInfoTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 300 lines | 188 code | 34 blank | 78 comment | 0 complexity | 755755fd257b1f1813f69bdcdaaf0370 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Block\Adminhtml\Edit\Tab\View;
  7. use Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo;
  8. use Magento\Framework\Stdlib\DateTime;
  9. /**
  10. * Customer personal information template block test.
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class PersonalInfoTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $defaultTimezone = 'America/Los_Angeles';
  20. /**
  21. * @var string
  22. */
  23. protected $pathToDefaultTimezone = 'path/to/default/timezone';
  24. /**
  25. * @var PersonalInfo
  26. */
  27. protected $block;
  28. /**
  29. * @var \Magento\Customer\Model\Log|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $customerLog;
  32. /**
  33. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $localeDate;
  36. /**
  37. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $scopeConfig;
  40. /**
  41. * @var \Magento\Customer\Model\CustomerRegistry
  42. */
  43. protected $customerRegistry;
  44. /**
  45. * @var \Magento\Customer\Model\Customer
  46. */
  47. protected $customerModel;
  48. /**
  49. * @return void
  50. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  51. */
  52. protected function setUp()
  53. {
  54. $customer = $this->getMock(
  55. \Magento\Customer\Api\Data\CustomerInterface::class,
  56. [],
  57. [],
  58. '',
  59. false
  60. );
  61. $customer->expects($this->any())->method('getId')->willReturn(1);
  62. $customer->expects($this->any())->method('getStoreId')->willReturn(1);
  63. $customerDataFactory = $this->getMock(
  64. \Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
  65. ['create'],
  66. [],
  67. '',
  68. false
  69. );
  70. $customerDataFactory->expects($this->any())->method('create')->willReturn($customer);
  71. $backendSession = $this->getMock(
  72. \Magento\Backend\Model\Session::class,
  73. ['getCustomerData'],
  74. [],
  75. '',
  76. false
  77. );
  78. $backendSession->expects($this->any())->method('getCustomerData')->willReturn(['account' => []]);
  79. $this->customerLog = $this->getMock(
  80. \Magento\Customer\Model\Log::class,
  81. ['getLastLoginAt', 'getLastVisitAt', 'getLastLogoutAt'],
  82. [],
  83. '',
  84. false
  85. );
  86. $this->customerLog->expects($this->any())->method('loadByCustomer')->willReturnSelf();
  87. $customerLogger = $this->getMock(
  88. \Magento\Customer\Model\Logger::class,
  89. ['get'],
  90. [],
  91. '',
  92. false
  93. );
  94. $customerLogger->expects($this->any())->method('get')->willReturn($this->customerLog);
  95. $dateTime = $this->getMock(
  96. \Magento\Framework\Stdlib\DateTime::class,
  97. ['now'],
  98. [],
  99. '',
  100. false
  101. );
  102. $dateTime->expects($this->any())->method('now')->willReturn('2015-03-04 12:00:00');
  103. $this->localeDate = $this->getMock(
  104. \Magento\Framework\Stdlib\DateTime\Timezone::class,
  105. ['scopeDate', 'formatDateTime', 'getDefaultTimezonePath'],
  106. [],
  107. '',
  108. false
  109. );
  110. $this->localeDate
  111. ->expects($this->any())
  112. ->method('getDefaultTimezonePath')
  113. ->willReturn($this->pathToDefaultTimezone);
  114. $this->scopeConfig = $this->getMock(
  115. \Magento\Framework\App\Config::class,
  116. ['getValue'],
  117. [],
  118. '',
  119. false
  120. );
  121. $this->customerRegistry = $this->getMock(
  122. \Magento\Customer\Model\CustomerRegistry::class,
  123. ['retrieve'],
  124. [],
  125. '',
  126. false
  127. );
  128. $this->customerModel = $this->getMock(
  129. \Magento\Customer\Model\Customer::class,
  130. ['isCustomerLocked'],
  131. [],
  132. '',
  133. false
  134. );
  135. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  136. $this->block = $objectManagerHelper->getObject(
  137. \Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo::class,
  138. [
  139. 'customerDataFactory' => $customerDataFactory,
  140. 'dateTime' => $dateTime,
  141. 'customerLogger' => $customerLogger,
  142. 'localeDate' => $this->localeDate,
  143. 'scopeConfig' => $this->scopeConfig,
  144. 'backendSession' => $backendSession,
  145. ]
  146. );
  147. $this->block->setCustomerRegistry($this->customerRegistry);
  148. }
  149. /**
  150. * @return void
  151. */
  152. public function testGetStoreLastLoginDateTimezone()
  153. {
  154. $this->scopeConfig
  155. ->expects($this->once())
  156. ->method('getValue')
  157. ->with(
  158. $this->pathToDefaultTimezone,
  159. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  160. )
  161. ->willReturn($this->defaultTimezone);
  162. $this->assertEquals($this->defaultTimezone, $this->block->getStoreLastLoginDateTimezone());
  163. }
  164. /**
  165. * @param string $status
  166. * @param string|null $lastLoginAt
  167. * @param string|null $lastVisitAt
  168. * @param string|null $lastLogoutAt
  169. * @return void
  170. * @dataProvider getCurrentStatusDataProvider
  171. */
  172. public function testGetCurrentStatus($status, $lastLoginAt, $lastVisitAt, $lastLogoutAt)
  173. {
  174. $this->scopeConfig->expects($this->any())
  175. ->method('getValue')
  176. ->with(
  177. 'customer/online_customers/online_minutes_interval',
  178. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  179. )
  180. ->willReturn(240); //TODO: it's value mocked because unit tests run data providers before all testsuite
  181. $this->customerLog->expects($this->any())->method('getLastLoginAt')->willReturn($lastLoginAt);
  182. $this->customerLog->expects($this->any())->method('getLastVisitAt')->willReturn($lastVisitAt);
  183. $this->customerLog->expects($this->any())->method('getLastLogoutAt')->willReturn($lastLogoutAt);
  184. $this->assertEquals($status, (string) $this->block->getCurrentStatus());
  185. }
  186. /**
  187. * @return array
  188. */
  189. public function getCurrentStatusDataProvider()
  190. {
  191. return [
  192. ['Offline', null, null, null],
  193. ['Offline', '2015-03-04 11:00:00', null, '2015-03-04 12:00:00'],
  194. ['Offline', '2015-03-04 11:00:00', '2015-03-04 11:40:00', null],
  195. ['Online', '2015-03-04 11:00:00', (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT), null]
  196. ];
  197. }
  198. /**
  199. * @param string $result
  200. * @param string|null $lastLoginAt
  201. * @dataProvider getLastLoginDateDataProvider
  202. * @return void
  203. */
  204. public function testGetLastLoginDate($result, $lastLoginAt)
  205. {
  206. $this->customerLog->expects($this->once())->method('getLastLoginAt')->willReturn($lastLoginAt);
  207. $this->localeDate->expects($this->any())->method('formatDateTime')->willReturn($lastLoginAt);
  208. $this->assertEquals($result, $this->block->getLastLoginDate());
  209. }
  210. /**
  211. * @return array
  212. */
  213. public function getLastLoginDateDataProvider()
  214. {
  215. return [
  216. ['2015-03-04 12:00:00', '2015-03-04 12:00:00'],
  217. ['Never', null]
  218. ];
  219. }
  220. /**
  221. * @param string $result
  222. * @param string|null $lastLoginAt
  223. * @dataProvider getStoreLastLoginDateDataProvider
  224. * @return void
  225. */
  226. public function testGetStoreLastLoginDate($result, $lastLoginAt)
  227. {
  228. $this->customerLog->expects($this->once())->method('getLastLoginAt')->willReturn($lastLoginAt);
  229. $this->localeDate->expects($this->any())->method('scopeDate')->will($this->returnValue($lastLoginAt));
  230. $this->localeDate->expects($this->any())->method('formatDateTime')->willReturn($lastLoginAt);
  231. $this->assertEquals($result, $this->block->getStoreLastLoginDate());
  232. }
  233. /**
  234. * @return array
  235. */
  236. public function getStoreLastLoginDateDataProvider()
  237. {
  238. return [
  239. ['2015-03-04 12:00:00', '2015-03-04 12:00:00'],
  240. ['Never', null]
  241. ];
  242. }
  243. /**
  244. * @param string $expectedResult
  245. * @param bool $value
  246. * @dataProvider getAccountLockDataProvider
  247. * @return void
  248. */
  249. public function testGetAccountLock($expectedResult, $value)
  250. {
  251. $this->customerRegistry->expects($this->once())->method('retrieve')->willReturn($this->customerModel);
  252. $this->customerModel->expects($this->once())->method('isCustomerLocked')->willReturn($value);
  253. $expectedResult = new \Magento\Framework\Phrase($expectedResult);
  254. $this->assertEquals($expectedResult, $this->block->getAccountLock());
  255. }
  256. /**
  257. * @return array
  258. */
  259. public function getAccountLockDataProvider()
  260. {
  261. return [
  262. ['result' => 'Locked', 'expectedValue' => true],
  263. ['result' => 'Unlocked', 'expectedValue' => false]
  264. ];
  265. }
  266. }