Skip to content

Commit a2afec1

Browse files
committed
Make the action parameter non-nullable in Discovery::getWopiClientURL().
1 parent 1f40305 commit a2afec1

File tree

4 files changed

+17
-23
lines changed

4 files changed

+17
-23
lines changed

src/Controller/ViewerController.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ public function editor(MediaInterface $media, Request $request, $edit = FALSE):
7070
try {
7171
// @todo Get client url for the correct MIME type.
7272
$discovery = $this->discoveryFetcher->getDiscovery();
73-
$wopi_client_url = $discovery->getWopiClientURL();
73+
$wopi_client_url = $edit
74+
? $discovery->getWopiClientURL(action: 'edit')
75+
: ($discovery->getWopiClientURL(action: 'view')
76+
// With the typical discovery.xml from Collabora, some MIME types that
77+
// are viewable have an 'edit' or 'view_comment' action but no 'view'
78+
// action.
79+
?? $discovery->getWopiClientURL(action: 'edit')
80+
?? $discovery->getWopiClientURL(action: 'view_comment'));
7481
}
7582
catch (CollaboraNotAvailableException $e) {
7683
$this->logger->warning(

src/Discovery/Discovery.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,11 @@ public function __construct(
3232
/**
3333
* {@inheritdoc}
3434
*/
35-
public function getWopiClientURL(string $mimetype = 'text/plain', ?string $action = NULL): ?string {
35+
public function getWopiClientURL(string $mimetype = 'text/plain', string $action = 'view'): ?string {
3636
$result = $this->parsedXml->xpath(sprintf(
37-
'/wopi-discovery/net-zone/app[@name=\'%s\']/action%s',
37+
"/wopi-discovery/net-zone/app[@name='%s']/action[@name='%s']",
3838
$mimetype,
39-
match ($action) {
40-
'edit',
41-
'view',
42-
'view_comment' => "[@name='$action']",
43-
NULL => '',
44-
}
39+
$action,
4540
));
4641
if (empty($result[0]['urlsrc'][0])) {
4742
return NULL;

src/Discovery/DiscoveryInterface.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ interface DiscoveryInterface {
2525
* @param string $mimetype
2626
* Mime type for which to get the WOPI client URL.
2727
* This refers to config entries in the discovery.xml file.
28-
* @param 'view'|'edit'|'view_comment'|null $action
29-
* Name of the action/operation for which to get the url, or NULL for any
30-
* action/operation.
28+
* @param string $action
29+
* Name of the action/operation for which to get the url.
30+
* Typical values are 'view', 'edit' or 'view_comment'.
3131
*
3232
* @return string|null
3333
* The WOPI client URL, or NULL if none provided for the MIME type and
3434
* operation.
3535
*/
36-
public function getWopiClientURL(string $mimetype = 'text/plain', ?string $action = NULL): ?string;
36+
public function getWopiClientURL(string $mimetype = 'text/plain', string $action = 'view'): ?string;
3737

3838
/**
3939
* Gets the public key used for proofing.

tests/src/Unit/CollaboraDiscoveryTest.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ public function testWopiClientUrl(): void {
2929
);
3030
$this->assertSame(
3131
'http://spreadsheet.collabora.test:9980/browser/61cf2b4/cool.html?',
32-
$discovery->getWopiClientURL('text/spreadsheet'),
32+
$discovery->getWopiClientURL('text/spreadsheet', 'edit'),
3333
);
3434
// Test unknown mime type.
35-
$this->assertNull(
36-
$discovery->getWopiClientURL('text/unknown'),
37-
);
35+
$this->assertNull($discovery->getWopiClientURL('text/unknown', 'view'));
3836
$this->assertSame(
3937
'http://csv.collabora.test:9980/browser/61cf2b4/cool.html?',
4038
$discovery->getWopiClientURL('text/csv', 'edit'),
@@ -52,7 +50,6 @@ public function testWopiClientUrl(): void {
5250
// well-defined behavior in that case.
5351
$this->assertNull($discovery->getWopiClientURL('image/png', 'edit'));
5452
$this->assertNull($discovery->getWopiClientURL('image/png', 'view'));
55-
$this->assertNotNull($discovery->getWopiClientURL('image/png'));
5653
}
5754

5855
/**
@@ -81,11 +78,6 @@ public function testRealisticDiscoveryXml(): void {
8178
$type_supported_actions[] = $action;
8279
}
8380
}
84-
$null_url = $discovery->getWopiClientURL($mimetype);
85-
$this->assertSame($null_url === NULL, $type_supported_actions === []);
86-
if ($null_url !== NULL) {
87-
$this->assertSame($known_url, $null_url);
88-
}
8981
sort($type_supported_actions);
9082
$supported_action_types[implode(',', $type_supported_actions)][] = $mimetype;
9183
}

0 commit comments

Comments
 (0)