Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unit test for token and message id extraction
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot-nextcloud[bot] committed Sep 29, 2022
commit 6a14dfc5d9c3c54fbb71869d93b9abf6bd92d9d7
6 changes: 3 additions & 3 deletions lib/Collaboration/Reference/TalkReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function matchReference(string $referenceText): bool {
* @return array|null
* @psalm-return ReferenceMatch|null
*/
private function getTalkAppLinkToken(string $referenceText): ?array {
protected function getTalkAppLinkToken(string $referenceText): ?array {
$indexPhpUrl = $this->urlGenerator->getAbsoluteURL('/index.php/call/');
$rewriteUrl = $this->urlGenerator->getAbsoluteURL('/call/');

Expand Down Expand Up @@ -109,7 +109,7 @@ private function getTalkAppLinkToken(string $referenceText): ?array {
if ($hashPosition !== false) {
$afterHash = substr($urlOfInterest, $hashPosition + 1);
if (preg_match('/^message_(\d+)$/', $afterHash, $matches)) {
$messageId = $matches[1];
$messageId = (int) $matches[1];
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ public function resolveReference(string $referenceText): ?IReference {
/**
* @throws RoomNotFoundException
*/
private function fetchReference(Reference $reference): void {
protected function fetchReference(Reference $reference): void {
if ($this->userId === null) {
throw new RoomNotFoundException();
}
Expand Down
105 changes: 105 additions & 0 deletions tests/php/Collaboration/Reference/TalkReferenceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php


declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Talk\Tests\php\Collaboration\Resources;

use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Chat\MessageParser;
use OCA\Talk\Collaboration\Reference\TalkReferenceProvider;
use OCA\Talk\Collaboration\Resources\ConversationProvider;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\Collaboration\Resources\IResource;
use OCP\Collaboration\Resources\ResourceException;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class TalkReferenceProviderTest extends TestCase {
/** @var IURLGenerator|MockObject */
protected $urlGenerator;
/** @var Manager|MockObject */
protected $roomManager;
/** @var ChatManager|MockObject */
protected $chatManager;
/** @var MessageParser|MockObject */
protected $messageParser;
/** @var IL10N|MockObject */
protected $l;
protected ?TalkReferenceProvider $provider = null;

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

$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->roomManager = $this->createMock(Manager::class);
$this->chatManager = $this->createMock(ChatManager::class);
$this->messageParser = $this->createMock(MessageParser::class);
$this->l = $this->createMock(IL10N::class);

$this->provider = new TalkReferenceProvider(
$this->urlGenerator,
$this->roomManager,
$this->chatManager,
$this->messageParser,
$this->l,
'test'
);
}

public function dataGetTalkAppLinkToken(): array {
return [
['https://localhost/', null],
['https://localhost/call', null],
['https://localhost/call/abcdef', ['token' => 'abcdef', 'message' => null]],
['https://localhost/call/abcdef?query=1', ['token' => 'abcdef', 'message' => null]],
['https://localhost/call/abcdef#hash=1', ['token' => 'abcdef', 'message' => null]],
['https://localhost/call/abcdef#message_123', ['token' => 'abcdef', 'message' => 123]],
['https://localhost/call/abcdef?query=1#message_123', ['token' => 'abcdef', 'message' => 123]],
['https://localhost/call/abcdef?query=1#message_123bcd', ['token' => 'abcdef', 'message' => null]],
];
}

/**
* @dataProvider dataGetTalkAppLinkToken
* @param string $reference
* @param array|null $expected
* @return void
*/
public function testGetTalkAppLinkToken(string $reference, ?array $expected): void {
$this->urlGenerator->expects($this->any())
->method('getAbsoluteURL')
->willReturnCallback(static fn($url) => 'https://localhost' . $url);

$actual = self::invokePrivate($this->provider, 'getTalkAppLinkToken', [$reference]);
self::assertSame($expected, $actual);
}
}