Skip to content

Commit

Permalink
Added Deskpro test data
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Apr 12, 2024
1 parent b1895e8 commit 22890ce
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 47 deletions.
37 changes: 0 additions & 37 deletions documentation/hoeringsportal_deskpro.hack.patch

This file was deleted.

8 changes: 2 additions & 6 deletions documentation/localDevelopment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
10 changes: 10 additions & 0 deletions web/modules/custom/hoeringsportal_deskpro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -503,7 +506,7 @@ public function getTicketCustomFields() {
* Get agents.
*/
public function getAgents() {
return $this->get('agents');
return $this->get('/agents');
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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: []
Original file line number Diff line number Diff line change
@@ -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: []

0 comments on commit 22890ce

Please sign in to comment.