2016-10-27 18:41:15 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
|
|
|
* @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 Test\App\AppStore\Fetcher;
|
|
|
|
|
|
|
|
use OC\App\AppStore\Fetcher\Fetcher;
|
2017-05-10 10:56:38 +03:00
|
|
|
use OC\Files\AppData\Factory;
|
2016-10-27 18:41:15 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
|
|
|
use OCP\Http\Client\IClient;
|
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
use OCP\Http\Client\IResponse;
|
|
|
|
use OCP\IConfig;
|
2017-05-12 11:02:15 +03:00
|
|
|
use OCP\ILogger;
|
2016-10-27 18:41:15 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
abstract class FetcherBase extends TestCase {
|
2017-05-10 10:56:38 +03:00
|
|
|
/** @var Factory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $appDataFactory;
|
2016-10-27 18:41:15 +03:00
|
|
|
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $appData;
|
|
|
|
/** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $clientService;
|
|
|
|
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $timeFactory;
|
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $config;
|
2017-05-12 11:02:15 +03:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $logger;
|
2016-10-27 18:41:15 +03:00
|
|
|
/** @var Fetcher */
|
|
|
|
protected $fetcher;
|
|
|
|
/** @var string */
|
|
|
|
protected $fileName;
|
|
|
|
/** @var string */
|
|
|
|
protected $endpoint;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2017-05-10 10:56:38 +03:00
|
|
|
$this->appDataFactory = $this->createMock(Factory::class);
|
2016-10-27 18:41:15 +03:00
|
|
|
$this->appData = $this->createMock(IAppData::class);
|
2017-05-10 10:56:38 +03:00
|
|
|
$this->appDataFactory->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('appstore')
|
|
|
|
->willReturn($this->appData);
|
2016-10-27 18:41:15 +03:00
|
|
|
$this->clientService = $this->createMock(IClientService::class);
|
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2017-05-12 11:02:15 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2017-02-23 23:56:28 +03:00
|
|
|
}
|
2016-12-16 00:04:03 +03:00
|
|
|
|
2017-02-23 23:56:28 +03:00
|
|
|
public function testGetWithAlreadyExistingFileAndUpToDateTimestampAndVersion() {
|
2016-12-16 00:04:03 +03:00
|
|
|
$this->config
|
2017-02-23 23:56:28 +03:00
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('appstoreenabled', true)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
2016-12-16 00:04:03 +03:00
|
|
|
->method('getSystemValue')
|
2017-11-17 22:41:58 +03:00
|
|
|
->with('has_internet_connection', true)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
2016-12-16 00:04:03 +03:00
|
|
|
->with(
|
|
|
|
$this->equalTo('version'),
|
|
|
|
$this->anything()
|
|
|
|
)->willReturn('11.0.0.2');
|
2016-10-27 18:41:15 +03:00
|
|
|
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$file
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getContent')
|
2016-12-16 00:04:03 +03:00
|
|
|
->willReturn('{"timestamp":1200,"data":[{"id":"MyApp"}],"ncversion":"11.0.0.2"}');
|
2016-10-27 18:41:15 +03:00
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1499);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'id' => 'MyApp',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->fetcher->get());
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:04:03 +03:00
|
|
|
public function testGetWithNotExistingFileAndUpToDateTimestampAndVersion() {
|
2017-02-23 23:56:28 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('appstoreenabled', true)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getSystemValue')
|
2017-11-17 22:41:58 +03:00
|
|
|
->with('has_internet_connection', true)
|
2017-02-23 23:56:28 +03:00
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
2017-11-17 22:41:58 +03:00
|
|
|
->with('appstoreenabled', true)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(3))
|
|
|
|
->method('getSystemValue')
|
2017-02-23 23:56:28 +03:00
|
|
|
->with(
|
|
|
|
$this->equalTo('version'),
|
|
|
|
$this->anything()
|
|
|
|
)->willReturn('11.0.0.2');
|
|
|
|
|
2016-10-27 18:41:15 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willThrowException(new NotFoundException());
|
|
|
|
$folder
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('newFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$client = $this->createMock(IClient::class);
|
|
|
|
$this->clientService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('newClient')
|
|
|
|
->willReturn($client);
|
|
|
|
$response = $this->createMock(IResponse::class);
|
|
|
|
$client
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($this->endpoint)
|
|
|
|
->willReturn($response);
|
|
|
|
$response
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getBody')
|
|
|
|
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
|
2016-12-22 11:46:10 +03:00
|
|
|
$response->method('getHeader')
|
|
|
|
->with($this->equalTo('ETag'))
|
|
|
|
->willReturn('"myETag"');
|
|
|
|
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
|
2016-10-27 18:41:15 +03:00
|
|
|
$file
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('putContent')
|
|
|
|
->with($fileData);
|
|
|
|
$file
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($fileData);
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1502);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'id' => 'MyNewApp',
|
|
|
|
'foo' => 'foo',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->fetcher->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() {
|
2017-05-02 09:59:53 +03:00
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->willReturnCallback(function($key, $default) {
|
|
|
|
if ($key === 'appstoreenabled') {
|
|
|
|
return true;
|
|
|
|
} else if ($key === 'version') {
|
|
|
|
return '11.0.0.2';
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
});
|
2017-02-23 23:56:28 +03:00
|
|
|
|
2016-10-27 18:41:15 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$file
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getContent')
|
2016-12-16 00:04:03 +03:00
|
|
|
->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.2"}');
|
2016-10-27 18:41:15 +03:00
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1501);
|
|
|
|
$client = $this->createMock(IClient::class);
|
|
|
|
$this->clientService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('newClient')
|
|
|
|
->willReturn($client);
|
|
|
|
$response = $this->createMock(IResponse::class);
|
|
|
|
$client
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($this->endpoint)
|
|
|
|
->willReturn($response);
|
|
|
|
$response
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getBody')
|
|
|
|
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
|
2016-12-22 11:46:10 +03:00
|
|
|
$response->method('getHeader')
|
|
|
|
->with($this->equalTo('ETag'))
|
|
|
|
->willReturn('"myETag"');
|
|
|
|
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
|
2016-10-27 18:41:15 +03:00
|
|
|
$file
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('putContent')
|
|
|
|
->with($fileData);
|
|
|
|
$file
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($fileData);
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1502);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'id' => 'MyNewApp',
|
|
|
|
'foo' => 'foo',
|
|
|
|
],
|
|
|
|
[
|
2016-12-16 00:04:03 +03:00
|
|
|
'id' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->fetcher->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithAlreadyExistingFileAndNoVersion() {
|
2017-02-23 23:56:28 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('appstoreenabled', true)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getSystemValue')
|
2017-11-17 22:41:58 +03:00
|
|
|
->with('has_internet_connection', true)
|
2017-02-23 23:56:28 +03:00
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
2017-11-17 22:41:58 +03:00
|
|
|
->with('appstoreenabled', true)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(3))
|
|
|
|
->method('getSystemValue')
|
2017-02-23 23:56:28 +03:00
|
|
|
->with(
|
|
|
|
$this->equalTo('version'),
|
|
|
|
$this->anything()
|
|
|
|
)->willReturn('11.0.0.2');
|
|
|
|
|
2016-12-16 00:04:03 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$file
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}');
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1201);
|
|
|
|
$client = $this->createMock(IClient::class);
|
|
|
|
$this->clientService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('newClient')
|
|
|
|
->willReturn($client);
|
|
|
|
$response = $this->createMock(IResponse::class);
|
|
|
|
$client
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($this->endpoint)
|
|
|
|
->willReturn($response);
|
|
|
|
$response
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getBody')
|
|
|
|
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
|
2016-12-22 11:46:10 +03:00
|
|
|
$response->method('getHeader')
|
|
|
|
->with($this->equalTo('ETag'))
|
|
|
|
->willReturn('"myETag"');
|
|
|
|
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
|
2016-12-16 00:04:03 +03:00
|
|
|
$file
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('putContent')
|
|
|
|
->with($fileData);
|
|
|
|
$file
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($fileData);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'id' => 'MyNewApp',
|
|
|
|
'foo' => 'foo',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->fetcher->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithAlreadyExistingFileAndOutdatedVersion() {
|
2017-02-23 23:56:28 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('appstoreenabled', true)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getSystemValue')
|
2017-11-17 22:41:58 +03:00
|
|
|
->with('has_internet_connection', true)
|
2017-02-23 23:56:28 +03:00
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
2017-11-17 22:41:58 +03:00
|
|
|
->with('appstoreenabled', true)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(3))
|
|
|
|
->method('getSystemValue')
|
2017-02-23 23:56:28 +03:00
|
|
|
->with(
|
|
|
|
$this->equalTo('version'),
|
|
|
|
$this->anything()
|
|
|
|
)->willReturn('11.0.0.2');
|
|
|
|
|
2016-12-16 00:04:03 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$file
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getContent')
|
2016-12-22 11:46:10 +03:00
|
|
|
->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.1"');
|
2016-12-16 00:04:03 +03:00
|
|
|
$this->timeFactory
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1201);
|
|
|
|
$client = $this->createMock(IClient::class);
|
|
|
|
$this->clientService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('newClient')
|
|
|
|
->willReturn($client);
|
|
|
|
$response = $this->createMock(IResponse::class);
|
|
|
|
$client
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($this->endpoint)
|
|
|
|
->willReturn($response);
|
|
|
|
$response
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getBody')
|
|
|
|
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
|
2016-12-22 11:46:10 +03:00
|
|
|
$response->method('getHeader')
|
|
|
|
->with($this->equalTo('ETag'))
|
|
|
|
->willReturn('"myETag"');
|
|
|
|
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
|
2016-12-16 00:04:03 +03:00
|
|
|
$file
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('putContent')
|
|
|
|
->with($fileData);
|
|
|
|
$file
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($fileData);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'id' => 'MyNewApp',
|
|
|
|
'foo' => 'foo',
|
|
|
|
],
|
|
|
|
[
|
2016-10-27 18:41:15 +03:00
|
|
|
'id' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->fetcher->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithExceptionInClient() {
|
2017-05-02 09:59:53 +03:00
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->willReturnCallback(function($key, $default) {
|
|
|
|
if ($key === 'appstoreenabled') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
});
|
2017-02-23 23:56:28 +03:00
|
|
|
|
2016-10-27 18:41:15 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$file
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}}');
|
|
|
|
$client = $this->createMock(IClient::class);
|
|
|
|
$this->clientService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('newClient')
|
|
|
|
->willReturn($client);
|
|
|
|
$client
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($this->endpoint)
|
|
|
|
->willThrowException(new \Exception());
|
|
|
|
|
|
|
|
$this->assertSame([], $this->fetcher->get());
|
|
|
|
}
|
2016-12-22 11:46:10 +03:00
|
|
|
|
|
|
|
public function testGetMatchingETag() {
|
2017-05-02 09:59:53 +03:00
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->willReturnCallback(function($key, $default) {
|
|
|
|
if ($key === 'appstoreenabled') {
|
|
|
|
return true;
|
|
|
|
} else if ($key === 'version') {
|
|
|
|
return '11.0.0.2';
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
});
|
2017-02-23 23:56:28 +03:00
|
|
|
|
2016-12-22 11:46:10 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$origData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
|
|
|
|
$file
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($origData);
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1501);
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1502);
|
|
|
|
$client = $this->createMock(IClient::class);
|
|
|
|
$this->clientService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('newClient')
|
|
|
|
->willReturn($client);
|
|
|
|
$response = $this->createMock(IResponse::class);
|
|
|
|
$client
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->endpoint),
|
|
|
|
$this->equalTo([
|
2017-05-12 10:58:18 +03:00
|
|
|
'timeout' => 10,
|
2016-12-22 11:46:10 +03:00
|
|
|
'headers' => [
|
|
|
|
'If-None-Match' => '"myETag"'
|
|
|
|
]
|
|
|
|
])
|
|
|
|
)->willReturn($response);
|
|
|
|
$response->method('getStatusCode')
|
|
|
|
->willReturn(304);
|
|
|
|
|
|
|
|
$newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
|
|
|
|
$file
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('putContent')
|
|
|
|
->with($newData);
|
|
|
|
$file
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($newData);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'id' => 'MyNewApp',
|
|
|
|
'foo' => 'foo',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertSame($expected, $this->fetcher->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNoMatchingETag() {
|
2017-05-02 09:59:53 +03:00
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->willReturnCallback(function($key, $default) {
|
|
|
|
if ($key === 'appstoreenabled') {
|
|
|
|
return true;
|
|
|
|
} else if ($key === 'version') {
|
|
|
|
return '11.0.0.2';
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
});
|
2017-02-23 23:56:28 +03:00
|
|
|
|
2016-12-22 11:46:10 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$file
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn('{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}');
|
|
|
|
$client = $this->createMock(IClient::class);
|
|
|
|
$this->clientService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('newClient')
|
|
|
|
->willReturn($client);
|
|
|
|
$response = $this->createMock(IResponse::class);
|
|
|
|
$client
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->endpoint),
|
|
|
|
$this->equalTo([
|
2017-05-12 10:58:18 +03:00
|
|
|
'timeout' => 10,
|
2016-12-22 11:46:10 +03:00
|
|
|
'headers' => [
|
|
|
|
'If-None-Match' => '"myETag"',
|
|
|
|
]
|
|
|
|
])
|
|
|
|
)
|
|
|
|
->willReturn($response);
|
|
|
|
$response->method('getStatusCode')
|
|
|
|
->willReturn(200);
|
|
|
|
$response
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getBody')
|
|
|
|
->willReturn('[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}]');
|
|
|
|
$response->method('getHeader')
|
|
|
|
->with($this->equalTo('ETag'))
|
|
|
|
->willReturn('"newETag"');
|
|
|
|
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"newETag\""}';
|
|
|
|
$file
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('putContent')
|
|
|
|
->with($fileData);
|
|
|
|
$file
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($fileData);
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1501);
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1502);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'id' => 'MyNewApp',
|
|
|
|
'foo' => 'foo',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->fetcher->get());
|
|
|
|
}
|
2017-05-02 09:59:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
public function testFetchAfterUpgradeNoETag() {
|
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->willReturnCallback(function($key, $default) {
|
|
|
|
if ($key === 'appstoreenabled') {
|
|
|
|
return true;
|
|
|
|
} else if ($key === 'version') {
|
|
|
|
return '11.0.0.3';
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getFile')
|
|
|
|
->with($this->fileName)
|
|
|
|
->willReturn($file);
|
|
|
|
$file
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn('{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}');
|
|
|
|
$client = $this->createMock(IClient::class);
|
|
|
|
$this->clientService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('newClient')
|
|
|
|
->willReturn($client);
|
|
|
|
$response = $this->createMock(IResponse::class);
|
|
|
|
$client
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->endpoint),
|
2017-05-12 10:58:18 +03:00
|
|
|
$this->equalTo([
|
|
|
|
'timeout' => 10,
|
|
|
|
])
|
2017-05-02 09:59:53 +03:00
|
|
|
)
|
|
|
|
->willReturn($response);
|
|
|
|
$response->method('getStatusCode')
|
|
|
|
->willReturn(200);
|
|
|
|
$response
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getBody')
|
|
|
|
->willReturn('[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}]');
|
|
|
|
$response->method('getHeader')
|
|
|
|
->with($this->equalTo('ETag'))
|
|
|
|
->willReturn('"newETag"');
|
|
|
|
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1501,"ncversion":"11.0.0.3","ETag":"\"newETag\""}';
|
|
|
|
$file
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('putContent')
|
|
|
|
->with($fileData);
|
|
|
|
$file
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($fileData);
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(1501);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'id' => 'MyNewApp',
|
|
|
|
'foo' => 'foo',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->fetcher->get());
|
|
|
|
}
|
2016-10-27 18:41:15 +03:00
|
|
|
}
|