From 57a7deedfe15649d2d8969a077ef8bce035d0f7d Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Wed, 27 Dec 2017 12:37:46 +0100 Subject: [PATCH 1/2] Improvement: Magento\Sales\Helper\Guest refactoring and bugfix --- app/code/Magento/Sales/Helper/Guest.php | 47 +++++++++---------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/app/code/Magento/Sales/Helper/Guest.php b/app/code/Magento/Sales/Helper/Guest.php index dd8845008d79e..8aed2a7000956 100644 --- a/app/code/Magento/Sales/Helper/Guest.php +++ b/app/code/Magento/Sales/Helper/Guest.php @@ -83,7 +83,7 @@ class Guest extends \Magento\Framework\App\Helper\AbstractHelper /** * @var \Magento\Store\Model\StoreManagerInterface */ - private $_storeManager; + private $storeManager; /** * @var string @@ -119,7 +119,7 @@ public function __construct( \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteria = null ) { $this->coreRegistry = $coreRegistry; - $this->_storeManager = $storeManager; + $this->storeManager = $storeManager; $this->customerSession = $customerSession; $this->cookieManager = $cookieManager; $this->cookieMetadataFactory = $cookieMetadataFactory; @@ -158,9 +158,10 @@ public function loadValidOrder(App\RequestInterface $request) // It is unique place in the class that process exception and only InputException. It is need because by // input data we found order and one more InputException could be throws deeper in stack trace try { - $order = (!empty($post) && isset($post['oar_order_id'], $post['oar_type'])) + $order = (!empty($post) + && isset($post['oar_order_id'], $post['oar_type']) + && !$this->hasPostDataEmptyFields($post)) ? $this->loadFromPost($post) : $this->loadFromCookie($fromCookie); - $this->validateOrderStoreId($order->getStoreId()); $this->coreRegistry->register('current_order', $order); return true; } catch (InputException $e) { @@ -186,7 +187,7 @@ public function getBreadcrumbs(\Magento\Framework\View\Result\Page $resultPage) [ 'label' => __('Home'), 'title' => __('Go to Home Page'), - 'link' => $this->_storeManager->getStore()->getBaseUrl() + 'link' => $this->storeManager->getStore()->getBaseUrl() ] ); $breadcrumbs->addCrumb( @@ -216,7 +217,7 @@ private function setGuestViewCookie($cookieValue) * Load order from cookie * * @param string $fromCookie - * @return Order + * @return \Magento\Sales\Model\Order\Interceptor * @throws InputException * @throws CookieSizeLimitReachedException * @throws FailureToSendException @@ -240,19 +241,16 @@ private function loadFromCookie($fromCookie) * Load order data from post * * @param array $postData - * @return Order + * @return \Magento\Sales\Model\Order\Interceptor * @throws InputException * @throws CookieSizeLimitReachedException * @throws FailureToSendException */ private function loadFromPost(array $postData) { - if ($this->hasPostDataEmptyFields($postData)) { - throw new InputException(); - } /** @var $order \Magento\Sales\Model\Order */ $order = $this->getOrderRecord($postData['oar_order_id']); - if (!$this->compareSoredBillingDataWithInput($order, $postData)) { + if (!$this->compareStoredBillingDataWithInput($order, $postData)) { throw new InputException(__('You entered incorrect data. Please try again.')); } $toCookie = base64_encode($order->getProtectCode() . ':' . $postData['oar_order_id']); @@ -267,7 +265,7 @@ private function loadFromPost(array $postData) * @param array $postData * @return bool */ - private function compareSoredBillingDataWithInput(Order $order, array $postData) + private function compareStoredBillingDataWithInput(Order $order, array $postData) { $type = $postData['oar_type']; $email = $postData['oar_email']; @@ -288,7 +286,7 @@ private function compareSoredBillingDataWithInput(Order $order, array $postData) private function hasPostDataEmptyFields(array $postData) { return empty($postData['oar_order_id']) || empty($postData['oar_billing_lastname']) || - empty($postData['oar_type']) || empty($this->_storeManager->getStore()->getId()) || + empty($postData['oar_type']) || empty($this->storeManager->getStore()->getId()) || !in_array($postData['oar_type'], ['email', 'zip'], true) || ('email' === $postData['oar_type'] && empty($postData['oar_email'])) || ('zip' === $postData['oar_type'] && empty($postData['oar_zip'])); @@ -298,7 +296,7 @@ private function hasPostDataEmptyFields(array $postData) * Get order by increment_id and store_id * * @param string $incrementId - * @return \Magento\Sales\Api\Data\OrderInterface + * @return array * @throws InputException */ private function getOrderRecord($incrementId) @@ -306,26 +304,15 @@ private function getOrderRecord($incrementId) $records = $this->orderRepository->getList( $this->searchCriteriaBuilder ->addFilter('increment_id', $incrementId) + ->addFilter('store_id', $this->storeManager->getStore()->getId()) ->create() ); - if ($records->getTotalCount() < 1) { - throw new InputException(__($this->inputExceptionMessage)); - } - $items = $records->getItems(); - return array_shift($items); - } - /** - * Check that store_id from order are equals with system - * - * @param int $orderStoreId - * @return void - * @throws InputException - */ - private function validateOrderStoreId($orderStoreId) - { - if ($orderStoreId != $this->_storeManager->getStore()->getId()) { + $items = $records->getItems(); + if (empty($items)) { throw new InputException(__($this->inputExceptionMessage)); } + + return array_shift($items); } } From 82327326130e743e4709438b0a49e9cc313f5f39 Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Wed, 27 Dec 2017 15:07:20 +0100 Subject: [PATCH 2/2] Fix: change return types to previous state because test was not passed with interceptor type --- app/code/Magento/Sales/Helper/Guest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Sales/Helper/Guest.php b/app/code/Magento/Sales/Helper/Guest.php index 8aed2a7000956..8407ce5a8d7cb 100644 --- a/app/code/Magento/Sales/Helper/Guest.php +++ b/app/code/Magento/Sales/Helper/Guest.php @@ -217,7 +217,7 @@ private function setGuestViewCookie($cookieValue) * Load order from cookie * * @param string $fromCookie - * @return \Magento\Sales\Model\Order\Interceptor + * @return Order * @throws InputException * @throws CookieSizeLimitReachedException * @throws FailureToSendException @@ -241,7 +241,7 @@ private function loadFromCookie($fromCookie) * Load order data from post * * @param array $postData - * @return \Magento\Sales\Model\Order\Interceptor + * @return Order * @throws InputException * @throws CookieSizeLimitReachedException * @throws FailureToSendException @@ -296,7 +296,7 @@ private function hasPostDataEmptyFields(array $postData) * Get order by increment_id and store_id * * @param string $incrementId - * @return array + * @return \Magento\Sales\Api\Data\OrderInterface * @throws InputException */ private function getOrderRecord($incrementId)