Files
bahman026andCarl Schwan 7d76f9dedb fix(files_external): handle empty FTP port parameter
The external storage settings submit an empty string when the Port field is
left blank. `??` only substitutes null or missing values, so `""` was assigned
to $port and passed on to FtpConnection::__construct(), whose $port parameter
is typed int. PHP rejects a non-numeric string there, so adding an FTP storage
without a port ended in a TypeError and an HTTP 500 - after the configuration
had already been saved.

Fall back to the default port of 21 unless the configured value is numeric,
mirroring the guard added for SFTP in #58350. Casting without the check would
not work, because (int)"" is 0 rather than 21.

Co-authored-by: Carl Schwan <carl@carlschwan.eu>
Signed-off-by: bahman026 <bahman026@gmail.com>
2026-08-31 10:42:29 +03:30

41 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_External\Tests;
use OCA\Files_External\Lib\Storage\FTP;
use PHPUnit\Framework\Attributes\DataProvider;
use Test\TestCase;
class FtpTest extends TestCase {
public static function portProvider(): array {
$parameters = [
'host' => 'somehost',
'user' => 'someuser',
'password' => 'somepassword',
];
return [
'no port given' => [$parameters, 21],
'empty port' => [array_merge($parameters, ['port' => '']), 21],
'null port' => [array_merge($parameters, ['port' => null]), 21],
'non numeric port' => [array_merge($parameters, ['port' => 'ftp']), 21],
'numeric string port' => [array_merge($parameters, ['port' => '2121']), 2121],
'integer port' => [array_merge($parameters, ['port' => 2121]), 2121],
];
}
#[DataProvider('portProvider')]
public function testPort(array $parameters, int $expectedPort): void {
$instance = new FTP($parameters);
$this->assertSame($expectedPort, self::invokePrivate($instance, 'port'));
}
}