/** * @dataProvider editZoneResponse */ function testUpdateZone($editResponse, $zoneSettingResponse) { $mock = new MockHandler([new Response(200, [], $zoneSettingResponse), new Response(200, [], $editResponse)]); // $zone_settings = new ZoneSettings('myid',) $api = new ZoneApi("68ow48650j63zfzx1w9jd29cr367u0ezb6a4g", "email", $mock); $zone_settings = $api->getZoneSettings("test"); $ddos_protection = $zone_settings->getSettingById(ZoneSettings::SETTING_ADVANCED_DDOS); $ddos_protection->setValue(FALSE); $minify = $zone_settings->getSettingById(ZoneSettings::SETTING_MINIFY); $minify->setValue(TRUE, TRUE, TRUE); $mobile_redirect = $zone_settings->getSettingById(ZoneSettings::SETTING_MOBILE_REDIRECT); $mobile_redirect->setMobileSubdomain("mobile"); $security_level = $zone_settings->getSettingById(ZoneSettings::SETTING_SECURITY_LEVEL); $security_level->setValue(ZoneSettingSecurityLevel::SECURITY_HIGH); $ssl = $zone_settings->getSettingById(ZoneSettings::SETTING_SSL); $ssl->setValue(ZoneSettingSsl::SSL_STRICT); $api->updateZone($zone_settings); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_user_input = $form_state->getValue('table'); $zone_id = $form_state->getValue('selected'); // @todo. Might be worth saving this in the form_state so we don't need // to load this 2x. $this->config = \Drupal::config('cloudflare.settings'); $zone_api = new ZoneApi($this->config->get('apikey'), $this->config->get('email')); $zone_settings = $zone_api->getZoneSettings($zone_id); foreach ($form_user_input as $setting_name => $value_wrapper) { $current_setting = $zone_settings->getSettingById($setting_name); $is_boolean = in_array($setting_name, ZoneSettings::getBooleanSettings()); $is_integer = in_array($setting_name, ZoneSettings::getIntegerSettings()); $is_select = in_array($setting_name, ZoneSettings::getSelectSettings()); // If the setting is not editable then there is nothing to change on the // API. if (!$current_setting->isEditable()) { continue; } if ($is_boolean) { $new_value = $value_wrapper['value'][0]; $value_changed = $new_value != $current_setting->getValue(); if ($value_changed) { $current_setting->setValue($new_value ? 'on' : 'off'); } } elseif ($is_integer) { $new_value = $value_wrapper['value'][0]; $value_changed = $new_value != $current_setting->getValue(); if ($value_changed) { $current_setting->setValue($new_value); } } elseif ($is_select) { $new_value = $value_wrapper['value'][0][$setting_name]; $value_changed = $new_value != $current_setting->getValue(); if ($value_changed) { $current_setting->setValue($new_value); } } else { switch ($setting_name) { case ZoneSettings::SETTING_MINIFY: $new_css = $value_wrapper['value'][0][ZoneSettings::SETTING_MINIFY_CSS]; $new_js = $value_wrapper['value'][0][ZoneSettings::SETTING_MINIFY_JS]; $new_html = $value_wrapper['value'][0][ZoneSettings::SETTING_MINIFY_HTML]; $old_css = $current_setting->isCssMinifyEnabled(); $old_js = $current_setting->isJsMinifyEnabled(); $old_html = $current_setting->isHtmlMinifyEnabled(); $value_changed = $new_css != $old_css || $new_js != $old_js || $new_html != $old_html; if ($value_changed) { $current_setting->setValue($new_css, $new_html, $new_js); } break; // @todo this has not been wired up yet. // @todo this has not been wired up yet. case ZoneSettings::SETTING_MOBILE_REDIRECT: $is_mobile_redirect_enabled = (bool) $value[ZoneSettings::SETTING_MOBILE_REDIRECT_ENABLED]; $mobile_subdomain = $value[ZoneSettings::SETTING_MOBILE_REDIRECT_MOBILE_SUBDOMAIN]; $is_strip_uri_enabled = (bool) $value[ZoneSettings::SETTING_MOBILE_REDIRECT_STRIP_URI]; break; // @todo this has not been wired up yet. // @todo this has not been wired up yet. case ZoneSettings::SETTING_SECURITY_HEADER: break; } } } try { $zone_api->updateZone($zone_settings); } catch (CloudFlareHttpException $e) { drupal_set_message("Unable to connect to CloudFlare. " . $e->getMessage(), 'error'); \Drupal::logger('cloudflare')->error($e->getMessage()); $form['zone']['#access'] = FALSE; return; } catch (CloudFlareApiException $e) { drupal_set_message("Unable to connect to CloudFlare. " . $e->getMessage(), 'error'); \Drupal::logger('cloudflare')->error($e->getMessage()); return; } drupal_set_message($this->t('The configuration options have been saved.')); }