From bd2545c8c4a493afc56ebd3f2cca55d5c852de3e Mon Sep 17 00:00:00 2001
From: Robin Appelman <robin@icewind.nl>
Date: Thu, 9 Sep 2021 14:18:13 +0200
Subject: [PATCH] more event tests

Signed-off-by: Robin Appelman <robin@icewind.nl>
---
 tests/CoreEventsTest.php | 116 +++++++++++++++++++++++++++++++++++++++
 tests/ListenerTest.php   |  61 ++++++++++++++++++++
 2 files changed, 177 insertions(+)
 create mode 100644 tests/CoreEventsTest.php

diff --git a/tests/CoreEventsTest.php b/tests/CoreEventsTest.php
new file mode 100644
index 0000000..1f3e615
--- /dev/null
+++ b/tests/CoreEventsTest.php
@@ -0,0 +1,116 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\NotifyPush\Tests;
+
+use OC\Files\Storage\Temporary;
+use OCA\NotifyPush\AppInfo\Application;
+use OCA\NotifyPush\Listener;
+use OCA\NotifyPush\Queue\IQueue;
+use OCP\Activity\IManager as IActivityManager;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\IGroupManager;
+use OCP\IUserManager;
+use OCP\Notification\IManager as INotificationManager;
+use Test\TestCase;
+
+/**
+ * @group DB
+ */
+class CoreEventsTest extends TestCase {
+	private function getListener(array &$events) {
+		$queue = $this->createMock(IQueue::class);
+		$queue->method('push')->willReturnCallback(function ($channel, $event) use (&$events) {
+			if (!isset($events[$channel])) {
+				$events[$channel] = [];
+			}
+			$events[$channel][] = $event;
+		});
+		$listener = new Listener($queue);
+		$app = \OC::$server->get(Application::class);
+		$app->attachHooks(\OC::$server->get(IEventDispatcher::class), $listener, \OC::$server->get(IActivityManager::class), \OC::$server->get(INotificationManager::class));
+		return $listener;
+	}
+
+	public function testFilesystemEvents() {
+		$storage = new Temporary([]);
+		$cache = $storage->getCache();
+		$scanner = $storage->getScanner();
+
+		$storage->mkdir('foobar');
+		$scanner->scan('');
+
+		$events = [];
+		$this->getListener($events);
+
+		$storage->touch('foobar', 100);
+		$storage->getUpdater()->update('foobar');
+
+		$this->assertEquals([
+			'notify_storage_update' => [
+				['storage' => $cache->getNumericStorageId(), 'path' => 'foobar'],
+				['storage' => $cache->getNumericStorageId(), 'path' => 'foobar'],
+				['storage' => $cache->getNumericStorageId(), 'path' => ''],
+			],
+		], $events);
+	}
+
+	public function testGroupEvents() {
+		$userManager = \OC::$server->get(IUserManager::class);
+		$groupManager = \OC::$server->get(IGroupManager::class);
+		$uid = uniqid('user_');
+		$gid = uniqid('user_');
+
+		$groupManager->createGroup($gid);
+		$userManager->createUser($uid, 'a');
+		$group = $groupManager->get($gid);
+		$user = $userManager->get($uid);
+
+		$events = [];
+		$this->getListener($events);
+
+		$group->addUser($user);
+
+		$this->assertEquals([
+			'notify_group_membership_update' => [
+				['user' => $uid, 'group' => $gid],
+			],
+			'notify_activity' => [
+				['user' => $uid],
+			],
+		], $events);
+
+		$events = [];
+
+		$group->removeUser($user);
+
+		$this->assertEquals([
+			'notify_group_membership_update' => [
+				['user' => $uid, 'group' => $gid],
+			],
+			'notify_activity' => [
+				['user' => $uid],
+			],
+		], $events);
+	}
+}
diff --git a/tests/ListenerTest.php b/tests/ListenerTest.php
index ed25144..2c42d03 100644
--- a/tests/ListenerTest.php
+++ b/tests/ListenerTest.php
@@ -27,6 +27,12 @@ use OCA\NotifyPush\Listener;
 use OCA\NotifyPush\Queue\IQueue;
 use OCP\Files\Cache\CacheEntryInsertedEvent;
 use OCP\Files\Storage\IStorage;
+use OCP\Group\Events\UserAddedEvent;
+use OCP\Group\Events\UserRemovedEvent;
+use OCP\IGroup;
+use OCP\IUser;
+use OCP\Share\Events\ShareCreatedEvent;
+use OCP\Share\IShare;
 use Test\TestCase;
 
 class ListenerTest extends TestCase {
@@ -58,4 +64,59 @@ class ListenerTest extends TestCase {
 			],
 		], $events);
 	}
+
+	public function testGroupEvents() {
+		$events = [];
+		$queue = $this->getQueue($events);
+		$listener = new Listener($queue);
+
+		$user = $this->createMock(IUser::class);
+		$user->method('getUID')->willReturn('user1');
+
+		$group = $this->createMock(IGroup::class);
+		$group->method('getGID')->willReturn('group1');
+
+		$listener->groupListener(new UserAddedEvent(
+			$group,
+			$user
+		));
+		$this->assertEquals([
+			'notify_group_membership_update' => [
+				['user' => 'user1', 'group' => 'group1'],
+			],
+		], $events);
+
+		$events = [];
+
+		$listener->groupListener(new UserRemovedEvent(
+			$group,
+			$user
+		));
+		$this->assertEquals([
+			'notify_group_membership_update' => [
+				['user' => 'user1', 'group' => 'group1'],
+			],
+		], $events);
+	}
+
+	public function testShareEvents() {
+		$events = [];
+		$queue = $this->getQueue($events);
+		$listener = new Listener($queue);
+
+		$share = $this->createMock(IShare::class);
+		$share->method('getShareType')
+			->willReturn(IShare::TYPE_USER);
+		$share->method('getSharedWith')
+			->willReturn('user1');
+
+		$listener->shareListener(new ShareCreatedEvent(
+			$share
+		));
+		$this->assertEquals([
+			'notify_user_share_created' => [
+				['user' => 'user1'],
+			],
+		], $events);
+	}
 }
-- 
GitLab