YouTubeOnlineTest.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. require_once 'Zend/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-2015 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. /**
  240. * @group ZF-9479
  241. */
  242. public function testPerformV2Query()
  243. {
  244. $this->gdata->setMajorProtocolVersion(2);
  245. $query = $this->gdata->newVideoQuery();
  246. $query->videoQuery = 'zend framework';
  247. $query->startIndex = 0;
  248. $query->maxResults = 10;
  249. $query->orderBy = 'viewCount';
  250. $query->safeSearch = 'strict';
  251. $videoFeed = $this->gdata->getVideoFeed($query);
  252. $this->assertTrue(count($videoFeed->entry) > 0,
  253. 'Could not retrieve a single entry for location search:' .
  254. $query->getQueryUrl(2));
  255. }
  256. public function testPerformV2Query_Location()
  257. {
  258. $this->gdata->setMajorProtocolVersion(2);
  259. $query = $this->gdata->newVideoQuery();
  260. // Setting location to New York City
  261. $query->setLocation('-37.0625,-95.677068');
  262. $query->setLocationRadius('1000km');
  263. $videoFeed = $this->gdata->getVideoFeed($query);
  264. $this->assertTrue(count($videoFeed->entry) > 0,
  265. 'Could not retrieve a single entry for location search:' .
  266. $query->getQueryUrl(2));
  267. }
  268. public function testPerformV2Query_SafeSearch()
  269. {
  270. $this->gdata->setMajorProtocolVersion(2);
  271. $query = $this->gdata->newVideoQuery();
  272. $query->setSafeSearch('strict');
  273. $videoFeed = $this->gdata->getVideoFeed($query);
  274. $this->assertTrue(count($videoFeed->entry) > 0,
  275. 'Could not retrieve a single entry for safeSearch=strict search:' .
  276. $query->getQueryUrl(2));
  277. }
  278. public function testPeformV2Query_Uploader()
  279. {
  280. $this->gdata->setMajorProtocolVersion(2);
  281. $query = $this->gdata->newVideoQuery();
  282. $query->setUploader('partner');
  283. $videoFeed = $this->gdata->getVideoFeed($query);
  284. $this->assertTrue(count($videoFeed->entry) > 0,
  285. 'Could not retrieve a single entry for uploader=partner search:' .
  286. $query->getQueryUrl(2));
  287. foreach($videoFeed as $videoEntry) {
  288. $mg = $videoEntry->getMediaGroup();
  289. $this->assertEquals('partner',
  290. $mg->getMediaCredit()->getYTtype());
  291. }
  292. }
  293. public function testAddUpdateAndDeletePlaylistV2()
  294. {
  295. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  296. $authenticationURL =
  297. 'https://www.google.com/youtube/accounts/ClientLogin';
  298. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  299. $this->user,
  300. $this->pass,
  301. $service,
  302. null, // client
  303. 'Google-UnitTests-1.0', // source
  304. null, // $loginToken
  305. null, // loginCaptcha
  306. $authenticationURL);
  307. $yt = new Zend_Gdata_YouTube(
  308. $httpClient, 'Google-UnitTests-1.0',
  309. 'ytapi-gdataops-12345-u78960r7-0',
  310. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  311. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  312. $yt->setMajorProtocolVersion(2);
  313. $feed = $yt->getPlaylistListFeed($this->ytAccount);
  314. // Add new
  315. $newPlaylist = $yt->newPlaylistListEntry();
  316. $newPlaylist->setMajorProtocolVersion(2);
  317. $titleString = $this->generateRandomString(10);
  318. $newPlaylist->title = $yt->newTitle()->setText($titleString);
  319. $newPlaylist->summary = $yt->newSummary()->setText('testing');
  320. $postUrl = 'https://gdata.youtube.com/feeds/api/users/default/playlists';
  321. $successfulInsertion = true;
  322. try {
  323. $yt->insertEntry($newPlaylist, $postUrl);
  324. } catch (Zend_Gdata_App_Exception $e) {
  325. $successfulInsertion = false;
  326. }
  327. $this->assertTrue($successfulInsertion, 'Failed to insert a new ' .
  328. 'playlist.');
  329. $playlistListFeed = $yt->getPlaylistListFeed('default');
  330. $playlistFound = false;
  331. $newPlaylistEntry = null;
  332. foreach ($playlistListFeed as $playlistListEntry) {
  333. if ($playlistListEntry->title->text == $titleString) {
  334. $playlistFound = true;
  335. $newPlaylistEntry = $playlistListEntry;
  336. break;
  337. }
  338. }
  339. $this->assertTrue($playlistFound, 'Could not find the newly inserted ' .
  340. 'playlist.');
  341. // Update it
  342. $newTitle = $this->generateRandomString(10);
  343. $newPlaylistEntry->title->setText($newTitle);
  344. $updatedSuccesfully = true;
  345. try {
  346. $newPlaylistEntry->save();
  347. } catch (Zend_Gdata_App_Exception $e) {
  348. $updatedSuccesfully = false;
  349. }
  350. $this->assertTrue($updatedSuccesfully, 'Could not succesfully update ' .
  351. 'a new playlist.');
  352. // Delete it
  353. $deletedSuccesfully = true;
  354. try {
  355. $newPlaylistEntry->delete();
  356. } catch (Zend_Gdata_App_Exception $e) {
  357. $deletedSuccesfully = false;
  358. }
  359. $this->assertTrue($deletedSuccesfully, 'Could not succesfully delete ' .
  360. 'a new playlist.');
  361. }
  362. public function testAddAndDeleteSubscriptionToChannelV2()
  363. {
  364. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  365. $authenticationURL =
  366. 'https://www.google.com/youtube/accounts/ClientLogin';
  367. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  368. $this->user,
  369. $this->pass,
  370. $service,
  371. null, // client
  372. 'Google-UnitTests-1.0', // source
  373. null, // loginToken
  374. null, // loginCaptcha
  375. $authenticationURL);
  376. $yt = new Zend_Gdata_YouTube(
  377. $httpClient, 'Google-UnitTests-1.0',
  378. 'ytapi-gdataops-12345-u78960r7-0',
  379. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  380. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  381. $yt->setMajorProtocolVersion(2);
  382. $channelToSubscribeTo = 'AssociatedPress';
  383. // Test for deletion first in case something went wrong
  384. // last time the test was run (network, etc...)
  385. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  386. $successDeletionUpFront = true;
  387. $message = null;
  388. foreach($subscriptionFeed as $subscriptionEntry) {
  389. $subscriptionType = null;
  390. $categories = $subscriptionEntry->getCategory();
  391. // examine the correct category element since there are multiple
  392. foreach($categories as $category) {
  393. if ($category->getScheme() ==
  394. 'http://gdata.youtube.com/schemas/2007/' .
  395. 'subscriptiontypes.cat') {
  396. $subscriptionType = $category->getTerm();
  397. }
  398. }
  399. if ($subscriptionType == 'channel') {
  400. if ($subscriptionEntry->getUsername()->text ==
  401. $channelToSubscribeTo) {
  402. try {
  403. $subscriptionEntry->delete();
  404. } catch (Zend_App_Exception $e) {
  405. $message = $e->getMessage();
  406. $successDeletionUpFront = false;
  407. }
  408. }
  409. }
  410. }
  411. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  412. 'subscription in unit test, could not delete prior to running ' .
  413. 'test -- ' . $message);
  414. // Channel
  415. $newSubscription = $yt->newSubscriptionEntry();
  416. $newSubscription->category = array(
  417. $yt->newCategory('channel',
  418. $this->subscriptionTypeSchema));
  419. $newSubscription->setUsername($yt->newUsername(
  420. $channelToSubscribeTo));
  421. $postUrl =
  422. 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
  423. $successPosting = true;
  424. $message = null;
  425. $insertedSubscription = null;
  426. try {
  427. $insertedSubscription = $yt->insertEntry(
  428. $newSubscription, $postUrl,
  429. 'Zend_Gdata_YouTube_SubscriptionEntry');
  430. } catch (Zend_App_Exception $e) {
  431. $message = $e->getMessage();
  432. $successPosting = false;
  433. }
  434. $this->assertTrue($successPosting, $message);
  435. // Delete it
  436. $successDeletion = true;
  437. $message = null;
  438. try {
  439. $insertedSubscription->delete();
  440. } catch (Zend_App_Exception $e) {
  441. $message = $e->getMessage();
  442. $successDeletion = false;
  443. }
  444. $this->assertTrue($successDeletion, $message);
  445. }
  446. public function testAddAndDeleteSubscriptionToFavoritesV2()
  447. {
  448. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  449. $authenticationURL =
  450. 'https://www.google.com/youtube/accounts/ClientLogin';
  451. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  452. $username = $this->user,
  453. $password = $this->pass,
  454. $service = $service,
  455. $client = null,
  456. $source = 'Google-UnitTests-1.0',
  457. $loginToken = null,
  458. $loginCaptcha = null,
  459. $authenticationURL);
  460. $yt = new Zend_Gdata_YouTube(
  461. $httpClient, 'Google-UnitTests-1.0',
  462. 'ytapi-gdataops-12345-u78960r7-0',
  463. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  464. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  465. $yt->setMajorProtocolVersion(2);
  466. $usernameOfFavoritesToSubscribeTo = 'CBS';
  467. // Test for deletion first in case something went wrong
  468. // last time the test was run (network, etc...)
  469. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  470. $successDeletionUpFront = true;
  471. $message = null;
  472. foreach($subscriptionFeed as $subscriptionEntry) {
  473. $subscriptionType = null;
  474. $categories = $subscriptionEntry->getCategory();
  475. // examine the correct category element since there are multiple
  476. foreach($categories as $category) {
  477. if ($category->getScheme() ==
  478. 'http://gdata.youtube.com/schemas/2007/' .
  479. 'subscriptiontypes.cat') {
  480. $subscriptionType = $category->getTerm();
  481. }
  482. }
  483. if ($subscriptionType == 'favorites') {
  484. if ($subscriptionEntry->getUsername()->text ==
  485. $usernameOfFavoritesToSubscribeTo) {
  486. try {
  487. $subscriptionEntry->delete();
  488. } catch (Zend_App_Exception $e) {
  489. $message = $e->getMessage();
  490. $successDeletionUpFront = false;
  491. }
  492. }
  493. }
  494. }
  495. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  496. 'subscription in unit test, could not delete prior to running ' .
  497. 'test -- ' . $message);
  498. // CBS's favorites
  499. $newSubscription = $yt->newSubscriptionEntry();
  500. $newSubscription->category = array(
  501. $yt->newCategory('favorites',
  502. $this->subscriptionTypeSchema));
  503. $newSubscription->setUsername($yt->newUsername(
  504. $usernameOfFavoritesToSubscribeTo));
  505. $postUrl =
  506. 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
  507. $successPosting = true;
  508. $message = null;
  509. $insertedSubscription = null;
  510. try {
  511. $insertedSubscription = $yt->insertEntry(
  512. $newSubscription, $postUrl,
  513. 'Zend_Gdata_YouTube_SubscriptionEntry');
  514. } catch (Zend_App_Exception $e) {
  515. $message = $e->getMessage();
  516. $successPosting = false;
  517. }
  518. $this->assertTrue($successPosting, $message);
  519. // Delete it
  520. $successDeletion = true;
  521. $message = null;
  522. try {
  523. $insertedSubscription->delete();
  524. } catch (Zend_App_Exception $e) {
  525. $message = $e->getMessage();
  526. $successDeletion = false;
  527. }
  528. $this->assertTrue($successDeletion, $message);
  529. }
  530. public function testAddAndDeleteSubscriptionToPlaylistV2()
  531. {
  532. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  533. $authenticationURL =
  534. 'https://www.google.com/youtube/accounts/ClientLogin';
  535. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  536. $username = $this->user,
  537. $password = $this->pass,
  538. $service = $service,
  539. $client = null,
  540. $source = 'Google-UnitTests-1.0',
  541. $loginToken = null,
  542. $loginCaptcha = null,
  543. $authenticationURL);
  544. $yt = new Zend_Gdata_YouTube(
  545. $httpClient, 'Google-UnitTests-1.0',
  546. 'ytapi-gdataops-12345-u78960r7-0',
  547. 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
  548. 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
  549. $yt->setMajorProtocolVersion(2);
  550. $playlistIdToSubscribeTo = '7A2BB4AFFEBED2A4';
  551. // Test for deletion first in case something went wrong
  552. // last time the test was run (network, etc...)
  553. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  554. $successDeletionUpFront = true;
  555. $message = null;
  556. foreach($subscriptionFeed as $subscriptionEntry) {
  557. $subscriptionType = null;
  558. $categories = $subscriptionEntry->getCategory();
  559. // examine the correct category element since there are multiple
  560. foreach($categories as $category) {
  561. if ($category->getScheme() ==
  562. 'http://gdata.youtube.com/schemas/2007/' .
  563. 'subscriptiontypes.cat') {
  564. $subscriptionType = $category->getTerm();
  565. }
  566. }
  567. if ($subscriptionType == 'playlist') {
  568. if ($subscriptionEntry->getPlaylistId()->text ==
  569. $playlistIdToSubscribeTo) {
  570. try {
  571. $subscriptionEntry->delete();
  572. } catch (Zend_App_Exception $e) {
  573. $message = $e->getMessage();
  574. $successDeletionUpFront = false;
  575. }
  576. }
  577. }
  578. }
  579. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  580. 'subscription in unit test, could not delete prior to running ' .
  581. 'test -- ' . $message);
  582. // Playlist of McGyver videos
  583. $newSubscription = $yt->newSubscriptionEntry();
  584. $newSubscription->setMajorProtocolVersion(2);
  585. $newSubscription->category = array(
  586. $yt->newCategory('playlist',
  587. $this->subscriptionTypeSchema));
  588. $newSubscription->setPlaylistId($yt->newPlaylistId(
  589. $playlistIdToSubscribeTo));
  590. $postUrl =
  591. 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
  592. $successPosting = true;
  593. $message = null;
  594. $insertedSubscription = null;
  595. try {
  596. $insertedSubscription = $yt->insertEntry(
  597. $newSubscription, $postUrl,
  598. 'Zend_Gdata_YouTube_SubscriptionEntry');
  599. } catch (Zend_App_Exception $e) {
  600. $message = $e->getMessage();
  601. $successPosting = false;
  602. }
  603. $this->assertTrue($successPosting, $message);
  604. // Delete it
  605. $successDeletion = true;
  606. $message = null;
  607. try {
  608. $insertedSubscription->delete();
  609. } catch (Zend_App_Exception $e) {
  610. $message = $e->getMessage();
  611. $successDeletion = false;
  612. }
  613. $this->assertTrue($successDeletion, $message);
  614. }
  615. public function testAddAndDeleteSubscriptionToQueryV2()
  616. {
  617. $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  618. $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  619. $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
  620. $authenticationURL =
  621. 'https://www.google.com/youtube/accounts/ClientLogin';
  622. $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
  623. $username = $this->user,
  624. $password = $this->pass,
  625. $service = $service,
  626. $client = null,
  627. $source = 'Google-UnitTests-1.0',
  628. $loginToken = null,
  629. $loginCaptcha = null,
  630. $authenticationURL);
  631. $yt = new Zend_Gdata_YouTube($httpClient, 'Google-UnitTests-1.0', $clientId, $developerKey);
  632. $yt->setMajorProtocolVersion(2);
  633. $queryStringToSubscribeTo = 'zend';
  634. // Test for deletion first in case something went wrong
  635. // last time the test was run (network, etc...)
  636. $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
  637. $successDeletionUpFront = true;
  638. $message = null;
  639. foreach($subscriptionFeed as $subscriptionEntry) {
  640. $subscriptionType = null;
  641. $categories = $subscriptionEntry->getCategory();
  642. // examine the correct category element since there are multiple
  643. foreach($categories as $category) {
  644. if ($category->getScheme() ==
  645. 'http://gdata.youtube.com/schemas/2007/' .
  646. 'subscriptiontypes.cat') {
  647. $subscriptionType = $category->getTerm();
  648. }
  649. }
  650. if ($subscriptionType == 'query') {
  651. if ($subscriptionEntry->getQueryString() ==
  652. $queryStringToSubscribeTo) {
  653. try {
  654. $subscriptionEntry->delete();
  655. } catch (Zend_App_Exception $e) {
  656. $message = $e->getMessage();
  657. $successDeletionUpFront = false;
  658. }
  659. }
  660. }
  661. }
  662. $this->assertTrue($successDeletionUpFront, 'Found existing ' .
  663. 'subscription in unit test, could not delete prior to running ' .
  664. 'test -- ' . $message);
  665. // Query
  666. $newSubscription = $yt->newSubscriptionEntry();
  667. $newSubscription->category = array(
  668. $yt->newCategory('query',
  669. $this->subscriptionTypeSchema));
  670. $newSubscription->setQueryString($yt->newQueryString(
  671. $queryStringToSubscribeTo));
  672. $postUrl =
  673. 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
  674. $successPosting = true;
  675. $message = null;
  676. $insertedSubscription = null;
  677. try {
  678. $insertedSubscription = $yt->insertEntry(
  679. $newSubscription, $postUrl,
  680. 'Zend_Gdata_YouTube_SubscriptionEntry');
  681. } catch (Zend_App_Exception $e) {
  682. $message = $e->getMessage();
  683. $successPosting = false;
  684. }
  685. $this->assertTrue($successPosting, $message);
  686. // Delete it
  687. $successDeletion = true;
  688. $message = null;
  689. try {
  690. $insertedSubscription->delete();
  691. } catch (Zend_App_Exception $e) {
  692. $message = $e->getMessage();
  693. $successDeletion = false;
  694. }
  695. $this->assertTrue($successDeletion, $message);
  696. }
  697. public function generateRandomString($length)
  698. {
  699. $outputString = null;
  700. for($i = 0; $i < $length; $i++) {
  701. $outputString .= chr(rand(65,90));
  702. }
  703. return $outputString;
  704. }
  705. public function testRetrieveActivityFeed()
  706. {
  707. $developerKey = constant(
  708. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  709. $clientId = constant(
  710. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  711. $client = Zend_Gdata_ClientLogin::getHttpClient(
  712. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  713. 'https://www.google.com/youtube/accounts/ClientLogin');
  714. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  715. $clientId, $developerKey);
  716. $youtube->setMajorProtocolVersion(2);
  717. $feed = $youtube->getActivityForUser($this->ytAccount);
  718. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  719. $this->assertTrue((count($feed->entries) > 0));
  720. $this->assertEquals('Activity of ' . $this->ytAccount,
  721. $feed->title->text);
  722. }
  723. public function testExceptionIfNotUsingDeveloperKey()
  724. {
  725. $exceptionThrown = false;
  726. $youtube = new Zend_Gdata_YouTube();
  727. $youtube->setMajorProtocolVersion(2);
  728. try {
  729. $youtube->getActivityForUser($this->ytAccount);
  730. } catch (Zend_Gdata_App_HttpException $e) {
  731. $exceptionThrown = true;
  732. }
  733. $this->assertTrue($exceptionThrown, 'Was expecting an exception when ' .
  734. 'making a request to the YouTube Activity feed without a ' .
  735. 'developer key.');
  736. }
  737. public function testRetrieveActivityFeedForMultipleUsers()
  738. {
  739. $developerKey = constant(
  740. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  741. $clientId = constant(
  742. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  743. $client = Zend_Gdata_ClientLogin::getHttpClient(
  744. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  745. 'https://www.google.com/youtube/accounts/ClientLogin');
  746. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  747. $clientId, $developerKey);
  748. $youtube->setMajorProtocolVersion(2);
  749. $feed = $youtube->getActivityForUser($this->ytAccount .
  750. ',associatedpress');
  751. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  752. $this->assertTrue((count($feed->entries) > 0));
  753. $this->assertEquals('Activity of ' . $this->ytAccount .
  754. ',associatedpress', $feed->title->text);
  755. }
  756. public function testRetrieveFriendFeed()
  757. {
  758. $developerKey = constant(
  759. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  760. $clientId = constant(
  761. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  762. $client = Zend_Gdata_ClientLogin::getHttpClient(
  763. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  764. 'https://www.google.com/youtube/accounts/ClientLogin');
  765. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  766. $clientId, $developerKey);
  767. $youtube->setMajorProtocolVersion(2);
  768. $feed = $youtube->getFriendActivityForCurrentUser();
  769. $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
  770. $this->assertTrue((count($feed->entries) > 0));
  771. $this->assertEquals('Activity of the friends of ' . $this->ytAccount,
  772. $feed->title->text);
  773. }
  774. public function testThrowExceptionOnActivityFeedRequestForMoreThan20Users()
  775. {
  776. $exceptionThrown = false;
  777. $listOfMoreThan20Users = null;
  778. $youtube = new Zend_Gdata_YouTube();
  779. $youtube->setMajorProtocolVersion(2);
  780. for ($x = 0; $x < 30; $x++) {
  781. $listOfMoreThan20Users .= "user$x";
  782. if ($x != 29) {
  783. $listOfMoreThan20Users .= ",";
  784. }
  785. }
  786. try {
  787. $youtube->getActivityForUser($listOfMoreThan20Users);
  788. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  789. $exceptionThrown = true;
  790. }
  791. $this->assertTrue($exceptionThrown, 'Was expecting an exception on ' .
  792. 'a request to ->getActivityForUser when more than 20 users were ' .
  793. 'specified in YouTube.php');
  794. }
  795. public function testGetInboxFeedForCurrentUserV1()
  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. $inboxFeed = $youtube->getInboxFeedForCurrentUser();
  807. $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
  808. $this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
  809. $this->ytAccount . ' had no messages in their inbox.');
  810. // get the first entry
  811. $inboxFeed->rewind();
  812. $inboxEntry = $inboxFeed->current();
  813. $this->assertTrue(
  814. $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
  815. $this->assertTrue($inboxEntry->getTitle()->text != '');
  816. }
  817. public function testGetInboxFeedForCurrentUserV2()
  818. {
  819. $developerKey = constant(
  820. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  821. $clientId = constant(
  822. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  823. $client = Zend_Gdata_ClientLogin::getHttpClient(
  824. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  825. 'https://www.google.com/youtube/accounts/ClientLogin');
  826. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  827. $clientId, $developerKey);
  828. $youtube->setMajorProtocolVersion(2);
  829. $inboxFeed = $youtube->getInboxFeedForCurrentUser();
  830. $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
  831. $this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
  832. $this->ytAccount . ' had no messages in their inbox.');
  833. // get the first entry
  834. $inboxFeed->rewind();
  835. $inboxEntry = $inboxFeed->current();
  836. $this->assertTrue(
  837. $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
  838. $this->assertTrue($inboxEntry->getTitle()->text != '');
  839. }
  840. public function testSendAMessageV2()
  841. {
  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. $this->user, $this->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. $developerKey = constant(
  864. 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  865. $clientId = constant(
  866. 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  867. $client = Zend_Gdata_ClientLogin::getHttpClient(
  868. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  869. 'https://www.google.com/youtube/accounts/ClientLogin');
  870. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  871. $clientId, $developerKey);
  872. $youtube->setMajorProtocolVersion(1);
  873. // get a video from the recently featured video feed
  874. $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
  875. $videoEntry = $videoFeed->entry[0];
  876. $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
  877. // sending message to gdpython (python client library unit test user)
  878. $sentMessage = $youtube->sendVideoMessage(
  879. 'Sending a v1 test message from Zend_Gdata_YouTubeOnlineTest.',
  880. $videoEntry, null, 'gdpython');
  881. $this->assertTrue(
  882. $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
  883. }
  884. public function testThrowExceptionOnSendingMessageWithoutVideo()
  885. {
  886. $exceptionCaught = false;
  887. $this->gdata = new Zend_Gdata_YouTube();
  888. try {
  889. $this->gdata->sendVideoMessage('Should fail', null, null, 'foo');
  890. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  891. $exceptionCaught = true;
  892. }
  893. $this->assertTrue($exceptionCaught, 'Was expecting an exception if ' .
  894. 'sending a message without a video');
  895. }
  896. public function testCommentOnAComment()
  897. {
  898. $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
  899. $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
  900. $client = Zend_Gdata_ClientLogin::getHttpClient(
  901. $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
  902. 'https://www.google.com/youtube/accounts/ClientLogin');
  903. $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
  904. $clientId, $developerKey);
  905. $youtube->setMajorProtocolVersion(2);
  906. $mostDiscussedFeed = $youtube->getVideoFeed(
  907. 'https://gdata.youtube.com/feeds/api/standardfeeds/most_discussed');
  908. // get first entry
  909. $mostDiscussedFeed->rewind();
  910. $firstEntry = $mostDiscussedFeed->current();
  911. $this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_VideoEntry);
  912. $commentFeed = $youtube->getVideoCommentFeed($firstEntry->getVideoId());
  913. // get first comment
  914. $commentFeed->rewind();
  915. $firstCommentEntry = $commentFeed->current();
  916. $commentedComment = $youtube->replyToCommentEntry($firstCommentEntry,
  917. 'awesome ! (ZFUnitTest-test)');
  918. $this->assertTrue(
  919. $commentedComment instanceof Zend_Gdata_YouTube_CommentEntry);
  920. }
  921. }