-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathAttachmentServiceTest.php
More file actions
190 lines (171 loc) · 6.73 KB
/
Copy pathAttachmentServiceTest.php
File metadata and controls
190 lines (171 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace OCA\Text\Tests;
use OCA\Text\AppInfo\Application;
use OCA\Text\Service\AttachmentService;
use OCP\Files\File;
use OCP\Files\Folder;
use PHPUnit\Framework\TestCase;
class AttachmentServiceTest extends TestCase {
private static array $attachmentNames = [
'aaa.png',
'aaa (2).png',
'aaa 2).png',
'aaa (2.png',
'aaa ((2.png',
'aaa 2)).png',
'a[a]a.png',
'a(a)a.png',
'a](a.png',
',;:!?.§-_a_',
'a`a`.png',
];
public function testDummy(): void {
$app = new Application();
$this->assertEquals('text', $app::APP_NAME);
}
public function testGetAttachmentNamesFromContent(): void {
$content = "some content\n";
foreach (self::$attachmentNames as $name) {
// this is how it's generated in MenuBar.vue
$linkText = preg_replace('/[[\]]/', '', $name);
$encodedName = urlencode($name);
$content .= '\n";
}
$content .= 'some content';
$computedNames = AttachmentService::getAttachmentNamesFromContent($content, 33);
foreach (self::$attachmentNames as $contentName) {
$this->assertContains($contentName, $computedNames);
}
}
public function testGetAttachmentIdsFromContent(): void {
$urls = [
'www.example.com',
'http://example.com',
'https://www.example.com/path/to/page',
'http://sub.domain.co.uk/index.php',
'https://1.2.3.4:8080/path',
'http://localhost:3000/',
'https://[2001:db8::1]/ipv6-check',
];
$id = 1;
$content = "some content\n";
foreach (self::$attachmentNames as $name) {
$linkText = preg_replace('/[[\]]/', '', $name);
foreach ($urls as $url) {
$addon = $id % 2 ? ' (preview)' : '';
$content .= "[{$linkText}]({$url}/f/{$id}{$addon})\n";
$id++;
}
}
$content .= 'some content';
$computedIds = AttachmentService::getAttachmentIdsFromContent($content);
$this->assertEquals(range(1, $id - 1), $computedIds);
}
public function testGetUniqueFileName(): void {
$fileNameList = [
'foo.png',
'bar',
'plop.png',
'plop (2).png',
'lala.png',
'lala (2).png',
'lala (3).png',
'yay.png',
'yay (2).png',
'yay (3).png',
'yay (5).png',
'file.ext.ext',
];
$folder = $this->createMock(Folder::class);
$folder->expects(self::any())
->method('nodeExists')
->willReturnCallback(function ($name) use ($fileNameList) {
return in_array($name, $fileNameList);
});
// files that do not exist yet
$this->assertEquals('doesNotExistYet', AttachmentService::getUniqueFileName($folder, 'doesNotExistYet'));
$this->assertEquals('doesNotExistYet.png', AttachmentService::getUniqueFileName($folder, 'doesNotExistYet.png'));
// files that already exist
$this->assertEquals('foo (2).png', AttachmentService::getUniqueFileName($folder, 'foo.png'));
$this->assertEquals('bar (2)', AttachmentService::getUniqueFileName($folder, 'bar'));
$this->assertEquals('plop (3).png', AttachmentService::getUniqueFileName($folder, 'plop.png'));
$this->assertEquals('lala (4).png', AttachmentService::getUniqueFileName($folder, 'lala.png'));
$this->assertEquals('yay (4).png', AttachmentService::getUniqueFileName($folder, 'yay.png'));
$this->assertEquals('file.ext (2).ext', AttachmentService::getUniqueFileName($folder, 'file.ext.ext'));
}
// Replacement expected
public static function contentReplacedProvider(): array {
return [
['', ''],
// Spaces surrounding link URL
['', ''],
// Escaped square brackets in title
['![title \[#1\]](.attachments.1/image.png)', '![title \[#1\]](.attachments.2/image.png)'],
// Angle brackets surrounding link URL
['', ''],
// Spaces and angle brackets surrounding link URL
['', ''],
// Spaces in title
['', ''],
// Several links in a row
['Some text\n\n\n\nMore text. [file.tar.gz](.attachments.1/file.tar.gz) ...', 'Some text\n\n\n\nMore text. [file.tar.gz](.attachments.2/file.tar.gz) ...'],
// Reference link syntax
['[reference]: .attachments.1/image.png', '[reference]: .attachments.2/image.png'],
// Reference link syntax with title
['[reference]: .attachments.1/image.png "title"', '[reference]: .attachments.2/image.png "title"'],
// Reference link syntax with escaped square brackets in reference name
['[reference \[#1\]]: .attachments.1/image.png "title"', '[reference \[#1\]]: .attachments.2/image.png "title"'],
];
}
// No replacement expected
public static function contentNotReplacedProvider(): array {
return [
// Empty title
[ '' ],
// Empty filename
[ '' ],
// Wrong fileId #1
[ '' ],
// Wrong fileId #2
[ '' ],
// Normal brackets around title
[ '!(image.png)(.attachments.1/image.png)' ],
// Square brackets in title
['![title [#1]](.attachments.1/image.png)' ],
// Reference link syntax with square brackets in reference name
['[reference [#1]]: .attachments.1/image.png' ],
];
}
/**
* @dataProvider contentReplacedProvider
*/
public function testReplaceAttachmentFolderId(string $sourceContent, string $targetContent): void {
$source = $this->createMock(File::class);
$source->method('getId')->willReturn(1);
$target = $this->createMock(File::class);
$target->method('getId')->willReturn(2);
$target->method('getContent')->willReturn($sourceContent);
$replacedContent = '';
$target->method('putContent')->willReturnCallback(function (string $content) use (&$replacedContent) {
$replacedContent = $content;
});
AttachmentService::replaceAttachmentFolderId($source, $target);
$this->assertEquals($targetContent, $replacedContent);
}
/**
* @dataProvider contentNotReplacedProvider
*/
public function testReplaceAttachmentFolderIdNoReplace(string $sourceContent): void {
$source = $this->createMock(File::class);
$source->method('getId')->willReturn(1);
$target = $this->createMock(File::class);
$target->method('getId')->willReturn(2);
$target->method('getContent')->willReturn($sourceContent);
$replacedContent = '';
$target->method('putContent')->willReturnCallback(function (string $content) use (&$replacedContent) {
$replacedContent = $content;
});
AttachmentService::replaceAttachmentFolderId($source, $target);
$this->assertEquals($sourceContent, $replacedContent);
}
}