BirthdayCalendarControllerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author François Freitag <mail@franek.fr>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\DAV\Tests\Unit\DAV\Controller;
  29. use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
  30. use OCA\DAV\CalDAV\CalDavBackend;
  31. use OCA\DAV\Controller\BirthdayCalendarController;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\IConfig;
  34. use OCP\IDBConnection;
  35. use OCP\IRequest;
  36. use OCP\IUser;
  37. use OCP\IUserManager;
  38. use Test\TestCase;
  39. class BirthdayCalendarControllerTest extends TestCase {
  40. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  41. private $config;
  42. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  43. private $request;
  44. /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  45. private $db;
  46. /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
  47. private $jobList;
  48. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  49. private $userManager;
  50. /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject */
  51. private $caldav;
  52. /** @var BirthdayCalendarController|\PHPUnit\Framework\MockObject\MockObject */
  53. private $controller;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->config = $this->createMock(IConfig::class);
  57. $this->request = $this->createMock(IRequest::class);
  58. $this->db = $this->createMock(IDBConnection::class);
  59. $this->jobList = $this->createMock(IJobList::class);
  60. $this->userManager = $this->createMock(IUserManager::class);
  61. $this->caldav = $this->createMock(CalDavBackend::class);
  62. $this->controller = new BirthdayCalendarController('dav',
  63. $this->request, $this->db, $this->config, $this->jobList,
  64. $this->userManager, $this->caldav);
  65. }
  66. public function testEnable(): void {
  67. $this->config->expects($this->once())
  68. ->method('setAppValue')
  69. ->with('dav', 'generateBirthdayCalendar', 'yes');
  70. $this->userManager->expects($this->once())
  71. ->method('callForSeenUsers')
  72. ->willReturnCallback(function ($closure): void {
  73. $user1 = $this->createMock(IUser::class);
  74. $user1->method('getUID')->willReturn('uid1');
  75. $user2 = $this->createMock(IUser::class);
  76. $user2->method('getUID')->willReturn('uid2');
  77. $user3 = $this->createMock(IUser::class);
  78. $user3->method('getUID')->willReturn('uid3');
  79. $closure($user1);
  80. $closure($user2);
  81. $closure($user3);
  82. });
  83. $this->jobList->expects($this->exactly(3))
  84. ->method('add')
  85. ->withConsecutive(
  86. [GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid1']],
  87. [GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid2']],
  88. [GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid3']],
  89. );
  90. $response = $this->controller->enable();
  91. $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
  92. }
  93. public function testDisable(): void {
  94. $this->config->expects($this->once())
  95. ->method('setAppValue')
  96. ->with('dav', 'generateBirthdayCalendar', 'no');
  97. $this->jobList->expects($this->once())
  98. ->method('remove')
  99. ->with(GenerateBirthdayCalendarBackgroundJob::class);
  100. $this->caldav->expects($this->once())
  101. ->method('deleteAllBirthdayCalendars');
  102. $response = $this->controller->disable();
  103. $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
  104. }
  105. }