Completed
Push — master ( 9d0462...594d22 )
by Joas
32:47 queued 11s
created
apps/comments/lib/Listener/CommentsEventListener.php 1 patch
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -18,45 +18,45 @@
 block discarded – undo
18 18
 
19 19
 /** @template-implements IEventListener<CommentsEvent|Event> */
20 20
 class CommentsEventListener implements IEventListener {
21
-	public function __construct(
22
-		private ActivityListener $activityListener,
23
-		private NotificationListener $notificationListener,
24
-	) {
25
-	}
26
-
27
-	public function handle(Event $event): void {
28
-		if (!$event instanceof CommentsEvent) {
29
-			return;
30
-		}
31
-
32
-		if ($event->getComment()->getObjectType() !== 'files') {
33
-			// this is a 'files'-specific Handler
34
-			return;
35
-		}
36
-
37
-		$eventType = $event->getEvent();
38
-		if ($eventType === CommentsEvent::EVENT_ADD) {
39
-			$this->notificationHandler($event);
40
-			$this->activityHandler($event);
41
-			return;
42
-		}
43
-
44
-		$applicableEvents = [
45
-			CommentsEvent::EVENT_PRE_UPDATE,
46
-			CommentsEvent::EVENT_UPDATE,
47
-			CommentsEvent::EVENT_DELETE,
48
-		];
49
-		if (in_array($eventType, $applicableEvents)) {
50
-			$this->notificationHandler($event);
51
-			return;
52
-		}
53
-	}
54
-
55
-	private function activityHandler(CommentsEvent $event): void {
56
-		$this->activityListener->commentEvent($event);
57
-	}
58
-
59
-	private function notificationHandler(CommentsEvent $event): void {
60
-		$this->notificationListener->evaluate($event);
61
-	}
21
+    public function __construct(
22
+        private ActivityListener $activityListener,
23
+        private NotificationListener $notificationListener,
24
+    ) {
25
+    }
26
+
27
+    public function handle(Event $event): void {
28
+        if (!$event instanceof CommentsEvent) {
29
+            return;
30
+        }
31
+
32
+        if ($event->getComment()->getObjectType() !== 'files') {
33
+            // this is a 'files'-specific Handler
34
+            return;
35
+        }
36
+
37
+        $eventType = $event->getEvent();
38
+        if ($eventType === CommentsEvent::EVENT_ADD) {
39
+            $this->notificationHandler($event);
40
+            $this->activityHandler($event);
41
+            return;
42
+        }
43
+
44
+        $applicableEvents = [
45
+            CommentsEvent::EVENT_PRE_UPDATE,
46
+            CommentsEvent::EVENT_UPDATE,
47
+            CommentsEvent::EVENT_DELETE,
48
+        ];
49
+        if (in_array($eventType, $applicableEvents)) {
50
+            $this->notificationHandler($event);
51
+            return;
52
+        }
53
+    }
54
+
55
+    private function activityHandler(CommentsEvent $event): void {
56
+        $this->activityListener->commentEvent($event);
57
+    }
58
+
59
+    private function notificationHandler(CommentsEvent $event): void {
60
+        $this->notificationListener->evaluate($event);
61
+    }
62 62
 }
Please login to merge, or discard this patch.
apps/comments/lib/Activity/Provider.php 1 patch
Indentation   +175 added lines, -175 removed lines patch added patch discarded remove patch
@@ -18,179 +18,179 @@
 block discarded – undo
18 18
 use OCP\L10N\IFactory;
19 19
 
20 20
 class Provider implements IProvider {
21
-	public function __construct(
22
-		protected IFactory $languageFactory,
23
-		protected IURLGenerator $url,
24
-		protected ICommentsManager $commentsManager,
25
-		protected IUserManager $userManager,
26
-		protected IManager $activityManager,
27
-	) {
28
-	}
29
-
30
-	/**
31
-	 * @param string $language
32
-	 * @param IEvent $event
33
-	 * @param IEvent|null $previousEvent
34
-	 * @return IEvent
35
-	 * @throws UnknownActivityException
36
-	 * @since 11.0.0
37
-	 */
38
-	public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
39
-		if ($event->getApp() !== 'comments') {
40
-			throw new UnknownActivityException();
41
-		}
42
-
43
-		if ($event->getSubject() === 'add_comment_subject') {
44
-			$l = $this->languageFactory->get('comments', $language);
45
-
46
-			$this->parseMessage($event);
47
-			if ($this->activityManager->getRequirePNG()) {
48
-				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png')));
49
-			} else {
50
-				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')));
51
-			}
52
-
53
-			if ($this->activityManager->isFormattingFilteredObject()) {
54
-				try {
55
-					return $this->parseShortVersion($event, $l);
56
-				} catch (UnknownActivityException) {
57
-					// Ignore and simply use the long version...
58
-				}
59
-			}
60
-
61
-			return $this->parseLongVersion($event, $l);
62
-		}
63
-		throw new UnknownActivityException();
64
-
65
-	}
66
-
67
-	/**
68
-	 * @throws UnknownActivityException
69
-	 */
70
-	protected function parseShortVersion(IEvent $event, IL10N $l): IEvent {
71
-		$subjectParameters = $this->getSubjectParameters($event);
72
-
73
-		if ($event->getSubject() === 'add_comment_subject') {
74
-			if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
75
-				$event->setRichSubject($l->t('You commented'), []);
76
-			} else {
77
-				$author = $this->generateUserParameter($subjectParameters['actor']);
78
-				$event->setRichSubject($l->t('{author} commented'), [
79
-					'author' => $author,
80
-				]);
81
-			}
82
-		} else {
83
-			throw new UnknownActivityException();
84
-		}
85
-
86
-		return $event;
87
-	}
88
-
89
-	/**
90
-	 * @throws UnknownActivityException
91
-	 */
92
-	protected function parseLongVersion(IEvent $event, IL10N $l): IEvent {
93
-		$subjectParameters = $this->getSubjectParameters($event);
94
-
95
-		if ($event->getSubject() === 'add_comment_subject') {
96
-			if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
97
-				$event->setParsedSubject($l->t('You commented on %1$s', [
98
-					$subjectParameters['filePath'],
99
-				]))
100
-					->setRichSubject($l->t('You commented on {file}'), [
101
-						'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
102
-					]);
103
-			} else {
104
-				$author = $this->generateUserParameter($subjectParameters['actor']);
105
-				$event->setParsedSubject($l->t('%1$s commented on %2$s', [
106
-					$author['name'],
107
-					$subjectParameters['filePath'],
108
-				]))
109
-					->setRichSubject($l->t('{author} commented on {file}'), [
110
-						'author' => $author,
111
-						'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
112
-					]);
113
-			}
114
-		} else {
115
-			throw new UnknownActivityException();
116
-		}
117
-
118
-		return $event;
119
-	}
120
-
121
-	protected function getSubjectParameters(IEvent $event): array {
122
-		$subjectParameters = $event->getSubjectParameters();
123
-		if (isset($subjectParameters['fileId'])) {
124
-			return $subjectParameters;
125
-		}
126
-
127
-		// Fix subjects from 12.0.3 and older
128
-		//
129
-		// Do NOT Remove unless necessary
130
-		// Removing this will break parsing of activities that were created on
131
-		// Nextcloud 12, so we should keep this as long as it's acceptable.
132
-		// Otherwise if people upgrade over multiple releases in a short period,
133
-		// they will get the dead entries in their stream.
134
-		return [
135
-			'actor' => $subjectParameters[0],
136
-			'fileId' => $event->getObjectId(),
137
-			'filePath' => trim($subjectParameters[1], '/'),
138
-		];
139
-	}
140
-
141
-	protected function parseMessage(IEvent $event): void {
142
-		$messageParameters = $event->getMessageParameters();
143
-		if (empty($messageParameters)) {
144
-			// Email
145
-			return;
146
-		}
147
-
148
-		$commentId = $messageParameters['commentId'] ?? $messageParameters[0];
149
-
150
-		try {
151
-			$comment = $this->commentsManager->get((string)$commentId);
152
-			$message = $comment->getMessage();
153
-
154
-			$mentionCount = 1;
155
-			$mentions = [];
156
-			foreach ($comment->getMentions() as $mention) {
157
-				if ($mention['type'] !== 'user') {
158
-					continue;
159
-				}
160
-
161
-				$message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message);
162
-				if (!str_contains($mention['id'], ' ') && !str_starts_with($mention['id'], 'guest/')) {
163
-					$message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message);
164
-				}
165
-
166
-				$mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']);
167
-				$mentionCount++;
168
-			}
169
-
170
-			$event->setParsedMessage($comment->getMessage())
171
-				->setRichMessage($message, $mentions);
172
-		} catch (NotFoundException $e) {
173
-		}
174
-	}
175
-
176
-	/**
177
-	 * @return array<string, string>
178
-	 */
179
-	protected function generateFileParameter(int $id, string $path): array {
180
-		return [
181
-			'type' => 'file',
182
-			'id' => (string)$id,
183
-			'name' => basename($path),
184
-			'path' => $path,
185
-			'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
186
-		];
187
-	}
188
-
189
-	protected function generateUserParameter(string $uid): array {
190
-		return [
191
-			'type' => 'user',
192
-			'id' => $uid,
193
-			'name' => $this->userManager->getDisplayName($uid) ?? $uid,
194
-		];
195
-	}
21
+    public function __construct(
22
+        protected IFactory $languageFactory,
23
+        protected IURLGenerator $url,
24
+        protected ICommentsManager $commentsManager,
25
+        protected IUserManager $userManager,
26
+        protected IManager $activityManager,
27
+    ) {
28
+    }
29
+
30
+    /**
31
+     * @param string $language
32
+     * @param IEvent $event
33
+     * @param IEvent|null $previousEvent
34
+     * @return IEvent
35
+     * @throws UnknownActivityException
36
+     * @since 11.0.0
37
+     */
38
+    public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
39
+        if ($event->getApp() !== 'comments') {
40
+            throw new UnknownActivityException();
41
+        }
42
+
43
+        if ($event->getSubject() === 'add_comment_subject') {
44
+            $l = $this->languageFactory->get('comments', $language);
45
+
46
+            $this->parseMessage($event);
47
+            if ($this->activityManager->getRequirePNG()) {
48
+                $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png')));
49
+            } else {
50
+                $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')));
51
+            }
52
+
53
+            if ($this->activityManager->isFormattingFilteredObject()) {
54
+                try {
55
+                    return $this->parseShortVersion($event, $l);
56
+                } catch (UnknownActivityException) {
57
+                    // Ignore and simply use the long version...
58
+                }
59
+            }
60
+
61
+            return $this->parseLongVersion($event, $l);
62
+        }
63
+        throw new UnknownActivityException();
64
+
65
+    }
66
+
67
+    /**
68
+     * @throws UnknownActivityException
69
+     */
70
+    protected function parseShortVersion(IEvent $event, IL10N $l): IEvent {
71
+        $subjectParameters = $this->getSubjectParameters($event);
72
+
73
+        if ($event->getSubject() === 'add_comment_subject') {
74
+            if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
75
+                $event->setRichSubject($l->t('You commented'), []);
76
+            } else {
77
+                $author = $this->generateUserParameter($subjectParameters['actor']);
78
+                $event->setRichSubject($l->t('{author} commented'), [
79
+                    'author' => $author,
80
+                ]);
81
+            }
82
+        } else {
83
+            throw new UnknownActivityException();
84
+        }
85
+
86
+        return $event;
87
+    }
88
+
89
+    /**
90
+     * @throws UnknownActivityException
91
+     */
92
+    protected function parseLongVersion(IEvent $event, IL10N $l): IEvent {
93
+        $subjectParameters = $this->getSubjectParameters($event);
94
+
95
+        if ($event->getSubject() === 'add_comment_subject') {
96
+            if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
97
+                $event->setParsedSubject($l->t('You commented on %1$s', [
98
+                    $subjectParameters['filePath'],
99
+                ]))
100
+                    ->setRichSubject($l->t('You commented on {file}'), [
101
+                        'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
102
+                    ]);
103
+            } else {
104
+                $author = $this->generateUserParameter($subjectParameters['actor']);
105
+                $event->setParsedSubject($l->t('%1$s commented on %2$s', [
106
+                    $author['name'],
107
+                    $subjectParameters['filePath'],
108
+                ]))
109
+                    ->setRichSubject($l->t('{author} commented on {file}'), [
110
+                        'author' => $author,
111
+                        'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
112
+                    ]);
113
+            }
114
+        } else {
115
+            throw new UnknownActivityException();
116
+        }
117
+
118
+        return $event;
119
+    }
120
+
121
+    protected function getSubjectParameters(IEvent $event): array {
122
+        $subjectParameters = $event->getSubjectParameters();
123
+        if (isset($subjectParameters['fileId'])) {
124
+            return $subjectParameters;
125
+        }
126
+
127
+        // Fix subjects from 12.0.3 and older
128
+        //
129
+        // Do NOT Remove unless necessary
130
+        // Removing this will break parsing of activities that were created on
131
+        // Nextcloud 12, so we should keep this as long as it's acceptable.
132
+        // Otherwise if people upgrade over multiple releases in a short period,
133
+        // they will get the dead entries in their stream.
134
+        return [
135
+            'actor' => $subjectParameters[0],
136
+            'fileId' => $event->getObjectId(),
137
+            'filePath' => trim($subjectParameters[1], '/'),
138
+        ];
139
+    }
140
+
141
+    protected function parseMessage(IEvent $event): void {
142
+        $messageParameters = $event->getMessageParameters();
143
+        if (empty($messageParameters)) {
144
+            // Email
145
+            return;
146
+        }
147
+
148
+        $commentId = $messageParameters['commentId'] ?? $messageParameters[0];
149
+
150
+        try {
151
+            $comment = $this->commentsManager->get((string)$commentId);
152
+            $message = $comment->getMessage();
153
+
154
+            $mentionCount = 1;
155
+            $mentions = [];
156
+            foreach ($comment->getMentions() as $mention) {
157
+                if ($mention['type'] !== 'user') {
158
+                    continue;
159
+                }
160
+
161
+                $message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message);
162
+                if (!str_contains($mention['id'], ' ') && !str_starts_with($mention['id'], 'guest/')) {
163
+                    $message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message);
164
+                }
165
+
166
+                $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']);
167
+                $mentionCount++;
168
+            }
169
+
170
+            $event->setParsedMessage($comment->getMessage())
171
+                ->setRichMessage($message, $mentions);
172
+        } catch (NotFoundException $e) {
173
+        }
174
+    }
175
+
176
+    /**
177
+     * @return array<string, string>
178
+     */
179
+    protected function generateFileParameter(int $id, string $path): array {
180
+        return [
181
+            'type' => 'file',
182
+            'id' => (string)$id,
183
+            'name' => basename($path),
184
+            'path' => $path,
185
+            'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
186
+        ];
187
+    }
188
+
189
+    protected function generateUserParameter(string $uid): array {
190
+        return [
191
+            'type' => 'user',
192
+            'id' => $uid,
193
+            'name' => $this->userManager->getDisplayName($uid) ?? $uid,
194
+        ];
195
+    }
196 196
 }
Please login to merge, or discard this patch.
apps/comments/lib/Activity/Setting.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -10,44 +10,44 @@
 block discarded – undo
10 10
 use OCP\IL10N;
11 11
 
12 12
 class Setting extends ActivitySettings {
13
-	public function __construct(
14
-		protected readonly IL10N $l,
15
-	) {
16
-	}
17
-
18
-	public function getIdentifier(): string {
19
-		return 'comments';
20
-	}
21
-
22
-	public function getName(): string {
23
-		return $this->l->t('<strong>Comments</strong> for files');
24
-	}
25
-
26
-	public function getGroupIdentifier(): string {
27
-		return 'files';
28
-	}
29
-
30
-	public function getGroupName(): string {
31
-		return $this->l->t('Files');
32
-	}
33
-
34
-	public function getPriority(): int {
35
-		return 50;
36
-	}
37
-
38
-	public function canChangeStream(): bool {
39
-		return true;
40
-	}
41
-
42
-	public function isDefaultEnabledStream(): bool {
43
-		return true;
44
-	}
45
-
46
-	public function canChangeMail(): bool {
47
-		return true;
48
-	}
49
-
50
-	public function isDefaultEnabledMail(): bool {
51
-		return false;
52
-	}
13
+    public function __construct(
14
+        protected readonly IL10N $l,
15
+    ) {
16
+    }
17
+
18
+    public function getIdentifier(): string {
19
+        return 'comments';
20
+    }
21
+
22
+    public function getName(): string {
23
+        return $this->l->t('<strong>Comments</strong> for files');
24
+    }
25
+
26
+    public function getGroupIdentifier(): string {
27
+        return 'files';
28
+    }
29
+
30
+    public function getGroupName(): string {
31
+        return $this->l->t('Files');
32
+    }
33
+
34
+    public function getPriority(): int {
35
+        return 50;
36
+    }
37
+
38
+    public function canChangeStream(): bool {
39
+        return true;
40
+    }
41
+
42
+    public function isDefaultEnabledStream(): bool {
43
+        return true;
44
+    }
45
+
46
+    public function canChangeMail(): bool {
47
+        return true;
48
+    }
49
+
50
+    public function isDefaultEnabledMail(): bool {
51
+        return false;
52
+    }
53 53
 }
Please login to merge, or discard this patch.
apps/comments/tests/Unit/Notification/ListenerTest.php 1 patch
Indentation   +163 added lines, -163 removed lines patch added patch discarded remove patch
@@ -21,167 +21,167 @@
 block discarded – undo
21 21
 use Test\TestCase;
22 22
 
23 23
 class ListenerTest extends TestCase {
24
-	protected IManager&MockObject $notificationManager;
25
-	protected IUserManager&MockObject $userManager;
26
-	protected IURLGenerator&MockObject $urlGenerator;
27
-	protected Listener $listener;
28
-
29
-	protected function setUp(): void {
30
-		parent::setUp();
31
-
32
-		$this->notificationManager = $this->createMock(IManager::class);
33
-		$this->userManager = $this->createMock(IUserManager::class);
34
-
35
-		$this->listener = new Listener(
36
-			$this->notificationManager,
37
-			$this->userManager
38
-		);
39
-	}
40
-
41
-	public static function eventProvider(): array {
42
-		return [
43
-			['add', 'notify'],
44
-			['update', 'notify'],
45
-			['pre_update', 'markProcessed'],
46
-			['delete', 'markProcessed']
47
-		];
48
-	}
49
-
50
-	/**
51
-	 * @param string $eventType
52
-	 * @param string $notificationMethod
53
-	 */
54
-	#[\PHPUnit\Framework\Attributes\DataProvider('eventProvider')]
55
-	public function testEvaluate(string $eventType, $notificationMethod): void {
56
-		/** @var IComment|MockObject $comment */
57
-		$comment = $this->createMock(IComment::class);
58
-		$comment->expects($this->any())
59
-			->method('getObjectType')
60
-			->willReturn('files');
61
-		$comment->expects($this->any())
62
-			->method('getCreationDateTime')
63
-			->willReturn(new \DateTime());
64
-		$comment->expects($this->once())
65
-			->method('getMentions')
66
-			->willReturn([
67
-				[ 'type' => 'user', 'id' => 'foobar'],
68
-				[ 'type' => 'user', 'id' => 'barfoo'],
69
-				[ 'type' => 'user', 'id' => '[email protected]'],
70
-				[ 'type' => 'user', 'id' => '[email protected]@foobar.io'],
71
-				[ 'type' => 'user', 'id' => '23452-4333-54353-2342'],
72
-				[ 'type' => 'user', 'id' => 'yolo'],
73
-			]);
74
-		$comment->expects($this->atLeastOnce())
75
-			->method('getId')
76
-			->willReturn('1234');
77
-
78
-		$event = match ($eventType) {
79
-			'add' => new CommentAddedEvent($comment),
80
-			'pre_update' => new BeforeCommentUpdatedEvent($comment),
81
-			'update' => new CommentUpdatedEvent($comment),
82
-			'delete' => new CommentDeletedEvent($comment),
83
-		};
84
-
85
-		/** @var INotification|MockObject $notification */
86
-		$notification = $this->createMock(INotification::class);
87
-		$notification->expects($this->any())
88
-			->method($this->anything())
89
-			->willReturn($notification);
90
-		$notification->expects($this->exactly(6))
91
-			->method('setUser');
92
-
93
-		$this->notificationManager->expects($this->once())
94
-			->method('createNotification')
95
-			->willReturn($notification);
96
-		$this->notificationManager->expects($this->exactly(6))
97
-			->method($notificationMethod)
98
-			->with($this->isInstanceOf('\OCP\Notification\INotification'));
99
-
100
-		$this->userManager->expects($this->exactly(6))
101
-			->method('userExists')
102
-			->willReturnMap([
103
-				['foobar', true],
104
-				['barfoo', true],
105
-				['[email protected]', true],
106
-				['[email protected]@foobar.io', true],
107
-				['23452-4333-54353-2342', true],
108
-				['yolo', true]
109
-			]);
110
-
111
-		$this->listener->evaluate($event);
112
-	}
113
-
114
-	#[\PHPUnit\Framework\Attributes\DataProvider('eventProvider')]
115
-	public function testEvaluateNoMentions(string $eventType): void {
116
-		/** @var IComment|MockObject $comment */
117
-		$comment = $this->createMock(IComment::class);
118
-		$comment->expects($this->any())
119
-			->method('getObjectType')
120
-			->willReturn('files');
121
-		$comment->expects($this->any())
122
-			->method('getCreationDateTime')
123
-			->willReturn(new \DateTime());
124
-		$comment->expects($this->once())
125
-			->method('getMentions')
126
-			->willReturn([]);
127
-
128
-		$event = match ($eventType) {
129
-			'add' => new CommentAddedEvent($comment),
130
-			'pre_update' => new BeforeCommentUpdatedEvent($comment),
131
-			'update' => new CommentUpdatedEvent($comment),
132
-			'delete' => new CommentDeletedEvent($comment),
133
-		};
134
-
135
-		$this->notificationManager->expects($this->never())
136
-			->method('createNotification');
137
-		$this->notificationManager->expects($this->never())
138
-			->method('notify');
139
-		$this->notificationManager->expects($this->never())
140
-			->method('markProcessed');
141
-
142
-		$this->userManager->expects($this->never())
143
-			->method('userExists');
144
-
145
-		$this->listener->evaluate($event);
146
-	}
147
-
148
-	public function testEvaluateUserDoesNotExist(): void {
149
-		/** @var IComment|MockObject $comment */
150
-		$comment = $this->createMock(IComment::class);
151
-		$comment->expects($this->any())
152
-			->method('getObjectType')
153
-			->willReturn('files');
154
-		$comment->expects($this->any())
155
-			->method('getCreationDateTime')
156
-			->willReturn(new \DateTime());
157
-		$comment->expects($this->once())
158
-			->method('getMentions')
159
-			->willReturn([[ 'type' => 'user', 'id' => 'foobar']]);
160
-		$comment->expects($this->atLeastOnce())
161
-			->method('getId')
162
-			->willReturn('1234');
163
-
164
-		$event = new CommentAddedEvent($comment);
165
-
166
-		/** @var INotification|MockObject $notification */
167
-		$notification = $this->createMock(INotification::class);
168
-		$notification->expects($this->any())
169
-			->method($this->anything())
170
-			->willReturn($notification);
171
-		$notification->expects($this->never())
172
-			->method('setUser');
173
-
174
-		$this->notificationManager->expects($this->once())
175
-			->method('createNotification')
176
-			->willReturn($notification);
177
-		$this->notificationManager->expects($this->never())
178
-			->method('notify');
179
-
180
-		$this->userManager->expects($this->once())
181
-			->method('userExists')
182
-			->with('foobar')
183
-			->willReturn(false);
184
-
185
-		$this->listener->evaluate($event);
186
-	}
24
+    protected IManager&MockObject $notificationManager;
25
+    protected IUserManager&MockObject $userManager;
26
+    protected IURLGenerator&MockObject $urlGenerator;
27
+    protected Listener $listener;
28
+
29
+    protected function setUp(): void {
30
+        parent::setUp();
31
+
32
+        $this->notificationManager = $this->createMock(IManager::class);
33
+        $this->userManager = $this->createMock(IUserManager::class);
34
+
35
+        $this->listener = new Listener(
36
+            $this->notificationManager,
37
+            $this->userManager
38
+        );
39
+    }
40
+
41
+    public static function eventProvider(): array {
42
+        return [
43
+            ['add', 'notify'],
44
+            ['update', 'notify'],
45
+            ['pre_update', 'markProcessed'],
46
+            ['delete', 'markProcessed']
47
+        ];
48
+    }
49
+
50
+    /**
51
+     * @param string $eventType
52
+     * @param string $notificationMethod
53
+     */
54
+    #[\PHPUnit\Framework\Attributes\DataProvider('eventProvider')]
55
+    public function testEvaluate(string $eventType, $notificationMethod): void {
56
+        /** @var IComment|MockObject $comment */
57
+        $comment = $this->createMock(IComment::class);
58
+        $comment->expects($this->any())
59
+            ->method('getObjectType')
60
+            ->willReturn('files');
61
+        $comment->expects($this->any())
62
+            ->method('getCreationDateTime')
63
+            ->willReturn(new \DateTime());
64
+        $comment->expects($this->once())
65
+            ->method('getMentions')
66
+            ->willReturn([
67
+                [ 'type' => 'user', 'id' => 'foobar'],
68
+                [ 'type' => 'user', 'id' => 'barfoo'],
69
+                [ 'type' => 'user', 'id' => '[email protected]'],
70
+                [ 'type' => 'user', 'id' => '[email protected]@foobar.io'],
71
+                [ 'type' => 'user', 'id' => '23452-4333-54353-2342'],
72
+                [ 'type' => 'user', 'id' => 'yolo'],
73
+            ]);
74
+        $comment->expects($this->atLeastOnce())
75
+            ->method('getId')
76
+            ->willReturn('1234');
77
+
78
+        $event = match ($eventType) {
79
+            'add' => new CommentAddedEvent($comment),
80
+            'pre_update' => new BeforeCommentUpdatedEvent($comment),
81
+            'update' => new CommentUpdatedEvent($comment),
82
+            'delete' => new CommentDeletedEvent($comment),
83
+        };
84
+
85
+        /** @var INotification|MockObject $notification */
86
+        $notification = $this->createMock(INotification::class);
87
+        $notification->expects($this->any())
88
+            ->method($this->anything())
89
+            ->willReturn($notification);
90
+        $notification->expects($this->exactly(6))
91
+            ->method('setUser');
92
+
93
+        $this->notificationManager->expects($this->once())
94
+            ->method('createNotification')
95
+            ->willReturn($notification);
96
+        $this->notificationManager->expects($this->exactly(6))
97
+            ->method($notificationMethod)
98
+            ->with($this->isInstanceOf('\OCP\Notification\INotification'));
99
+
100
+        $this->userManager->expects($this->exactly(6))
101
+            ->method('userExists')
102
+            ->willReturnMap([
103
+                ['foobar', true],
104
+                ['barfoo', true],
105
+                ['[email protected]', true],
106
+                ['[email protected]@foobar.io', true],
107
+                ['23452-4333-54353-2342', true],
108
+                ['yolo', true]
109
+            ]);
110
+
111
+        $this->listener->evaluate($event);
112
+    }
113
+
114
+    #[\PHPUnit\Framework\Attributes\DataProvider('eventProvider')]
115
+    public function testEvaluateNoMentions(string $eventType): void {
116
+        /** @var IComment|MockObject $comment */
117
+        $comment = $this->createMock(IComment::class);
118
+        $comment->expects($this->any())
119
+            ->method('getObjectType')
120
+            ->willReturn('files');
121
+        $comment->expects($this->any())
122
+            ->method('getCreationDateTime')
123
+            ->willReturn(new \DateTime());
124
+        $comment->expects($this->once())
125
+            ->method('getMentions')
126
+            ->willReturn([]);
127
+
128
+        $event = match ($eventType) {
129
+            'add' => new CommentAddedEvent($comment),
130
+            'pre_update' => new BeforeCommentUpdatedEvent($comment),
131
+            'update' => new CommentUpdatedEvent($comment),
132
+            'delete' => new CommentDeletedEvent($comment),
133
+        };
134
+
135
+        $this->notificationManager->expects($this->never())
136
+            ->method('createNotification');
137
+        $this->notificationManager->expects($this->never())
138
+            ->method('notify');
139
+        $this->notificationManager->expects($this->never())
140
+            ->method('markProcessed');
141
+
142
+        $this->userManager->expects($this->never())
143
+            ->method('userExists');
144
+
145
+        $this->listener->evaluate($event);
146
+    }
147
+
148
+    public function testEvaluateUserDoesNotExist(): void {
149
+        /** @var IComment|MockObject $comment */
150
+        $comment = $this->createMock(IComment::class);
151
+        $comment->expects($this->any())
152
+            ->method('getObjectType')
153
+            ->willReturn('files');
154
+        $comment->expects($this->any())
155
+            ->method('getCreationDateTime')
156
+            ->willReturn(new \DateTime());
157
+        $comment->expects($this->once())
158
+            ->method('getMentions')
159
+            ->willReturn([[ 'type' => 'user', 'id' => 'foobar']]);
160
+        $comment->expects($this->atLeastOnce())
161
+            ->method('getId')
162
+            ->willReturn('1234');
163
+
164
+        $event = new CommentAddedEvent($comment);
165
+
166
+        /** @var INotification|MockObject $notification */
167
+        $notification = $this->createMock(INotification::class);
168
+        $notification->expects($this->any())
169
+            ->method($this->anything())
170
+            ->willReturn($notification);
171
+        $notification->expects($this->never())
172
+            ->method('setUser');
173
+
174
+        $this->notificationManager->expects($this->once())
175
+            ->method('createNotification')
176
+            ->willReturn($notification);
177
+        $this->notificationManager->expects($this->never())
178
+            ->method('notify');
179
+
180
+        $this->userManager->expects($this->once())
181
+            ->method('userExists')
182
+            ->with('foobar')
183
+            ->willReturn(false);
184
+
185
+        $this->listener->evaluate($event);
186
+    }
187 187
 }
Please login to merge, or discard this patch.
apps/comments/tests/Unit/EventHandlerTest.php 1 patch
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -21,69 +21,69 @@
 block discarded – undo
21 21
 use Test\TestCase;
22 22
 
23 23
 class EventHandlerTest extends TestCase {
24
-	protected ActivityListener&MockObject $activityListener;
25
-	protected NotificationListener&MockObject $notificationListener;
26
-	protected CommentsEventListener $eventHandler;
27
-
28
-	protected function setUp(): void {
29
-		parent::setUp();
30
-
31
-		$this->activityListener = $this->createMock(ActivityListener::class);
32
-		$this->notificationListener = $this->createMock(NotificationListener::class);
33
-
34
-		$this->eventHandler = new CommentsEventListener($this->activityListener, $this->notificationListener);
35
-	}
36
-
37
-	public function testNotFiles(): void {
38
-		/** @var IComment|MockObject $comment */
39
-		$comment = $this->createMock(IComment::class);
40
-		$comment->expects($this->once())
41
-			->method('getObjectType')
42
-			->willReturn('smiles');
43
-
44
-		/** @var CommentsEvent|MockObject $event */
45
-		$event = $this->createMock(CommentsEvent::class);
46
-		$event->expects($this->once())
47
-			->method('getComment')
48
-			->willReturn($comment);
49
-		$event->expects($this->never())
50
-			->method('getEvent');
51
-
52
-		$this->eventHandler->handle($event);
53
-	}
54
-
55
-	public static function handledProvider(): array {
56
-		return [
57
-			['delete'],
58
-			['update'],
59
-			['pre_update'],
60
-			['add']
61
-		];
62
-	}
63
-
64
-	#[\PHPUnit\Framework\Attributes\DataProvider('handledProvider')]
65
-	public function testHandled(string $eventType): void {
66
-		/** @var IComment|MockObject $comment */
67
-		$comment = $this->createMock(IComment::class);
68
-		$comment->expects($this->once())
69
-			->method('getObjectType')
70
-			->willReturn('files');
71
-
72
-		$event = match ($eventType) {
73
-			'add' => new CommentAddedEvent($comment),
74
-			'pre_update' => new BeforeCommentUpdatedEvent($comment),
75
-			'update' => new CommentUpdatedEvent($comment),
76
-			'delete' => new CommentDeletedEvent($comment),
77
-		};
78
-
79
-		$this->notificationListener->expects($this->once())
80
-			->method('evaluate')
81
-			->with($event);
82
-
83
-		$this->activityListener->expects($this->any())
84
-			->method('commentEvent')
85
-			->with($event);
86
-
87
-		$this->eventHandler->handle($event);
88
-	}
24
+    protected ActivityListener&MockObject $activityListener;
25
+    protected NotificationListener&MockObject $notificationListener;
26
+    protected CommentsEventListener $eventHandler;
27
+
28
+    protected function setUp(): void {
29
+        parent::setUp();
30
+
31
+        $this->activityListener = $this->createMock(ActivityListener::class);
32
+        $this->notificationListener = $this->createMock(NotificationListener::class);
33
+
34
+        $this->eventHandler = new CommentsEventListener($this->activityListener, $this->notificationListener);
35
+    }
36
+
37
+    public function testNotFiles(): void {
38
+        /** @var IComment|MockObject $comment */
39
+        $comment = $this->createMock(IComment::class);
40
+        $comment->expects($this->once())
41
+            ->method('getObjectType')
42
+            ->willReturn('smiles');
43
+
44
+        /** @var CommentsEvent|MockObject $event */
45
+        $event = $this->createMock(CommentsEvent::class);
46
+        $event->expects($this->once())
47
+            ->method('getComment')
48
+            ->willReturn($comment);
49
+        $event->expects($this->never())
50
+            ->method('getEvent');
51
+
52
+        $this->eventHandler->handle($event);
53
+    }
54
+
55
+    public static function handledProvider(): array {
56
+        return [
57
+            ['delete'],
58
+            ['update'],
59
+            ['pre_update'],
60
+            ['add']
61
+        ];
62
+    }
63
+
64
+    #[\PHPUnit\Framework\Attributes\DataProvider('handledProvider')]
65
+    public function testHandled(string $eventType): void {
66
+        /** @var IComment|MockObject $comment */
67
+        $comment = $this->createMock(IComment::class);
68
+        $comment->expects($this->once())
69
+            ->method('getObjectType')
70
+            ->willReturn('files');
71
+
72
+        $event = match ($eventType) {
73
+            'add' => new CommentAddedEvent($comment),
74
+            'pre_update' => new BeforeCommentUpdatedEvent($comment),
75
+            'update' => new CommentUpdatedEvent($comment),
76
+            'delete' => new CommentDeletedEvent($comment),
77
+        };
78
+
79
+        $this->notificationListener->expects($this->once())
80
+            ->method('evaluate')
81
+            ->with($event);
82
+
83
+        $this->activityListener->expects($this->any())
84
+            ->method('commentEvent')
85
+            ->with($event);
86
+
87
+        $this->eventHandler->handle($event);
88
+    }
89 89
 }
Please login to merge, or discard this patch.
apps/comments/tests/Unit/Activity/ListenerTest.php 1 patch
Indentation   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -27,126 +27,126 @@
 block discarded – undo
27 27
 use Test\TestCase;
28 28
 
29 29
 class ListenerTest extends TestCase {
30
-	protected IManager&MockObject $activityManager;
31
-	protected IUserSession&MockObject $session;
32
-	protected IAppManager&MockObject $appManager;
33
-	protected IMountProviderCollection&MockObject $mountProviderCollection;
34
-	protected IRootFolder&MockObject $rootFolder;
35
-	protected IShareHelper&MockObject $shareHelper;
36
-	protected Listener $listener;
37
-
38
-	protected function setUp(): void {
39
-		parent::setUp();
40
-
41
-		$this->activityManager = $this->createMock(IManager::class);
42
-		$this->session = $this->createMock(IUserSession::class);
43
-		$this->appManager = $this->createMock(IAppManager::class);
44
-		$this->mountProviderCollection = $this->createMock(IMountProviderCollection::class);
45
-		$this->rootFolder = $this->createMock(IRootFolder::class);
46
-		$this->shareHelper = $this->createMock(IShareHelper::class);
47
-
48
-		$this->listener = new Listener(
49
-			$this->activityManager,
50
-			$this->session,
51
-			$this->appManager,
52
-			$this->mountProviderCollection,
53
-			$this->rootFolder,
54
-			$this->shareHelper
55
-		);
56
-	}
57
-
58
-	public function testCommentEvent(): void {
59
-		$this->appManager->expects($this->any())
60
-			->method('isEnabledForAnyone')
61
-			->with('activity')
62
-			->willReturn(true);
63
-
64
-		$comment = $this->createMock(IComment::class);
65
-		$comment->expects($this->any())
66
-			->method('getObjectType')
67
-			->willReturn('files');
68
-
69
-		$event = new CommentAddedEvent($comment);
70
-
71
-		/** @var IUser|MockObject $ownerUser */
72
-		$ownerUser = $this->createMock(IUser::class);
73
-		$ownerUser->expects($this->any())
74
-			->method('getUID')
75
-			->willReturn('937393');
76
-
77
-		/** @var MockObject $mount */
78
-		$mount = $this->createMock(ICachedMountFileInfo::class);
79
-		$mount->expects($this->any())
80
-			->method('getUser')
81
-			->willReturn($ownerUser); // perhaps not the right user, but does not matter in this scenario
82
-
83
-		$mounts = [ $mount, $mount ]; // to make sure duplicates are dealt with
84
-
85
-		$userMountCache = $this->createMock(IUserMountCache::class);
86
-		$userMountCache->expects($this->any())
87
-			->method('getMountsForFileId')
88
-			->willReturn($mounts);
89
-
90
-		$this->mountProviderCollection->expects($this->any())
91
-			->method('getMountCache')
92
-			->willReturn($userMountCache);
93
-
94
-		$node = $this->createMock(Node::class);
95
-		$nodes = [ $node ];
96
-
97
-		$ownerFolder = $this->createMock(Folder::class);
98
-		$ownerFolder->expects($this->any())
99
-			->method('getById')
100
-			->willReturn($nodes);
101
-
102
-		$this->rootFolder->expects($this->any())
103
-			->method('getUserFolder')
104
-			->willReturn($ownerFolder);
105
-
106
-		$al = [ 'users' => [
107
-			'873304' => 'i/got/it/here',
108
-			'254342' => 'there/i/have/it',
109
-			'sandra' => 'and/here/i/placed/it'
110
-		]];
111
-		$this->shareHelper->expects($this->any())
112
-			->method('getPathsForAccessList')
113
-			->willReturn($al);
114
-
115
-		$this->session->expects($this->any())
116
-			->method('getUser')
117
-			->willReturn($ownerUser);
118
-
119
-		/** @var MockObject $activity */
120
-		$activity = $this->createMock(IEvent::class);
121
-		$activity->expects($this->exactly(count($al['users'])))
122
-			->method('setAffectedUser');
123
-		$activity->expects($this->once())
124
-			->method('setApp')
125
-			->with('comments')
126
-			->willReturnSelf();
127
-		$activity->expects($this->once())
128
-			->method('setType')
129
-			->with('comments')
130
-			->willReturnSelf();
131
-		$activity->expects($this->once())
132
-			->method('setAuthor')
133
-			->with($ownerUser->getUID())
134
-			->willReturnSelf();
135
-		$activity->expects($this->once())
136
-			->method('setObject')
137
-			->with('files', $this->anything())
138
-			->willReturnSelf();
139
-		$activity->expects($this->once())
140
-			->method('setMessage')
141
-			->with('add_comment_message', $this->anything())
142
-			->willReturnSelf();
143
-
144
-		$this->activityManager->expects($this->once())
145
-			->method('generateEvent')
146
-			->willReturn($activity);
147
-		$this->activityManager->expects($this->exactly(count($al['users'])))
148
-			->method('publish');
149
-
150
-		$this->listener->commentEvent($event);
151
-	}
30
+    protected IManager&MockObject $activityManager;
31
+    protected IUserSession&MockObject $session;
32
+    protected IAppManager&MockObject $appManager;
33
+    protected IMountProviderCollection&MockObject $mountProviderCollection;
34
+    protected IRootFolder&MockObject $rootFolder;
35
+    protected IShareHelper&MockObject $shareHelper;
36
+    protected Listener $listener;
37
+
38
+    protected function setUp(): void {
39
+        parent::setUp();
40
+
41
+        $this->activityManager = $this->createMock(IManager::class);
42
+        $this->session = $this->createMock(IUserSession::class);
43
+        $this->appManager = $this->createMock(IAppManager::class);
44
+        $this->mountProviderCollection = $this->createMock(IMountProviderCollection::class);
45
+        $this->rootFolder = $this->createMock(IRootFolder::class);
46
+        $this->shareHelper = $this->createMock(IShareHelper::class);
47
+
48
+        $this->listener = new Listener(
49
+            $this->activityManager,
50
+            $this->session,
51
+            $this->appManager,
52
+            $this->mountProviderCollection,
53
+            $this->rootFolder,
54
+            $this->shareHelper
55
+        );
56
+    }
57
+
58
+    public function testCommentEvent(): void {
59
+        $this->appManager->expects($this->any())
60
+            ->method('isEnabledForAnyone')
61
+            ->with('activity')
62
+            ->willReturn(true);
63
+
64
+        $comment = $this->createMock(IComment::class);
65
+        $comment->expects($this->any())
66
+            ->method('getObjectType')
67
+            ->willReturn('files');
68
+
69
+        $event = new CommentAddedEvent($comment);
70
+
71
+        /** @var IUser|MockObject $ownerUser */
72
+        $ownerUser = $this->createMock(IUser::class);
73
+        $ownerUser->expects($this->any())
74
+            ->method('getUID')
75
+            ->willReturn('937393');
76
+
77
+        /** @var MockObject $mount */
78
+        $mount = $this->createMock(ICachedMountFileInfo::class);
79
+        $mount->expects($this->any())
80
+            ->method('getUser')
81
+            ->willReturn($ownerUser); // perhaps not the right user, but does not matter in this scenario
82
+
83
+        $mounts = [ $mount, $mount ]; // to make sure duplicates are dealt with
84
+
85
+        $userMountCache = $this->createMock(IUserMountCache::class);
86
+        $userMountCache->expects($this->any())
87
+            ->method('getMountsForFileId')
88
+            ->willReturn($mounts);
89
+
90
+        $this->mountProviderCollection->expects($this->any())
91
+            ->method('getMountCache')
92
+            ->willReturn($userMountCache);
93
+
94
+        $node = $this->createMock(Node::class);
95
+        $nodes = [ $node ];
96
+
97
+        $ownerFolder = $this->createMock(Folder::class);
98
+        $ownerFolder->expects($this->any())
99
+            ->method('getById')
100
+            ->willReturn($nodes);
101
+
102
+        $this->rootFolder->expects($this->any())
103
+            ->method('getUserFolder')
104
+            ->willReturn($ownerFolder);
105
+
106
+        $al = [ 'users' => [
107
+            '873304' => 'i/got/it/here',
108
+            '254342' => 'there/i/have/it',
109
+            'sandra' => 'and/here/i/placed/it'
110
+        ]];
111
+        $this->shareHelper->expects($this->any())
112
+            ->method('getPathsForAccessList')
113
+            ->willReturn($al);
114
+
115
+        $this->session->expects($this->any())
116
+            ->method('getUser')
117
+            ->willReturn($ownerUser);
118
+
119
+        /** @var MockObject $activity */
120
+        $activity = $this->createMock(IEvent::class);
121
+        $activity->expects($this->exactly(count($al['users'])))
122
+            ->method('setAffectedUser');
123
+        $activity->expects($this->once())
124
+            ->method('setApp')
125
+            ->with('comments')
126
+            ->willReturnSelf();
127
+        $activity->expects($this->once())
128
+            ->method('setType')
129
+            ->with('comments')
130
+            ->willReturnSelf();
131
+        $activity->expects($this->once())
132
+            ->method('setAuthor')
133
+            ->with($ownerUser->getUID())
134
+            ->willReturnSelf();
135
+        $activity->expects($this->once())
136
+            ->method('setObject')
137
+            ->with('files', $this->anything())
138
+            ->willReturnSelf();
139
+        $activity->expects($this->once())
140
+            ->method('setMessage')
141
+            ->with('add_comment_message', $this->anything())
142
+            ->willReturnSelf();
143
+
144
+        $this->activityManager->expects($this->once())
145
+            ->method('generateEvent')
146
+            ->willReturn($activity);
147
+        $this->activityManager->expects($this->exactly(count($al['users'])))
148
+            ->method('publish');
149
+
150
+        $this->listener->commentEvent($event);
151
+    }
152 152
 }
Please login to merge, or discard this patch.