Skip to content

Commit 4f8f9f2

Browse files
test coverage
1 parent bc52285 commit 4f8f9f2

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Test\Unit\Model\ForgotPasswordToken;
9+
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\ForgotPasswordToken\GetCustomerByToken;
12+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
13+
use Magento\Customer\Model\ResourceModel\Customer;
14+
use Magento\Customer\Model\ForgotPasswordToken\ConfirmCustomerByToken;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Test for \Magento\Customer\Model\ForgotPasswordToken\ConfirmCustomerByToken.
20+
*/
21+
class ConfirmCustomerByTokenTest extends TestCase
22+
{
23+
private const STUB_RESET_PASSWORD_TOKEN = 'resetPassword';
24+
25+
/**
26+
* @var ConfirmCustomerByToken;
27+
*/
28+
private $model;
29+
30+
/**
31+
* @var CustomerInterface|MockObject
32+
*/
33+
private $customerMock;
34+
35+
/**
36+
* @var CustomerResource|MockObject
37+
*/
38+
private $customerResourceMock;
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
protected function setUp(): void
44+
{
45+
$this->customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
46+
$this->customerResourceMock = $this->createMock(CustomerResource::class);
47+
48+
$getCustomerByTokenMock = $this->createMock(GetCustomerByToken::class);
49+
$getCustomerByTokenMock->method('execute')->willReturn($this->customerMock);
50+
51+
$this->model = new ConfirmCustomerByToken($getCustomerByTokenMock, $this->customerResourceMock);
52+
}
53+
54+
/**
55+
* Confirm customer with confirmation
56+
*
57+
* @return void
58+
*/
59+
public function testExecuteWithConfirmation(): void
60+
{
61+
$customerId = 777;
62+
63+
$this->customerMock->expects($this->once())
64+
->method('getConfirmation')
65+
->willReturn('GWz2ik7Kts517MXAgrm4DzfcxKayGCm4');
66+
$this->customerMock->expects($this->once())
67+
->method('getId')
68+
->willReturn($customerId);
69+
$this->customerResourceMock->expects($this->once())
70+
->method('updateColumn')
71+
->with($customerId, 'confirmation', null);
72+
73+
$this->model->execute(self::STUB_RESET_PASSWORD_TOKEN);
74+
}
75+
76+
/**
77+
* Confirm customer without confirmation
78+
*
79+
* @return void
80+
*/
81+
public function testExecuteWithoutConfirmation(): void
82+
{
83+
$this->customerMock->expects($this->once())
84+
->method('getConfirmation')
85+
->willReturn(null);
86+
$this->customerResourceMock->expects($this->never())
87+
->method('updateColumn');
88+
89+
$this->model->execute(self::STUB_RESET_PASSWORD_TOKEN);
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\ForgotPasswordToken;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Test for \Magento\Customer\Model\ForgotPasswordToken\ConfirmCustomerByToken.
19+
*/
20+
class ConfirmCustomerByTokenTest extends TestCase
21+
{
22+
private const STUB_CUSTOMER_RESET_TOKEN = 'token12345';
23+
24+
/**
25+
* @var ObjectManagerInterface
26+
*/
27+
private $objectManager;
28+
29+
/**
30+
* @var ConfirmCustomerByToken
31+
*/
32+
private $confirmCustomerByToken;
33+
34+
/**
35+
* @var AdapterInterface
36+
*/
37+
private $connection;
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
protected function setUp(): void
43+
{
44+
$this->objectManager = Bootstrap::getObjectManager();
45+
46+
$resource = $this->objectManager->get(ResourceConnection::class);
47+
$this->connection = $resource->getConnection();
48+
49+
$this->confirmCustomerByToken = $this->objectManager->get(ConfirmCustomerByToken::class);
50+
}
51+
52+
/**
53+
* Customer address shouldn't validate during confirm customer by token
54+
*
55+
* @magentoDataFixture Magento/Customer/_files/customer.php
56+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
57+
*
58+
* @return void
59+
*/
60+
public function testExecuteWithInvalidAddress(): void
61+
{
62+
$id = 1;
63+
64+
$customerModel = $this->objectManager->create(Customer::class);
65+
$customerModel->load($id);
66+
$customerModel->setRpToken(self::STUB_CUSTOMER_RESET_TOKEN);
67+
$customerModel->setRpTokenCreatedAt(date('Y-m-d H:i:s'));
68+
$customerModel->setConfirmation($customerModel->getRandomConfirmationKey());
69+
$customerModel->save();
70+
71+
//make city address invalid
72+
$this->makeCityInvalid($id);
73+
74+
$this->confirmCustomerByToken->execute(self::STUB_CUSTOMER_RESET_TOKEN);
75+
$this->assertNull($customerModel->load($id)->getConfirmation());
76+
}
77+
78+
/**
79+
* Set city invalid for customer address
80+
*
81+
* @param int $id
82+
* @return void
83+
*/
84+
private function makeCityInvalid(int $id): void
85+
{
86+
$this->connection->update(
87+
$this->connection->getTableName('customer_address_entity'),
88+
['city' => ''],
89+
$this->connection->quoteInto('entity_id = ?', $id)
90+
);
91+
}
92+
}

0 commit comments

Comments
 (0)