RegisterTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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\Form;
  7. use Magento\Customer\Block\Form\Register;
  8. use Magento\Customer\Model\AccountManagement;
  9. use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
  10. /**
  11. * Test class for \Magento\Customer\Block\Form\Register.
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class RegisterTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /** Constants used by the various unit tests */
  18. const POST_ACTION_URL = 'http://localhost/index.php/customer/account/createpost';
  19. const LOGIN_URL = 'http://localhost/index.php/customer/account/login';
  20. const COUNTRY_ID = 'US';
  21. const FORM_DATA = 'form_data';
  22. const REGION_ATTRIBUTE_VALUE = 'California';
  23. const REGION_ID_ATTRIBUTE_CODE = 'region_id';
  24. const REGION_ID_ATTRIBUTE_VALUE = '12';
  25. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Directory\Helper\Data */
  26. private $directoryHelperMock;
  27. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Config\ScopeConfigInterface */
  28. private $_scopeConfig;
  29. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Session */
  30. private $_customerSession;
  31. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Module\Manager */
  32. private $_moduleManager;
  33. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Url */
  34. private $_customerUrl;
  35. /** @var Register */
  36. private $_block;
  37. protected function setUp()
  38. {
  39. $this->_scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  40. $this->_moduleManager = $this->createMock(\Magento\Framework\Module\Manager::class);
  41. $this->directoryHelperMock = $this->createMock(\Magento\Directory\Helper\Data::class);
  42. $this->_customerUrl = $this->createMock(\Magento\Customer\Model\Url::class);
  43. $this->_customerSession = $this->createPartialMock(
  44. \Magento\Customer\Model\Session::class,
  45. ['getCustomerFormData']
  46. );
  47. $context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
  48. $context->expects($this->any())->method('getScopeConfig')->will($this->returnValue($this->_scopeConfig));
  49. $this->_block = new \Magento\Customer\Block\Form\Register(
  50. $context,
  51. $this->directoryHelperMock,
  52. $this->getMockForAbstractClass(\Magento\Framework\Json\EncoderInterface::class, [], '', false),
  53. $this->createMock(\Magento\Framework\App\Cache\Type\Config::class),
  54. $this->createMock(\Magento\Directory\Model\ResourceModel\Region\CollectionFactory::class),
  55. $this->createMock(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class),
  56. $this->_moduleManager,
  57. $this->_customerSession,
  58. $this->_customerUrl
  59. );
  60. }
  61. /**
  62. * @param string $path
  63. * @param mixed $configValue
  64. *
  65. * @dataProvider getConfigProvider
  66. */
  67. public function testGetConfig($path, $configValue)
  68. {
  69. $this->_scopeConfig->expects($this->once())->method('getValue')->will($this->returnValue($configValue));
  70. $this->assertEquals($configValue, $this->_block->getConfig($path));
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function getConfigProvider()
  76. {
  77. return [
  78. ['/path/to/config/value', 'config value'],
  79. ['/path/to/config/value/that/does/not/exist', null]
  80. ];
  81. }
  82. public function testGetPostActionUrl()
  83. {
  84. $this->_customerUrl->expects(
  85. $this->once()
  86. )->method(
  87. 'getRegisterPostUrl'
  88. )->will(
  89. $this->returnValue(self::POST_ACTION_URL)
  90. );
  91. $this->assertEquals(self::POST_ACTION_URL, $this->_block->getPostActionUrl());
  92. }
  93. /**
  94. * Tests the use case where 'back_url' has not been set on the block.
  95. */
  96. public function testGetBackUrlNullData()
  97. {
  98. $this->_customerUrl->expects(
  99. $this->once()
  100. )->method(
  101. 'getLoginUrl'
  102. )->will(
  103. $this->returnValue(self::LOGIN_URL)
  104. );
  105. $this->assertEquals(self::LOGIN_URL, $this->_block->getBackUrl());
  106. }
  107. /**
  108. * Tests the use case where 'back_url' has been set on the block.
  109. */
  110. public function testGetBackUrlNotNullData()
  111. {
  112. $this->_block->setData('back_url', self::LOGIN_URL);
  113. $this->assertEquals(self::LOGIN_URL, $this->_block->getBackUrl());
  114. }
  115. /**
  116. * Form data has been set on the block so Form\Register::getFormData() simply returns it.
  117. */
  118. public function testGetFormDataNotNullFormData()
  119. {
  120. $data = new \Magento\Framework\DataObject();
  121. $this->_block->setData(self::FORM_DATA, $data);
  122. $this->assertSame($data, $this->_block->getFormData());
  123. }
  124. /**
  125. * Form data has not been set on the block and there is no customer data in the customer session. So
  126. * we expect an empty \Magento\Framework\DataObject.
  127. */
  128. public function testGetFormDataNullFormData()
  129. {
  130. $data = new \Magento\Framework\DataObject();
  131. $this->_customerSession->expects($this->once())->method('getCustomerFormData')->will($this->returnValue(null));
  132. $this->assertEquals($data, $this->_block->getFormData());
  133. $this->assertEquals($data, $this->_block->getData(self::FORM_DATA));
  134. }
  135. /**
  136. * Form data has not been set on the block, but there is customer data from the customer session.
  137. * The customer data is something other than 'region_id' so that code path is skipped.
  138. */
  139. public function testGetFormDataNullFormDataCustomerFormData()
  140. {
  141. $data = new \Magento\Framework\DataObject();
  142. $data->setFirstname('John');
  143. $data->setCustomerData(1);
  144. $customerFormData = ['firstname' => 'John'];
  145. $this->_customerSession->expects(
  146. $this->once()
  147. )->method(
  148. 'getCustomerFormData'
  149. )->will(
  150. $this->returnValue($customerFormData)
  151. );
  152. $this->assertEquals($data, $this->_block->getFormData());
  153. $this->assertEquals($data, $this->_block->getData(self::FORM_DATA));
  154. }
  155. /**
  156. * Form data has not been set on the block, but there is customer data from the customer session.
  157. * The customer data is the 'region_id' so that code path is executed.
  158. */
  159. public function testGetFormDataCustomerFormDataRegionId()
  160. {
  161. $data = new \Magento\Framework\DataObject();
  162. $data->setRegionId(self::REGION_ID_ATTRIBUTE_VALUE);
  163. $data->setCustomerData(1);
  164. $data[self::REGION_ID_ATTRIBUTE_CODE] = (int)self::REGION_ID_ATTRIBUTE_VALUE;
  165. $customerFormData = [self::REGION_ID_ATTRIBUTE_CODE => self::REGION_ID_ATTRIBUTE_VALUE];
  166. $this->_customerSession->expects(
  167. $this->once()
  168. )->method(
  169. 'getCustomerFormData'
  170. )->will(
  171. $this->returnValue($customerFormData)
  172. );
  173. $formData = $this->_block->getFormData();
  174. $this->assertEquals($data, $formData);
  175. $this->assertTrue(isset($formData[self::REGION_ID_ATTRIBUTE_CODE]));
  176. $this->assertSame((int)self::REGION_ID_ATTRIBUTE_VALUE, $formData[self::REGION_ID_ATTRIBUTE_CODE]);
  177. }
  178. /**
  179. * Tests the Form\Register::getCountryId() use case where CountryId has been set on the form data
  180. * Object that has been set on the block.
  181. */
  182. public function testGetCountryIdFormData()
  183. {
  184. $formData = new \Magento\Framework\DataObject();
  185. $formData->setCountryId(self::COUNTRY_ID);
  186. $this->_block->setData(self::FORM_DATA, $formData);
  187. $this->assertEquals(self::COUNTRY_ID, $this->_block->getCountryId());
  188. }
  189. /**
  190. * Tests the default country use case of parent::getCountryId() where CountryId has not been set
  191. * and the 'country_id' attribute has also not been set.
  192. */
  193. public function testGetCountryIdParentNullData()
  194. {
  195. $this->directoryHelperMock->expects(
  196. $this->once()
  197. )->method(
  198. 'getDefaultCountry'
  199. )->will(
  200. $this->returnValue(self::COUNTRY_ID)
  201. );
  202. $this->assertEquals(self::COUNTRY_ID, $this->_block->getCountryId());
  203. }
  204. /**
  205. * Tests the parent::getCountryId() use case where CountryId has not been set and the 'country_id'
  206. * attribute code has been set on the block.
  207. */
  208. public function testGetCountryIdParentNotNullData()
  209. {
  210. $this->_block->setData('country_id', self::COUNTRY_ID);
  211. $this->assertEquals(self::COUNTRY_ID, $this->_block->getCountryId());
  212. }
  213. /**
  214. * Tests the first if conditional of Form\Register::getRegion(), which checks to see if Region has
  215. * been set on the form data Object that's set on the block.
  216. */
  217. public function testGetRegionByRegion()
  218. {
  219. $formData = new \Magento\Framework\DataObject();
  220. $formData->setRegion(self::REGION_ATTRIBUTE_VALUE);
  221. $this->_block->setData(self::FORM_DATA, $formData);
  222. $this->assertSame(self::REGION_ATTRIBUTE_VALUE, $this->_block->getRegion());
  223. }
  224. /**
  225. * Tests the second if conditional of Form\Register::getRegion(), which checks to see if RegionId
  226. * has been set on the form data Object that's set on the block.
  227. */
  228. public function testGetRegionByRegionId()
  229. {
  230. $formData = new \Magento\Framework\DataObject();
  231. $formData->setRegionId(self::REGION_ID_ATTRIBUTE_VALUE);
  232. $this->_block->setData(self::FORM_DATA, $formData);
  233. $this->assertSame(self::REGION_ID_ATTRIBUTE_VALUE, $this->_block->getRegion());
  234. }
  235. /**
  236. * Neither Region, nor RegionId have been set on the form data Object that's set on the block so a
  237. * null value is expected.
  238. */
  239. public function testGetRegionNull()
  240. {
  241. $formData = new \Magento\Framework\DataObject();
  242. $this->_block->setData(self::FORM_DATA, $formData);
  243. $this->assertNull($this->_block->getRegion());
  244. }
  245. /**
  246. * @param boolean $isNewsletterEnabled
  247. * @param string $isNewsletterActive
  248. * @param boolean $expectedValue
  249. *
  250. * @dataProvider isNewsletterEnabledProvider
  251. */
  252. public function testIsNewsletterEnabled($isNewsletterEnabled, $isNewsletterActive, $expectedValue)
  253. {
  254. $this->_moduleManager->expects(
  255. $this->once()
  256. )->method(
  257. 'isOutputEnabled'
  258. )->with(
  259. 'Magento_Newsletter'
  260. )->will(
  261. $this->returnValue($isNewsletterEnabled)
  262. );
  263. $this->_scopeConfig->expects(
  264. $this->any()
  265. )->method(
  266. 'getValue'
  267. )->with(
  268. PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE
  269. )->will(
  270. $this->returnValue($isNewsletterActive)
  271. );
  272. $this->assertEquals($expectedValue, $this->_block->isNewsletterEnabled());
  273. }
  274. /**
  275. * @return array
  276. */
  277. public function isNewsletterEnabledProvider()
  278. {
  279. return [[true, true, true], [true, false, false], [false, true, false], [false, false, false]];
  280. }
  281. /**
  282. * This test is designed to execute all code paths of Form\Register::getFormData() when testing the
  283. * Form\Register::restoreSessionData() method.
  284. */
  285. public function testRestoreSessionData()
  286. {
  287. $data = new \Magento\Framework\DataObject();
  288. $data->setRegionId(self::REGION_ID_ATTRIBUTE_VALUE);
  289. $data->setCustomerData(1);
  290. $data[self::REGION_ID_ATTRIBUTE_CODE] = (int)self::REGION_ID_ATTRIBUTE_VALUE;
  291. $customerFormData = [self::REGION_ID_ATTRIBUTE_CODE => self::REGION_ID_ATTRIBUTE_VALUE];
  292. $this->_customerSession->expects(
  293. $this->once()
  294. )->method(
  295. 'getCustomerFormData'
  296. )->will(
  297. $this->returnValue($customerFormData)
  298. );
  299. $form = $this->createMock(\Magento\Customer\Model\Metadata\Form::class);
  300. $request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class, [], '', false);
  301. $formData = $this->_block->getFormData();
  302. $form->expects(
  303. $this->once()
  304. )->method(
  305. 'prepareRequest'
  306. )->with(
  307. $formData->getData()
  308. )->will(
  309. $this->returnValue($request)
  310. );
  311. $form->expects(
  312. $this->once()
  313. )->method(
  314. 'extractData'
  315. )->with(
  316. $request,
  317. null,
  318. false
  319. )->will(
  320. $this->returnValue($customerFormData)
  321. );
  322. $form->expects($this->once())->method('restoreData')->will($this->returnValue($customerFormData));
  323. $block = $this->_block->restoreSessionData($form, null, false);
  324. $this->assertSame($this->_block, $block);
  325. $this->assertEquals($data, $block->getData(self::FORM_DATA));
  326. }
  327. /**
  328. * Test get minimum password length
  329. */
  330. public function testGetMinimumPasswordLength()
  331. {
  332. $this->_scopeConfig->expects(
  333. $this->once()
  334. )->method(
  335. 'getValue'
  336. )->with(
  337. AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH
  338. )->will(
  339. $this->returnValue(6)
  340. );
  341. $this->assertEquals(6, $this->_block->getMinimumPasswordLength());
  342. }
  343. /**
  344. * Test get required character classes number
  345. */
  346. public function testGetRequiredCharacterClassesNumber()
  347. {
  348. $this->_scopeConfig->expects(
  349. $this->once()
  350. )->method(
  351. 'getValue'
  352. )->with(
  353. AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER
  354. )->will(
  355. $this->returnValue(3)
  356. );
  357. $this->assertEquals(3, $this->_block->getRequiredCharacterClassesNumber());
  358. }
  359. }