2
0

YouTubeOnlineTest.php 41 KB

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