aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/tests/Controller/ConversionApiControllerTest.php
blob: a2f1fccd978a6851f8a9efc5128d62eb969d373b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\Files\Controller;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\Files\Conversion\IConversionManager;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IRequest;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
 * Class ConversionApiController
 *
 * @package OCA\Files\Controller
 */
class ConversionApiControllerTest extends TestCase {
	private string $appName = 'files';
	private ConversionApiController $conversionApiController;
	private IRequest&MockObject $request;
	private IConversionManager&MockObject $fileConversionManager;
	private IRootFolder&MockObject $rootFolder;
	private File&MockObject $file;
	private Folder&MockObject $userFolder;
	private IL10N&MockObject $l10n;
	private string $user;

	protected function setUp(): void {
		parent::setUp();

		$this->request = $this->createMock(IRequest::class);
		$this->fileConversionManager = $this->createMock(IConversionManager::class);
		$this->file = $this->createMock(File::class);
		$this->l10n = $this->createMock(IL10N::class);
		$this->user = 'userid';

		$this->userFolder = $this->createMock(Folder::class);

		$this->rootFolder = $this->createMock(IRootFolder::class);
		$this->rootFolder->method('getUserFolder')->with($this->user)->willReturn($this->userFolder);

		$this->conversionApiController = new ConversionApiController(
			$this->appName,
			$this->request,
			$this->fileConversionManager,
			$this->rootFolder,
			$this->l10n,
			$this->user,
		);
	}

	public function testThrowsNotFoundException() {
		$this->expectException(OCSNotFoundException::class);
		$this->conversionApiController->convert(42, 'image/png');
	}

	public function testThrowsOcsException() {
		$this->userFolder->method('getFirstNodeById')->with(42)->willReturn($this->file);
		$this->fileConversionManager->method('convert')->willThrowException(new \Exception());

		$this->expectException(OCSException::class);
		$this->conversionApiController->convert(42, 'image/png');
	}

	public function testConvert() {
		$convertedFileAbsolutePath = $this->user . '/files/test.png';

		$this->userFolder->method('getFirstNodeById')->with(42)->willReturn($this->file);
		$this->userFolder->method('getRelativePath')->with($convertedFileAbsolutePath)->willReturn('/test.png');
		$this->userFolder->method('get')->with('/test.png')->willReturn($this->file);

		$this->file->method('getId')->willReturn(42);

		$this->fileConversionManager->method('convert')->with($this->file, 'image/png', null)->willReturn($convertedFileAbsolutePath);

		$actual = $this->conversionApiController->convert(42, 'image/png', null);
		$expected = new DataResponse([
			'path' => '/test.png',
			'fileId' => 42,
		], Http::STATUS_CREATED);

		$this->assertEquals($expected, $actual);
	}
}