summaryrefslogtreecommitdiffstats
path: root/tests/php/Service/RecordingServiceTest.php
blob: 917e8728477f624f45c193dce8de147708cb5955 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\Talk\Service;

/**
 * Overwrite is_uploaded_file in the OCA\Talk\Service namespace
 * to allow proper unit testing of the postAvatar call.
 */
function is_uploaded_file($filename) {
	return file_exists($filename);
}

namespace OCA\Talk\Tests\php\Service;

use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Config;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Participant;
use OCA\Talk\Recording\BackendNotifier;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\RecordingService;
use OCA\Talk\Service\RoomService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\Notification\IManager;
use OCP\Share\IManager as ShareManager;
use OCP\SpeechToText\ISpeechToTextManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class RecordingServiceTest extends TestCase {
	private IMimeTypeDetector $mimeTypeDetector;
	protected ParticipantService&MockObject $participantService;
	protected IRootFolder&MockObject $rootFolder;
	protected Config&MockObject $config;
	protected IConfig&MockObject $serverConfig;
	protected IManager&MockObject $notificationManager;
	protected Manager&MockObject $roomManager;
	protected ITimeFactory&MockObject $timeFactory;
	protected RoomService&MockObject $roomService;
	protected ShareManager&MockObject $shareManager;
	protected ChatManager&MockObject $chatManager;
	protected LoggerInterface&MockObject $logger;
	protected BackendNotifier&MockObject $backendNotifier;
	protected ISpeechToTextManager&MockObject $speechToTextManager;
	protected RecordingService $recordingService;

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

		$this->mimeTypeDetector = \OCP\Server::get(IMimeTypeDetector::class);
		$this->participantService = $this->createMock(ParticipantService::class);
		$this->rootFolder = $this->createMock(IRootFolder::class);
		$this->notificationManager = $this->createMock(IManager::class);
		$this->roomManager = $this->createMock(Manager::class);
		$this->timeFactory = $this->createMock(ITimeFactory::class);
		$this->config = $this->createMock(Config::class);
		$this->serverConfig = $this->createMock(IConfig::class);
		$this->roomService = $this->createMock(RoomService::class);
		$this->shareManager = $this->createMock(ShareManager::class);
		$this->chatManager = $this->createMock(ChatManager::class);
		$this->logger = $this->createMock(LoggerInterface::class);
		$this->backendNotifier = $this->createMock(BackendNotifier::class);
		$this->speechToTextManager = $this->createMock(ISpeechToTextManager::class);

		$this->recordingService = new RecordingService(
			$this->mimeTypeDetector,
			$this->participantService,
			$this->rootFolder,
			$this->notificationManager,
			$this->roomManager,
			$this->timeFactory,
			$this->config,
			$this->serverConfig,
			$this->roomService,
			$this->shareManager,
			$this->chatManager,
			$this->logger,
			$this->backendNotifier,
			$this->speechToTextManager,
		);
	}

	public static function dataValidateFileFormat(): array {
		return [
			# file_invalid_path
			['', '', 'file_invalid_path'],
			# file_mimetype
			['', realpath(__DIR__ . '/../../../img/app.svg'), 'file_mimetype'],
			['name.ogg', realpath(__DIR__ . '/../../../img/app.svg'), 'file_mimetype'],
			# file_extension
			['', realpath(__DIR__ . '/../../../img/join_call.ogg'), 'file_extension'],
			['name', realpath(__DIR__ . '/../../../img/join_call.ogg'), 'file_extension'],
			['name.mp3', realpath(__DIR__ . '/../../../img/join_call.ogg'), 'file_extension'],
			# Success
			['name.ogg', realpath(__DIR__ . '/../../../img/join_call.ogg'), ''],
		];
	}

	/**
	 * @dataProvider dataValidateFileFormat
	 */
	public function testValidateFileFormat(string $fileName, string $fileRealPath, string $exceptionMessage): void {
		if ($exceptionMessage) {
			$this->expectExceptionMessage($exceptionMessage);
		} else {
			$this->expectNotToPerformAssertions();
		}
		$this->recordingService->validateFileFormat($fileName, $fileRealPath);
	}

	public static function dataGetResourceFromFileArray(): array {
		$fileWithContent = tempnam(sys_get_temp_dir(), 'txt');
		file_put_contents($fileWithContent, 'bla');
		return [
			[['error' => 1, 'tmp_name' => ''], '', 'invalid_file'],
			[['error' => 1, 'tmp_name' => 'a'], '', 'invalid_file'],
			# Empty file
			[['error' => 0, 'tmp_name' => tempnam(sys_get_temp_dir(), 'txt')], '', 'empty_file'],
			# file with content
			[['error' => 0, 'tmp_name' => $fileWithContent], 'bla', ''],
		];
	}

	/**
	 * @dataProvider dataGetResourceFromFileArray
	 */
	public function testGetResourceFromFileArray(array $file, string $expected, string $exceptionMessage): void {
		if ($exceptionMessage) {
			$this->expectExceptionMessage($exceptionMessage);
		}

		$room = $this->createMock(Room::class);
		$attendee = Attendee::fromRow([
			'actor_type' => Attendee::ACTOR_USERS,
			'actor_id' => 'participant1',
		]);
		$participant = new Participant($room, $attendee, null);

		$actual = stream_get_contents($this->recordingService->getResourceFromFileArray($file, $room, $participant));
		$this->assertEquals($expected, $actual);
	}
}