diff --git a/documentation/hoeringsportal_deskpro.hack.patch b/documentation/hoeringsportal_deskpro.hack.patch deleted file mode 100644 index a88cf19b5..000000000 --- a/documentation/hoeringsportal_deskpro.hack.patch +++ /dev/null @@ -1,37 +0,0 @@ -diff --git a/web/modules/custom/hoeringsportal_deskpro/hoeringsportal_deskpro.module b/web/modules/custom/hoeringsportal_deskpro/hoeringsportal_deskpro.module -index d5026c34..5ce33a38 100644 ---- a/web/modules/custom/hoeringsportal_deskpro/hoeringsportal_deskpro.module -+++ b/web/modules/custom/hoeringsportal_deskpro/hoeringsportal_deskpro.module -@@ -73,6 +73,7 @@ function hoeringsportal_deskpro_theme() { - * Implements hook_form_alter(). - */ - function hoeringsportal_deskpro_form_alter(&$form, FormStateInterface $form_state, $form_id) { -+ return; - if ($form_id === 'node_hearing_form' || $form_id === 'node_hearing_edit_form') { - // Department as select control. - if (isset($form['field_deskpro_department_id']['widget'][0]['value']['#type']) -diff --git a/web/modules/custom/hoeringsportal_deskpro/src/Plugin/Validation/Constraint/AgentEmailContraintValidator.php b/web/modules/custom/hoeringsportal_deskpro/src/Plugin/Validation/Constraint/AgentEmailContraintValidator.php -index a80a11fd..1c8e335b 100644 ---- a/web/modules/custom/hoeringsportal_deskpro/src/Plugin/Validation/Constraint/AgentEmailContraintValidator.php -+++ b/web/modules/custom/hoeringsportal_deskpro/src/Plugin/Validation/Constraint/AgentEmailContraintValidator.php -@@ -41,6 +41,7 @@ public static function create(ContainerInterface $container) { - * {@inheritDoc} - */ - public function validate($value, Constraint $constraint) { -+ return; - // This is a single-item field so we only need to - // validate the first item. - $item = $value->first(); -diff --git a/web/modules/custom/hoeringsportal_deskpro/src/Service/DeskproService.php b/web/modules/custom/hoeringsportal_deskpro/src/Service/DeskproService.php -index e020e97c..cd206dfb 100644 ---- a/web/modules/custom/hoeringsportal_deskpro/src/Service/DeskproService.php -+++ b/web/modules/custom/hoeringsportal_deskpro/src/Service/DeskproService.php -@@ -531,6 +531,8 @@ public function getDefaultTicketLanguage() { - * Convenience function for getting data from the Deskpro client. - */ - private function get($endpoint, array $query = []) { -+ return new ApiResponse([], [], []); -+ - $cache = \Drupal::cache('data'); - $cacheKey = __METHOD__ . '||' . json_encode(func_get_args()); - $cacheTtl = $this->config->getCacheTtl(); diff --git a/documentation/localDevelopment.md b/documentation/localDevelopment.md index 189d2d1ab..4bfd05007 100644 --- a/documentation/localDevelopment.md +++ b/documentation/localDevelopment.md @@ -157,10 +157,6 @@ for further details. composer install --no-dev --optimize-autoloader ``` -## Deskpro local hack patch +## Deskpro -Run - -```sh -git apply < documentation/hoeringsportal_deskpro.hack.patch -``` +See [hoeringsportal_deskpro/README.md](web/modules/custom/hoeringsportal_deskpro/README.md#test-mode). diff --git a/web/modules/custom/hoeringsportal_deskpro/README.md b/web/modules/custom/hoeringsportal_deskpro/README.md index 954ce3b88..7052b1616 100644 --- a/web/modules/custom/hoeringsportal_deskpro/README.md +++ b/web/modules/custom/hoeringsportal_deskpro/README.md @@ -8,6 +8,16 @@ Edit configuration on `/admin/site-setup/deskpro`. Check out `/hoeringsportal_deskpro/api/docs` for details. +## Test mode + +During testing and development, you can make this module run in test mode and not call an actual Deskpro API. Enable +test mode in `settings.local.php`: + +```php +# settings.local.php +$settings['hoeringsportal_deskpro']['test_mode'] = TRUE; +``` + ## Drush commands ```sh diff --git a/web/modules/custom/hoeringsportal_deskpro/src/Service/DeskproService.php b/web/modules/custom/hoeringsportal_deskpro/src/Service/DeskproService.php index e020e97c9..b802f2cff 100644 --- a/web/modules/custom/hoeringsportal_deskpro/src/Service/DeskproService.php +++ b/web/modules/custom/hoeringsportal_deskpro/src/Service/DeskproService.php @@ -3,12 +3,15 @@ namespace Drupal\hoeringsportal_deskpro\Service; use Deskpro\API\APIResponse; +use Deskpro\API\APIResponseInterface; use Deskpro\API\DeskproClient; use Deskpro\API\Exception\APIException; use Drupal\Core\Language\LanguageManagerInterface; +use Drupal\Core\Site\Settings; use Drupal\hoeringsportal_deskpro\Exception\DeskproException; use Drupal\hoeringsportal_deskpro\State\DeskproConfig; use GuzzleHttp\Client; +use Symfony\Component\Yaml\Yaml; /** * Deskpro service. @@ -283,7 +286,7 @@ public function getTicketMessages($ticketId, array $query = []) { public function getMessageAttachments($message, array $query = []) { $query['ticket'] = $message['ticket']; $query['id'] = $message['id']; - $response = $this->get('tickets/{ticket}/messages/{id}/attachments', $query); + $response = $this->get('/tickets/{ticket}/messages/{id}/attachments', $query); return $response; } @@ -293,7 +296,7 @@ public function getMessageAttachments($message, array $query = []) { */ public function getPerson($id, array $query = []) { $query['id'] = $id; - $response = $this->get('people/{id}', $query); + $response = $this->get('/people/{id}', $query); return $response; } @@ -503,7 +506,7 @@ public function getTicketCustomFields() { * Get agents. */ public function getAgents() { - return $this->get('agents'); + return $this->get('/agents'); } /** @@ -530,7 +533,11 @@ public function getDefaultTicketLanguage() { /** * Convenience function for getting data from the Deskpro client. */ - private function get($endpoint, array $query = []) { + private function get($endpoint, array $query = []): APIResponseInterface { + if (self::isTestMode()) { + return $this->createMockResponse($endpoint, $query); + } + $cache = \Drupal::cache('data'); $cacheKey = __METHOD__ . '||' . json_encode(func_get_args()); $cacheTtl = $this->config->getCacheTtl(); @@ -728,6 +735,36 @@ public function getLanguages(array $query = []) { return $response; } + /** + * Is test mode? + */ + public static function isTestMode() { + return (bool) (Settings::get('hoeringsportal_deskpro')['test_mode'] ?? FALSE); + } + + /** + * Create mock response. + */ + private function createMockResponse(string $endpoint, array $query): APIResponseInterface { + $data = []; + $meta = []; + $linked = []; + + $filename = __DIR__ . '/mock/' . $endpoint . '.yaml'; + if (file_exists($filename)) { + try { + $stuff = Yaml::parseFile($filename); + $data = $stuff['data'] ?? []; + $meta = $stuff['meta'] ?? []; + $linked = $stuff['linked'] ?? []; + } + catch (\Exception) { + } + } + + return new APIResponse($data, $meta, $linked); + } + /** * Filter tickets. */ diff --git a/web/modules/custom/hoeringsportal_deskpro/src/Service/mock/agents.yaml b/web/modules/custom/hoeringsportal_deskpro/src/Service/mock/agents.yaml new file mode 100644 index 000000000..8353b9371 --- /dev/null +++ b/web/modules/custom/hoeringsportal_deskpro/src/Service/mock/agents.yaml @@ -0,0 +1,20 @@ +data: + - id: 007 + primary_email: 007@example.com + first_name: James + last_name: Bond + title_prefix: '' + name: James Bond + display_name: James Bond + is_agent: true + avatar: + default_url_pattern: https://example.com/file.php/avatar/{{IMG_SIZE}}/default.jpg?size-fit=1 + url_pattern: null + base_gravatar_url: null + online: false + online_for_chat: false + last_seen: null + agent_data: null + +meta: [] +linked: [] diff --git a/web/modules/custom/hoeringsportal_deskpro/src/Service/mock/ticket_departments.yaml b/web/modules/custom/hoeringsportal_deskpro/src/Service/mock/ticket_departments.yaml new file mode 100644 index 000000000..9a266157f --- /dev/null +++ b/web/modules/custom/hoeringsportal_deskpro/src/Service/mock/ticket_departments.yaml @@ -0,0 +1,19 @@ +data: + - id: 1 + parent: null + children: [] + title: Høringssvar + user_title: Høringssvar + full_title: Høringssvar + is_tickets_enabled: true + is_chat_enabled: false + display_order: 0 + avatar: + default_url_pattern: https://example.com/file.php/o-avatar/default?s={{IMG_SIZE}}&size-fit=1 + url_pattern: null + base_gravatar_url: null + brands: + - 1 + +meta: [] +linked: []