Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GuestTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class GuestTest extends \PHPUnit\Framework\TestCase
15 {
17  protected $guest;
18 
20  protected $sessionMock;
21 
23  protected $cookieManagerMock;
24 
27 
30 
32  protected $orderFactoryMock;
33 
35  protected $viewInterfaceMock;
36 
38  protected $storeModelMock;
39 
41  protected $salesOrderMock;
42 
46  private $orderRepository;
47 
51  private $searchCriteriaBuilder;
52 
53  protected function setUp()
54  {
55  $appContextHelperMock = $this->createMock(\Magento\Framework\App\Helper\Context::class);
56  $storeManagerInterfaceMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
57  $registryMock = $this->createMock(\Magento\Framework\Registry::class);
58  $this->sessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
59  $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
60  $this->cookieMetadataFactoryMock = $this->createMock(
61  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
62  );
63  $this->managerInterfaceMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
64  $this->orderFactoryMock = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
65  $this->viewInterfaceMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
66  $this->storeModelMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->salesOrderMock = $this->createPartialMock(
70  \Magento\Sales\Model\Order::class,
71  [
72  'getProtectCode',
73  'loadByIncrementIdAndStoreId',
74  'loadByIncrementId',
75  'getId',
76  'getStoreId',
77  'getBillingAddress',
78  '__wakeup'
79  ]
80  );
81  $this->orderRepository = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
82  ->setMethods(['getList'])
83  ->disableOriginalConstructor()
84  ->getMockForAbstractClass();
85  $this->searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
86  ->setMethods(['addFilter', 'create'])
87  ->disableOriginalConstructor()
88  ->getMockForAbstractClass();
89  $orderSearchResult = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderSearchResultInterface::class)
90  ->setMethods(['getTotalCount', 'getItems'])
91  ->disableOriginalConstructor()
92  ->getMockForAbstractClass();
93  $this->searchCriteriaBuilder->method('addFilter')->willReturnSelf();
94  $resultRedirectFactory =
95  $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
96  ->setMethods(['create'])
97  ->disableOriginalConstructor()
98  ->getMock();
99  $this->orderRepository->method('getList')->willReturn($orderSearchResult);
100  $orderSearchResult->method('getTotalCount')->willReturn(1);
101  $orderSearchResult->method('getItems')->willReturn([ 2 => $this->salesOrderMock]);
102  $searchCriteria = $this
103  ->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
104  ->getMockForAbstractClass();
105  $storeManagerInterfaceMock->expects($this->any())->method('getStore')->willReturn($this->storeModelMock);
106  $this->searchCriteriaBuilder->method('create')->willReturn($searchCriteria);
107  $this->storeModelMock->method('getId')->willReturn(1);
108  $this->salesOrderMock->expects($this->any())->method('getStoreId')->willReturn(1);
109  $objectManagerHelper = new ObjectManagerHelper($this);
110  $this->guest = $objectManagerHelper->getObject(
111  \Magento\Sales\Helper\Guest::class,
112  [
113  'context' => $appContextHelperMock,
114  'storeManager' => $storeManagerInterfaceMock,
115  'coreRegistry' => $registryMock,
116  'customerSession' => $this->sessionMock,
117  'cookieManager' => $this->cookieManagerMock,
118  'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
119  'messageManager' => $this->managerInterfaceMock,
120  'orderFactory' => $this->orderFactoryMock,
121  'view' => $this->viewInterfaceMock,
122  'orderRepository' => $this->orderRepository,
123  'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
124  'resultRedirectFactory' => $resultRedirectFactory
125  ]
126  );
127  }
128 
130  {
131  $post = [
132  'oar_order_id' => 1,
133  'oar_type' => 'email',
134  'oar_billing_lastname' => 'oar_billing_lastname',
135  'oar_email' => 'oar_email',
136  'oar_zip' => 'oar_zip',
137 
138  ];
139  $incrementId = $post['oar_order_id'];
140  $protectedCode = 'protectedCode';
141  $this->sessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
142  $requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
143  $requestMock->expects($this->once())->method('getPostValue')->willReturn($post);
144  $this->salesOrderMock->expects($this->any())->method('getId')->willReturn($incrementId);
145 
146  $billingAddressMock = $this->createPartialMock(
147  \Magento\Sales\Model\Order\Address::class,
148  ['getLastname', 'getEmail', '__wakeup']
149  );
150  $billingAddressMock->expects($this->once())->method('getLastname')->willReturn(($post['oar_billing_lastname']));
151  $billingAddressMock->expects($this->once())->method('getEmail')->willReturn(($post['oar_email']));
152  $this->salesOrderMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock);
153  $this->salesOrderMock->expects($this->once())->method('getProtectCode')->willReturn($protectedCode);
154  $metaDataMock = $this->createMock(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class);
155  $metaDataMock->expects($this->once())->method('setPath')
156  ->with(Guest::COOKIE_PATH)
157  ->willReturnSelf();
158  $metaDataMock->expects($this->once())
159  ->method('setHttpOnly')
160  ->with(true)
161  ->willReturnSelf();
162  $this->cookieMetadataFactoryMock->expects($this->once())
163  ->method('createPublicCookieMetadata')
164  ->willReturn($metaDataMock);
165  $this->cookieManagerMock->expects($this->once())
166  ->method('setPublicCookie')
167  ->with(Guest::COOKIE_NAME, $this->anything(), $metaDataMock);
168  $this->assertTrue($this->guest->loadValidOrder($requestMock));
169  }
170 
172  {
173  $protectedCode = 'protectedCode';
174  $incrementId = 1;
175  $cookieData = $protectedCode . ':' . $incrementId;
176  $cookieDataHash = base64_encode($cookieData);
177  $this->sessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
178  $this->cookieManagerMock->expects($this->once())
179  ->method('getCookie')
180  ->with(Guest::COOKIE_NAME)
181  ->willReturn($cookieDataHash);
182  $this->salesOrderMock->expects($this->any())->method('getId')->willReturn($incrementId);
183  $this->salesOrderMock->expects($this->once())->method('getProtectCode')->willReturn($protectedCode);
184  $metaDataMock = $this->createMock(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class);
185  $metaDataMock->expects($this->once())
186  ->method('setPath')
187  ->with(Guest::COOKIE_PATH)
188  ->willReturnSelf();
189  $metaDataMock->expects($this->once())
190  ->method('setHttpOnly')
191  ->with(true)
192  ->willReturnSelf();
193  $this->cookieMetadataFactoryMock->expects($this->once())
194  ->method('createPublicCookieMetadata')
195  ->willReturn($metaDataMock);
196  $this->cookieManagerMock->expects($this->once())
197  ->method('setPublicCookie')
198  ->with(Guest::COOKIE_NAME, $this->anything(), $metaDataMock);
199  $requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
200  $this->assertTrue($this->guest->loadValidOrder($requestMock));
201  }
202 }
$searchCriteria