scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace Fusonic\OpenGraph\Test; |
||
4 | |||
5 | use Fusonic\OpenGraph\Consumer; |
||
6 | |||
7 | class ConsumerTest extends \PHPUnit_Framework_TestCase |
||
8 | { |
||
9 | /** |
||
10 | * Checks crawler to read basic properties. |
||
11 | */ |
||
12 | public function testLoadHtmlBasics() |
||
13 | { |
||
14 | $content = <<<LONG |
||
15 | <html> |
||
16 | <head> |
||
17 | <meta property="og:description" content="Description"> |
||
18 | <meta property="og:determiner" content="auto"> |
||
19 | <meta property="og:locale" content="en_GB"> |
||
20 | <meta property="og:locale:alternate" content="en_US"> |
||
21 | <meta property="og:locale:alternate" content="de_AT"> |
||
22 | <meta property="og:rich_attachment" content="True"> |
||
23 | <meta property="og:see_also" content="https://github.com/fusonic/fusonic-linq"> |
||
24 | <meta property="og:see_also" content="https://github.com/fusonic/fusonic-spreadsheetexport"> |
||
25 | <meta property="og:site_name" content="Site name"> |
||
26 | <meta property="og:title" content="Title"> |
||
27 | <meta property="og:updated_time" content="2014-07-20T17:51:00+02:00"> |
||
28 | <meta property="og:url" content="https://github.com/fusonic/fusonic-opengraph"> |
||
29 | </head> |
||
30 | <body></body> |
||
31 | </html> |
||
32 | LONG; |
||
33 | |||
34 | $consumer = new Consumer(); |
||
35 | |||
36 | $res = $consumer->loadHtml($content, "about:blank"); |
||
37 | |||
38 | $this->assertEquals("Description", $res->description); |
||
39 | $this->assertEquals("auto", $res->determiner); |
||
40 | $this->assertEquals("en_GB", $res->locale); |
||
41 | $this->assertContains("en_US", $res->localeAlternate); |
||
42 | $this->assertContains("de_AT", $res->localeAlternate); |
||
43 | $this->assertTrue($res->richAttachment); |
||
44 | $this->assertContains("https://github.com/fusonic/fusonic-linq", $res->seeAlso); |
||
45 | $this->assertContains("https://github.com/fusonic/fusonic-spreadsheetexport", $res->seeAlso); |
||
46 | $this->assertEquals("Site name", $res->siteName); |
||
47 | $this->assertEquals("Title", $res->title); |
||
48 | $this->assertTrue($res->updatedTime instanceof \DateTime); |
||
49 | $this->assertEquals("https://github.com/fusonic/fusonic-opengraph", $res->url); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Checks crawler not to use fallback if disabled even if no OG data is provided. |
||
54 | */ |
||
55 | public function testLoadHtmlFallbacksOff() |
||
56 | { |
||
57 | $content = <<<LONG |
||
58 | <html> |
||
59 | <head> |
||
60 | <title>Title</title> |
||
61 | <meta property="description" content="Description"> |
||
62 | </head> |
||
63 | <body></body> |
||
64 | </html> |
||
65 | LONG; |
||
66 | |||
67 | $consumer = new Consumer(); |
||
68 | |||
69 | $res = $consumer->loadHtml($content, "about:blank"); |
||
70 | |||
71 | $this->assertNull($res->description); |
||
72 | $this->assertNull($res->title); |
||
73 | $this->assertNull($res->url); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Checks crawler to correctly use fallback elements when activated. |
||
78 | */ |
||
79 | public function testLoadHtmlFallbacksOn() |
||
80 | { |
||
81 | $content = <<<LONG |
||
82 | <html> |
||
83 | <head> |
||
84 | <title>Title</title> |
||
85 | <meta property="description" content="Description"> |
||
86 | </head> |
||
87 | <body></body> |
||
88 | </html> |
||
89 | LONG; |
||
90 | |||
91 | $consumer = new Consumer(); |
||
92 | $consumer->useFallbackMode = true; |
||
93 | |||
94 | $res = $consumer->loadHtml($content, "about:blank"); |
||
95 | |||
96 | $this->assertEquals("Description", $res->description); |
||
97 | $this->assertEquals("Title", $res->title); |
||
98 | $this->assertEquals("about:blank", $res->url); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Checks crawler to handle arrays of elements with child-properties like described in the |
||
103 | * Open Graph documentation (http://ogp.me/#array). |
||
104 | */ |
||
105 | public function testLoadHtmlArrayHandling() |
||
106 | { |
||
107 | $content = <<<LONG |
||
108 | <html> |
||
109 | <head> |
||
110 | <meta property="og:image" content="http://example.com/rock.jpg"> |
||
111 | <meta property="og:image:width" content="300"> |
||
112 | <meta property="og:image:height" content="300"> |
||
113 | <meta property="og:image" content="http://example.com/rock2.jpg"> |
||
114 | <meta property="og:image" content="http://example.com/rock3.jpg"> |
||
115 | <meta property="og:image:height" content="1000"> |
||
116 | </head> |
||
117 | <body></body> |
||
118 | </html> |
||
119 | LONG; |
||
120 | |||
121 | $consumer = new Consumer(); |
||
122 | |||
123 | $res = $consumer->loadHtml($content); |
||
124 | |||
125 | $this->assertEquals(3, count($res->images)); |
||
126 | $this->assertEquals("http://example.com/rock.jpg", $res->images[0]->url); |
||
127 | $this->assertEquals(300, $res->images[0]->width); |
||
128 | $this->assertEquals(300, $res->images[0]->height); |
||
129 | $this->assertEquals("http://example.com/rock2.jpg", $res->images[1]->url); |
||
130 | $this->assertNull($res->images[1]->width); |
||
131 | $this->assertNull($res->images[1]->height); |
||
132 | $this->assertEquals("http://example.com/rock3.jpg", $res->images[2]->url); |
||
133 | $this->assertNull($res->images[2]->width); |
||
134 | $this->assertEquals(1000, $res->images[2]->height); |
||
135 | } |
||
136 | |||
137 | public function testLoadHtmlImages() |
||
138 | { |
||
139 | $content = <<<LONG |
||
140 | <html> |
||
141 | <head> |
||
142 | <meta property="og:image" content="http://example.com/rock.jpg"> |
||
143 | <meta property="og:image:secure_url" content="https://example.com/rock.jpg"> |
||
144 | <meta property="og:image:width" content="300"> |
||
145 | <meta property="og:image:height" content="300"> |
||
146 | <meta property="og:image:type" content="image/jpg"> |
||
147 | </head> |
||
148 | <body></body> |
||
149 | </html> |
||
150 | LONG; |
||
151 | |||
152 | $consumer = new Consumer(); |
||
153 | |||
154 | $res = $consumer->loadHtml($content); |
||
155 | |||
156 | $this->assertEquals(1, count($res->images)); |
||
157 | $this->assertEquals("http://example.com/rock.jpg", $res->images[0]->url); |
||
158 | $this->assertEquals("https://example.com/rock.jpg", $res->images[0]->secureUrl); |
||
159 | $this->assertEquals(300, $res->images[0]->width); |
||
160 | $this->assertEquals(300, $res->images[0]->height); |
||
161 | $this->assertEquals("image/jpg", $res->images[0]->type); |
||
162 | } |
||
163 | |||
164 | public function testLoadHtmlVideos() |
||
165 | { |
||
166 | $content = <<<LONG |
||
167 | <html> |
||
168 | <head> |
||
169 | <meta property="og:video" content="http://example.com/rock.ogv"> |
||
170 | <meta property="og:video:secure_url" content="https://example.com/rock.ogv"> |
||
171 | <meta property="og:video:width" content="300"> |
||
172 | <meta property="og:video:height" content="300"> |
||
173 | <meta property="og:video:type" content="video/ogv"> |
||
174 | </head> |
||
175 | <body></body> |
||
176 | </html> |
||
177 | LONG; |
||
178 | |||
179 | $consumer = new Consumer(); |
||
180 | |||
181 | $res = $consumer->loadHtml($content); |
||
182 | |||
183 | $this->assertEquals(1, count($res->videos)); |
||
184 | $this->assertEquals("http://example.com/rock.ogv", $res->videos[0]->url); |
||
185 | $this->assertEquals("https://example.com/rock.ogv", $res->videos[0]->secureUrl); |
||
186 | $this->assertEquals(300, $res->videos[0]->width); |
||
187 | $this->assertEquals(300, $res->videos[0]->height); |
||
188 | $this->assertEquals("video/ogv", $res->videos[0]->type); |
||
189 | } |
||
190 | |||
191 | public function testLoadHtmlAudios() |
||
192 | { |
||
193 | $content = <<<LONG |
||
194 | <html> |
||
195 | <head> |
||
196 | <meta property="og:audio" content="http://example.com/rock.mp3"> |
||
197 | <meta property="og:audio:secure_url" content="https://example.com/rock.mp3"> |
||
198 | <meta property="og:audio:type" content="audio/mp3"> |
||
199 | </head> |
||
200 | <body></body> |
||
201 | </html> |
||
202 | LONG; |
||
203 | |||
204 | $consumer = new Consumer(); |
||
205 | |||
206 | $res = $consumer->loadHtml($content); |
||
207 | |||
208 | $this->assertEquals(1, count($res->audios)); |
||
209 | $this->assertEquals("http://example.com/rock.mp3", $res->audios[0]->url); |
||
210 | $this->assertEquals("https://example.com/rock.mp3", $res->audios[0]->secureUrl); |
||
211 | $this->assertEquals("audio/mp3", $res->audios[0]->type); |
||
212 | } |
||
213 | |||
214 | public function testCrawlHtmlImageExceptionDebugOff() |
||
215 | { |
||
216 | $content = <<<LONG |
||
217 | <html> |
||
218 | <head> |
||
219 | <meta property="og:image:width" content="300"> |
||
220 | </head> |
||
221 | <body></body> |
||
222 | </html> |
||
223 | LONG; |
||
224 | |||
225 | $consumer = new Consumer(); |
||
226 | |||
227 | $res = $consumer->loadHtml($content); |
||
228 | |||
229 | $this->assertEquals(0, count($res->images)); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @expectedException UnexpectedValueException |
||
234 | */ |
||
235 | public function testCrawlHtmlImageExceptionDebugOn() |
||
236 | { |
||
237 | $content = <<<LONG |
||
238 | <html> |
||
239 | <head> |
||
240 | <meta property="og:image:width" content="300"> |
||
241 | </head> |
||
242 | <body></body> |
||
243 | </html> |
||
244 | LONG; |
||
245 | |||
246 | $consumer = new Consumer(); |
||
247 | $consumer->debug = true; |
||
248 | |||
249 | $res = $consumer->loadHtml($content); |
||
250 | } |
||
251 | |||
252 | public function testCrawlHtmlVideoExceptionDebugOff() |
||
253 | { |
||
254 | $content = <<<LONG |
||
255 | <html> |
||
256 | <head> |
||
257 | <meta property="og:video:width" content="300"> |
||
258 | </head> |
||
259 | <body></body> |
||
260 | </html> |
||
261 | LONG; |
||
262 | |||
263 | $consumer = new Consumer(); |
||
264 | |||
265 | $res = $consumer->loadHtml($content); |
||
266 | |||
267 | $this->assertEquals(0, count($res->videos)); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @expectedException UnexpectedValueException |
||
272 | */ |
||
273 | public function testCrawlHtmlVideoExceptionDebugOn() |
||
274 | { |
||
275 | $content = <<<LONG |
||
276 | <html> |
||
277 | <head> |
||
278 | <meta property="og:video:width" content="300"> |
||
279 | </head> |
||
280 | <body></body> |
||
281 | </html> |
||
282 | LONG; |
||
283 | |||
284 | $consumer = new Consumer(); |
||
285 | $consumer->debug = true; |
||
286 | |||
287 | $res = $consumer->loadHtml($content); |
||
288 | } |
||
289 | |||
290 | public function testCrawlHtmlAudioExceptionDebugOff() |
||
291 | { |
||
292 | $content = <<<LONG |
||
293 | <html> |
||
294 | <head> |
||
295 | <meta property="og:audio:secure_url" content="300"> |
||
296 | </head> |
||
297 | <body></body> |
||
298 | </html> |
||
299 | LONG; |
||
300 | |||
301 | $consumer = new Consumer(); |
||
302 | |||
303 | $res = $consumer->loadHtml($content); |
||
304 | |||
305 | $this->assertEquals(0, count($res->audios)); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @expectedException UnexpectedValueException |
||
310 | */ |
||
311 | public function testCrawlHtmlAudioExceptionDebugOn() |
||
312 | { |
||
313 | $content = <<<LONG |
||
314 | <html> |
||
315 | <head> |
||
316 | <meta property="og:audio:type" content="audio/mp3"> |
||
317 | </head> |
||
318 | <body></body> |
||
319 | </html> |
||
320 | LONG; |
||
321 | |||
322 | $consumer = new Consumer(); |
||
323 | $consumer->debug = true; |
||
324 | |||
325 | $res = $consumer->loadHtml($content); |
||
326 | } |
||
327 | |||
328 | public function testLoadHtmlSpecialCharacters() |
||
329 | { |
||
330 | $content = <<<LONG |
||
331 | <html> |
||
332 | <head> |
||
333 | <meta property="og:title" content="Apples & Bananas - just "Fruits""> |
||
334 | </head> |
||
335 | <body></body> |
||
336 | </html> |
||
337 | LONG; |
||
338 | |||
339 | $consumer = new Consumer(); |
||
340 | |||
341 | $res = $consumer->loadHtml($content); |
||
342 | |||
343 | $this->assertEquals("Apples & Bananas - just \"Fruits\"", $res->title); |
||
344 | } |
||
345 | |||
346 | public function testReadMetaName() |
||
347 | { |
||
348 | $content = <<<LONG |
||
349 | <html> |
||
350 | <head> |
||
351 | <meta name="og:title" content="A 'name' attribute instead of 'property'"> |
||
352 | </head> |
||
353 | <body></body> |
||
354 | </html> |
||
355 | LONG; |
||
356 | |||
357 | $consumer = new Consumer(); |
||
358 | |||
359 | $res = $consumer->loadHtml($content); |
||
360 | |||
361 | $this->assertEquals("A 'name' attribute instead of 'property'", $res->title); |
||
362 | } |
||
363 | } |