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 09a8456
Show file tree
Hide file tree
Showing 4 changed files with 28 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
23 changes: 21 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,30 @@ 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);
}

/**
* Gets a service that is instance of a specific class.
*
* This is better for static analysis than \Drupal::service().
*
* @template T of object
*
* @param class-string<T> $class
* The service class or interface, which is also used as the service id.
*
* @return T&object
* The service instance.
*/
protected function service(string $class): object {
$service = \Drupal::service($class);
$this->assertInstanceOf($class, $service);
return $service;
}

}

0 comments on commit 09a8456

Please sign in to comment.