/** * @dataProvider getFailedApiResponses */ public function testFailedApiResponses($code) { $this->setExpectedException('CloudFlarePhpSdk\\Exceptions\\CloudFlareApiException'); $mock = new MockHandler([new Response(200, [], $code)]); $api = new ZoneApi("68ow48650j63zfzx1w9jd29cr367u0ezb6a4g", "email", $mock); $zones = $api->listZones(); }
/** * Form callback to purge paths. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ public function purgePaths(array &$form, FormStateInterface $form_state) { $config = $this->config('cloudflare.settings'); $api_key = $config->get('apikey'); $email = $config->get('email'); $zone = $config->get('zone'); try { $zone_api = new ZoneApi($api_key, $email); // @todo rethink how to handle cloudflare zones in Drupal. if (is_null($zone)) { $zones = $zone_api->listZones(); $zone = $zones[0]->getZoneId(); } $zone_api->purgeAllFiles($zone); } catch (CloudFlareHttpException $e) { drupal_set_message("Unable to clear paths. " . $e->getMessage(), 'error'); \Drupal::logger('cloudflare')->error($e->getMessage()); return; } catch (CloudFlareApiException $e) { drupal_set_message("Unable to clear paths. " . $e->getMessage(), 'error'); \Drupal::logger('cloudflare')->error($e->getMessage()); return; } // If no exceptions have been thrown then the request has been successful. drupal_set_message("The zone: {$zone} was successfully cleared."); }
/** * @dataProvider httpMockErrorResponses */ function testInvalidHttpResponses($code, $response_header) { $this->setExpectedException('CloudFlarePhpSdk\\Exceptions\\CloudFlareHttpException'); $mock = new MockHandler([new Response($code, $response_header)]); $api = new ZoneApi("68ow48650j63zfzx1w9jd29cr367u0ezb6a4g", "email", $mock); $api->listZones(); }
/** * {@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.')); }