[ZF-10089] Zend_Log
[zend.git] / tests / Zend / Gdata / App / EntryTest.php
blob29b1aff2258a1053e03c491ef78bfda78372e82b
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Gdata_App
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id $
23 require_once 'Zend/Gdata/App/Entry.php';
24 require_once 'Zend/Gdata/App.php';
25 require_once 'Zend/Gdata/TestUtility/MockHttpClient.php';
26 require_once 'Zend/Gdata/HttpClient.php';
28 /**
29 * @category Zend
30 * @package Zend_Gdata_App
31 * @subpackage UnitTests
32 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
34 * @group Zend_Gdata
35 * @group Zend_Gdata_App
37 class Zend_Gdata_App_EntryTest extends PHPUnit_Framework_TestCase
40 public function setUp()
42 $this->enryText = file_get_contents(
43 'Zend/Gdata/App/_files/EntrySample1.xml',
44 true);
45 $this->httpEntrySample = file_get_contents(
46 'Zend/Gdata/App/_files/EntrySampleHttp1.txt',
47 true);
48 $this->enry = new Zend_Gdata_App_Entry();
50 $this->adapter = new Test_Zend_Gdata_MockHttpClient();
51 $this->client = new Zend_Gdata_HttpClient();
52 $this->client->setAdapter($this->adapter);
53 $this->service = new Zend_Gdata_App($this->client);
56 public function testEmptyEntryShouldHaveEmptyExtensionsList()
58 $this->assertTrue(is_array($this->enry->extensionElements));
59 $this->assertTrue(count($this->enry->extensionElements) == 0);
62 public function testEmptyEntryToAndFromStringShouldMatch()
64 $enryXml = $this->enry->saveXML();
65 $newEntry = new Zend_Gdata_App_Entry();
66 $newEntry->transferFromXML($enryXml);
67 $newEntryXml = $newEntry->saveXML();
68 $this->assertTrue($enryXml == $newEntryXml);
71 public function testConvertEntryToAndFromString()
73 $this->enry->transferFromXML($this->enryText);
74 $enryXml = $this->enry->saveXML();
75 $newEntry = new Zend_Gdata_App_Entry();
76 $newEntry->transferFromXML($enryXml);
78 $this->assertEquals(1, count($newEntry->entry));
79 $this->assertEquals('dive into mark', $newEntry->title->text);
80 $this->assertEquals('text', $newEntry->title->type);
81 $this->assertEquals('2005-07-31T12:29:29Z', $newEntry->updated->text);
82 $this->assertEquals('tag:example.org,2003:3', $newEntry->id->text);
83 $this->assertEquals(2, count($newEntry->link));
84 $this->assertEquals('http://example.org/',
85 $newEntry->getAlternateLink()->href);
86 $this->assertEquals('en',
87 $newEntry->getAlternateLink()->hrefLang);
88 $this->assertEquals('text/html',
89 $newEntry->getAlternateLink()->type);
90 $this->assertEquals('http://example.org/enry.atom',
91 $newEntry->getSelfLink()->href);
92 $this->assertEquals('application/atom+xml',
93 $newEntry->getSelfLink()->type);
94 $this->assertEquals('Copyright (c) 2003, Mark Pilgrim',
95 $newEntry->rights->text);
96 $entry = $newEntry->entry[0];
97 $this->assertEquals('Atom draft-07 snapshot', $entry->title->text);
98 $this->assertEquals('tag:example.org,2003:3.2397',
99 $entry->id->text);
100 $this->assertEquals('2005-07-31T12:29:29Z', $entry->updated->text);
101 $this->assertEquals('2003-12-13T08:29:29-04:00',
102 $entry->published->text);
103 $this->assertEquals('Mark Pilgrim',
104 $entry->author[0]->name->text);
105 $this->assertEquals('http://example.org/',
106 $entry->author[0]->uri->text);
107 $this->assertEquals(2, count($entry->contributor));
108 $this->assertEquals('Sam Ruby',
109 $entry->contributor[0]->name->text);
110 $this->assertEquals('Joe Gregorio',
111 $entry->contributor[1]->name->text);
112 $this->assertEquals('xhtml', $entry->content->type);
116 public function testCanSetAndGetEtag()
118 $data = "W/&amp;FooBarBaz&amp;";
119 $this->enry->setEtag($data);
120 $this->assertEquals($this->enry->getEtag(), $data);
123 public function testCanSetAndgetService()
125 $data = new Zend_Gdata_App();
126 $this->enry->setService($data);
127 $this->assertEquals($this->enry->getService(), $data);
129 $data = null;
130 $this->enry->setService($data);
131 $this->assertEquals($this->enry->getService(), $data);
134 public function testsetServiceProvidesFluentInterface()
136 $result = $this->enry->setService(null);
137 $this->assertEquals($this->enry, $result);
140 public function testGetHttpClientPullsFromServiceInstance()
142 $s = new Zend_Gdata_App();
143 $this->enry->setService($s);
145 $c = new Zend_Gdata_HttpClient();
146 $s->setHttpClient($c);
147 $this->assertEquals($this->enry->getHttpClient(),
148 $s->getHttpClient());
150 $c = new Zend_Http_Client();
151 $s->setHttpClient($c);
152 $this->assertEquals($this->enry->getHttpClient(),
153 $s->getHttpClient($c));
156 public function testSetHttpClientPushesIntoServiceInstance()
158 $s = new Zend_Gdata_App();
159 $this->enry->setService($s);
161 $c = new Zend_Gdata_HttpClient();
162 $this->enry->setHttpClient($c);
163 $this->assertEquals(get_class($s->getHttpClient()),
164 'Zend_Gdata_HttpClient');
166 $c = new Zend_Http_Client();
167 $this->enry->setHttpClient($c);
168 $this->assertEquals(get_class($s->getHttpClient()),
169 'Zend_Http_Client');
172 public function testSaveSupportsGdataV2()
174 // Prepare mock response
175 $this->adapter->setResponse("HTTP/1.1 201 Created");
177 // Make sure that we're using protocol v2
178 $this->service->setMajorProtocolVersion(2);
179 $this->enry->setService($this->service);
181 // Set a URL for posting, so that save() will work
182 $editLink = new Zend_Gdata_App_extension_Link('http://example.com',
183 'edit');
184 $this->enry->setLink(array($editLink));
186 // Perform a (mock) save
187 $this->enry->save();
189 // Check to make sure that a v2 header was sent
190 $headers = $this->adapter->popRequest()->headers;
191 $found = false;
192 foreach ($headers as $header) {
193 if ($header == 'GData-Version: 2')
194 $found = true;
196 $this->assertTrue($found,
197 'GData-Version header missing or incorrect.');
200 public function testDeleteSupportsGdataV2()
202 // Prepare mock response
203 $this->adapter->setResponse("HTTP/1.1 200 OK");
205 // Make sure that we're using protocol v2
206 $this->service->setMajorProtocolVersion(2);
207 $this->enry->setService($this->service);
209 // Set a URL for posting, so that save() will work
210 $editLink = new Zend_Gdata_App_extension_Link('http://example.com',
211 'edit');
212 $this->enry->setLink(array($editLink));
214 // Perform a (mock) save
215 $this->enry->delete();
217 // Check to make sure that a v2 header was sent
218 $headers = $this->adapter->popRequest()->headers;
219 $found = false;
220 foreach ($headers as $header) {
221 if ($header == 'GData-Version: 2')
222 $found = true;
224 $this->assertTrue($found,
225 'GData-Version header missing or incorrect.');
228 public function testIfMatchHeaderCanBeSetOnSave()
230 $etagOverride = 'foo';
231 $etag = 'ABCD1234';
232 $this->service->setMajorProtocolVersion(2);
233 $this->adapter->setResponse($this->httpEntrySample);
234 $entry = $this->service->newEntry();
235 $entry->link = array(new Zend_Gdata_App_Extension_Link(
236 'http://www.example.com',
237 'edit',
238 'application/atom+xml'));
239 $entry->setEtag($etag);
240 $newEntry = $entry->save(null, null,
241 array('If-Match' => $etagOverride));
242 $headers = $this->adapter->popRequest()->headers;
243 $found = false;
244 foreach ($headers as $header) {
245 if ($header == 'If-Match: ' . $etagOverride)
246 $found = true;
248 $this->assertTrue($found,
249 'If-Match header not found or incorrect');
252 public function testIfNoneMatchHeaderCanBeSetOnSave()
254 $etagOverride = 'foo';
255 $etag = 'ABCD1234';
256 $this->service->setMajorProtocolVersion(2);
257 $this->adapter->setResponse($this->httpEntrySample);
258 $entry = $this->service->newEntry();
259 $entry->link = array(new Zend_Gdata_App_Extension_Link(
260 'http://www.example.com',
261 'edit',
262 'application/atom+xml'));
263 $entry->setEtag($etag);
264 $newEntry = $entry->save(null, null,
265 array('If-None-Match' => $etagOverride));
266 $headers = $this->adapter->popRequest()->headers;
267 $found = false;
268 foreach ($headers as $header) {
269 if ($header == 'If-None-Match: ' . $etagOverride)
270 $found = true;
272 $this->assertTrue($found,
273 'If-None-Match header not found or incorrect');
276 public function testCanSetUriOnSave()
278 $uri = 'http://example.net/foo/bar';
279 $this->adapter->setResponse($this->httpEntrySample);
280 $entry = $this->service->newEntry();
281 $newEntry = $entry->save($uri);
282 $request = $this->adapter->popRequest();
283 $uriObject = Zend_Uri_Http::fromString($uri);
284 $uriObject->setPort('80');
285 $this->assertEquals($uriObject, $request->uri);
288 public function testCanSetClassnameOnSave()
290 $className = 'Zend_Gdata_Entry';
291 $this->adapter->setResponse($this->httpEntrySample);
292 $entry = $this->service->newEntry();
293 $entry->link = array(new Zend_Gdata_App_Extension_Link(
294 'http://www.example.com',
295 'edit',
296 'application/atom+xml'));
297 $newEntry = $entry->save(null, $className);
298 $this->assertEquals($className, get_class($newEntry));
301 public function testIfNoneMatchSetOnReload()
303 $etag = 'ABCD1234';
304 $this->adapter->setResponse($this->httpEntrySample);
305 $entry = $this->service->newEntry();
306 $entry->link = array(new Zend_Gdata_App_Extension_Link(
307 'http://www.example.com',
308 'edit',
309 'application/atom+xml'));
310 $entry->setEtag($etag);
311 $newEntry = $entry->reload();
312 $headers = $this->adapter->popRequest()->headers;
313 $found = false;
314 foreach ($headers as $header) {
315 if ($header == 'If-None-Match: ' . $etag)
316 $found = true;
318 $this->assertTrue($found,
319 'If-None-Match header not found or incorrect');
322 public function testIfNoneMatchCanBeSetOnReload()
324 $etagOverride = 'foo';
325 $etag = 'ABCD1234';
326 $this->adapter->setResponse($this->httpEntrySample);
327 $entry = $this->service->newEntry();
328 $entry->link = array(new Zend_Gdata_App_Extension_Link(
329 'http://www.example.com',
330 'edit',
331 'application/atom+xml'));
332 $entry->setEtag($etag);
333 $newEntry = $entry->reload(null, null,
334 array('If-None-Match' => $etagOverride));
335 $headers = $this->adapter->popRequest()->headers;
336 $found = false;
337 foreach ($headers as $header) {
338 if ($header == 'If-None-Match: ' . $etagOverride)
339 $found = true;
341 $this->assertTrue($found,
342 'If-None-Match header not found or incorrect');
345 public function testReloadReturnsEntryObject()
347 $etag = 'ABCD1234';
348 $this->adapter->setResponse($this->httpEntrySample);
349 $entry = $this->service->newEntry();
350 $entry->link = array(new Zend_Gdata_App_Extension_Link(
351 'http://www.example.com',
352 'edit',
353 'application/atom+xml'));
354 $entry->setEtag($etag);
355 $newEntry = $entry->reload();
356 $this->assertEquals('Zend_Gdata_App_Entry', get_class($newEntry));
359 public function testReloadPopulatesEntryObject()
361 $etag = 'ABCD1234';
362 $this->adapter->setResponse($this->httpEntrySample);
363 $entry = $this->service->newEntry();
364 $entry->link = array(new Zend_Gdata_App_Extension_Link(
365 'http://www.example.com',
366 'edit',
367 'application/atom+xml'));
368 $entry->setEtag($etag);
369 $newEntry = $entry->reload();
370 $this->assertEquals('Hello world', $newEntry->title->text);
373 public function testReloadDoesntThrowExceptionIfNoEtag()
375 $this->adapter->setResponse($this->httpEntrySample);
376 $entry = $this->service->newEntry();
377 $entry->link = array(new Zend_Gdata_App_Extension_Link(
378 'http://www.example.com',
379 'edit',
380 'application/atom+xml'));
381 $newEntry = $entry->reload();
382 $this->assertEquals('Zend_Gdata_App_Entry', get_class($newEntry));
385 public function testReloadExtractsURIFromEditLink()
387 $expectedUri = 'http://www.example.com';
388 $etag = 'ABCD1234';
389 $this->service->setMajorProtocolVersion(2);
390 $this->adapter->setResponse($this->httpEntrySample);
391 $entry = $this->service->newEntry();
392 $entry->link = array(new Zend_Gdata_App_Extension_Link(
393 $expectedUri,
394 'edit',
395 'application/atom+xml'));
396 $entry->setEtag($etag);
397 $newEntry = $entry->reload();
398 $requestUri = $this->adapter->popRequest()->uri;
399 $expectedUriObject = Zend_Uri_Http::fromString($expectedUri);
400 $expectedUriObject->setPort('80');
401 $this->assertEquals($expectedUriObject, $requestUri);
404 public function testReloadAllowsCustomURI()
406 $uriOverride = 'http://www.example.org';
407 $etag = 'ABCD1234';
408 $this->service->setMajorProtocolVersion(2);
409 $this->adapter->setResponse($this->httpEntrySample);
410 $entry = $this->service->newEntry();
411 $entry->link = array(new Zend_Gdata_App_Extension_Link(
412 'http://www.example.com',
413 'edit',
414 'application/atom+xml'));
415 $entry->setEtag($etag);
416 $newEntry = $entry->reload($uriOverride);
417 $requestUri = $this->adapter->popRequest()->uri;
418 $uriOverrideObject = Zend_Uri_Http::fromString($uriOverride);
419 $uriOverrideObject->setPort('80');
420 $this->assertEquals($uriOverrideObject, $requestUri);
423 public function testReloadReturnsNullIfEntryNotModified()
425 $etag = 'ABCD1234';
426 $this->service->setMajorProtocolVersion(2);
427 $this->adapter->setResponse('HTTP/1.1 304 Not Modified');
428 $entry = $this->service->newEntry();
429 $entry->link = array(new Zend_Gdata_App_Extension_Link(
430 'http://www.example.com',
431 'edit',
432 'application/atom+xml'));
433 $entry->setEtag($etag);
434 $newEntry = $entry->reload();
435 $this->assertEquals(null, $newEntry);
438 public function testCanSetReloadReturnClassname()
440 $className = 'Zend_Gdata_Entry';
441 $etag = 'ABCD1234';
442 $this->service->setMajorProtocolVersion(2);
443 $this->adapter->setResponse($this->httpEntrySample);
444 $entry = $this->service->newEntry();
445 $entry->link = array(new Zend_Gdata_App_Extension_Link(
446 'http://www.example.com',
447 'edit',
448 'application/atom+xml'));
449 $entry->setEtag($etag);
450 $newEntry = $entry->reload(null, $className);
451 $this->assertEquals($className, get_class($newEntry));
454 public function testReloadInheritsClassname()
456 $className = 'Zend_Gdata_Entry';
457 $etag = 'ABCD1234';
458 $this->service->setMajorProtocolVersion(2);
459 $this->adapter->setResponse($this->httpEntrySample);
460 $entry = new $className;
461 $entry->setService($this->service);
462 $entry->link = array(new Zend_Gdata_App_Extension_Link(
463 'http://www.example.com',
464 'edit',
465 'application/atom+xml'));
466 $entry->setEtag($etag);
467 $newEntry = $entry->reload();
468 $this->assertEquals($className, get_class($newEntry));
471 public function testCanSetMajorProtocolVersion()
473 $expectedVersion = 42;
474 $entry = $this->service->newEntry();
475 $entry->setMajorProtocolVersion($expectedVersion);
476 $receivedVersion = $entry->getMajorProtocolVersion();
477 $this->assertEquals($expectedVersion, $receivedVersion);
480 public function testCanSetMinorProtocolVersion()
482 $expectedVersion = 42;
483 $entry = $this->service->newEntry();
484 $entry->setMinorProtocolVersion($expectedVersion);
485 $receivedVersion = $entry->getMinorProtocolVersion();
486 $this->assertEquals($expectedVersion, $receivedVersion);
489 public function testMajorProtocolVersionCannotBeZero()
491 $expectedVersion = 0;
492 $entry = $this->service->newEntry();
493 $this->setExpectedException('Zend_Gdata_App_InvalidArgumentException');
494 $entry->setMajorProtocolVersion($expectedVersion);
497 public function testMajorProtocolVersionCannotBeNegative()
499 $expectedVersion = -1;
500 $entry = $this->service->newEntry();
501 $this->setExpectedException('Zend_Gdata_App_InvalidArgumentException');
502 $entry->setMajorProtocolVersion($expectedVersion);
505 public function testMajorProtocolVersionMayBeNull()
507 $expectedVersion = null;
508 $entry = $this->service->newEntry();
509 $entry->setMajorProtocolVersion($expectedVersion);
510 $receivedVersion = $entry->getMajorProtocolVersion();
511 $this->assertNull($receivedVersion);
514 public function testMinorProtocolVersionMayBeZero()
516 $expectedVersion = 0;
517 $entry = $this->service->newEntry();
518 $entry->setMinorProtocolVersion($expectedVersion);
519 $receivedVersion = $entry->getMinorProtocolVersion();
520 $this->assertEquals($expectedVersion, $receivedVersion);
523 public function testMinorProtocolVersionCannotBeNegative()
525 $expectedVersion = -1;
526 $entry = $this->service->newEntry();
527 $this->setExpectedException('Zend_Gdata_App_InvalidArgumentException');
528 $entry->setMinorProtocolVersion($expectedVersion);
531 public function testMinorProtocolVersionMayBeNull()
533 $expectedVersion = null;
534 $entry = $this->service->newEntry();
535 $entry->setMinorProtocolVersion($expectedVersion);
536 $receivedVersion = $entry->getMinorProtocolVersion();
537 $this->assertNull($receivedVersion);
540 public function testDefaultMajorProtocolVersionIs1()
542 $entry = $this->service->newEntry();
543 $this->assertEquals(1, $entry->getMajorProtocolVersion());
546 public function testDefaultMinorProtocolVersionIsNull()
548 $entry = $this->service->newEntry();
549 $this->assertNull($entry->getMinorProtocolVersion());
552 public function testLookupNamespaceUsesCurrentVersion()
554 $prefix = 'test';
555 $v1TestString = 'TEST-v1';
556 $v2TestString = 'TEST-v2';
558 Zend_Gdata_App_Base::flushNamespaceLookupCache();
559 $entry = $this->service->newEntry();
560 $entry->registerNamespace($prefix, $v1TestString, 1, 0);
561 $entry->registerNamespace($prefix, $v2TestString, 2, 0);
562 $entry->setMajorProtocolVersion(1);
563 $result = $entry->lookupNamespace($prefix);
564 $this->assertEquals($v1TestString, $result);
565 $entry->setMajorProtocolVersion(2);
566 $result = $entry->lookupNamespace($prefix);
567 $this->assertEquals($v2TestString, $result);
568 $entry->setMajorProtocolVersion(null); // Should default to latest
569 $result = $entry->lookupNamespace($prefix);
570 $this->assertEquals($v2TestString, $result);
573 public function testLookupNamespaceObeysParentBehavior()
575 $prefix = 'test';
576 $testString10 = 'TEST-v1-0';
577 $testString20 = 'TEST-v2-0';
578 $testString11 = 'TEST-v1-1';
579 $testString21 = 'TEST-v2-1';
580 $testString12 = 'TEST-v1-2';
581 $testString22 = 'TEST-v2-2';
583 Zend_Gdata_App_Base::flushNamespaceLookupCache();
584 $entry = $this->service->newEntry();
585 $entry->registerNamespace($prefix, $testString10, 1, 0);
586 $entry->registerNamespace($prefix, $testString20, 2, 0);
587 $entry->registerNamespace($prefix, $testString11, 1, 1);
588 $entry->registerNamespace($prefix, $testString21, 2, 1);
589 $entry->registerNamespace($prefix, $testString12, 1, 2);
590 $entry->registerNamespace($prefix, $testString22, 2, 2);
592 // Assumes default version (1)
593 $result = $entry->lookupNamespace($prefix, 1, null);
594 $this->assertEquals($testString12, $result);
595 $result = $entry->lookupNamespace($prefix, 2, null);
596 $this->assertEquals($testString22, $result);
597 $result = $entry->lookupNamespace($prefix, 1, 1);
598 $this->assertEquals($testString11, $result);
599 $result = $entry->lookupNamespace($prefix, 2, 1);
600 $this->assertEquals($testString21, $result);
601 $result = $entry->lookupNamespace($prefix, null, null);
602 $this->assertEquals($testString12, $result);
603 $result = $entry->lookupNamespace($prefix, null, 1);
604 $this->assertEquals($testString11, $result);
606 // Override to retrieve latest version
607 $entry->setMajorProtocolVersion(null);
608 $result = $entry->lookupNamespace($prefix, null, null);
609 $this->assertEquals($testString22, $result);
610 $result = $entry->lookupNamespace($prefix, null, 1);
611 $this->assertEquals($testString21, $result);