YouTubeOnlineTest.php 41 KB

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