]>
Commit | Line | Data |
---|---|---|
558d9aab JB |
1 | <?php |
2 | ||
a2c1b94e | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
558d9aab | 4 | |
0c5bcd82 | 5 | use Psr\Log\NullLogger; |
558d9aab | 6 | use Wallabag\CoreBundle\Helper\ContentProxy; |
c2656f96 JB |
7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\Tag; | |
619cc453 | 9 | use Wallabag\UserBundle\Entity\User; |
6bc6fb1f | 10 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; |
558d9aab | 11 | |
a2c1b94e | 12 | class ContentProxyTest extends \PHPUnit_Framework_TestCase |
558d9aab | 13 | { |
ac1509a6 | 14 | private $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.'; |
a2c1b94e | 15 | |
4d0ec0e7 JB |
16 | public function testWithBadUrl() |
17 | { | |
18 | $tagger = $this->getTaggerMock(); | |
19 | $tagger->expects($this->once()) | |
20 | ->method('tag'); | |
21 | ||
22 | $graby = $this->getMockBuilder('Graby\Graby') | |
4094ea47 | 23 | ->setMethods(['fetchContent']) |
4d0ec0e7 JB |
24 | ->disableOriginalConstructor() |
25 | ->getMock(); | |
26 | ||
27 | $graby->expects($this->any()) | |
28 | ->method('fetchContent') | |
4094ea47 | 29 | ->willReturn([ |
4d0ec0e7 JB |
30 | 'html' => false, |
31 | 'title' => '', | |
32 | 'url' => '', | |
33 | 'content_type' => '', | |
34 | 'language' => '', | |
4094ea47 | 35 | ]); |
4d0ec0e7 | 36 | |
6bc6fb1f | 37 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
4d0ec0e7 JB |
38 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80'); |
39 | ||
40 | $this->assertEquals('http://user@:80', $entry->getUrl()); | |
41 | $this->assertEmpty($entry->getTitle()); | |
fc2b7bda | 42 | $this->assertEquals($this->fetchingErrorMessage, $entry->getContent()); |
4d0ec0e7 JB |
43 | $this->assertEmpty($entry->getPreviewPicture()); |
44 | $this->assertEmpty($entry->getMimetype()); | |
45 | $this->assertEmpty($entry->getLanguage()); | |
46 | $this->assertEquals(0.0, $entry->getReadingTime()); | |
47 | $this->assertEquals(false, $entry->getDomainName()); | |
48 | } | |
49 | ||
558d9aab JB |
50 | public function testWithEmptyContent() |
51 | { | |
f530f7f5 KG |
52 | $tagger = $this->getTaggerMock(); |
53 | $tagger->expects($this->once()) | |
54 | ->method('tag'); | |
55 | ||
558d9aab | 56 | $graby = $this->getMockBuilder('Graby\Graby') |
4094ea47 | 57 | ->setMethods(['fetchContent']) |
558d9aab JB |
58 | ->disableOriginalConstructor() |
59 | ->getMock(); | |
60 | ||
61 | $graby->expects($this->any()) | |
62 | ->method('fetchContent') | |
4094ea47 | 63 | ->willReturn([ |
98f0929f JB |
64 | 'html' => false, |
65 | 'title' => '', | |
66 | 'url' => '', | |
67 | 'content_type' => '', | |
68 | 'language' => '', | |
4094ea47 | 69 | ]); |
558d9aab | 70 | |
6bc6fb1f | 71 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
558d9aab JB |
72 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); |
73 | ||
74 | $this->assertEquals('http://0.0.0.0', $entry->getUrl()); | |
75 | $this->assertEmpty($entry->getTitle()); | |
fc2b7bda | 76 | $this->assertEquals($this->fetchingErrorMessage, $entry->getContent()); |
558d9aab JB |
77 | $this->assertEmpty($entry->getPreviewPicture()); |
78 | $this->assertEmpty($entry->getMimetype()); | |
98f0929f | 79 | $this->assertEmpty($entry->getLanguage()); |
da3d4998 JB |
80 | $this->assertEquals(0.0, $entry->getReadingTime()); |
81 | $this->assertEquals('0.0.0.0', $entry->getDomainName()); | |
558d9aab JB |
82 | } |
83 | ||
84 | public function testWithEmptyContentButOG() | |
85 | { | |
f530f7f5 KG |
86 | $tagger = $this->getTaggerMock(); |
87 | $tagger->expects($this->once()) | |
88 | ->method('tag'); | |
89 | ||
558d9aab | 90 | $graby = $this->getMockBuilder('Graby\Graby') |
4094ea47 | 91 | ->setMethods(['fetchContent']) |
558d9aab JB |
92 | ->disableOriginalConstructor() |
93 | ->getMock(); | |
94 | ||
95 | $graby->expects($this->any()) | |
96 | ->method('fetchContent') | |
4094ea47 | 97 | ->willReturn([ |
98f0929f JB |
98 | 'html' => false, |
99 | 'title' => '', | |
100 | 'url' => '', | |
101 | 'content_type' => '', | |
102 | 'language' => '', | |
10b35097 | 103 | 'status' => '', |
4094ea47 | 104 | 'open_graph' => [ |
98f0929f JB |
105 | 'og_title' => 'my title', |
106 | 'og_description' => 'desc', | |
4094ea47 JB |
107 | ], |
108 | ]); | |
558d9aab | 109 | |
6bc6fb1f | 110 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
da3d4998 | 111 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); |
558d9aab | 112 | |
da3d4998 | 113 | $this->assertEquals('http://domain.io', $entry->getUrl()); |
558d9aab | 114 | $this->assertEquals('my title', $entry->getTitle()); |
d09fe4d2 | 115 | $this->assertEquals($this->fetchingErrorMessage.'<p><i>But we found a short description: </i></p>desc', $entry->getContent()); |
558d9aab | 116 | $this->assertEmpty($entry->getPreviewPicture()); |
98f0929f | 117 | $this->assertEmpty($entry->getLanguage()); |
10b35097 | 118 | $this->assertEmpty($entry->getHttpStatus()); |
558d9aab | 119 | $this->assertEmpty($entry->getMimetype()); |
da3d4998 JB |
120 | $this->assertEquals(0.0, $entry->getReadingTime()); |
121 | $this->assertEquals('domain.io', $entry->getDomainName()); | |
558d9aab JB |
122 | } |
123 | ||
124 | public function testWithContent() | |
125 | { | |
f530f7f5 KG |
126 | $tagger = $this->getTaggerMock(); |
127 | $tagger->expects($this->once()) | |
128 | ->method('tag'); | |
129 | ||
558d9aab | 130 | $graby = $this->getMockBuilder('Graby\Graby') |
4094ea47 | 131 | ->setMethods(['fetchContent']) |
558d9aab JB |
132 | ->disableOriginalConstructor() |
133 | ->getMock(); | |
134 | ||
135 | $graby->expects($this->any()) | |
136 | ->method('fetchContent') | |
4094ea47 | 137 | ->willReturn([ |
da3d4998 | 138 | 'html' => str_repeat('this is my content', 325), |
558d9aab JB |
139 | 'title' => 'this is my title', |
140 | 'url' => 'http://1.1.1.1', | |
141 | 'content_type' => 'text/html', | |
98f0929f | 142 | 'language' => 'fr', |
10b35097 | 143 | 'status' => '200', |
4094ea47 | 144 | 'open_graph' => [ |
558d9aab JB |
145 | 'og_title' => 'my OG title', |
146 | 'og_description' => 'OG desc', | |
f1e29e69 | 147 | 'og_image' => 'http://3.3.3.3/cover.jpg', |
4094ea47 JB |
148 | ], |
149 | ]); | |
558d9aab | 150 | |
6bc6fb1f | 151 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
558d9aab JB |
152 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); |
153 | ||
154 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | |
155 | $this->assertEquals('this is my title', $entry->getTitle()); | |
da3d4998 | 156 | $this->assertContains('this is my content', $entry->getContent()); |
558d9aab JB |
157 | $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); |
158 | $this->assertEquals('text/html', $entry->getMimetype()); | |
98f0929f | 159 | $this->assertEquals('fr', $entry->getLanguage()); |
10b35097 | 160 | $this->assertEquals('200', $entry->getHttpStatus()); |
da3d4998 JB |
161 | $this->assertEquals(4.0, $entry->getReadingTime()); |
162 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | |
558d9aab | 163 | } |
f530f7f5 | 164 | |
3d71d403 JB |
165 | public function testWithContentAndNoOgImage() |
166 | { | |
167 | $tagger = $this->getTaggerMock(); | |
168 | $tagger->expects($this->once()) | |
169 | ->method('tag'); | |
170 | ||
171 | $graby = $this->getMockBuilder('Graby\Graby') | |
172 | ->setMethods(['fetchContent']) | |
173 | ->disableOriginalConstructor() | |
174 | ->getMock(); | |
175 | ||
176 | $graby->expects($this->any()) | |
177 | ->method('fetchContent') | |
178 | ->willReturn([ | |
179 | 'html' => str_repeat('this is my content', 325), | |
180 | 'title' => 'this is my title', | |
181 | 'url' => 'http://1.1.1.1', | |
182 | 'content_type' => 'text/html', | |
183 | 'language' => 'fr', | |
184 | 'status' => '200', | |
185 | 'open_graph' => [ | |
186 | 'og_title' => 'my OG title', | |
187 | 'og_description' => 'OG desc', | |
188 | 'og_image' => false, | |
189 | ], | |
190 | ]); | |
191 | ||
6bc6fb1f | 192 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
3d71d403 JB |
193 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); |
194 | ||
195 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | |
196 | $this->assertEquals('this is my title', $entry->getTitle()); | |
197 | $this->assertContains('this is my content', $entry->getContent()); | |
198 | $this->assertNull($entry->getPreviewPicture()); | |
199 | $this->assertEquals('text/html', $entry->getMimetype()); | |
200 | $this->assertEquals('fr', $entry->getLanguage()); | |
201 | $this->assertEquals('200', $entry->getHttpStatus()); | |
202 | $this->assertEquals(4.0, $entry->getReadingTime()); | |
203 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | |
204 | } | |
205 | ||
4d0ec0e7 JB |
206 | public function testWithForcedContent() |
207 | { | |
208 | $tagger = $this->getTaggerMock(); | |
209 | $tagger->expects($this->once()) | |
210 | ->method('tag'); | |
211 | ||
212 | $graby = $this->getMockBuilder('Graby\Graby')->getMock(); | |
213 | ||
6bc6fb1f | 214 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
4d0ec0e7 JB |
215 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ |
216 | 'html' => str_repeat('this is my content', 325), | |
217 | 'title' => 'this is my title', | |
218 | 'url' => 'http://1.1.1.1', | |
219 | 'content_type' => 'text/html', | |
220 | 'language' => 'fr', | |
221 | ]); | |
222 | ||
223 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | |
224 | $this->assertEquals('this is my title', $entry->getTitle()); | |
225 | $this->assertContains('this is my content', $entry->getContent()); | |
226 | $this->assertEquals('text/html', $entry->getMimetype()); | |
227 | $this->assertEquals('fr', $entry->getLanguage()); | |
228 | $this->assertEquals(4.0, $entry->getReadingTime()); | |
229 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | |
230 | } | |
231 | ||
232 | public function testTaggerThrowException() | |
233 | { | |
234 | $graby = $this->getMockBuilder('Graby\Graby') | |
235 | ->disableOriginalConstructor() | |
236 | ->getMock(); | |
237 | ||
238 | $tagger = $this->getTaggerMock(); | |
239 | $tagger->expects($this->once()) | |
240 | ->method('tag') | |
241 | ->will($this->throwException(new \Exception())); | |
242 | ||
6bc6fb1f | 243 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
4d0ec0e7 JB |
244 | |
245 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | |
246 | 'html' => str_repeat('this is my content', 325), | |
247 | 'title' => 'this is my title', | |
248 | 'url' => 'http://1.1.1.1', | |
249 | 'content_type' => 'text/html', | |
250 | 'language' => 'fr', | |
251 | ]); | |
252 | ||
253 | $this->assertCount(0, $entry->getTags()); | |
254 | } | |
255 | ||
f530f7f5 KG |
256 | private function getTaggerMock() |
257 | { | |
6bc6fb1f | 258 | return $this->getMockBuilder(RuleBasedTagger::class) |
4094ea47 | 259 | ->setMethods(['tag']) |
f530f7f5 KG |
260 | ->disableOriginalConstructor() |
261 | ->getMock(); | |
262 | } | |
1c9cd2a7 | 263 | |
0c5bcd82 | 264 | private function getLogger() |
1c9cd2a7 | 265 | { |
0c5bcd82 | 266 | return new NullLogger(); |
1c9cd2a7 | 267 | } |
558d9aab | 268 | } |