You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActivityUpdaterListenerTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Thomas Citharel <nextcloud@tcit.fr>
  5. *
  6. * @author Thomas Citharel <nextcloud@tcit.fr>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Tests\Unit\Listener;
  25. use OCA\DAV\CalDAV\Activity\Backend as ActivityBackend;
  26. use OCA\DAV\CalDAV\Activity\Provider\Event;
  27. use OCA\DAV\DAV\Sharing\Plugin as SharingPlugin;
  28. use OCA\DAV\Events\CalendarDeletedEvent;
  29. use OCA\DAV\Events\CalendarObjectDeletedEvent;
  30. use OCA\DAV\Listener\ActivityUpdaterListener;
  31. use PHPUnit\Framework\MockObject\MockObject;
  32. use Psr\Log\LoggerInterface;
  33. use Test\TestCase;
  34. class ActivityUpdaterListenerTest extends TestCase {
  35. /** @var ActivityBackend|MockObject */
  36. private $activityBackend;
  37. /** @var LoggerInterface|MockObject */
  38. private $logger;
  39. private ActivityUpdaterListener $listener;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->activityBackend = $this->createMock(ActivityBackend::class);
  43. $this->logger = $this->createMock(LoggerInterface::class);
  44. $this->listener = new ActivityUpdaterListener(
  45. $this->activityBackend,
  46. $this->logger
  47. );
  48. }
  49. /**
  50. * @dataProvider dataForTestHandleCalendarObjectDeletedEvent
  51. */
  52. public function testHandleCalendarObjectDeletedEvent(int $calendarId, array $calendarData, array $shares, array $objectData, bool $createsActivity): void {
  53. $event = new CalendarObjectDeletedEvent($calendarId, $calendarData, $shares, $objectData);
  54. $this->logger->expects($this->once())->method('debug')->with(
  55. $createsActivity ? "Activity generated for deleted calendar object in calendar $calendarId" : "Calendar object in calendar $calendarId was already in trashbin, skipping deletion activity"
  56. );
  57. $this->activityBackend->expects($createsActivity ? $this->once() : $this->never())->method('onTouchCalendarObject')->with(
  58. Event::SUBJECT_OBJECT_DELETE,
  59. $calendarData,
  60. $shares,
  61. $objectData
  62. );
  63. $this->listener->handle($event);
  64. }
  65. public function dataForTestHandleCalendarObjectDeletedEvent(): array {
  66. return [
  67. [1, [], [], [], true],
  68. [1, [], [], ['{' . SharingPlugin::NS_NEXTCLOUD . '}deleted-at' => 120], false],
  69. ];
  70. }
  71. /**
  72. * @dataProvider dataForTestHandleCalendarDeletedEvent
  73. */
  74. public function testHandleCalendarDeletedEvent(int $calendarId, array $calendarData, array $shares, bool $createsActivity): void {
  75. $event = new CalendarDeletedEvent($calendarId, $calendarData, $shares);
  76. $this->logger->expects($this->once())->method('debug')->with(
  77. $createsActivity ? "Activity generated for deleted calendar $calendarId" : "Calendar $calendarId was already in trashbin, skipping deletion activity"
  78. );
  79. $this->activityBackend->expects($createsActivity ? $this->once() : $this->never())->method('onCalendarDelete')->with(
  80. $calendarData,
  81. $shares
  82. );
  83. $this->listener->handle($event);
  84. }
  85. public function dataForTestHandleCalendarDeletedEvent(): array {
  86. return [
  87. [1, [], [], true],
  88. [1, ['{' . SharingPlugin::NS_NEXTCLOUD . '}deleted-at' => 120], [], false],
  89. ];
  90. }
  91. }