]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
CS & improve tags assertions
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / ContentProxyTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Helper;
4
5 use Psr\Log\NullLogger;
6 use Wallabag\CoreBundle\Helper\ContentProxy;
7 use Wallabag\CoreBundle\Entity\Entry;
8 use Wallabag\CoreBundle\Entity\Tag;
9 use Wallabag\UserBundle\Entity\User;
10 use Wallabag\CoreBundle\Helper\RuleBasedTagger;
11
12 class ContentProxyTest extends \PHPUnit_Framework_TestCase
13 {
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>.';
15
16 public function testWithBadUrl()
17 {
18 $tagger = $this->getTaggerMock();
19 $tagger->expects($this->once())
20 ->method('tag');
21
22 $graby = $this->getMockBuilder('Graby\Graby')
23 ->setMethods(['fetchContent'])
24 ->disableOriginalConstructor()
25 ->getMock();
26
27 $graby->expects($this->any())
28 ->method('fetchContent')
29 ->willReturn([
30 'html' => false,
31 'title' => '',
32 'url' => '',
33 'content_type' => '',
34 'language' => '',
35 ]);
36
37 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage);
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());
42 $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
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
50 public function testWithEmptyContent()
51 {
52 $tagger = $this->getTaggerMock();
53 $tagger->expects($this->once())
54 ->method('tag');
55
56 $graby = $this->getMockBuilder('Graby\Graby')
57 ->setMethods(['fetchContent'])
58 ->disableOriginalConstructor()
59 ->getMock();
60
61 $graby->expects($this->any())
62 ->method('fetchContent')
63 ->willReturn([
64 'html' => false,
65 'title' => '',
66 'url' => '',
67 'content_type' => '',
68 'language' => '',
69 ]);
70
71 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage);
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());
76 $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
77 $this->assertEmpty($entry->getPreviewPicture());
78 $this->assertEmpty($entry->getMimetype());
79 $this->assertEmpty($entry->getLanguage());
80 $this->assertEquals(0.0, $entry->getReadingTime());
81 $this->assertEquals('0.0.0.0', $entry->getDomainName());
82 }
83
84 public function testWithEmptyContentButOG()
85 {
86 $tagger = $this->getTaggerMock();
87 $tagger->expects($this->once())
88 ->method('tag');
89
90 $graby = $this->getMockBuilder('Graby\Graby')
91 ->setMethods(['fetchContent'])
92 ->disableOriginalConstructor()
93 ->getMock();
94
95 $graby->expects($this->any())
96 ->method('fetchContent')
97 ->willReturn([
98 'html' => false,
99 'title' => '',
100 'url' => '',
101 'content_type' => '',
102 'language' => '',
103 'status' => '',
104 'open_graph' => [
105 'og_title' => 'my title',
106 'og_description' => 'desc',
107 ],
108 ]);
109
110 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage);
111 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
112
113 $this->assertEquals('http://domain.io', $entry->getUrl());
114 $this->assertEquals('my title', $entry->getTitle());
115 $this->assertEquals($this->fetchingErrorMessage.'<p><i>But we found a short description: </i></p>desc', $entry->getContent());
116 $this->assertEmpty($entry->getPreviewPicture());
117 $this->assertEmpty($entry->getLanguage());
118 $this->assertEmpty($entry->getHttpStatus());
119 $this->assertEmpty($entry->getMimetype());
120 $this->assertEquals(0.0, $entry->getReadingTime());
121 $this->assertEquals('domain.io', $entry->getDomainName());
122 }
123
124 public function testWithContent()
125 {
126 $tagger = $this->getTaggerMock();
127 $tagger->expects($this->once())
128 ->method('tag');
129
130 $graby = $this->getMockBuilder('Graby\Graby')
131 ->setMethods(['fetchContent'])
132 ->disableOriginalConstructor()
133 ->getMock();
134
135 $graby->expects($this->any())
136 ->method('fetchContent')
137 ->willReturn([
138 'html' => str_repeat('this is my content', 325),
139 'title' => 'this is my title',
140 'url' => 'http://1.1.1.1',
141 'content_type' => 'text/html',
142 'language' => 'fr',
143 'status' => '200',
144 'open_graph' => [
145 'og_title' => 'my OG title',
146 'og_description' => 'OG desc',
147 'og_image' => 'http://3.3.3.3/cover.jpg',
148 ],
149 ]);
150
151 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage);
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());
156 $this->assertContains('this is my content', $entry->getContent());
157 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
158 $this->assertEquals('text/html', $entry->getMimetype());
159 $this->assertEquals('fr', $entry->getLanguage());
160 $this->assertEquals('200', $entry->getHttpStatus());
161 $this->assertEquals(4.0, $entry->getReadingTime());
162 $this->assertEquals('1.1.1.1', $entry->getDomainName());
163 }
164
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
192 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage);
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
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
214 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage);
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
243 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage);
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
256 private function getTaggerMock()
257 {
258 return $this->getMockBuilder(RuleBasedTagger::class)
259 ->setMethods(['tag'])
260 ->disableOriginalConstructor()
261 ->getMock();
262 }
263
264 private function getLogger()
265 {
266 return new NullLogger();
267 }
268 }