ResetTokenTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\UpdateNotification\Tests;
  8. use OCA\UpdateNotification\BackgroundJob\ResetToken as BackgroundJobResetToken;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\IAppConfig;
  11. use OCP\IConfig;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Test\TestCase;
  14. class ResetTokenTest extends TestCase {
  15. private IConfig|MockObject $config;
  16. private IAppConfig|MockObject $appConfig;
  17. private ITimeFactory|MockObject $timeFactory;
  18. private BackgroundJobResetToken $resetTokenBackgroundJob;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->appConfig = $this->createMock(IAppConfig::class);
  22. $this->config = $this->createMock(IConfig::class);
  23. $this->timeFactory = $this->createMock(ITimeFactory::class);
  24. $this->resetTokenBackgroundJob = new BackgroundJobResetToken(
  25. $this->timeFactory,
  26. $this->config,
  27. $this->appConfig,
  28. );
  29. }
  30. public function testRunWithNotExpiredToken() {
  31. $this->timeFactory
  32. ->expects($this->atLeastOnce())
  33. ->method('getTime')
  34. ->willReturn(123);
  35. $this->appConfig
  36. ->expects($this->once())
  37. ->method('getValueInt')
  38. ->with('core', 'updater.secret.created', 123);
  39. $this->config
  40. ->expects($this->once())
  41. ->method('getSystemValueBool')
  42. ->with('config_is_read_only')
  43. ->willReturn(false);
  44. $this->config
  45. ->expects($this->never())
  46. ->method('deleteSystemValue');
  47. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  48. }
  49. public function testRunWithExpiredToken() {
  50. $this->timeFactory
  51. ->expects($this->once())
  52. ->method('getTime')
  53. ->willReturn(1455045234);
  54. $this->appConfig
  55. ->expects($this->once())
  56. ->method('getValueInt')
  57. ->with('core', 'updater.secret.created', 1455045234)
  58. ->willReturn(2 * 24 * 60 * 60 + 1); // over 2 days
  59. $this->config
  60. ->expects($this->once())
  61. ->method('deleteSystemValue')
  62. ->with('updater.secret');
  63. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  64. }
  65. public function testRunWithExpiredTokenAndReadOnlyConfigFile() {
  66. $this->timeFactory
  67. ->expects($this->never())
  68. ->method('getTime');
  69. $this->appConfig
  70. ->expects($this->never())
  71. ->method('getValueInt');
  72. $this->config
  73. ->expects($this->once())
  74. ->method('getSystemValueBool')
  75. ->with('config_is_read_only')
  76. ->willReturn(true);
  77. $this->config
  78. ->expects($this->never())
  79. ->method('deleteSystemValue');
  80. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  81. }
  82. }