/** * @covers sAdmin::sValidateStep2 */ public function testsValidateStep2() { // Test with no rules, should always validate $result = $this->module->sValidateStep2(array()); $this->assertInternalType('array', $result); $this->assertArrayHasKey('sErrorFlag', $result); $this->assertArrayHasKey('sErrorMessages', $result); $this->assertCount(0, $result['sErrorFlag']); $this->assertCount(0, $result['sErrorMessages']); $testRuleSet = array('testField1' => array('required' => 1), 'testField2' => array('required' => 0), 'testField3' => array('required' => 1)); // Test failing validation, should have 2 failing fields $result = $this->module->sValidateStep2($testRuleSet); $this->assertInternalType('array', $result); $this->assertArrayHasKey('sErrorFlag', $result); $this->assertArrayHasKey('sErrorMessages', $result); $this->assertCount(2, $result['sErrorFlag']); $this->assertArrayHasKey('testField1', $result['sErrorFlag']); $this->assertArrayHasKey('testField3', $result['sErrorFlag']); $this->assertCount(1, $result['sErrorMessages']); // Setup dummy test data and test with it, see it passes $this->front->Request()->setPost(array('testField1' => 'testValue', 'testField2' => 'testValue', 'testField3' => 'testValue')); $result = $this->module->sValidateStep2($testRuleSet); $this->assertInternalType('array', $result); $this->assertArrayHasKey('sErrorFlag', $result); $this->assertArrayHasKey('sErrorMessages', $result); $this->assertCount(0, $result['sErrorFlag']); $this->assertCount(0, $result['sErrorMessages']); }
/** * Save billing action * * Save billing address data */ public function saveBillingAction() { if ($this->Request()->isPost()) { $countryData = $this->admin->sGetCountryList(); $countryIds = array_column($countryData, 'id'); $rules = array('salutation' => array('required' => 1), 'company' => array('required' => 0), 'firstname' => array('required' => 1), 'lastname' => array('required' => 1), 'street' => array('required' => 1), 'streetnumber' => array('required' => 1), 'zipcode' => array('required' => 1), 'city' => array('required' => 1), 'phone' => array('required' => intval(Shopware()->Config()->get('requirePhoneField'))), 'fax' => array('required' => 0), 'country' => array('required' => 1, 'in' => $countryIds), 'department' => array('required' => 0), 'shippingAddress' => array('required' => 0), 'text1' => array('required' => 0), 'text2' => array('required' => 0), 'text3' => array('required' => 0), 'text4' => array('required' => 0), 'text5' => array('required' => 0), 'text6' => array('required' => 0), 'birthyear' => array('required' => 0), 'birthmonth' => array('required' => 0), 'birthday' => array('required' => 0)); $values = $this->Request()->getPost('register'); // State selection if (!empty($values["billing"]["country"])) { $stateSelectionRequired = Shopware()->Db()->fetchRow("SELECT display_state_in_registration, force_state_in_registration\n FROM s_core_countries WHERE id = ?", array($values["billing"]["country"])); if ($stateSelectionRequired["display_state_in_registration"]) { $countryDataIndex = array_search($values["shipping"]["country"], $countryIds); $statesIds = array_column($countryData[$countryDataIndex]['states'], 'id'); // if not required, allow empty values if (!$stateSelectionRequired["force_state_in_registration"]) { $statesIds[] = ""; } $rules["stateID"] = array("required" => $stateSelectionRequired["force_state_in_registration"], 'in' => $statesIds); } if ($stateSelectionRequired["display_state_in_registration"] != true && $stateSelectionRequired["force_state_in_registration"] != true) { $this->admin->sSYSTEM->_POST["register"]["billing"]["stateID"] = $values["billing"]["stateID"] = 0; } else { $this->admin->sSYSTEM->_POST["register"]["billing"]["stateID"] = $values["billing"]["stateID"] = $values["billing"]["country_state_" . $values["billing"]["country"]]; } unset($values["billing"]["country_state_" . $values["billing"]["country"]]); } if ($this->Request()->getParam('sSelectAddress')) { $address = $this->admin->sGetPreviousAddresses('billing', $this->Request()->getParam('sSelectAddress')); if (!empty($address['hash'])) { $address = array_merge($this->View()->sUserData['billingaddress'], $address); $this->admin->sSYSTEM->_POST = $address; } } if (!empty($values['personal']['customer_type']) && $values['personal']['customer_type'] == 'private') { $values['billing']['company'] = ''; $values['billing']['department'] = ''; $values['billing']['ustid'] = ''; } elseif (!empty($values['personal']['customer_type']) || !empty($values['billing']['company'])) { $rules['ustid'] = array('required' => 0); } if (!empty($values)) { $this->admin->sSYSTEM->_POST = array_merge($values['personal'], $values['billing'], $this->admin->sSYSTEM->_POST->toArray()); } $checkData = $this->admin->sValidateStep2($rules, true); if (!empty($checkData['sErrorMessages'])) { $this->View()->sErrorFlag = $checkData['sErrorFlag']; $this->View()->sErrorMessages = $checkData['sErrorMessages']; return $this->forward('billing'); } else { $this->admin->sUpdateBilling(); } } if (!($target = $this->Request()->getParam('sTarget'))) { $target = 'account'; } $this->redirect(array('controller' => $target, 'action' => 'index', 'success' => 'billing')); }
/** * Validates the billing information * and returns an json string with error * codes and messages * * @return void */ public function ajaxValidateBillingAction() { $rules = array('salutation' => array('required' => 1), 'company' => array('required' => 0), 'firstname' => array('required' => 1), 'lastname' => array('required' => 1), 'street' => array('required' => 1), 'streetnumber' => array('required' => 1), 'zipcode' => array('required' => 1), 'city' => array('required' => 1), 'country' => array('required' => 1), 'department' => array('required' => 0)); if (!empty($this->post['personal']['customer_type']) && $this->post['personal']['customer_type'] == 'business') { $rules['company']['required'] = 1; } $this->admin->sSYSTEM->_POST = array_merge($this->post['personal'], $this->post['billing']); $checkData = $this->admin->sValidateStep2($rules); $error_messages = array(); $error_flags = array(); if (!empty($checkData['sErrorMessages'])) { foreach ($checkData['sErrorMessages'] as $error_message) { $error_messages[] = utf8_encode($error_message); } } foreach ($rules as $field => $rule) { $error_flags[$field] = !empty($checkData['sErrorFlag'][$field]); } echo Zend_Json::encode(array('success' => empty($error_messages), 'error_flags' => $error_flags, 'error_messages' => $error_messages)); }
/** * @covers sAdmin::sValidateStep2 */ public function testsValidateStep2() { // Test with no rules, should always validate $result = $this->module->sValidateStep2(array()); $this->assertInternalType('array', $result); $this->assertArrayHasKey('sErrorFlag', $result); $this->assertArrayHasKey('sErrorMessages', $result); $this->assertCount(0, $result['sErrorFlag']); $this->assertCount(0, $result['sErrorMessages']); $testRuleSet = array('testField1' => array('required' => 1), 'testField2' => array('required' => 0), 'testField3' => array('required' => 1)); // Test failing validation, should have 2 failing fields $result = $this->module->sValidateStep2($testRuleSet); $this->assertInternalType('array', $result); $this->assertArrayHasKey('sErrorFlag', $result); $this->assertArrayHasKey('sErrorMessages', $result); $this->assertCount(2, $result['sErrorFlag']); $this->assertArrayHasKey('testField1', $result['sErrorFlag']); $this->assertArrayHasKey('testField3', $result['sErrorFlag']); $this->assertCount(1, $result['sErrorMessages']); // Setup dummy test data and test with it, see it passes $this->front->Request()->setPost(array('testField1' => 'testValue', 'testField2' => 'testValue', 'testField3' => 'testValue')); $result = $this->module->sValidateStep2($testRuleSet); $this->assertInternalType('array', $result); $this->assertArrayHasKey('sErrorFlag', $result); $this->assertArrayHasKey('sErrorMessages', $result); $this->assertCount(0, $result['sErrorFlag']); $this->assertCount(0, $result['sErrorMessages']); // Test that using vat id will trigger aux function to validate it $this->config->offsetSet('sVATCHECKENDABLED', true); $testRuleSet['ustid'] = array('required' => 1); $this->front->Request()->setPost('ustid', '12345'); $result = $this->module->sValidateStep2($testRuleSet); $this->assertInternalType('array', $result); $this->assertArrayHasKey('sErrorFlag', $result); $this->assertArrayHasKey('sErrorMessages', $result); $this->assertCount(1, $result['sErrorFlag']); $this->assertCount(1, $result['sErrorMessages']); $this->assertContains('VatFailureInvalid', $result['sErrorFlag']); $this->assertContains('VatFailureErrorInfo', $result['sErrorFlag']); }