YouTubeOnlineTest.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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->user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  35. $this->pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  36. $this->subscriptionTypeSchema = 'http://gdata.youtube.com/schemas/' .
  37. '2007/subscriptiontypes.cat';
  38. $this->gdata = new Zend_Gdata_YouTube();
  39. }
  40. public function tearDown()
  41. {
  42. }
  43. public function testRetrieveSubScriptionFeed()
  44. {
  45. $feed = $this->gdata->getSubscriptionFeed($this->ytAccount);
  46. $this->assertTrue($feed->totalResults->text > 0);
  47. $this->assertEquals('Subscriptions of ' . $this->ytAccount,
  48. $feed->title->text);
  49. $this->assertTrue(count($feed->entry) > 0);
  50. foreach ($feed->entry as $entry) {
  51. $this->assertTrue($entry->title->text != '');
  52. }
  53. }
  54. public function testRetrieveContactFeed()
  55. {
  56. $feed = $this->gdata->getContactFeed($this->ytAccount);
  57. $this->assertTrue($feed->totalResults->text > 0);
  58. $this->assertEquals('Contacts of ' . $this->ytAccount,
  59. $feed->title->text);
  60. $this->assertTrue(count($feed->entry) > 0);
  61. foreach ($feed->entry as $entry) {
  62. $this->assertTrue($entry->title->text != '');
  63. }
  64. $this->assertEquals('ytgdatatest1', $feed->entry[0]->username->text);
  65. }
  66. public function testRetrieveUserVideos()
  67. {
  68. $feed = $this->gdata->getUserUploads($this->ytAccount);
  69. $this->assertEquals('Uploads by ' . $this->ytAccount,
  70. $feed->title->text);
  71. $this->assertTrue(count($feed->entry) === 1);
  72. }
  73. public function testRetrieveVideoFeed()
  74. {
  75. $feed = $this->gdata->getVideoFeed();
  76. $query = new Zend_Gdata_YouTube_VideoQuery();
  77. $query->setVideoQuery('puppy');
  78. $feed = $this->gdata->getVideoFeed($query);
  79. foreach ($feed as $videoEntry) {
  80. $videoResponsesLink = $videoEntry->getVideoResponsesLink();
  81. $videoRatingsLink = $videoEntry->getVideoRatingsLink();
  82. $videoComplaintsLink = $videoEntry->getVideoComplaintsLink();
  83. }
  84. $feed = $this->gdata->getVideoFeed($query->getQueryUrl());
  85. }
  86. public function testRetrieveVideoEntry()
  87. {
  88. $entry = $this->gdata->getVideoEntry('66wj2g5yz0M');
  89. $this->assertEquals('TestMovie', $entry->title->text);
  90. $entry = $this->gdata->getVideoEntry(null, 'http://gdata.youtube.com/feeds/api/videos/66wj2g5yz0M');
  91. $this->assertEquals('TestMovie', $entry->title->text);
  92. }
  93. public function testRetrieveOtherFeeds()
  94. {
  95. $feed = $this->gdata->getRelatedVideoFeed('66wj2g5yz0M');
  96. $feed = $this->gdata->getVideoResponseFeed('66wj2g5yz0M');
  97. $feed = $this->gdata->getVideoCommentFeed('66wj2g5yz0M');
  98. $feed = $this->gdata->getWatchOnMobileVideoFeed();
  99. $feed = $this->gdata->getUserFavorites($this->ytAccount);
  100. }
  101. public function testRetrieveUserProfile()
  102. {
  103. $entry = $this->gdata->getUserProfile($this->ytAccount);
  104. $this->assertEquals($this->ytAccount . ' Channel', $entry->title->text);
  105. $this->assertEquals($this->ytAccount, $entry->username->text);
  106. $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.',
  107. $entry->description->text);
  108. $this->assertEquals(32, $entry->age->text);
  109. $this->assertEquals('crime and punishment, ps i love you, the stand', $entry->books->text);
  110. $this->assertEquals('Google', $entry->company->text);
  111. $this->assertEquals('software engineering, information architecture, photography, travel', $entry->hobbies->text);
  112. $this->assertEquals('Mountain View, CA', $entry->hometown->text);
  113. $this->assertEquals('San Francisco, CA 94114, US', $entry->location->text);
  114. $this->assertEquals('monk, heroes, law and order, top gun', $entry->movies->text);
  115. $this->assertEquals('imogen heap, frou frou, thievory corp, morcheeba, barenaked ladies', $entry->music->text);
  116. $this->assertEquals('Developer Programs', $entry->occupation->text);
  117. $this->assertEquals('University of the World', $entry->school->text);
  118. $this->assertEquals('f', $entry->gender->text);
  119. $this->assertEquals('taken', $entry->relationship->text);
  120. }
  121. public function testRetrieveAndUpdatePlaylistList()
  122. {
  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 = $this->user,
  127. $password = $this->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. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  274. $authenticationURL =
  275. 'https://www.google.com/youtube/accounts/ClientLogin';
  276. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  277. $username = $this->user,
  278. $password = $this->pass,
  279. $service = $service,
  280. $client = null,
  281. $source = 'Google-UnitTests-1.0',
  282. $loginToken = null,
  283. $loginCaptcha = null,
  284. $authenticationURL);
  285. $yt = new Zend_Gdata_YouTube(
  286. $httpClient, 'Google-UnitTests-1.0',
  287. 'ytapi-gdataops-12345-u78960r7-0',
  288. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  289. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  290. $yt->setMajorProtocolVersion(2);
  291. $feed = $yt->getPlaylistListFeed($this->ytAccount);
  292. // Add new
  293. $newPlaylist = $yt->newPlaylistListEntry();
  294. $newPlaylist->setMajorProtocolVersion(2);
  295. $titleString = $this->generateRandomString(10);
  296. $newPlaylist->title = $yt->newTitle()->setText($titleString);
  297. $newPlaylist->summary = $yt->newSummary()->setText('testing');
  298. $postUrl = 'http://gdata.youtube.com/feeds/api/users/default/playlists';
  299. $successfulInsertion = true;
  300. try {
  301. $yt->insertEntry($newPlaylist, $postUrl);
  302. } catch (Zend_Gdata_App_Exception $e) {
  303. $successfulInsertion = false;
  304. }
  305. $this->assertTrue($successfulInsertion, 'Failed to insert a new ' .
  306. 'playlist.');
  307. $playlistListFeed = $yt->getPlaylistListFeed('default');
  308. $playlistFound = false;
  309. $newPlaylistEntry = null;
  310. foreach ($playlistListFeed as $playlistListEntry) {
  311. if ($playlistListEntry->title->text == $titleString) {
  312. $playlistFound = true;
  313. $newPlaylistEntry = $playlistListEntry;
  314. break;
  315. }
  316. }
  317. $this->assertTrue($playlistFound, 'Could not find the newly inserted ' .
  318. 'playlist.');
  319. // Update it
  320. $newTitle = $this->generateRandomString(10);
  321. $newPlaylistEntry->title->setText($newTitle);
  322. $updatedSuccesfully = true;
  323. try {
  324. $newPlaylistEntry->save();
  325. } catch (Zend_Gdata_App_Exception $e) {
  326. $updatedSuccesfully = false;
  327. }
  328. $this->assertTrue($updatedSuccesfully, 'Could not succesfully update ' .
  329. 'a new playlist.');
  330. // Delete it
  331. $deletedSuccesfully = true;
  332. try {
  333. $newPlaylistEntry->delete();
  334. } catch (Zend_Gdata_App_Exception $e) {
  335. $deletedSuccesfully = false;
  336. }
  337. $this->assertTrue($deletedSuccesfully, 'Could not succesfully delete ' .
  338. 'a new playlist.');
  339. }
  340. public function testAddAndDeleteSubscriptionToChannelV2()
  341. {
  342. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  343. $authenticationURL =
  344. 'https://www.google.com/youtube/accounts/ClientLogin';
  345. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  346. $username = $this->user,
  347. $password = $this->pass,
  348. $service = $service,
  349. $client = null,
  350. $source = 'Google-UnitTests-1.0',
  351. $loginToken = null,
  352. $loginCaptcha = null,
  353. $authenticationURL);
  354. $yt = new Zend_Gdata_YouTube(
  355. $httpClient, 'Google-UnitTests-1.0',
  356. 'ytapi-gdataops-12345-u78960r7-0',
  357. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  358. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  359. $yt->setMajorProtocolVersion(2);
  360. $channelToSubscribeTo = 'AssociatedPress';
  361. // Test for deletion first in case something went wrong
  362. // last time the test was run (network, etc...)
  363. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  364. $successDeletionUpFront = true;
  365. $message = null;
  366. foreach($subscriptionFeed as $subscriptionEntry) {
  367. $subscriptionType = null;
  368. $categories = $subscriptionEntry->getCategory();
  369. // examine the correct category element since there are multiple
  370. foreach($categories as $category) {
  371. if ($category->getScheme() ==
  372. 'http://gdata.youtube.com/schemas/2007/' .
  373. 'subscriptiontypes.cat') {
  374. $subscriptionType = $category->getTerm();
  375. }
  376. }
  377. if ($subscriptionType == 'channel') {
  378. if ($subscriptionEntry->getUsername()->text ==
  379. $channelToSubscribeTo) {
  380. try {
  381. $subscriptionEntry->delete();
  382. } catch (Zend_App_Exception $e) {
  383. $message = $e->getMessage();
  384. $successDeletionUpFront = false;
  385. }
  386. }
  387. }
  388. }
  389. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  390. 'subscription in unit test, could not delete prior to running ' .
  391. 'test -- ' . $message);
  392. // Channel
  393. $newSubscription = $yt->newSubscriptionEntry();
  394. $newSubscription->category = array(
  395. $yt->newCategory('channel',
  396. $this->subscriptionTypeSchema));
  397. $newSubscription->setUsername($yt->newUsername(
  398. $channelToSubscribeTo));
  399. $postUrl =
  400. 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
  401. $successPosting = true;
  402. $message = null;
  403. $insertedSubscription = null;
  404. try {
  405. $insertedSubscription = $yt->insertEntry(
  406. $newSubscription, $postUrl,
  407. 'Zend_Gdata_YouTube_SubscriptionEntry');
  408. } catch (Zend_App_Exception $e) {
  409. $message = $e->getMessage();
  410. $successPosting = false;
  411. }
  412. $this->assertTrue($successPosting, $message);
  413. // Delete it
  414. $successDeletion = true;
  415. $message = null;
  416. try {
  417. $insertedSubscription->delete();
  418. } catch (Zend_App_Exception $e) {
  419. $message = $e->getMessage();
  420. $successDeletion = false;
  421. }
  422. $this->assertTrue($successDeletion, $message);
  423. }
  424. public function testAddAndDeleteSubscriptionToFavoritesV2()
  425. {
  426. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  427. $authenticationURL =
  428. 'https://www.google.com/youtube/accounts/ClientLogin';
  429. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  430. $username = $this->user,
  431. $password = $this->pass,
  432. $service = $service,
  433. $client = null,
  434. $source = 'Google-UnitTests-1.0',
  435. $loginToken = null,
  436. $loginCaptcha = null,
  437. $authenticationURL);
  438. $yt = new Zend_Gdata_YouTube(
  439. $httpClient, 'Google-UnitTests-1.0',
  440. 'ytapi-gdataops-12345-u78960r7-0',
  441. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  442. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  443. $yt->setMajorProtocolVersion(2);
  444. $usernameOfFavoritesToSubscribeTo = 'CBS';
  445. // Test for deletion first in case something went wrong
  446. // last time the test was run (network, etc...)
  447. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  448. $successDeletionUpFront = true;
  449. $message = null;
  450. foreach($subscriptionFeed as $subscriptionEntry) {
  451. $subscriptionType = null;
  452. $categories = $subscriptionEntry->getCategory();
  453. // examine the correct category element since there are multiple
  454. foreach($categories as $category) {
  455. if ($category->getScheme() ==
  456. 'http://gdata.youtube.com/schemas/2007/' .
  457. 'subscriptiontypes.cat') {
  458. $subscriptionType = $category->getTerm();
  459. }
  460. }
  461. if ($subscriptionType == 'favorites') {
  462. if ($subscriptionEntry->getUsername()->text ==
  463. $usernameOfFavoritesToSubscribeTo) {
  464. try {
  465. $subscriptionEntry->delete();
  466. } catch (Zend_App_Exception $e) {
  467. $message = $e->getMessage();
  468. $successDeletionUpFront = false;
  469. }
  470. }
  471. }
  472. }
  473. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  474. 'subscription in unit test, could not delete prior to running ' .
  475. 'test -- ' . $message);
  476. // CBS's favorites
  477. $newSubscription = $yt->newSubscriptionEntry();
  478. $newSubscription->category = array(
  479. $yt->newCategory('favorites',
  480. $this->subscriptionTypeSchema));
  481. $newSubscription->setUsername($yt->newUsername(
  482. $usernameOfFavoritesToSubscribeTo));
  483. $postUrl =
  484. 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
  485. $successPosting = true;
  486. $message = null;
  487. $insertedSubscription = null;
  488. try {
  489. $insertedSubscription = $yt->insertEntry(
  490. $newSubscription, $postUrl,
  491. 'Zend_Gdata_YouTube_SubscriptionEntry');
  492. } catch (Zend_App_Exception $e) {
  493. $message = $e->getMessage();
  494. $successPosting = false;
  495. }
  496. $this->assertTrue($successPosting, $message);
  497. // Delete it
  498. $successDeletion = true;
  499. $message = null;
  500. try {
  501. $insertedSubscription->delete();
  502. } catch (Zend_App_Exception $e) {
  503. $message = $e->getMessage();
  504. $successDeletion = false;
  505. }
  506. $this->assertTrue($successDeletion, $message);
  507. }
  508. public function testAddAndDeleteSubscriptionToPlaylistV2()
  509. {
  510. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  511. $authenticationURL =
  512. 'https://www.google.com/youtube/accounts/ClientLogin';
  513. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  514. $username = $this->user,
  515. $password = $this->pass,
  516. $service = $service,
  517. $client = null,
  518. $source = 'Google-UnitTests-1.0',
  519. $loginToken = null,
  520. $loginCaptcha = null,
  521. $authenticationURL);
  522. $yt = new Zend_Gdata_YouTube(
  523. $httpClient, 'Google-UnitTests-1.0',
  524. 'ytapi-gdataops-12345-u78960r7-0',
  525. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  526. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  527. $yt->setMajorProtocolVersion(2);
  528. $playlistIdToSubscribeTo = '7A2BB4AFFEBED2A4';
  529. // Test for deletion first in case something went wrong
  530. // last time the test was run (network, etc...)
  531. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  532. $successDeletionUpFront = true;
  533. $message = null;
  534. foreach($subscriptionFeed as $subscriptionEntry) {
  535. $subscriptionType = null;
  536. $categories = $subscriptionEntry->getCategory();
  537. // examine the correct category element since there are multiple
  538. foreach($categories as $category) {
  539. if ($category->getScheme() ==
  540. 'http://gdata.youtube.com/schemas/2007/' .
  541. 'subscriptiontypes.cat') {
  542. $subscriptionType = $category->getTerm();
  543. }
  544. }
  545. if ($subscriptionType == 'playlist') {
  546. if ($subscriptionEntry->getPlaylistId()->text ==
  547. $playlistIdToSubscribeTo) {
  548. try {
  549. $subscriptionEntry->delete();
  550. } catch (Zend_App_Exception $e) {
  551. $message = $e->getMessage();
  552. $successDeletionUpFront = false;
  553. }
  554. }
  555. }
  556. }
  557. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  558. 'subscription in unit test, could not delete prior to running ' .
  559. 'test -- ' . $message);
  560. // Playlist of McGyver videos
  561. $newSubscription = $yt->newSubscriptionEntry();
  562. $newSubscription->setMajorProtocolVersion(2);
  563. $newSubscription->category = array(
  564. $yt->newCategory('playlist',
  565. $this->subscriptionTypeSchema));
  566. $newSubscription->setPlaylistId($yt->newPlaylistId(
  567. $playlistIdToSubscribeTo));
  568. $postUrl =
  569. 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
  570. $successPosting = true;
  571. $message = null;
  572. $insertedSubscription = null;
  573. try {
  574. $insertedSubscription = $yt->insertEntry(
  575. $newSubscription, $postUrl,
  576. 'Zend_Gdata_YouTube_SubscriptionEntry');
  577. } catch (Zend_App_Exception $e) {
  578. $message = $e->getMessage();
  579. $successPosting = false;
  580. }
  581. $this->assertTrue($successPosting, $message);
  582. // Delete it
  583. $successDeletion = true;
  584. $message = null;
  585. try {
  586. $insertedSubscription->delete();
  587. } catch (Zend_App_Exception $e) {
  588. $message = $e->getMessage();
  589. $successDeletion = false;
  590. }
  591. $this->assertTrue($successDeletion, $message);
  592. }
  593. public function testAddAndDeleteSubscriptionToQueryV2()
  594. {
  595. $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  596. $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  597. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  598. $authenticationURL =
  599. 'https://www.google.com/youtube/accounts/ClientLogin';
  600. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  601. $username = $this->user,
  602. $password = $this->pass,
  603. $service = $service,
  604. $client = null,
  605. $source = 'Google-UnitTests-1.0',
  606. $loginToken = null,
  607. $loginCaptcha = null,
  608. $authenticationURL);
  609. $yt = new Zend_Gdata_YouTube($httpClient, 'Google-UnitTests-1.0', $clientId, $developerKey);
  610. $yt->setMajorProtocolVersion(2);
  611. $queryStringToSubscribeTo = 'zend';
  612. // Test for deletion first in case something went wrong
  613. // last time the test was run (network, etc...)
  614. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  615. $successDeletionUpFront = true;
  616. $message = null;
  617. foreach($subscriptionFeed as $subscriptionEntry) {
  618. $subscriptionType = null;
  619. $categories = $subscriptionEntry->getCategory();
  620. // examine the correct category element since there are multiple
  621. foreach($categories as $category) {
  622. if ($category->getScheme() ==
  623. 'http://gdata.youtube.com/schemas/2007/' .
  624. 'subscriptiontypes.cat') {
  625. $subscriptionType = $category->getTerm();
  626. }
  627. }
  628. if ($subscriptionType == 'query') {
  629. if ($subscriptionEntry->getQueryString() ==
  630. $queryStringToSubscribeTo) {
  631. try {
  632. $subscriptionEntry->delete();
  633. } catch (Zend_App_Exception $e) {
  634. $message = $e->getMessage();
  635. $successDeletionUpFront = false;
  636. }
  637. }
  638. }
  639. }
  640. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  641. 'subscription in unit test, could not delete prior to running ' .
  642. 'test -- ' . $message);
  643. // Query
  644. $newSubscription = $yt->newSubscriptionEntry();
  645. $newSubscription->category = array(
  646. $yt->newCategory('query',
  647. $this->subscriptionTypeSchema));
  648. $newSubscription->setQueryString($yt->newQueryString(
  649. $queryStringToSubscribeTo));
  650. $postUrl =
  651. 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
  652. $successPosting = true;
  653. $message = null;
  654. $insertedSubscription = null;
  655. try {
  656. $insertedSubscription = $yt->insertEntry(
  657. $newSubscription, $postUrl,
  658. 'Zend_Gdata_YouTube_SubscriptionEntry');
  659. } catch (Zend_App_Exception $e) {
  660. $message = $e->getMessage();
  661. $successPosting = false;
  662. }
  663. $this->assertTrue($successPosting, $message);
  664. // Delete it
  665. $successDeletion = true;
  666. $message = null;
  667. try {
  668. $insertedSubscription->delete();
  669. } catch (Zend_App_Exception $e) {
  670. $message = $e->getMessage();
  671. $successDeletion = false;
  672. }
  673. $this->assertTrue($successDeletion, $message);
  674. }
  675. public function generateRandomString($length)
  676. {
  677. $outputString = null;
  678. for($i = 0; $i < $length; $i++) {
  679. $outputString .= chr(rand(65,90));
  680. }
  681. return $outputString;
  682. }
  683. public function testRetrieveActivityFeed()
  684. {
  685. $developerKey = constant(
  686. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  687. $clientId = constant(
  688. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  689. $client = Zend_Gdata_ClientLogin::getHttpClient(
  690. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  691. 'https://www.google.com/youtube/accounts/ClientLogin');
  692. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  693. $clientId, $developerKey);
  694. $youtube->setMajorProtocolVersion(2);
  695. $feed = $youtube->getActivityForUser($this->ytAccount);
  696. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  697. $this->assertTrue((count($feed->entries) > 0));
  698. $this->assertEquals('Activity of ' . $this->ytAccount,
  699. $feed->title->text);
  700. }
  701. public function testExceptionIfNotUsingDeveloperKey()
  702. {
  703. $exceptionThrown = false;
  704. $youtube = new Zend_Gdata_YouTube();
  705. $youtube->setMajorProtocolVersion(2);
  706. try {
  707. $youtube->getActivityForUser($this->ytAccount);
  708. } catch (Zend_Gdata_App_HttpException $e) {
  709. $exceptionThrown = true;
  710. }
  711. $this->assertTrue($exceptionThrown, 'Was expecting an exception when ' .
  712. 'making a request to the YouTube Activity feed without a ' .
  713. 'developer key.');
  714. }
  715. public function testRetrieveActivityFeedForMultipleUsers()
  716. {
  717. $developerKey = constant(
  718. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  719. $clientId = constant(
  720. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  721. $client = Zend_Gdata_ClientLogin::getHttpClient(
  722. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  723. 'https://www.google.com/youtube/accounts/ClientLogin');
  724. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  725. $clientId, $developerKey);
  726. $youtube->setMajorProtocolVersion(2);
  727. $feed = $youtube->getActivityForUser($this->ytAccount .
  728. ',associatedpress');
  729. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  730. $this->assertTrue((count($feed->entries) > 0));
  731. $this->assertEquals('Activity of ' . $this->ytAccount .
  732. ',associatedpress', $feed->title->text);
  733. }
  734. public function testRetrieveFriendFeed()
  735. {
  736. $developerKey = constant(
  737. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  738. $clientId = constant(
  739. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  740. $client = Zend_Gdata_ClientLogin::getHttpClient(
  741. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  742. 'https://www.google.com/youtube/accounts/ClientLogin');
  743. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  744. $clientId, $developerKey);
  745. $youtube->setMajorProtocolVersion(2);
  746. $feed = $youtube->getFriendActivityForCurrentUser();
  747. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  748. $this->assertTrue((count($feed->entries) > 0));
  749. $this->assertEquals('Activity of the friends of ' . $this->ytAccount,
  750. $feed->title->text);
  751. }
  752. public function testThrowExceptionOnActivityFeedRequestForMoreThan20Users()
  753. {
  754. $exceptionThrown = false;
  755. $listOfMoreThan20Users = null;
  756. $youtube = new Zend_Gdata_YouTube();
  757. $youtube->setMajorProtocolVersion(2);
  758. for ($x = 0; $x < 30; $x++) {
  759. $listOfMoreThan20Users .= "user$x";
  760. if ($x != 29) {
  761. $listOfMoreThan20Users .= ",";
  762. }
  763. }
  764. try {
  765. $youtube->getActivityForUser($listOfMoreThan20Users);
  766. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  767. $exceptionThrown = true;
  768. }
  769. $this->assertTrue($exceptionThrown, 'Was expecting an exception on ' .
  770. 'a request to ->getActivityForUser when more than 20 users were ' .
  771. 'specified in YouTube.php');
  772. }
  773. public function testGetInboxFeedForCurrentUserV1()
  774. {
  775. $developerKey = constant(
  776. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  777. $clientId = constant(
  778. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  779. $client = Zend_Gdata_ClientLogin::getHttpClient(
  780. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  781. 'https://www.google.com/youtube/accounts/ClientLogin');
  782. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  783. $clientId, $developerKey);
  784. $inboxFeed = $youtube->getInboxFeedForCurrentUser();
  785. $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
  786. $this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
  787. $this->ytAccount . ' had no messages in their inbox.');
  788. // get the first entry
  789. $inboxFeed->rewind();
  790. $inboxEntry = $inboxFeed->current();
  791. $this->assertTrue(
  792. $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
  793. $this->assertTrue($inboxEntry->getTitle()->text != '');
  794. }
  795. public function testGetInboxFeedForCurrentUserV2()
  796. {
  797. $developerKey = constant(
  798. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  799. $clientId = constant(
  800. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  801. $client = Zend_Gdata_ClientLogin::getHttpClient(
  802. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  803. 'https://www.google.com/youtube/accounts/ClientLogin');
  804. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  805. $clientId, $developerKey);
  806. $youtube->setMajorProtocolVersion(2);
  807. $inboxFeed = $youtube->getInboxFeedForCurrentUser();
  808. $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
  809. $this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
  810. $this->ytAccount . ' had no messages in their inbox.');
  811. // get the first entry
  812. $inboxFeed->rewind();
  813. $inboxEntry = $inboxFeed->current();
  814. $this->assertTrue(
  815. $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
  816. $this->assertTrue($inboxEntry->getTitle()->text != '');
  817. }
  818. public function testSendAMessageV2()
  819. {
  820. $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  821. $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  822. $client = Zend_Gdata_ClientLogin::getHttpClient(
  823. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  824. 'https://www.google.com/youtube/accounts/ClientLogin');
  825. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  826. $clientId, $developerKey);
  827. $youtube->setMajorProtocolVersion(2);
  828. // get a video from the recently featured video feed
  829. $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
  830. $videoEntry = $videoFeed->entry[0];
  831. $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
  832. // sending message to gdpython (python client library unit test user)
  833. $sentMessage = $youtube->sendVideoMessage(
  834. 'Sending a v2 test message from Zend_Gdata_YouTubeOnlineTest.',
  835. $videoEntry, null, 'gdpython');
  836. $this->assertTrue(
  837. $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
  838. }
  839. public function testSendAMessageV1()
  840. {
  841. $developerKey = constant(
  842. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  843. $clientId = constant(
  844. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  845. $client = Zend_Gdata_ClientLogin::getHttpClient(
  846. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  847. 'https://www.google.com/youtube/accounts/ClientLogin');
  848. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  849. $clientId, $developerKey);
  850. $youtube->setMajorProtocolVersion(1);
  851. // get a video from the recently featured video feed
  852. $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
  853. $videoEntry = $videoFeed->entry[0];
  854. $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
  855. // sending message to gdpython (python client library unit test user)
  856. $sentMessage = $youtube->sendVideoMessage(
  857. 'Sending a v1 test message from Zend_Gdata_YouTubeOnlineTest.',
  858. $videoEntry, null, 'gdpython');
  859. $this->assertTrue(
  860. $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
  861. }
  862. public function testThrowExceptionOnSendingMessageWithoutVideo()
  863. {
  864. $exceptionCaught = false;
  865. $this->gdata = new Zend_Gdata_YouTube();
  866. try {
  867. $this->gdata->sendVideoMessage('Should fail', null, null, 'foo');
  868. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  869. $exceptionCaught = true;
  870. }
  871. $this->assertTrue($exceptionCaught, 'Was expecting an exception if ' .
  872. 'sending a message without a video');
  873. }
  874. public function testCommentOnAComment()
  875. {
  876. $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  877. $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  878. $client = Zend_Gdata_ClientLogin::getHttpClient(
  879. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  880. 'https://www.google.com/youtube/accounts/ClientLogin');
  881. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  882. $clientId, $developerKey);
  883. $youtube->setMajorProtocolVersion(2);
  884. $mostDiscussedFeed = $youtube->getVideoFeed(
  885. 'http://gdata.youtube.com/feeds/api/standardfeeds/most_discussed');
  886. // get first entry
  887. $mostDiscussedFeed->rewind();
  888. $firstEntry = $mostDiscussedFeed->current();
  889. $this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_VideoEntry);
  890. $commentFeed = $youtube->getVideoCommentFeed($firstEntry->getVideoId());
  891. // get first comment
  892. $commentFeed->rewind();
  893. $firstCommentEntry = $commentFeed->current();
  894. $commentedComment = $youtube->replyToCommentEntry($firstCommentEntry,
  895. 'awesome ! (ZFUnitTest-test)');
  896. $this->assertTrue(
  897. $commentedComment instanceof Zend_Gdata_YouTube_CommentEntry);
  898. }
  899. }