Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerTest.php
Go to the documentation of this file.
1 <?php
13 
16 
20 class CustomerTest extends \PHPUnit\Framework\TestCase
21 {
23  protected $_model;
24 
26  protected $_website;
27 
29  protected $_storeManager;
30 
32  protected $_config;
33 
35  protected $_attribute;
36 
38  protected $_scopeConfigMock;
39 
42 
44  protected $_transportMock;
45 
47  protected $_encryptor;
48 
51 
54 
56  protected $registryMock;
57 
59  protected $resourceMock;
60 
64  private $dataObjectProcessor;
65 
69  private $accountConfirmation;
70 
71  protected function setUp()
72  {
73  $this->_website = $this->createMock(\Magento\Store\Model\Website::class);
74  $this->_config = $this->createMock(\Magento\Eav\Model\Config::class);
75  $this->_attribute = $this->createMock(\Magento\Eav\Model\Attribute::class);
76  $this->_storeManager = $this->createMock(\Magento\Store\Model\StoreManager::class);
77  $this->_storetMock = $this->createMock(\Magento\Store\Model\Store::class);
78  $this->_scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
79  $this->_transportBuilderMock = $this->createMock(\Magento\Framework\Mail\Template\TransportBuilder::class);
80  $this->_transportMock = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
81  $this->attributeFactoryMock = $this->createPartialMock(
82  \Magento\Customer\Model\AttributeFactory::class,
83  ['create']
84  );
85  $this->attributeCustomerMock = $this->createMock(\Magento\Customer\Model\Attribute::class);
86  $this->resourceMock = $this->createPartialMock(
87  \Magento\Customer\Model\ResourceModel\Customer::class, // \Magento\Framework\DataObject::class,
88  ['getIdFieldName']
89  );
90 
91  $this->dataObjectProcessor = $this->createPartialMock(
92  \Magento\Framework\Reflection\DataObjectProcessor::class,
93  ['buildOutputDataArray']
94  );
95 
96  $this->resourceMock->expects($this->any())
97  ->method('getIdFieldName')
98  ->will($this->returnValue('id'));
99  $this->registryMock = $this->createPartialMock(\Magento\Framework\Registry::class, ['registry']);
100  $this->_encryptor = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
101  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
102  $this->accountConfirmation = $this->createMock(AccountConfirmation::class);
103  $this->_model = $helper->getObject(
104  \Magento\Customer\Model\Customer::class,
105  [
106  'storeManager' => $this->_storeManager,
107  'config' => $this->_config,
108  'transportBuilder' => $this->_transportBuilderMock,
109  'scopeConfig' => $this->_scopeConfigMock,
110  'encryptor' => $this->_encryptor,
111  'attributeFactory' => $this->attributeFactoryMock,
112  'registry' => $this->registryMock,
113  'resource' => $this->resourceMock,
114  'dataObjectProcessor' => $this->dataObjectProcessor,
115  'accountConfirmation' => $this->accountConfirmation
116  ]
117  );
118  }
119 
120  public function testHashPassword()
121  {
122  $this->_encryptor->expects(
123  $this->once()
124  )->method(
125  'getHash'
126  )->with(
127  'password',
128  'salt'
129  )->will(
130  $this->returnValue('hash')
131  );
132  $this->assertEquals('hash', $this->_model->hashPassword('password', 'salt'));
133  }
134 
140  {
141  $this->_model->sendNewAccountEmail('test');
142  }
143 
145  {
146  $store = $this->createMock(\Magento\Store\Model\Store::class);
147  $website = $this->createMock(\Magento\Store\Model\Website::class);
148  $website->expects($this->once())
149  ->method('getStoreIds')
150  ->will($this->returnValue([1, 2, 3, 4]));
151  $this->_storeManager->expects($this->once())
152  ->method('getWebsite')
153  ->with(1)
154  ->will($this->returnValue($website));
155  $this->_storeManager->expects($this->once())
156  ->method('getStore')
157  ->with(1)
158  ->will($this->returnValue($store));
159 
160  $this->_config->expects($this->exactly(3))
161  ->method('getAttribute')
162  ->will($this->returnValue($this->_attribute));
163 
164  $this->_attribute->expects($this->exactly(3))
165  ->method('getIsVisible')
166  ->will($this->returnValue(true));
167 
168  $methods = [
169  'setTemplateIdentifier',
170  'setTemplateOptions',
171  'setTemplateVars',
172  'setFrom',
173  'addTo',
174  ];
175  foreach ($methods as $method) {
176  $this->_transportBuilderMock->expects($this->once())
177  ->method($method)
178  ->will($this->returnSelf());
179  }
180  $transportMock = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
181  $transportMock->expects($this->once())
182  ->method('sendMessage')
183  ->will($this->returnSelf());
184  $this->_transportBuilderMock->expects($this->once())
185  ->method('getTransport')
186  ->will($this->returnValue($transportMock));
187 
188  $this->_model->setData([
189  'website_id' => 1,
190  'store_id' => 1,
191  'email' => '[email protected]',
192  'firstname' => 'FirstName',
193  'lastname' => 'LastName',
194  'middlename' => 'MiddleName',
195  'prefix' => 'Name Prefix',
196  ]);
197  $this->_model->sendNewAccountEmail('registered');
198  }
199 
205  public function testIsCustomerLocked($lockExpires, $expectedResult)
206  {
207  $this->_model->setLockExpires($lockExpires);
208  $this->assertEquals($expectedResult, $this->_model->isCustomerLocked());
209  }
210 
215  {
216  return [
217  ['lockExpirationDate' => date("F j, Y", strtotime('-1 days')), 'expectedResult' => false],
218  ['lockExpirationDate' => date("F j, Y", strtotime('+1 days')), 'expectedResult' => true]
219  ];
220  }
221 
229  public function testIsConfirmationRequired(
230  $customerId,
231  $websiteId,
232  $isConfirmationRequired,
233  $expected
234  ) {
236 
237  $this->_model->setData('id', $customerId);
238  $this->_model->setData('website_id', $websiteId);
239  $this->_model->setData('email', $customerEmail);
240 
241  $this->accountConfirmation->expects($this->once())
242  ->method('isConfirmationRequired')
244  ->willReturn($isConfirmationRequired);
245 
246  $this->assertEquals($expected, $this->_model->isConfirmationRequired());
247  }
248 
253  {
254  return [
255  [null, null, false, false],
256  [1, 1, true, true],
257  [1, null, true, true],
258  ];
259  }
260 
261  public function testUpdateData()
262  {
263  $customerDataAttributes = [
264  'attribute' => 'attribute',
265  'test1' => 'test1',
266  'test33' => 'test33',
267  ];
268 
269  $customer = $this->createPartialMock(
270  \Magento\Customer\Model\Data\Customer::class,
271  [
272  'getCustomAttributes',
273  'getId',
274  ]
275  );
276 
277  $attribute = $this->createPartialMock(
278  \Magento\Framework\Api\AttributeValue::class,
279  [
280  'getAttributeCode',
281  'getValue',
282  ]
283  );
284 
285  $this->dataObjectProcessor->expects($this->once())
286  ->method('buildOutputDataArray')
287  ->withConsecutive(
288  [$customer, \Magento\Customer\Api\Data\CustomerInterface::class]
289  )->willReturn($customerDataAttributes);
290 
291  $attribute->expects($this->exactly(3))
292  ->method('getAttributeCode')
293  ->willReturn('test33');
294 
295  $attribute->expects($this->exactly(2))
296  ->method('getValue')
297  ->willReturn('test33');
298 
299  $customer->expects($this->once())
300  ->method('getCustomAttributes')
301  ->willReturn([$attribute->getAttributeCode() => $attribute]);
302 
303  $this->_model->updateData($customer);
304 
305  foreach ($customerDataAttributes as $key => $value) {
306  $expectedResult[strtolower(trim(preg_replace('/([A-Z]|[0-9]+)/', "_$1", $key), '_'))] = $value;
307  }
308 
309  $expectedResult[$attribute->getAttributeCode()] = $attribute->getValue();
310  $expectedResult['attribute_set_id'] =
312 
313  $this->assertEquals($this->_model->getData(), $expectedResult);
314  }
315 }
$helper
Definition: iframe.phtml:13
$customer
Definition: customers.php:11
$methods
Definition: billing.phtml:71
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
testIsCustomerLocked($lockExpires, $expectedResult)
testIsConfirmationRequired( $customerId, $websiteId, $isConfirmationRequired, $expected)