YouTubeOnlineTest.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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/Gdata/YouTube.php';
  23. require_once 'Zend/Gdata/YouTube/VideoQuery.php';
  24. require_once 'Zend/Gdata/ClientLogin.php';
  25. /**
  26. * @package Zend_Gdata
  27. * @subpackage UnitTests
  28. */
  29. class Zend_Gdata_YouTubeOnlineTest extends PHPUnit_Framework_TestCase
  30. {
  31. public function setUp()
  32. {
  33. $this->ytAccount = constant('TESTS_ZEND_GDATA_YOUTUBE_ACCOUNT');
  34. $this->subscriptionTypeSchema = 'http://gdata.youtube.com/schemas/' .
  35. '2007/subscriptiontypes.cat';
  36. $this->gdata = new Zend_Gdata_YouTube();
  37. }
  38. public function tearDown()
  39. {
  40. }
  41. public function testRetrieveSubScriptionFeed()
  42. {
  43. $feed = $this->gdata->getSubscriptionFeed($this->ytAccount);
  44. $this->assertTrue($feed->totalResults->text > 0);
  45. $this->assertEquals('Subscriptions of ' . $this->ytAccount,
  46. $feed->title->text);
  47. $this->assertTrue(count($feed->entry) > 0);
  48. foreach ($feed->entry as $entry) {
  49. $this->assertTrue($entry->title->text != '');
  50. }
  51. }
  52. public function testRetrieveContactFeed()
  53. {
  54. $feed = $this->gdata->getContactFeed($this->ytAccount);
  55. $this->assertTrue($feed->totalResults->text > 0);
  56. $this->assertEquals('Contacts of ' . $this->ytAccount,
  57. $feed->title->text);
  58. $this->assertTrue(count($feed->entry) > 0);
  59. foreach ($feed->entry as $entry) {
  60. $this->assertTrue($entry->title->text != '');
  61. }
  62. $this->assertEquals('ytgdatatest1', $feed->entry[0]->username->text);
  63. }
  64. public function testRetrieveUserVideos()
  65. {
  66. $feed = $this->gdata->getUserUploads($this->ytAccount);
  67. $this->assertEquals('Uploads by ' . $this->ytAccount,
  68. $feed->title->text);
  69. $this->assertTrue(count($feed->entry) === 1);
  70. }
  71. public function testRetrieveVideoFeed()
  72. {
  73. $feed = $this->gdata->getVideoFeed();
  74. $query = new Zend_Gdata_YouTube_VideoQuery();
  75. $query->setVideoQuery('puppy');
  76. $feed = $this->gdata->getVideoFeed($query);
  77. foreach ($feed as $videoEntry) {
  78. $videoResponsesLink = $videoEntry->getVideoResponsesLink();
  79. $videoRatingsLink = $videoEntry->getVideoRatingsLink();
  80. $videoComplaintsLink = $videoEntry->getVideoComplaintsLink();
  81. }
  82. $feed = $this->gdata->getVideoFeed($query->getQueryUrl());
  83. }
  84. public function testRetrieveVideoEntry()
  85. {
  86. $entry = $this->gdata->getVideoEntry('66wj2g5yz0M');
  87. $this->assertEquals('TestMovie', $entry->title->text);
  88. $entry = $this->gdata->getVideoEntry(null, 'http://gdata.youtube.com/feeds/api/videos/66wj2g5yz0M');
  89. $this->assertEquals('TestMovie', $entry->title->text);
  90. }
  91. public function testRetrieveOtherFeeds()
  92. {
  93. $feed = $this->gdata->getRelatedVideoFeed('66wj2g5yz0M');
  94. $feed = $this->gdata->getVideoResponseFeed('66wj2g5yz0M');
  95. $feed = $this->gdata->getVideoCommentFeed('66wj2g5yz0M');
  96. $feed = $this->gdata->getWatchOnMobileVideoFeed();
  97. $feed = $this->gdata->getUserFavorites($this->ytAccount);
  98. }
  99. public function testRetrieveUserProfile()
  100. {
  101. $entry = $this->gdata->getUserProfile($this->ytAccount);
  102. $this->assertEquals($this->ytAccount . ' Channel', $entry->title->text);
  103. $this->assertEquals($this->ytAccount, $entry->username->text);
  104. $this->assertEquals('I\'m a lonely test account, with little to do but sit around and wait for people to use me. I get bored in between releases and often sleep to pass the time. Please use me more often, as I love to show off my talent in breaking your code.',
  105. $entry->description->text);
  106. $this->assertEquals(32, $entry->age->text);
  107. $this->assertEquals('crime and punishment, ps i love you, the stand', $entry->books->text);
  108. $this->assertEquals('Google', $entry->company->text);
  109. $this->assertEquals('software engineering, information architecture, photography, travel', $entry->hobbies->text);
  110. $this->assertEquals('Mountain View, CA', $entry->hometown->text);
  111. $this->assertEquals('San Francisco, CA 94114, US', $entry->location->text);
  112. $this->assertEquals('monk, heroes, law and order, top gun', $entry->movies->text);
  113. $this->assertEquals('imogen heap, frou frou, thievory corp, morcheeba, barenaked ladies', $entry->music->text);
  114. $this->assertEquals('Developer Programs', $entry->occupation->text);
  115. $this->assertEquals('University of the World', $entry->school->text);
  116. $this->assertEquals('f', $entry->gender->text);
  117. $this->assertEquals('taken', $entry->relationship->text);
  118. }
  119. public function testRetrieveAndUpdatePlaylistList()
  120. {
  121. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  122. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  123. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  124. $authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
  125. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  126. $username = $user,
  127. $password = $pass,
  128. $service = $service,
  129. $client = null,
  130. $source = 'Google-UnitTests-1.0',
  131. $loginToken = null,
  132. $loginCaptcha = null,
  133. $authenticationURL);
  134. $this->gdata = new Zend_Gdata_YouTube($httpClient,
  135. 'Google-UnitTests-1.0', 'ytapi-gdataops-12345-u78960r7-0',
  136. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7Eyu1IuvkioESq' .
  137. 'zRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  138. $this->gdata->setMajorProtocolVersion(2);
  139. $feed = $this->gdata->getPlaylistListFeed($this->ytAccount);
  140. $this->assertTrue($feed->totalResults->text > 0);
  141. $this->assertEquals('Playlists of ' . $this->ytAccount,
  142. $feed->title->text);
  143. $this->assertTrue(count($feed->entry) > 0);
  144. $i = 0;
  145. foreach ($feed->entry as $entry) {
  146. $this->assertTrue($entry->title->text != '');
  147. if ($i == 0) {
  148. $entry->title->setText('new playlist title');
  149. $entry->save();
  150. }
  151. $i++;
  152. }
  153. }
  154. public function testRetrievePlaylistV2()
  155. {
  156. $this->gdata->setMajorProtocolVersion(2);
  157. $feed = $this->gdata->getPlaylistListFeed($this->ytAccount);
  158. $firstEntry = $feed->entries[0];
  159. $this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_PlaylistListEntry);
  160. $this->assertTrue($firstEntry->getSummary()->text != null);
  161. }
  162. public function testRetrievePlaylistVideoFeed()
  163. {
  164. $listFeed = $this->gdata->getPlaylistListFeed($this->ytAccount);
  165. $feed = $this->gdata->getPlaylistVideoFeed($listFeed->entry[0]->feedLink[0]->href);
  166. $this->assertTrue($feed->totalResults->text > 0);
  167. $this->assertTrue(count($feed->entry) > 0);
  168. foreach ($feed->entry as $entry) {
  169. $this->assertTrue($entry->title->text != '');
  170. }
  171. }
  172. public function testRetrieveTopRatedVideos()
  173. {
  174. $feed = $this->gdata->getTopRatedVideoFeed();
  175. $this->assertTrue($feed->totalResults->text > 10);
  176. $this->assertEquals('Top Rated', $feed->title->text);
  177. $this->assertTrue(count($feed->entry) > 0);
  178. foreach ($feed->entry as $entry) {
  179. $this->assertTrue($entry->rating->average > 3);
  180. $this->assertEquals(1, $entry->rating->min);
  181. $this->assertEquals(5, $entry->rating->max);
  182. $this->assertTrue($entry->rating->numRaters > 2);
  183. }
  184. }
  185. public function testRetrieveTopRatedVideosV2()
  186. {
  187. $this->gdata->setMajorProtocolVersion(2);
  188. $feed = $this->gdata->getTopRatedVideoFeed();
  189. $client = $this->gdata->getHttpClient();
  190. $positionOfAPIProjection = strpos(
  191. $client->getLastRequest(), "/feeds/api/");
  192. $this->assertTrue(is_numeric($positionOfAPIProjection));
  193. }
  194. public function testRetrieveMostViewedVideosV2()
  195. {
  196. $this->gdata->setMajorProtocolVersion(2);
  197. $feed = $this->gdata->getMostViewedVideoFeed();
  198. $client = $this->gdata->getHttpClient();
  199. $positionOfAPIProjection = strpos(
  200. $client->getLastRequest(), "/feeds/api/");
  201. $this->assertTrue(is_numeric($positionOfAPIProjection));
  202. }
  203. public function testRetrieveRecentlyFeaturedVideosV2()
  204. {
  205. $this->gdata->setMajorProtocolVersion(2);
  206. $feed = $this->gdata->getRecentlyFeaturedVideoFeed();
  207. $client = $this->gdata->getHttpClient();
  208. $positionOfAPIProjection = strpos(
  209. $client->getLastRequest(), "/feeds/api/");
  210. $this->assertTrue(is_numeric($positionOfAPIProjection));
  211. }
  212. public function testWatchOnMobileVideosV2()
  213. {
  214. $this->gdata->setMajorProtocolVersion(2);
  215. $feed = $this->gdata->getWatchOnMobileVideoFeed();
  216. $client = $this->gdata->getHttpClient();
  217. $positionOfAPIProjection = strpos(
  218. $client->getLastRequest(), "/feeds/api/");
  219. $this->assertTrue(is_numeric($positionOfAPIProjection));
  220. }
  221. public function testRetrieveMostViewedVideos()
  222. {
  223. $feed = $this->gdata->getMostViewedVideoFeed();
  224. $this->assertTrue($feed->totalResults->text > 10);
  225. $this->assertEquals('Most Viewed', $feed->title->text);
  226. $this->assertTrue(count($feed->entry) > 0);
  227. foreach ($feed->entry as $entry) {
  228. if ($entry->rating) {
  229. $this->assertEquals(1, $entry->rating->min);
  230. $this->assertEquals(5, $entry->rating->max);
  231. }
  232. }
  233. }
  234. public function testPerformV2Query_Location()
  235. {
  236. $this->gdata->setMajorProtocolVersion(2);
  237. $query = $this->gdata->newVideoQuery();
  238. // Setting location to New York City
  239. $query->setLocation('-37.0625,-95.677068');
  240. $query->setLocationRadius('1000km');
  241. $videoFeed = $this->gdata->getVideoFeed($query);
  242. $this->assertTrue(count($videoFeed->entry) > 0,
  243. 'Could not retrieve a single entry for location search:' .
  244. $query->getQueryUrl(2));
  245. }
  246. public function testPerformV2Query_SafeSearch()
  247. {
  248. $this->gdata->setMajorProtocolVersion(2);
  249. $query = $this->gdata->newVideoQuery();
  250. $query->setSafeSearch('strict');
  251. $videoFeed = $this->gdata->getVideoFeed($query);
  252. $this->assertTrue(count($videoFeed->entry) > 0,
  253. 'Could not retrieve a single entry for safeSearch=strict search:' .
  254. $query->getQueryUrl(2));
  255. }
  256. public function testPeformV2Query_Uploader()
  257. {
  258. $this->gdata->setMajorProtocolVersion(2);
  259. $query = $this->gdata->newVideoQuery();
  260. $query->setUploader('partner');
  261. $videoFeed = $this->gdata->getVideoFeed($query);
  262. $this->assertTrue(count($videoFeed->entry) > 0,
  263. 'Could not retrieve a single entry for uploader=partner search:' .
  264. $query->getQueryUrl(2));
  265. foreach($videoFeed as $videoEntry) {
  266. $mg = $videoEntry->getMediaGroup();
  267. $this->assertEquals('partner',
  268. $mg->getMediaCredit()->getYTtype());
  269. }
  270. }
  271. public function testAddUpdateAndDeletePlaylistV2()
  272. {
  273. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  274. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  275. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  276. $authenticationURL =
  277. 'https://www.google.com/youtube/accounts/ClientLogin';
  278. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  279. $username = $user,
  280. $password = $pass,
  281. $service = $service,
  282. $client = null,
  283. $source = 'Google-UnitTests-1.0',
  284. $loginToken = null,
  285. $loginCaptcha = null,
  286. $authenticationURL);
  287. $yt = new Zend_Gdata_YouTube(
  288. $httpClient, 'Google-UnitTests-1.0',
  289. 'ytapi-gdataops-12345-u78960r7-0',
  290. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  291. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  292. $yt->setMajorProtocolVersion(2);
  293. $feed = $yt->getPlaylistListFeed($this->ytAccount);
  294. // Add new
  295. $newPlaylist = $yt->newPlaylistListEntry();
  296. $newPlaylist->setMajorProtocolVersion(2);
  297. $titleString = $this->generateRandomString(10);
  298. $newPlaylist->title = $yt->newTitle()->setText($titleString);
  299. $newPlaylist->summary = $yt->newSummary()->setText('testing');
  300. $postUrl = 'http://gdata.youtube.com/feeds/api/users/default/playlists';
  301. $successfulInsertion = true;
  302. try {
  303. $yt->insertEntry($newPlaylist, $postUrl);
  304. } catch (Zend_Gdata_App_Exception $e) {
  305. $successfulInsertion = false;
  306. }
  307. $this->assertTrue($successfulInsertion, 'Failed to insert a new ' .
  308. 'playlist.');
  309. $playlistListFeed = $yt->getPlaylistListFeed('default');
  310. $playlistFound = false;
  311. $newPlaylistEntry = null;
  312. foreach ($playlistListFeed as $playlistListEntry) {
  313. if ($playlistListEntry->title->text == $titleString) {
  314. $playlistFound = true;
  315. $newPlaylistEntry = $playlistListEntry;
  316. break;
  317. }
  318. }
  319. $this->assertTrue($playlistFound, 'Could not find the newly inserted ' .
  320. 'playlist.');
  321. // Update it
  322. $newTitle = $this->generateRandomString(10);
  323. $newPlaylistEntry->title->setText($newTitle);
  324. $updatedSuccesfully = true;
  325. try {
  326. $newPlaylistEntry->save();
  327. } catch (Zend_Gdata_App_Exception $e) {
  328. $updatedSuccesfully = false;
  329. }
  330. $this->assertTrue($updatedSuccesfully, 'Could not succesfully update ' .
  331. 'a new playlist.');
  332. // Delete it
  333. $deletedSuccesfully = true;
  334. try {
  335. $newPlaylistEntry->delete();
  336. } catch (Zend_Gdata_App_Exception $e) {
  337. $deletedSuccesfully = false;
  338. }
  339. $this->assertTrue($deletedSuccesfully, 'Could not succesfully delete ' .
  340. 'a new playlist.');
  341. }
  342. public function testAddAndDeleteSubscriptionToChannelV2()
  343. {
  344. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  345. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  346. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  347. $authenticationURL =
  348. 'https://www.google.com/youtube/accounts/ClientLogin';
  349. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  350. $username = $user,
  351. $password = $pass,
  352. $service = $service,
  353. $client = null,
  354. $source = 'Google-UnitTests-1.0',
  355. $loginToken = null,
  356. $loginCaptcha = null,
  357. $authenticationURL);
  358. $yt = new Zend_Gdata_YouTube(
  359. $httpClient, 'Google-UnitTests-1.0',
  360. 'ytapi-gdataops-12345-u78960r7-0',
  361. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  362. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  363. $yt->setMajorProtocolVersion(2);
  364. $channelToSubscribeTo = 'AssociatedPress';
  365. // Test for deletion first in case something went wrong
  366. // last time the test was run (network, etc...)
  367. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  368. $successDeletionUpFront = true;
  369. $message = null;
  370. foreach($subscriptionFeed as $subscriptionEntry) {
  371. $subscriptionType = null;
  372. $categories = $subscriptionEntry->getCategory();
  373. // examine the correct category element since there are multiple
  374. foreach($categories as $category) {
  375. if ($category->getScheme() ==
  376. 'http://gdata.youtube.com/schemas/2007/' .
  377. 'subscriptiontypes.cat') {
  378. $subscriptionType = $category->getTerm();
  379. }
  380. }
  381. if ($subscriptionType == 'channel') {
  382. if ($subscriptionEntry->getUsername()->text ==
  383. $channelToSubscribeTo) {
  384. try {
  385. $subscriptionEntry->delete();
  386. } catch (Zend_App_Exception $e) {
  387. $message = $e->getMessage();
  388. $successDeletionUpFront = false;
  389. }
  390. }
  391. }
  392. }
  393. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  394. 'subscription in unit test, could not delete prior to running ' .
  395. 'test -- ' . $message);
  396. // Channel
  397. $newSubscription = $yt->newSubscriptionEntry();
  398. $newSubscription->category = array(
  399. $yt->newCategory('channel',
  400. $this->subscriptionTypeSchema));
  401. $newSubscription->setUsername($yt->newUsername(
  402. $channelToSubscribeTo));
  403. $postUrl =
  404. 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
  405. $successPosting = true;
  406. $message = null;
  407. $insertedSubscription = null;
  408. try {
  409. $insertedSubscription = $yt->insertEntry(
  410. $newSubscription, $postUrl,
  411. 'Zend_Gdata_YouTube_SubscriptionEntry');
  412. } catch (Zend_App_Exception $e) {
  413. $message = $e->getMessage();
  414. $successPosting = false;
  415. }
  416. $this->assertTrue($successPosting, $message);
  417. // Delete it
  418. $successDeletion = true;
  419. $message = null;
  420. try {
  421. $insertedSubscription->delete();
  422. } catch (Zend_App_Exception $e) {
  423. $message = $e->getMessage();
  424. $successDeletion = false;
  425. }
  426. $this->assertTrue($successDeletion, $message);
  427. }
  428. public function testAddAndDeleteSubscriptionToFavoritesV2()
  429. {
  430. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  431. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  432. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  433. $authenticationURL =
  434. 'https://www.google.com/youtube/accounts/ClientLogin';
  435. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  436. $username = $user,
  437. $password = $pass,
  438. $service = $service,
  439. $client = null,
  440. $source = 'Google-UnitTests-1.0',
  441. $loginToken = null,
  442. $loginCaptcha = null,
  443. $authenticationURL);
  444. $yt = new Zend_Gdata_YouTube(
  445. $httpClient, 'Google-UnitTests-1.0',
  446. 'ytapi-gdataops-12345-u78960r7-0',
  447. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  448. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  449. $yt->setMajorProtocolVersion(2);
  450. $usernameOfFavoritesToSubscribeTo = 'CBS';
  451. // Test for deletion first in case something went wrong
  452. // last time the test was run (network, etc...)
  453. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  454. $successDeletionUpFront = true;
  455. $message = null;
  456. foreach($subscriptionFeed as $subscriptionEntry) {
  457. $subscriptionType = null;
  458. $categories = $subscriptionEntry->getCategory();
  459. // examine the correct category element since there are multiple
  460. foreach($categories as $category) {
  461. if ($category->getScheme() ==
  462. 'http://gdata.youtube.com/schemas/2007/' .
  463. 'subscriptiontypes.cat') {
  464. $subscriptionType = $category->getTerm();
  465. }
  466. }
  467. if ($subscriptionType == 'favorites') {
  468. if ($subscriptionEntry->getUsername()->text ==
  469. $usernameOfFavoritesToSubscribeTo) {
  470. try {
  471. $subscriptionEntry->delete();
  472. } catch (Zend_App_Exception $e) {
  473. $message = $e->getMessage();
  474. $successDeletionUpFront = false;
  475. }
  476. }
  477. }
  478. }
  479. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  480. 'subscription in unit test, could not delete prior to running ' .
  481. 'test -- ' . $message);
  482. // CBS's favorites
  483. $newSubscription = $yt->newSubscriptionEntry();
  484. $newSubscription->category = array(
  485. $yt->newCategory('favorites',
  486. $this->subscriptionTypeSchema));
  487. $newSubscription->setUsername($yt->newUsername(
  488. $usernameOfFavoritesToSubscribeTo));
  489. $postUrl =
  490. 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
  491. $successPosting = true;
  492. $message = null;
  493. $insertedSubscription = null;
  494. try {
  495. $insertedSubscription = $yt->insertEntry(
  496. $newSubscription, $postUrl,
  497. 'Zend_Gdata_YouTube_SubscriptionEntry');
  498. } catch (Zend_App_Exception $e) {
  499. $message = $e->getMessage();
  500. $successPosting = false;
  501. }
  502. $this->assertTrue($successPosting, $message);
  503. // Delete it
  504. $successDeletion = true;
  505. $message = null;
  506. try {
  507. $insertedSubscription->delete();
  508. } catch (Zend_App_Exception $e) {
  509. $message = $e->getMessage();
  510. $successDeletion = false;
  511. }
  512. $this->assertTrue($successDeletion, $message);
  513. }
  514. public function testAddAndDeleteSubscriptionToPlaylistV2()
  515. {
  516. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  517. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  518. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  519. $authenticationURL =
  520. 'https://www.google.com/youtube/accounts/ClientLogin';
  521. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  522. $username = $user,
  523. $password = $pass,
  524. $service = $service,
  525. $client = null,
  526. $source = 'Google-UnitTests-1.0',
  527. $loginToken = null,
  528. $loginCaptcha = null,
  529. $authenticationURL);
  530. $yt = new Zend_Gdata_YouTube(
  531. $httpClient, 'Google-UnitTests-1.0',
  532. 'ytapi-gdataops-12345-u78960r7-0',
  533. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  534. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  535. $yt->setMajorProtocolVersion(2);
  536. $playlistIdToSubscribeTo = '7A2BB4AFFEBED2A4';
  537. // Test for deletion first in case something went wrong
  538. // last time the test was run (network, etc...)
  539. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  540. $successDeletionUpFront = true;
  541. $message = null;
  542. foreach($subscriptionFeed as $subscriptionEntry) {
  543. $subscriptionType = null;
  544. $categories = $subscriptionEntry->getCategory();
  545. // examine the correct category element since there are multiple
  546. foreach($categories as $category) {
  547. if ($category->getScheme() ==
  548. 'http://gdata.youtube.com/schemas/2007/' .
  549. 'subscriptiontypes.cat') {
  550. $subscriptionType = $category->getTerm();
  551. }
  552. }
  553. if ($subscriptionType == 'playlist') {
  554. if ($subscriptionEntry->getPlaylistId()->text ==
  555. $playlistIdToSubscribeTo) {
  556. try {
  557. $subscriptionEntry->delete();
  558. } catch (Zend_App_Exception $e) {
  559. $message = $e->getMessage();
  560. $successDeletionUpFront = false;
  561. }
  562. }
  563. }
  564. }
  565. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  566. 'subscription in unit test, could not delete prior to running ' .
  567. 'test -- ' . $message);
  568. // Playlist of McGyver videos
  569. $newSubscription = $yt->newSubscriptionEntry();
  570. $newSubscription->setMajorProtocolVersion(2);
  571. $newSubscription->category = array(
  572. $yt->newCategory('playlist',
  573. $this->subscriptionTypeSchema));
  574. $newSubscription->setPlaylistId($yt->newPlaylistId(
  575. $playlistIdToSubscribeTo));
  576. $postUrl =
  577. 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
  578. $successPosting = true;
  579. $message = null;
  580. $insertedSubscription = null;
  581. try {
  582. $insertedSubscription = $yt->insertEntry(
  583. $newSubscription, $postUrl,
  584. 'Zend_Gdata_YouTube_SubscriptionEntry');
  585. } catch (Zend_App_Exception $e) {
  586. $message = $e->getMessage();
  587. $successPosting = false;
  588. }
  589. $this->assertTrue($successPosting, $message);
  590. // Delete it
  591. $successDeletion = true;
  592. $message = null;
  593. try {
  594. $insertedSubscription->delete();
  595. } catch (Zend_App_Exception $e) {
  596. $message = $e->getMessage();
  597. $successDeletion = false;
  598. }
  599. $this->assertTrue($successDeletion, $message);
  600. }
  601. public function testAddAndDeleteSubscriptionToQueryV2()
  602. {
  603. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  604. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  605. $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  606. $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  607. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  608. $authenticationURL =
  609. 'https://www.google.com/youtube/accounts/ClientLogin';
  610. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  611. $username = $user,
  612. $password = $pass,
  613. $service = $service,
  614. $client = null,
  615. $source = 'Google-UnitTests-1.0',
  616. $loginToken = null,
  617. $loginCaptcha = null,
  618. $authenticationURL);
  619. $yt = new Zend_Gdata_YouTube($httpClient, $clientId, $developerKey);
  620. $yt->setMajorProtocolVersion(2);
  621. $queryStringToSubscribeTo = 'zend';
  622. // Test for deletion first in case something went wrong
  623. // last time the test was run (network, etc...)
  624. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  625. $successDeletionUpFront = true;
  626. $message = null;
  627. foreach($subscriptionFeed as $subscriptionEntry) {
  628. $subscriptionType = null;
  629. $categories = $subscriptionEntry->getCategory();
  630. // examine the correct category element since there are multiple
  631. foreach($categories as $category) {
  632. if ($category->getScheme() ==
  633. 'http://gdata.youtube.com/schemas/2007/' .
  634. 'subscriptiontypes.cat') {
  635. $subscriptionType = $category->getTerm();
  636. }
  637. }
  638. if ($subscriptionType == 'query') {
  639. if ($subscriptionEntry->getQueryString() ==
  640. $queryStringToSubscribeTo) {
  641. try {
  642. $subscriptionEntry->delete();
  643. } catch (Zend_App_Exception $e) {
  644. $message = $e->getMessage();
  645. $successDeletionUpFront = false;
  646. }
  647. }
  648. }
  649. }
  650. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  651. 'subscription in unit test, could not delete prior to running ' .
  652. 'test -- ' . $message);
  653. // Query
  654. $newSubscription = $yt->newSubscriptionEntry();
  655. $newSubscription->category = array(
  656. $yt->newCategory('query',
  657. $this->subscriptionTypeSchema));
  658. $newSubscription->setQueryString($yt->newQueryString(
  659. $queryStringToSubscribeTo));
  660. $postUrl =
  661. 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
  662. $successPosting = true;
  663. $message = null;
  664. $insertedSubscription = null;
  665. try {
  666. $insertedSubscription = $yt->insertEntry(
  667. $newSubscription, $postUrl,
  668. 'Zend_Gdata_YouTube_SubscriptionEntry');
  669. } catch (Zend_App_Exception $e) {
  670. $message = $e->getMessage();
  671. $successPosting = false;
  672. }
  673. $this->assertTrue($successPosting, $message);
  674. // Delete it
  675. $successDeletion = true;
  676. $message = null;
  677. try {
  678. $insertedSubscription->delete();
  679. } catch (Zend_App_Exception $e) {
  680. $message = $e->getMessage();
  681. $successDeletion = false;
  682. }
  683. $this->assertTrue($successDeletion, $message);
  684. }
  685. public function generateRandomString($length)
  686. {
  687. $outputString = null;
  688. for($i = 0; $i < $length; $i++) {
  689. $outputString .= chr(rand(65,90));
  690. }
  691. return $outputString;
  692. }
  693. public function testRetrieveActivityFeed()
  694. {
  695. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  696. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  697. $developerKey = constant(
  698. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  699. $clientId = constant(
  700. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  701. $client = Zend_Gdata_ClientLogin::getHttpClient(
  702. $user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  703. 'https://www.google.com/youtube/accounts/ClientLogin');
  704. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  705. $clientId, $developerKey);
  706. $youtube->setMajorProtocolVersion(2);
  707. $feed = $youtube->getActivityForUser($this->ytAccount);
  708. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  709. $this->assertTrue((count($feed->entries) > 0));
  710. $this->assertEquals('Activity of ' . $this->ytAccount,
  711. $feed->title->text);
  712. }
  713. public function testExceptionIfNotUsingDeveloperKey()
  714. {
  715. $exceptionThrown = false;
  716. $youtube = new Zend_Gdata_YouTube();
  717. $youtube->setMajorProtocolVersion(2);
  718. try {
  719. $youtube->getActivityForUser($this->ytAccount);
  720. } catch (Zend_Gdata_App_HttpException $e) {
  721. $exceptionThrown = true;
  722. }
  723. $this->assertTrue($exceptionThrown, 'Was expecting an exception when ' .
  724. 'making a request to the YouTube Activity feed without a ' .
  725. 'developer key.');
  726. }
  727. public function testRetrieveActivityFeedForMultipleUsers()
  728. {
  729. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  730. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  731. $developerKey = constant(
  732. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  733. $clientId = constant(
  734. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  735. $client = Zend_Gdata_ClientLogin::getHttpClient(
  736. $user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  737. 'https://www.google.com/youtube/accounts/ClientLogin');
  738. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  739. $clientId, $developerKey);
  740. $youtube->setMajorProtocolVersion(2);
  741. $feed = $youtube->getActivityForUser($this->ytAccount .
  742. ',associatedpress');
  743. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  744. $this->assertTrue((count($feed->entries) > 0));
  745. $this->assertEquals('Activity of ' . $this->ytAccount .
  746. ',associatedpress', $feed->title->text);
  747. }
  748. public function testRetrieveFriendFeed()
  749. {
  750. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  751. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  752. $developerKey = constant(
  753. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  754. $clientId = constant(
  755. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  756. $client = Zend_Gdata_ClientLogin::getHttpClient(
  757. $user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  758. 'https://www.google.com/youtube/accounts/ClientLogin');
  759. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  760. $clientId, $developerKey);
  761. $youtube->setMajorProtocolVersion(2);
  762. $feed = $youtube->getFriendActivityForCurrentUser();
  763. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  764. $this->assertTrue((count($feed->entries) > 0));
  765. $this->assertEquals('Activity of the friends of ' . $this->ytAccount,
  766. $feed->title->text);
  767. }
  768. public function testThrowExceptionOnActivityFeedRequestForMoreThan20Users()
  769. {
  770. $exceptionThrown = false;
  771. $listOfMoreThan20Users = null;
  772. $youtube = new Zend_Gdata_YouTube();
  773. $youtube->setMajorProtocolVersion(2);
  774. for ($x = 0; $x < 30; $x++) {
  775. $listOfMoreThan20Users .= "user$x";
  776. if ($x != 29) {
  777. $listOfMoreThan20Users .= ",";
  778. }
  779. }
  780. try {
  781. $youtube->getActivityForUser($listOfMoreThan20Users);
  782. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  783. $exceptionThrown = true;
  784. }
  785. $this->assertTrue($exceptionThrown, 'Was expecting an exception on ' .
  786. 'a request to ->getActivityForUser when more than 20 users were ' .
  787. 'specified in YouTube.php');
  788. }
  789. public function testGetInboxFeedForCurrentUserV1()
  790. {
  791. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  792. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  793. $developerKey = constant(
  794. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  795. $clientId = constant(
  796. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  797. $client = Zend_Gdata_ClientLogin::getHttpClient(
  798. $user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  799. 'https://www.google.com/youtube/accounts/ClientLogin');
  800. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  801. $clientId, $developerKey);
  802. $inboxFeed = $youtube->getInboxFeedForCurrentUser();
  803. $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
  804. $this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
  805. $this->ytAccount . ' had no messages in their inbox.');
  806. // get the first entry
  807. $inboxFeed->rewind();
  808. $inboxEntry = $inboxFeed->current();
  809. $this->assertTrue(
  810. $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
  811. $this->assertTrue($inboxEntry->getTitle()->text != '');
  812. }
  813. public function testGetInboxFeedForCurrentUserV2()
  814. {
  815. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  816. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  817. $developerKey = constant(
  818. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  819. $clientId = constant(
  820. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  821. $client = Zend_Gdata_ClientLogin::getHttpClient(
  822. $user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  823. 'https://www.google.com/youtube/accounts/ClientLogin');
  824. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  825. $clientId, $developerKey);
  826. $youtube->setMajorProtocolVersion(2);
  827. $inboxFeed = $youtube->getInboxFeedForCurrentUser();
  828. $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
  829. $this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
  830. $this->ytAccount . ' had no messages in their inbox.');
  831. // get the first entry
  832. $inboxFeed->rewind();
  833. $inboxEntry = $inboxFeed->current();
  834. $this->assertTrue(
  835. $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
  836. $this->assertTrue($inboxEntry->getTitle()->text != '');
  837. }
  838. public function testSendAMessageV2()
  839. {
  840. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  841. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  842. $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  843. $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  844. $client = Zend_Gdata_ClientLogin::getHttpClient(
  845. $user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  846. 'https://www.google.com/youtube/accounts/ClientLogin');
  847. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  848. $clientId, $developerKey);
  849. $youtube->setMajorProtocolVersion(2);
  850. // get a video from the recently featured video feed
  851. $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
  852. $videoEntry = $videoFeed->entry[0];
  853. $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
  854. // sending message to gdpython (python client library unit test user)
  855. $sentMessage = $youtube->sendVideoMessage(
  856. 'Sending a v2 test message from Zend_Gdata_YouTubeOnlineTest.',
  857. $videoEntry, null, 'gdpython');
  858. $this->assertTrue(
  859. $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
  860. }
  861. public function testSendAMessageV1()
  862. {
  863. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  864. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  865. $developerKey = constant(
  866. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  867. $clientId = constant(
  868. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  869. $client = Zend_Gdata_ClientLogin::getHttpClient(
  870. $user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  871. 'https://www.google.com/youtube/accounts/ClientLogin');
  872. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  873. $clientId, $developerKey);
  874. $youtube->setMajorProtocolVersion(1);
  875. // get a video from the recently featured video feed
  876. $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
  877. $videoEntry = $videoFeed->entry[0];
  878. $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
  879. // sending message to gdpython (python client library unit test user)
  880. $sentMessage = $youtube->sendVideoMessage(
  881. 'Sending a v1 test message from Zend_Gdata_YouTubeOnlineTest.',
  882. $videoEntry, null, 'gdpython');
  883. $this->assertTrue(
  884. $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
  885. }
  886. public function testThrowExceptionOnSendingMessageWithoutVideo()
  887. {
  888. $exceptionCaught = false;
  889. $this->gdata = new Zend_Gdata_YouTube();
  890. try {
  891. $this->gdata->sendVideoMessage('Should fail', null, null, 'foo');
  892. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  893. $exceptionCaught = true;
  894. }
  895. $this->assertTrue($exceptionCaught, 'Was expecting an exception if ' .
  896. 'sending a message without a video');
  897. }
  898. public function testCommentOnAComment()
  899. {
  900. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  901. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  902. $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  903. $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  904. $client = Zend_Gdata_ClientLogin::getHttpClient(
  905. $user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  906. 'https://www.google.com/youtube/accounts/ClientLogin');
  907. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  908. $clientId, $developerKey);
  909. $youtube->setMajorProtocolVersion(2);
  910. $mostDiscussedFeed = $youtube->getVideoFeed(
  911. 'http://gdata.youtube.com/feeds/api/standardfeeds/most_discussed');
  912. // get first entry
  913. $mostDiscussedFeed->rewind();
  914. $firstEntry = $mostDiscussedFeed->current();
  915. $this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_VideoEntry);
  916. $commentFeed = $youtube->getVideoCommentFeed($firstEntry->getVideoId());
  917. // get first comment
  918. $commentFeed->rewind();
  919. $firstCommentEntry = $commentFeed->current();
  920. $commentedComment = $youtube->replyToCommentEntry($firstCommentEntry,
  921. 'awesome ! (ZFUnitTest-test)');
  922. $this->assertTrue(
  923. $commentedComment instanceof Zend_Gdata_YouTube_CommentEntry);
  924. }
  925. }