Skip to content

Commit

Permalink
Add content verification.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronGilMartinez committed Dec 12, 2024
1 parent bff1582 commit b6619c3
Showing 1 changed file with 60 additions and 12 deletions.
72 changes: 60 additions & 12 deletions tests/src/Kernel/WopiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@

namespace Drupal\Tests\collabora_online\Kernel;

use Drupal\collabora_online\Jwt\JwtTranscoderInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\media\MediaInterface;
use Drupal\user\UserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Tests the WopiController.
*
* @see \Drupal\collabora_online\Controller\WopiController
*/
class WopiControllerTest extends CollaboraKernelTestBase {

Expand All @@ -30,6 +37,27 @@ class WopiControllerTest extends CollaboraKernelTestBase {
'jwt',
];

/**
* The user with access to perform operations.
*
* @var \Drupal\user\UserInterface
*/
protected UserInterface $user;

/**
* The media where to perform operations.
*
* @var \Drupal\media\MediaInterface
*/
protected MediaInterface $media;

/**
* The source file.
*
* @var \Drupal\file\FileInterface
*/
protected FileInterface $file;

/**
* {@inheritdoc}
*/
Expand All @@ -39,42 +67,62 @@ protected function setUp(): void {
$this->installConfig([
'jwt',
]);

$this->user = $this->createUser(['access content']);
$this->media = $this->createMediaEntity('document');
$fid = $this->media->getSource()->getSourceFieldValue($this->media);
$this->file = File::load($fid);

$this->setCurrentUser($this->user);
}

/**
* Tests collabora-online.wopi.info.
* Tests sucessful response for check file info.
*
* @see \Drupal\collabora_online\Controller\WopiController::wopiCheckFileInfo()
*/
public function testWopiInfoController(): void {
$media = $this->createMediaEntity('document');
public function testWopiInfoSucess(): void {
/** @var \Drupal\collabora_online\Jwt\JwtTranscoderInterface $transcoder */
$transcoder = \Drupal::service('Drupal\collabora_online\Jwt\JwtTranscoderInterface');

$transcoder = \Drupal::service(JwtTranscoderInterface::class);
$expire_timestamp = gettimeofday(TRUE) + 1000;

$access_token = $transcoder->encode([
'fid' => '1',
'uid' => '2',
'fid' => $this->file->id(),
'uid' => $this->user->id(),
'wri' => FALSE,
'exp' => $expire_timestamp,
], $expire_timestamp);

$request = Request::create(
'/cool/wopi/files/1',
'/cool/wopi/files/' . $this->media->id(),
'GET',
[
'id' => '1',
'id' => $this->media->id(),
'action' => 'info',
'access_token' => $access_token,
]
);

$this->setCurrentUser($this->createUser(['access content']));

/** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
$kernel = \Drupal::getContainer()->get('http_kernel');
$kernel = \Drupal::service('http_kernel');
$response = $kernel->handle($request);
$content = json_decode($response->getContent(), TRUE);
$mtime = date_create_immutable_from_format('U', (string) $this->file->getChangedTime());

$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$this->assertEquals([
'BaseFileName' => $this->file->getFilename(),
'Size' => $this->file->getSize(),
'LastModifiedTime' => $mtime->format('c'),
'UserId' => $this->user->id(),
'UserFriendlyName' => $this->user->getDisplayName(),
'UserExtraInfo' => [
'mail' => $this->user->getEmail(),
],
'UserCanWrite' => FALSE,
'IsAdminUser' => FALSE,
'IsAnonymousUser' => FALSE,
], $content);
}

}

0 comments on commit b6619c3

Please sign in to comment.