GdataOnlineTest.php 13 KB

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