BirthdayCalendarControllerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\Unit\DAV\Controller;
  7. use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCA\DAV\Controller\BirthdayCalendarController;
  10. use OCP\BackgroundJob\IJobList;
  11. use OCP\IConfig;
  12. use OCP\IDBConnection;
  13. use OCP\IRequest;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use Test\TestCase;
  17. class BirthdayCalendarControllerTest extends TestCase {
  18. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  19. private $config;
  20. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  21. private $request;
  22. /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  23. private $db;
  24. /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
  25. private $jobList;
  26. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  27. private $userManager;
  28. /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject */
  29. private $caldav;
  30. /** @var BirthdayCalendarController|\PHPUnit\Framework\MockObject\MockObject */
  31. private $controller;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->config = $this->createMock(IConfig::class);
  35. $this->request = $this->createMock(IRequest::class);
  36. $this->db = $this->createMock(IDBConnection::class);
  37. $this->jobList = $this->createMock(IJobList::class);
  38. $this->userManager = $this->createMock(IUserManager::class);
  39. $this->caldav = $this->createMock(CalDavBackend::class);
  40. $this->controller = new BirthdayCalendarController('dav',
  41. $this->request, $this->db, $this->config, $this->jobList,
  42. $this->userManager, $this->caldav);
  43. }
  44. public function testEnable(): void {
  45. $this->config->expects($this->once())
  46. ->method('setAppValue')
  47. ->with('dav', 'generateBirthdayCalendar', 'yes');
  48. $this->userManager->expects($this->once())
  49. ->method('callForSeenUsers')
  50. ->willReturnCallback(function ($closure): void {
  51. $user1 = $this->createMock(IUser::class);
  52. $user1->method('getUID')->willReturn('uid1');
  53. $user2 = $this->createMock(IUser::class);
  54. $user2->method('getUID')->willReturn('uid2');
  55. $user3 = $this->createMock(IUser::class);
  56. $user3->method('getUID')->willReturn('uid3');
  57. $closure($user1);
  58. $closure($user2);
  59. $closure($user3);
  60. });
  61. $this->jobList->expects($this->exactly(3))
  62. ->method('add')
  63. ->withConsecutive(
  64. [GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid1']],
  65. [GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid2']],
  66. [GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid3']],
  67. );
  68. $response = $this->controller->enable();
  69. $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
  70. }
  71. public function testDisable(): void {
  72. $this->config->expects($this->once())
  73. ->method('setAppValue')
  74. ->with('dav', 'generateBirthdayCalendar', 'no');
  75. $this->jobList->expects($this->once())
  76. ->method('remove')
  77. ->with(GenerateBirthdayCalendarBackgroundJob::class);
  78. $this->caldav->expects($this->once())
  79. ->method('deleteAllBirthdayCalendars');
  80. $response = $this->controller->disable();
  81. $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
  82. }
  83. }