GdataOnlineTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  22. require_once 'Zend/Http/Client.php';
  23. require_once 'Zend/Gdata.php';
  24. require_once 'Zend/Gdata/App/MediaEntry.php';
  25. require_once 'Zend/Gdata/App/MediaFileSource.php';
  26. require_once 'Zend/Gdata/ClientLogin.php';
  27. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  28. /**
  29. * @package Zend_Gdata
  30. * @subpackage UnitTests
  31. */
  32. class Zend_Gdata_GdataOnlineTest extends PHPUnit_Framework_TestCase
  33. {
  34. private $blog = null; // blog ID from config
  35. public function setUp()
  36. {
  37. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  38. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  39. $this->blog = constant('TESTS_ZEND_GDATA_BLOG_ID');
  40. $service = 'blogger';
  41. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  42. $this->gdata = new Zend_Gdata($client);
  43. $this->gdata->setMajorProtocolVersion(2);
  44. }
  45. public function testPostAndDeleteByEntry()
  46. {
  47. $postUrl = 'http://www.blogger.com/feeds/' . $this->blog .
  48. '/posts/default';
  49. $entry = $this->gdata->newEntry();
  50. $entry->title = $this->gdata->newTitle('PHP test blog post');
  51. $entry->content = $this->gdata->newContent('Blog post content...');
  52. $insertedEntry = $this->gdata->insertEntry($entry, $postUrl);
  53. $this->assertEquals('PHP test blog post', $insertedEntry->title->text);
  54. $this->assertEquals('Blog post content...',
  55. $insertedEntry->content->text);
  56. $this->assertTrue(
  57. strpos($insertedEntry->getEditLink()->href, 'http') === 0);
  58. $this->gdata->delete($insertedEntry);
  59. }
  60. public function testPostAndDeleteByUrl()
  61. {
  62. $postUrl = 'http://www.blogger.com/feeds/' . $this->blog .
  63. '/posts/default';
  64. $entry = $this->gdata->newEntry();
  65. $entry->title = $this->gdata->newTitle('PHP test blog post');
  66. $entry->content = $this->gdata->newContent('Blog post content...');
  67. $insertedEntry = $this->gdata->insertEntry($entry, $postUrl);
  68. $this->assertTrue(
  69. strpos($insertedEntry->getEditLink()->href, 'http') === 0);
  70. $this->gdata->delete($insertedEntry->getEditLink()->href);
  71. }
  72. public function testPostRetrieveEntryAndDelete()
  73. {
  74. $postUrl = 'http://www.blogger.com/feeds/' . $this->blog .
  75. '/posts/default';
  76. $entry = $this->gdata->newEntry();
  77. $entry->title = $this->gdata->newTitle(' PHP test blog post ');
  78. $this->assertTrue(isset($entry->title));
  79. $entry->content = $this->gdata->newContent('Blog post content...');
  80. /* testing getText and __toString */
  81. $this->assertEquals("PHP test blog post",
  82. $entry->title->getText());
  83. $this->assertEquals(" PHP test blog post ",
  84. $entry->title->getText(false));
  85. $this->assertEquals($entry->title->getText(),
  86. $entry->title->__toString());
  87. $insertedEntry = $this->gdata->insertEntry($entry, $postUrl);
  88. $retrievedEntryQuery = $this->gdata->newQuery(
  89. $insertedEntry->getSelfLink()->href);
  90. $retrievedEntry = $this->gdata->getEntry($retrievedEntryQuery);
  91. $this->assertTrue(
  92. strpos($retrievedEntry->getEditLink()->href, 'http') === 0);
  93. $this->gdata->delete($retrievedEntry);
  94. }
  95. public function testPostUpdateAndDeleteEntry()
  96. {
  97. $postUrl = 'http://www.blogger.com/feeds/' . $this->blog .
  98. '/posts/default';
  99. $entry = $this->gdata->newEntry();
  100. $entry->title = $this->gdata->newTitle('PHP test blog post');
  101. $entry->content = $this->gdata->newContent('Blog post content...');
  102. $insertedEntry = $this->gdata->insertEntry($entry, $postUrl);
  103. $this->assertTrue(
  104. strpos($insertedEntry->getEditLink()->href, 'http') === 0);
  105. $insertedEntry->title->text = 'PHP test blog post modified';
  106. $updatedEntry = $this->gdata->updateEntry($insertedEntry);
  107. $this->assertEquals('PHP test blog post modified',
  108. $updatedEntry->title->text);
  109. $updatedEntry->title->text = 'PHP test blog post modified twice';
  110. // entry->saveXML() and entry->getXML() should be the same
  111. $this->assertEquals($updatedEntry->saveXML(),
  112. $updatedEntry->getXML());
  113. $newlyUpdatedEntry = $this->gdata->updateEntry($updatedEntry);
  114. $this->assertEquals('PHP test blog post modified twice',
  115. $updatedEntry->title->text);
  116. $updatedEntry->delete();
  117. }
  118. public function testFeedImplementation()
  119. {
  120. $blogsUrl = 'http://www.blogger.com/feeds/default/blogs';
  121. $blogsQuery = $this->gdata->newQuery($blogsUrl);
  122. $retrievedFeed = $this->gdata->getFeed($blogsQuery);
  123. // rewind the retrieved feed first
  124. $retrievedFeed->rewind();
  125. // Make sure the iterator and array impls match
  126. $entry1 = $retrievedFeed->current();
  127. $entry2 = $retrievedFeed[0];
  128. $this->assertEquals($entry1, $entry2);
  129. /*
  130. TODO: Fix these tests
  131. // Test ArrayAccess interface
  132. $firstBlogTitle = $retrievedFeed[0]->title->text;
  133. $entries = $retrievedFeed->entry;
  134. $entries[0]->title->text = $firstBlogTitle . "**";
  135. $retrievedFeed[0] = $entries[0];
  136. $this->assertEquals($retrievedFeed->entry[0]->title->text,
  137. $retrievedFeed[0]->title->text);
  138. $this->assertEquals($firstBlogTitle . "**",
  139. $retrievedFeed[0]->title->text);
  140. */
  141. }
  142. public function testBadFeedRetrieval()
  143. {
  144. $feed = $this->gdata->newFeed();
  145. try {
  146. $returnedFeed = $this->gdata->getFeed($feed);
  147. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  148. // we're expecting to cause an exception here
  149. }
  150. }
  151. public function testBadEntryRetrieval()
  152. {
  153. $entry = $this->gdata->newEntry();
  154. try {
  155. $returnedEntry = $this->gdata->getEntry($entry);
  156. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  157. // we're expecting to cause an exception here
  158. }
  159. }
  160. public function testMediaUpload()
  161. {
  162. // the standard sevice for Gdata testing is Blogger, due to the strong
  163. // match to the standard Gdata/APP protocol. However, Blogger doesn't
  164. // currently support media uploads, so we're using Picasa Web Albums
  165. // for this test instead
  166. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  167. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  168. $this->blog = constant('TESTS_ZEND_GDATA_BLOG_ID');
  169. $service = 'lh2';
  170. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  171. $gd = new Zend_Gdata($client);
  172. // setup the photo content
  173. $fs = $gd->newMediaFileSource('Zend/Gdata/_files/testImage.jpg');
  174. $fs->setContentType('image/jpeg');
  175. // create a new picasa album
  176. $albumEntry = $gd->newEntry();
  177. $albumEntry->setTitle($gd->newTitle('My New Test Album'));
  178. $albumEntry->setCategory(array($gd->newCategory(
  179. 'http://schemas.google.com/photos/2007#album',
  180. 'http://schemas.google.com/g/2005#kind'
  181. )));
  182. $createdAlbumEntry = $gd->insertEntry($albumEntry,
  183. 'http://picasaweb.google.com/data/feed/api/user/default');
  184. $this->assertEquals('My New Test Album',
  185. $createdAlbumEntry->title->text);
  186. $albumUrl = $createdAlbumEntry->getLink('http://schemas.google.com/g/2005#feed')->href;
  187. // post the photo to the new album, without any metadata
  188. // other than the slug
  189. // add a slug header to the media file source
  190. $fs->setSlug('Going to the park');
  191. $createdPhotoBinaryOnly = $gd->insertEntry($fs, $albumUrl);
  192. $this->assertEquals('Going to the park',
  193. $createdPhotoBinaryOnly->title->text);
  194. // post the photo to the new album along with the entry
  195. // remove slug header from the media file source
  196. $fs->setSlug(null);
  197. // setup an entry with metadata
  198. $mediaEntry = $gd->newMediaEntry();
  199. $mediaEntry->setMediaSource($fs);
  200. $mediaEntry->setTitle($gd->newTitle('My New Test Photo'));
  201. $mediaEntry->setSummary($gd->newSummary('My New Test Photo Summary'));
  202. $mediaEntry->setCategory(array($gd->newCategory(
  203. 'http://schemas.google.com/photos/2007#photo ',
  204. 'http://schemas.google.com/g/2005#kind'
  205. )));
  206. $createdPhotoMultipart = $gd->insertEntry($mediaEntry, $albumUrl);
  207. $this->assertEquals('My New Test Photo',
  208. $createdPhotoMultipart->title->text);
  209. // cleanup and remove the album
  210. // first we wait 5 seconds
  211. sleep(5);
  212. try {
  213. $albumEntry->delete();
  214. } catch (Zend_Gdata_App_Exception $e) {
  215. $this->fail('Tried to delete the test album, got exception: ' .
  216. $e->getMessage());
  217. }
  218. }
  219. function testIsAuthenticated()
  220. {
  221. $this->assertTrue($this->gdata->isAuthenticated());
  222. }
  223. function testRetrieveNextAndPreviousFeedsFromService()
  224. {
  225. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  226. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  227. $this->blog = constant('TESTS_ZEND_GDATA_BLOG_ID');
  228. $service = 'youtube';
  229. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  230. $gd = new Zend_Gdata($client);
  231. $feed = $gd->getFeed(
  232. 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured',
  233. 'Zend_Gdata_App_Feed');
  234. $this->assertNotNull($feed);
  235. $this->assertTrue($feed instanceof Zend_Gdata_App_Feed);
  236. $this->assertEquals($feed->count(), 25);
  237. $nextFeed = $gd->getNextFeed($feed);
  238. $this->assertNotNull($nextFeed);
  239. $this->assertTrue($nextFeed instanceof Zend_Gdata_App_Feed);
  240. $this->assertEquals($nextFeed->count(), 25);
  241. $previousFeed = $gd->getPreviousFeed($nextFeed);
  242. $this->assertNotNull($previousFeed);
  243. $this->assertTrue($previousFeed instanceof Zend_Gdata_App_Feed);
  244. $this->assertEquals($previousFeed->count(), 25);
  245. }
  246. function testRetrieveNextFeedAndPreviousFeedsFromFeed()
  247. {
  248. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  249. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  250. $this->blog = constant('TESTS_ZEND_GDATA_BLOG_ID');
  251. $service = 'youtube';
  252. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  253. $gd = new Zend_Gdata($client);
  254. $feed = $gd->getFeed(
  255. 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured',
  256. 'Zend_Gdata_App_Feed');
  257. $nextFeed = $feed->getNextFeed();
  258. $this->assertNotNull($nextFeed);
  259. $this->assertTrue($nextFeed instanceof Zend_Gdata_App_Feed);
  260. $this->assertEquals($nextFeed->count(), 25);
  261. $previousFeed = $nextFeed->getPreviousFeed();
  262. $this->assertNotNull($previousFeed);
  263. $this->assertTrue($previousFeed instanceof Zend_Gdata_App_Feed);
  264. $this->assertEquals($previousFeed->count(), 25);
  265. }
  266. public function testDisableXMLToObjectMappingReturnsStringForFeed()
  267. {
  268. $gdata = new Zend_Gdata();
  269. $gdata->useObjectMapping(false);
  270. $xmlString = $gdata->getFeed(
  271. 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated');
  272. $this->assertEquals('string', gettype($xmlString));
  273. }
  274. public function testDisableXMLToObjectMappingReturnsStringForEntry()
  275. {
  276. $gdata = new Zend_Gdata();
  277. $gdata->useObjectMapping(false);
  278. $xmlString = $gdata->getFeed(
  279. 'http://gdata.youtube.com/feeds/api/videos/O4SWAfisH-8');
  280. $this->assertEquals('string', gettype($xmlString));
  281. }
  282. public function testDisableAndReEnableXMLToObjectMappingReturnsObject()
  283. {
  284. $gdata = new Zend_Gdata();
  285. $gdata->useObjectMapping(false);
  286. $xmlString = $gdata->getEntry(
  287. 'http://gdata.youtube.com/feeds/api/videos/O4SWAfisH-8');
  288. $this->assertEquals('string', gettype($xmlString));
  289. $gdata->useObjectMapping(true);
  290. $entry = $gdata->getEntry(
  291. 'http://gdata.youtube.com/feeds/api/videos/O4SWAfisH-8');
  292. $this->assertTrue($entry instanceof Zend_Gdata_Entry);
  293. }
  294. }