/** * @param null|string $requestCountry * @param null|string $requestDefaultCountry * @param bool $canUseDefault * @param bool $inherit * @dataProvider renderDataProvider */ public function testRender($requestCountry, $requestDefaultCountry, $canUseDefault, $inherit) { $this->_request->expects($this->any())->method('getParam')->will($this->returnCallback(function ($param) use($requestCountry, $requestDefaultCountry) { if ($param == \Magento\Paypal\Model\Config\StructurePlugin::REQUEST_PARAM_COUNTRY) { return $requestCountry; } if ($param == Country::REQUEST_PARAM_DEFAULT_COUNTRY) { return $requestDefaultCountry; } return $param; })); $this->_element->setInherit($inherit); $this->_element->setCanUseDefaultValue($canUseDefault); $constraints = [new \PHPUnit_Framework_Constraint_StringContains('document.observe("dom:loaded", function() {'), new \PHPUnit_Framework_Constraint_StringContains('$("' . $this->_element->getHtmlId() . '").observe("change", function () {')]; if ($canUseDefault && $requestCountry == 'US' && $requestDefaultCountry) { $constraints[] = new \PHPUnit_Framework_Constraint_StringContains('$("' . $this->_element->getHtmlId() . '_inherit").observe("click", function () {'); } $this->_jsHelper->expects($this->once())->method('getScript')->with(new \PHPUnit_Framework_Constraint_And($constraints)); $this->_url->expects($this->once())->method('getUrl')->with('*/*/*', ['section' => 'section', 'website' => 'website', 'store' => 'store', \Magento\Paypal\Model\Config\StructurePlugin::REQUEST_PARAM_COUNTRY => '__country__']); $this->_model->render($this->_element); }
/** * Render country field considering request parameter * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @return string */ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $country = $this->getRequest()->getParam(StructurePlugin::REQUEST_PARAM_COUNTRY); if ($country) { $element->setValue($country); } if ($element->getCanUseDefaultValue()) { $this->_defaultCountry = $this->_scopeConfig->getValue(self::FIELD_CONFIG_PATH); if (!$this->_defaultCountry) { $this->_defaultCountry = $this->directoryHelper->getDefaultCountry(); } if ($country) { $shouldInherit = $country == $this->_defaultCountry && $this->getRequest()->getParam(self::REQUEST_PARAM_DEFAULT_COUNTRY); $element->setInherit($shouldInherit); } if ($element->getInherit()) { $this->_defaultCountry = null; } } return parent::render($element); }