@@ -160,4 +160,84 @@ public function testReplaceAttachmentFolderIdNoReplace(string $sourceContent): v
160160 AttachmentService::replaceAttachmentFolderId ($ source , $ target );
161161 $ this ->assertEquals ($ sourceContent , $ replacedContent );
162162 }
163+
164+ // Replacement expected
165+ public static function contentReplaceAttachmentFileIdProvider (): array {
166+ return [
167+ ['[image.png](https://localhost:8000/f/1) ' , '[image.png](https://localhost:8000/f/2) ' ],
168+ ['[image.png](https://localhost:8000/f/1(preview)) ' , '[image.png](https://localhost:8000/f/2(preview)) ' ],
169+ ['[image.png](https://localhost:8000/f/1 (preview)) ' , '[image.png](https://localhost:8000/f/2 (preview)) ' ],
170+ // Link in title
171+ ['[https://localhost:8000/f/1](https://localhost:8000/f/1) ' , '[https://localhost:8000/f/1](https://localhost:8000/f/2) ' ],
172+ ['[https://localhost:8000/f/1](https://localhost:8000/f/1 (preview)) ' , '[https://localhost:8000/f/1](https://localhost:8000/f/2 (preview)) ' ],
173+ // Spaces surrounding link URL
174+ ['[image.png]( https://localhost:8000/f/1 ) ' , '[image.png]( https://localhost:8000/f/2 ) ' ],
175+ ['[image.png]( https://localhost:8000/f/1 (preview)) ' , '[image.png]( https://localhost:8000/f/2 (preview)) ' ],
176+ ['[image.png]( https://localhost:8000/f/1 (preview) ) ' , '[image.png]( https://localhost:8000/f/2 (preview) ) ' ],
177+ // Escaped square brackets in title
178+ ['[title \[#1\]](https://localhost:8000/f/1) ' , '[title \[#1\]](https://localhost:8000/f/2) ' ],
179+ ['[title \[#1\]](https://localhost:8000/f/1 (preview)) ' , '[title \[#1\]](https://localhost:8000/f/2 (preview)) ' ],
180+ // Spaces in title
181+ ['[title with space](https://localhost:8000/f/1) ' , '[title with space](https://localhost:8000/f/2) ' ],
182+ ['[title with space](https://localhost:8000/f/1 (preview)) ' , '[title with space](https://localhost:8000/f/2 (preview)) ' ],
183+ // Several links in a row
184+ ['Some text\n\n[image.png](https://localhost:8000/f/1 (preview))\n\nMore text. [file.tar.gz](https://localhost:8000/f/3) ... ' , 'Some text\n\n[image.png](https://localhost:8000/f/2 (preview))\n\nMore text. [file.tar.gz](https://localhost:8000/f/4) ... ' ],
185+ ];
186+ }
187+
188+ /**
189+ * @dataProvider contentReplaceAttachmentFileIdProvider
190+ */
191+ public function testReplaceAttachmentFileId (string $ sourceContent , string $ targetContent ): void {
192+ $ target = $ this ->createMock (File::class);
193+ $ target ->method ('getId ' )->willReturn (2 );
194+ $ target ->method ('getContent ' )->willReturn ($ sourceContent );
195+ $ replacedContent = '' ;
196+ $ target ->method ('putContent ' )->willReturnCallback (function (string $ content ) use (&$ replacedContent ) {
197+ $ replacedContent = $ content ;
198+ });
199+
200+ $ fileIdMapping = [
201+ [1 , 2 ],
202+ [3 , 4 ],
203+ ];
204+ AttachmentService::replaceAttachmentFileIds ($ target , $ fileIdMapping );
205+ $ this ->assertEquals ($ targetContent , $ replacedContent );
206+ }
207+
208+ // No replacement expected
209+ public static function contentReplaceAttachmentFileIdNoReplaceProvider (): array {
210+ return [
211+ // Empty title
212+ [ '[](https://localhost:8000/f/1) ' ],
213+ // Different url
214+ [ '[title](https://localhost:8000/f/1/a/asdf.png) ' ],
215+ // Wrong fileId #1
216+ [ '[image.png](https://localhost:8000/f/112) ' ],
217+ // Wrong fileId #2
218+ [ '[image.png](https://localhost:8000/f/12) ' ],
219+ // Normal brackets around title
220+ [ '(image.png)(https://localhost:8000/f/1) ' ],
221+ // Square brackets in title
222+ ['[title [#1]](https://localhost:8000/f/1) ' ],
223+ // Space between brackets
224+ ['[title] (https://localhost:8000/f/1) ' ],
225+ ];
226+ }
227+
228+ /**
229+ * @dataProvider contentReplaceAttachmentFileIdNoReplaceProvider
230+ */
231+ public function testReplaceAttachmentFileIdNoReplace (string $ sourceContent ): void {
232+ $ target = $ this ->createMock (File::class);
233+ $ target ->method ('getId ' )->willReturn (2 );
234+ $ target ->method ('getContent ' )->willReturn ($ sourceContent );
235+ $ replacedContent = '' ;
236+ $ target ->method ('putContent ' )->willReturnCallback (function (string $ content ) use (&$ replacedContent ) {
237+ $ replacedContent = $ content ;
238+ });
239+
240+ AttachmentService::replaceAttachmentFileIds ($ target , [[1 , 2 ]]);
241+ $ this ->assertEquals ($ sourceContent , $ replacedContent );
242+ }
163243}
0 commit comments