1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-11 19:24:01 +02:00

[ticket/12479] Convert file_downloader to use guzzle instead of fsockopen

PHPBB-12479
This commit is contained in:
Marc Alexander
2024-07-23 21:04:34 +02:00
parent 9eb30b5d8e
commit f7d16fc626
2 changed files with 329 additions and 62 deletions

View File

@@ -1,4 +1,7 @@
<?php
use GuzzleHttp\Exception\RequestException;
/**
*
* This file is part of the phpBB Forum Software package.
@@ -17,6 +20,11 @@ class version_helper_remote_test extends \phpbb_test_case
protected $cache;
protected $version_helper;
// Guzzle mock data
protected $guzzle_status = 200; // Default to 200 status
protected $guzzle_data;
protected $guzzle_mock;
protected function setUp(): void
{
parent::setUp();
@@ -40,7 +48,29 @@ class version_helper_remote_test extends \phpbb_test_case
->method('get')
->with($this->anything())
->will($this->returnValue(false));
$this->file_downloader = new phpbb_mock_file_downloader();
$this->guzzle_mock = $this->getMockBuilder('\GuzzleHttp\Client')
->addMethods(['set_data'])
->onlyMethods(['request'])
->getMock();
$this->guzzle_mock->method('set_data')
->will($this->returnCallback(function($data)
{
$this->guzzle_data = $data;
}
));
$this->guzzle_mock->method('request')
->will($this->returnCallback(function()
{
return new \GuzzleHttp\Psr7\Response($this->guzzle_status, [], $this->guzzle_data);
}
));
$this->file_downloader = $this->getMockBuilder('\phpbb\file_downloader')
->onlyMethods(['create_client'])
->getMock();
$this->file_downloader->method('create_client')
->will($this->returnValue($this->guzzle_mock));
$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
@@ -202,7 +232,7 @@ class version_helper_remote_test extends \phpbb_test_case
*/
public function test_get_versions($input, $valid_data, $expected_return = '', $expected_exception = '')
{
$this->file_downloader->set($input);
$this->guzzle_mock->set_data($input);
// version_helper->get_versions() doesn't return a value on VERSIONCHECK_FAIL but only throws exception
// so the $return is undefined. Define it here
@@ -213,7 +243,7 @@ class version_helper_remote_test extends \phpbb_test_case
try {
$return = $this->version_helper->get_versions();
} catch (\phpbb\exception\runtime_exception $e) {
$this->assertEquals((string)$e->getMessage(), $expected_exception);
$this->assertEquals($expected_exception, $e->getMessage());
}
}
else
@@ -223,4 +253,215 @@ class version_helper_remote_test extends \phpbb_test_case
$this->assertEquals($expected_return, $return);
}
public function test_version_phpbb_com()
{
$this->guzzle_mock = $this->getMockBuilder('\GuzzleHttp\Client')
->onlyMethods(['request'])
->getMock();
$this->guzzle_mock->method('request')
->will($this->returnCallback(function()
{
return new \GuzzleHttp\Psr7\Response(200, [], file_get_contents(__DIR__ . '/fixture/30x.txt'));
}
));
$file_downloader = $this->getMockBuilder(\phpbb\file_downloader::class)
->onlyMethods(['create_client'])
->getMock();
$file_downloader->method('create_client')
->willReturn($this->guzzle_mock);
$hostname = 'version.phpbb.com';
if (!checkdnsrr($hostname, 'A'))
{
$this->markTestSkipped(sprintf(
'Could not find a DNS record for hostname %s. ' .
'Assuming network is down.',
$hostname
));
}
$file = $file_downloader->get($hostname, '/phpbb', '30x.txt');
$errstr = $file_downloader->get_error_string();
$errno = $file_downloader->get_error_number();
$this->assertNotEquals(
0,
strlen($file),
'Failed asserting that the response is not empty.'
);
$this->assertSame(
'',
$errstr,
'Failed asserting that the error string is empty.'
);
$this->assertSame(
0,
$errno,
'Failed asserting that the error number is 0 (i.e. no error occurred).'
);
$lines = explode("\n", $file);
$this->assertGreaterThanOrEqual(
2,
count($lines),
'Failed asserting that the version file has at least two lines.'
);
$this->assertStringStartsWith(
'3.',
$lines[0],
"Failed asserting that the first line of the version file starts with '3.'"
);
$this->assertNotSame(
false,
filter_var($lines[1], FILTER_VALIDATE_URL),
'Failed asserting that the second line of the version file is a valid URL.'
);
$this->assertStringContainsString('http', $lines[1]);
$this->assertStringContainsString('phpbb.com', $lines[1], '', true);
}
public function test_file_downloader_file_not_found()
{
$this->guzzle_mock = $this->getMockBuilder('\GuzzleHttp\Client')
->onlyMethods(['request'])
->getMock();
$this->guzzle_mock->method('request')
->will($this->returnCallback(function()
{
return new \GuzzleHttp\Psr7\Response(404, [], '');
}
));
$file_downloader = $this->getMockBuilder(\phpbb\file_downloader::class)
->onlyMethods(['create_client'])
->getMock();
$file_downloader->method('create_client')
->willReturn($this->guzzle_mock);
$this->expectException(\phpbb\exception\runtime_exception::class);
$this->expectExceptionMessage('FILE_NOT_FOUND');
$file_downloader->get('foo.com', 'bar', 'foo.txt');
}
public function test_file_downloader_exception_not_found()
{
$this->guzzle_mock = $this->getMockBuilder('\GuzzleHttp\Client')
->onlyMethods(['request'])
->getMock();
$this->guzzle_mock->method('request')
->will($this->returnCallback(function($method, $uri)
{
$request = new \GuzzleHttp\Psr7\Request('GET', $uri);
$response = new \GuzzleHttp\Psr7\Response(404, [], '');
throw new RequestException('FILE_NOT_FOUND', $request, $response);
}
));
$file_downloader = $this->getMockBuilder(\phpbb\file_downloader::class)
->onlyMethods(['create_client'])
->getMock();
$file_downloader->method('create_client')
->willReturn($this->guzzle_mock);
$this->expectException(\phpbb\exception\runtime_exception::class);
$this->expectExceptionMessage('FILE_NOT_FOUND');
$file_downloader->get('foo.com', 'bar', 'foo.txt');
}
public function test_file_downloader_exception_moved()
{
$this->guzzle_mock = $this->getMockBuilder('\GuzzleHttp\Client')
->onlyMethods(['request'])
->getMock();
$this->guzzle_mock->method('request')
->will($this->returnCallback(function($method, $uri)
{
$request = new \GuzzleHttp\Psr7\Request('GET', $uri);
$response = new \GuzzleHttp\Psr7\Response(302, [], '');
throw new RequestException('FILE_MOVED', $request, $response);
}
));
$file_downloader = $this->getMockBuilder(\phpbb\file_downloader::class)
->onlyMethods(['create_client'])
->getMock();
$file_downloader->method('create_client')
->willReturn($this->guzzle_mock);
$this->assertFalse($file_downloader->get('foo.com', 'bar', 'foo.txt'));
$this->assertEquals(302, $file_downloader->get_error_number());
$this->assertEquals('FILE_MOVED', $file_downloader->get_error_string());
}
public function test_file_downloader_exception_timeout()
{
$this->guzzle_mock = $this->getMockBuilder('\GuzzleHttp\Client')
->onlyMethods(['request'])
->getMock();
$this->guzzle_mock->method('request')
->will($this->returnCallback(function($method, $uri)
{
$request = new \GuzzleHttp\Psr7\Request('GET', $uri);
throw new RequestException('FILE_NOT_FOUND', $request);
}
));
$file_downloader = $this->getMockBuilder(\phpbb\file_downloader::class)
->onlyMethods(['create_client'])
->getMock();
$file_downloader->method('create_client')
->willReturn($this->guzzle_mock);
$this->expectException(\phpbb\exception\runtime_exception::class);
$this->expectExceptionMessage('FSOCK_TIMEOUT');
$file_downloader->get('foo.com', 'bar', 'foo.txt');
}
public function test_file_downloader_exception_other()
{
$this->guzzle_mock = $this->getMockBuilder('\GuzzleHttp\Client')
->onlyMethods(['request'])
->getMock();
$this->guzzle_mock->method('request')
->will($this->returnCallback(function($method, $uri)
{
throw new \RuntimeException('FSOCK_NOT_SUPPORTED');
}
));
$file_downloader = $this->getMockBuilder(\phpbb\file_downloader::class)
->onlyMethods(['create_client'])
->getMock();
$file_downloader->method('create_client')
->willReturn($this->guzzle_mock);
$this->expectException(\phpbb\exception\runtime_exception::class);
$this->expectExceptionMessage('FSOCK_DISABLED');
$file_downloader->get('foo.com', 'bar', 'foo.txt');
}
}