GdataOnlineTest.php 13 KB

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