public function testGetFormKeyExists() { $this->sessionMock->expects($this->exactly(2))->method('getData')->with(FormKey::FORM_KEY)->will($this->returnValue('random_string')); $this->mathRandomMock->expects($this->never())->method('getRandomString'); $this->sessionMock->expects($this->never())->method('setData'); $this->assertEquals('random_string', $this->formKey->getFormKey()); }
/** * Register form key in session from cookie value * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->cookieFormKey->get()) { $this->updateCookieFormKey($this->cookieFormKey->get()); $this->sessionFormKey->set($this->escaper->escapeHtml($this->cookieFormKey->get())); } }
/** * Validate form key * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function validate(\Magento\Framework\App\RequestInterface $request) { $formKey = $request->getParam('form_key', null); if (!$formKey || $formKey !== $this->_formKey->getFormKey()) { return false; } return true; }
/** * TODO * * @return \Magento\Framework\View\Result\PageFactory */ public function execute() { $formKey = $this->formKey->getFormKey(); try { $cronjobs = $this->cronData->getCronjobData(); $result = ['cronjobs' => $cronjobs, 'form_key' => $formKey]; echo json_encode($result, true); } catch (\Exception $e) { $result = ['cronjobs' => [], 'form_key' => $formKey]; echo json_encode($result, true); } }
/** * Generate secret key for controller and action based on form key * * @param string $routeName * @param string $controller Controller name * @param string $action Action name * @return string */ public function getSecretKey($routeName = null, $controller = null, $action = null) { $salt = $this->formKey->getFormKey(); $request = $this->_getRequest(); if (!$routeName) { if ($request->getBeforeForwardInfo('route_name') !== null) { $routeName = $request->getBeforeForwardInfo('route_name'); } else { $routeName = $request->getRouteName(); } } if (!$controller) { if ($request->getBeforeForwardInfo('controller_name') !== null) { $controller = $request->getBeforeForwardInfo('controller_name'); } else { $controller = $request->getControllerName(); } } if (!$action) { if ($request->getBeforeForwardInfo('action_name') !== null) { $action = $request->getBeforeForwardInfo('action_name'); } else { $action = $request->getActionName(); } } $secret = $routeName . $controller . $action . $salt; return $this->_encryptor->getHash($secret); }
/** * @magentoDataFixture Magento/Customer/_files/customer_sample.php */ public function testNotExistingCustomerDeleteAction() { $this->getRequest()->setParam('id', 2); $this->getRequest()->setParam('form_key', $this->formKey->getFormKey()); $this->getRequest()->setMethod(\Zend\Http\Request::METHOD_POST); $this->dispatch('backend/customer/index/delete'); $this->assertRedirect($this->stringContains('customer/index')); $this->assertSessionMessages($this->equalTo(['No such entity with customerId = 2']), \Magento\Framework\Message\MessageInterface::TYPE_ERROR); }
/** * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture Magento/Customer/_files/customer_address.php */ public function testWrongAddressDeleteAction() { $this->getRequest()->setParam('id', 555); $this->getRequest()->setParam('form_key', $this->formKey->getFormKey()); // we are overwriting the address coming from the fixture $this->dispatch('customer/address/delete'); $this->assertRedirect($this->stringContains('customer/address/index')); $this->assertSessionMessages($this->equalTo(['We can\'t delete the address right now.']), \Magento\Framework\Message\MessageInterface::TYPE_ERROR); }
public function testExecute() { $formKey = 'form_key'; $escapedFormKey = 'escaped_form_key'; $cookieDomain = 'example.com'; $cookiePath = '/'; $cookieLifetime = 3600; $cookieMetadata = $this->getMockBuilder('Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata')->disableOriginalConstructor()->getMock(); $this->cookieFormKey->expects(static::any())->method('get')->willReturn($formKey); $this->cookieMetadataFactory->expects(static::once())->method('createPublicCookieMetadata')->willReturn($cookieMetadata); $this->sessionConfig->expects(static::once())->method('getCookieDomain')->willReturn($cookieDomain); $cookieMetadata->expects(static::once())->method('setDomain')->with($cookieDomain); $this->sessionConfig->expects(static::once())->method('getCookiePath')->willReturn($cookiePath); $cookieMetadata->expects(static::once())->method('setPath')->with($cookiePath); $this->sessionConfig->expects(static::once())->method('getCookieLifetime')->willReturn($cookieLifetime); $cookieMetadata->expects(static::once())->method('setDuration')->with($cookieLifetime); $this->cookieFormKey->expects(static::once())->method('set')->with($formKey, $cookieMetadata); $this->escaper->expects(static::once())->method('escapeHtml')->with($formKey)->willReturn($escapedFormKey); $this->sessionFormKey->expects(static::once())->method('set')->with($escapedFormKey); $this->observer->execute($this->observerMock); }
/** * @return string */ public function toHtml() { Profiler::start('form/toHtml'); $html = ''; $useContainer = $this->getUseContainer(); if ($useContainer) { $html .= '<form ' . $this->serialize($this->getHtmlAttributes()) . '>'; $html .= '<div>'; if (strtolower($this->getData('method')) == 'post') { $html .= '<input name="form_key" type="hidden" value="' . $this->formKey->getFormKey() . '" />'; } $html .= '</div>'; } foreach ($this->getElements() as $element) { $html .= $element->toHtml(); } if ($useContainer) { $html .= '</form>'; } Profiler::stop('form/toHtml'); return $html; }
/** * {@inheritdoc} */ public function getConfig() { $configuration = []; $configuration['giftMessage'] = []; $orderLevelGiftMessageConfiguration = (bool) $this->scopeConfiguration->getValue(GiftMessageHelper::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $itemLevelGiftMessageConfiguration = (bool) $this->scopeConfiguration->getValue(GiftMessageHelper::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if ($orderLevelGiftMessageConfiguration) { $orderMessages = $this->getOrderLevelGiftMessages(); $configuration['isOrderLevelGiftOptionsEnabled'] = (bool) $this->isQuoteVirtual() ? false : true; $configuration['giftMessage']['orderLevel'] = $orderMessages === null ? true : $orderMessages->getData(); } $itemMessages = $this->getItemLevelGiftMessages(); $configuration['isItemLevelGiftOptionsEnabled'] = $itemLevelGiftMessageConfiguration; $configuration['giftMessage']['itemLevel'] = $itemMessages === null ? true : $itemMessages; $configuration['priceFormat'] = $this->localeFormat->getPriceFormat(null, $this->checkoutSession->getQuote()->getQuoteCurrencyCode()); $configuration['storeCode'] = $this->getStoreCode(); $configuration['isCustomerLoggedIn'] = $this->isCustomerLoggedIn(); $configuration['formKey'] = $this->formKey->getFormKey(); $store = $this->storeManager->getStore(); $configuration['baseUrl'] = $store->isFrontUrlSecure() ? $store->getBaseUrl(UrlInterface::URL_TYPE_LINK, true) : $store->getBaseUrl(UrlInterface::URL_TYPE_LINK, false); return $configuration; }
/** * Retrieve form key * * @return string * @codeCoverageIgnore */ public function getFormKey() { return $this->formKey->getFormKey(); }
/** * Retrieve configuration options for tax class editable multiselect * * @param string $classType * @return array */ public function getTaxClassSelectConfig($classType) { $config = array('new_url' => $this->getUrl('tax/tax/ajaxSave/'), 'save_url' => $this->getUrl('tax/tax/ajaxSave/'), 'delete_url' => $this->getUrl('tax/tax/ajaxDelete/'), 'delete_confirm_message' => __('Do you really want to delete this tax class?'), 'target_select_id' => 'tax_' . strtolower($classType) . '_class', 'add_button_caption' => __('Add New Tax Class'), 'submit_data' => array('class_type' => $classType, 'form_key' => $this->formKey->getFormKey()), 'entity_id_name' => 'class_id', 'entity_value_name' => 'class_name', 'is_entity_editable' => true); return $config; }
/** * {@inheritdoc} */ public function getConfig() { return ['formKey' => $this->formKey->getFormKey(), 'customerData' => $this->getCustomerData(), 'quoteData' => $this->getQuoteData(), 'quoteItemData' => $this->getQuoteItemData(), 'isCustomerLoggedIn' => $this->isCustomerLoggedIn(), 'selectedShippingMethod' => $this->getSelectedShippingMethod(), 'storeCode' => $this->getStoreCode(), 'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(), 'isCustomerLoginRequired' => $this->isCustomerLoginRequired(), 'registerUrl' => $this->getRegisterUrl(), 'customerAddressCount' => $this->getCustomerAddressCount(), 'forgotPasswordUrl' => $this->getForgotPasswordUrl(), 'staticBaseUrl' => $this->getStaticBaseUrl(), 'priceFormat' => $this->localeFormat->getPriceFormat(null, $this->checkoutSession->getQuote()->getQuoteCurrencyCode()), 'basePriceFormat' => $this->localeFormat->getPriceFormat(null, $this->currencyManager->getDefaultCurrency())]; }
public function testSet() { $formKeyValue = 'Form key'; $this->sessionMock->expects(static::once())->method('setData')->with(FormKey::FORM_KEY, $formKeyValue); $this->formKey->set($formKeyValue); }
/** * {@inheritdoc} */ public function getConfig() { $quoteId = $this->checkoutSession->getQuote()->getId(); $output['formKey'] = $this->formKey->getFormKey(); $output['customerData'] = $this->getCustomerData(); $output['quoteData'] = $this->getQuoteData(); $output['quoteItemData'] = $this->getQuoteItemData(); $output['isCustomerLoggedIn'] = $this->isCustomerLoggedIn(); $output['selectedShippingMethod'] = $this->getSelectedShippingMethod(); $output['storeCode'] = $this->getStoreCode(); $output['isGuestCheckoutAllowed'] = $this->isGuestCheckoutAllowed(); $output['isCustomerLoginRequired'] = $this->isCustomerLoginRequired(); $output['registerUrl'] = $this->getRegisterUrl(); $output['checkoutUrl'] = $this->getCheckoutUrl(); $output['pageNotFoundUrl'] = $this->pageNotFoundUrl(); $output['forgotPasswordUrl'] = $this->getForgotPasswordUrl(); $output['staticBaseUrl'] = $this->getStaticBaseUrl(); $output['priceFormat'] = $this->localeFormat->getPriceFormat(null, $this->checkoutSession->getQuote()->getQuoteCurrencyCode()); $output['basePriceFormat'] = $this->localeFormat->getPriceFormat(null, $this->checkoutSession->getQuote()->getBaseCurrencyCode()); $output['postCodes'] = $this->postCodesConfig->getPostCodes(); $output['imageData'] = $this->imageProvider->getImages($quoteId); $output['defaultCountryId'] = $this->directoryHelper->getDefaultCountry(); $output['totalsData'] = $this->getTotalsData(); $output['shippingPolicy'] = ['isEnabled' => $this->scopeConfig->isSetFlag('shipping/shipping_policy/enable_shipping_policy', ScopeInterface::SCOPE_STORE), 'shippingPolicyContent' => nl2br($this->scopeConfig->getValue('shipping/shipping_policy/shipping_policy_content', ScopeInterface::SCOPE_STORE))]; $output['activeCarriers'] = $this->getActiveCarriers(); $output['originCountryCode'] = $this->getOriginCountryCode(); $output['paymentMethods'] = $this->getPaymentMethods(); $output['autocomplete'] = $this->isAutocompleteEnabled(); return $output; }
/** * Unified validation/authentication URL getter * * @param string $suffix * @param bool $current * @return string */ protected function _getUrl($suffix, $current = false) { $params = array('_secure' => true, '_current' => $current, 'form_key' => $this->formKey->getFormKey(), 'isIframe' => true); return $this->_url->getUrl($this->_urlPrefix . $suffix, $params); }
/** * Get add all to cart url * @return string */ public function getAddAllToCartUrl() { return $this->getUrl('*/*/allcart', array('wishlist_id' => $this->getWishlistInstance()->getId(), 'form_key' => $this->_formKey->getFormKey())); }
/** * {@inheritdoc} */ public function getConfig() { $quoteId = $this->checkoutSession->getQuote()->getId(); return ['formKey' => $this->formKey->getFormKey(), 'customerData' => $this->getCustomerData(), 'quoteData' => $this->getQuoteData(), 'quoteItemData' => $this->getQuoteItemData(), 'isCustomerLoggedIn' => $this->isCustomerLoggedIn(), 'selectedShippingMethod' => $this->getSelectedShippingMethod(), 'storeCode' => $this->getStoreCode(), 'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(), 'isCustomerLoginRequired' => $this->isCustomerLoginRequired(), 'registerUrl' => $this->getRegisterUrl(), 'customerAddressCount' => $this->getCustomerAddressCount(), 'forgotPasswordUrl' => $this->getForgotPasswordUrl(), 'staticBaseUrl' => $this->getStaticBaseUrl(), 'priceFormat' => $this->localeFormat->getPriceFormat(null, $this->checkoutSession->getQuote()->getQuoteCurrencyCode()), 'basePriceFormat' => $this->localeFormat->getPriceFormat(null, $this->currencyManager->getDefaultCurrency()), 'postCodes' => $this->postCodesConfig->getPostCodes(), 'imageData' => $this->imageProvider->getImages($quoteId), 'countryData' => $this->getCountryData(), 'totalsData' => $this->getTotalsData(), 'shippingRates' => $this->getDefaultShippingRates(), 'shippingPolicy' => ['isEnabled' => $this->scopeConfig->isSetFlag('shipping/shipping_policy/enable_shipping_policy', ScopeInterface::SCOPE_STORE), 'shippingPolicyContent' => nl2br($this->scopeConfig->getValue('shipping/shipping_policy/shipping_policy_content', ScopeInterface::SCOPE_STORE))], 'activeCarriers' => $this->getActiveCarriers(), 'originCountryCode' => $this->getOriginCountryCode(), 'paymentMethods' => $this->getPaymentMethods()]; }