AppDiscoverFetcherTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\App\AppStore\Fetcher;
  7. use OC\App\AppStore\Fetcher\AppDiscoverFetcher;
  8. use OC\App\CompareVersion;
  9. use OCP\Files\NotFoundException;
  10. use OCP\Files\SimpleFS\ISimpleFile;
  11. use OCP\Files\SimpleFS\ISimpleFolder;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. class AppDiscoverFetcherTest extends FetcherBase {
  14. protected CompareVersion|MockObject $compareVersion;
  15. protected function setUp(): void {
  16. parent::setUp();
  17. $this->fileName = 'discover.json';
  18. $this->endpoint = 'https://apps.nextcloud.com/api/v1/discover.json';
  19. $this->compareVersion = $this->createMock(CompareVersion::class);
  20. $this->fetcher = new AppDiscoverFetcher(
  21. $this->appDataFactory,
  22. $this->clientService,
  23. $this->timeFactory,
  24. $this->config,
  25. $this->logger,
  26. $this->registry,
  27. $this->compareVersion,
  28. );
  29. }
  30. public function testAppstoreDisabled() {
  31. $this->config
  32. ->method('getSystemValueBool')
  33. ->willReturnCallback(function ($var, $default) {
  34. if ($var === 'appstoreenabled') {
  35. return false;
  36. }
  37. return $default;
  38. });
  39. $this->appData
  40. ->expects($this->never())
  41. ->method('getFolder');
  42. $this->assertEquals([], $this->fetcher->get());
  43. }
  44. public function testNoInternet() {
  45. $this->config
  46. ->method('getSystemValueBool')
  47. ->willReturnCallback(function ($var, $default) {
  48. if ($var === 'has_internet_connection') {
  49. return false;
  50. }
  51. return $default;
  52. });
  53. $this->config
  54. ->method('getSystemValueString')
  55. ->willReturnCallback(function ($var, $default) {
  56. return $default;
  57. });
  58. $this->appData
  59. ->expects($this->never())
  60. ->method('getFolder');
  61. $this->assertEquals([], $this->fetcher->get());
  62. }
  63. /**
  64. * @dataProvider dataGetETag
  65. */
  66. public function testGetEtag(string|null $expected, bool $throws, string $content = '') {
  67. $folder = $this->createMock(ISimpleFolder::class);
  68. if (!$throws) {
  69. $file = $this->createMock(ISimpleFile::class);
  70. $file->expects($this->once())
  71. ->method('getContent')
  72. ->willReturn($content);
  73. $folder->expects($this->once())
  74. ->method('getFile')
  75. ->with('discover.json')
  76. ->willReturn($file);
  77. } else {
  78. $folder->expects($this->once())
  79. ->method('getFile')
  80. ->with('discover.json')
  81. ->willThrowException(new NotFoundException(''));
  82. }
  83. $this->appData->expects($this->once())
  84. ->method('getFolder')
  85. ->with('/')
  86. ->willReturn($folder);
  87. $etag = $this->fetcher->getETag();
  88. $this->assertEquals($expected, $etag);
  89. if ($expected !== null) {
  90. $this->assertTrue(gettype($etag) === 'string');
  91. }
  92. }
  93. public function dataGetETag(): array {
  94. return [
  95. 'file not found' => [null, true],
  96. 'empty file' => [null, false, ''],
  97. 'missing etag' => [null, false, '{ "foo": "bar" }'],
  98. 'valid etag' => ['test', false, '{ "ETag": "test" }'],
  99. 'numeric etag' => ['132', false, '{ "ETag": 132 }'],
  100. ];
  101. }
  102. }