Skip to content

Commit

Permalink
Issue CollaboraOnline#56: Convert CoolRequest into a service.
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Nov 12, 2024
1 parent 9deac47 commit 6fd954b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions collabora_online.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
_defaults:
autowire: true
Drupal\collabora_online\Cool\CoolRequest: { }
2 changes: 1 addition & 1 deletion src/Cool/CoolRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function strStartsWith($s, $ss) {
}

/**
* Helper class to fetch a WOPI client url.
* Service to fetch a WOPI client url.
*/
class CoolRequest {

Expand Down
4 changes: 2 additions & 2 deletions src/Cool/CoolUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ public static function getViewerRender(Media $media, bool $can_write, $options =
$wopi_base = $default_config->get('cool')['wopi_base'];
$allowfullscreen = $default_config->get('cool')['allowfullscreen'] ?? FALSE;

$req = new CoolRequest();
$discovery_xml_service = \Drupal::service(CoolRequest::class);
try {
$wopi_client = $req->getWopiClientURL();
$wopi_client = $discovery_xml_service->getWopiClientURL();
}
catch (CoolRequestException $e) {
return [
Expand Down
17 changes: 15 additions & 2 deletions tests/src/ExistingSite/FetchClientUrlTestDISABLED.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FetchClientUrlTestDISABLED extends ExistingSiteBase {
* Tests fetching the client url.
*/
public function testFetchClientUrl(): void {
$cool_request = new CoolRequest();
$cool_request = $this->service(CoolRequest::class);
$client_url = $cool_request->getWopiClientURL();
// The protocol, domain and port are known when this test runs in the
// docker-compose setup.
Expand All @@ -27,11 +27,24 @@ public function testFetchClientUrl(): void {
* Tests error behavior when fetching client url.
*/
public function testFetchClientUrlError(): void {
$cool_request = new CoolRequest();
$cool_request = $this->service(CoolRequest::class);
$client_url = $cool_request->getWopiClientURL();
// The protocol, domain and port are known when this test runs in the
// docker-compose setup.
$this->assertMatchesRegularExpression('@^http://collabora\.test:9980/browser/[0-9a-f]+/cool\.html\?$@', $client_url);
}

/**
* @template T of object
*
* @param class-string<T> $class
*
* @return T&object
*/
protected function service(string $class): object {
$service = \Drupal::service($class);
$this->assertInstanceOf($class, $service);
return $service;
}

}

0 comments on commit 6fd954b

Please sign in to comment.