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.
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
23 require_once dirname(dirname(dirname(__FILE__
))) . DIRECTORY_SEPARATOR
. 'TestHelper.php';
25 require_once 'Zend/Gdata/YouTube.php';
26 require_once 'Zend/Gdata/YouTube/VideoQuery.php';
27 require_once 'Zend/Gdata/ClientLogin.php';
31 * @package Zend_Gdata_YouTube
32 * @subpackage UnitTests
33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
36 * @group Zend_Gdata_YouTube
38 class Zend_Gdata_YouTubeOnlineTest
extends PHPUnit_Framework_TestCase
41 public function setUp()
43 $this->ytAccount
= constant('TESTS_ZEND_GDATA_YOUTUBE_ACCOUNT');
44 $this->user
= constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
45 $this->pass
= constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
46 $this->subscriptionTypeSchema
= 'http://gdata.youtube.com/schemas/' .
47 '2007/subscriptiontypes.cat';
48 $this->gdata
= new Zend_Gdata_YouTube();
51 public function tearDown()
55 public function testRetrieveSubScriptionFeed()
57 $feed = $this->gdata
->getSubscriptionFeed($this->ytAccount
);
58 $this->assertTrue($feed->totalResults
->text
> 0);
59 $this->assertEquals('Subscriptions of ' . $this->ytAccount
,
61 $this->assertTrue(count($feed->entry
) > 0);
62 foreach ($feed->entry
as $entry) {
63 $this->assertTrue($entry->title
->text
!= '');
67 public function testRetrieveContactFeed()
69 $feed = $this->gdata
->getContactFeed($this->ytAccount
);
70 $this->assertTrue($feed->totalResults
->text
> 0);
71 $this->assertEquals('Contacts of ' . $this->ytAccount
,
73 $this->assertTrue(count($feed->entry
) > 0);
74 foreach ($feed->entry
as $entry) {
75 $this->assertTrue($entry->title
->text
!= '');
77 $this->assertEquals('ytgdatatest1', $feed->entry
[0]->username
->text
);
80 public function testRetrieveUserVideos()
82 $feed = $this->gdata
->getUserUploads($this->ytAccount
);
83 $this->assertEquals('Uploads by ' . $this->ytAccount
,
85 $this->assertTrue(count($feed->entry
) === 1);
88 public function testRetrieveVideoFeed()
90 $feed = $this->gdata
->getVideoFeed();
92 $query = new Zend_Gdata_YouTube_VideoQuery();
93 $query->setVideoQuery('puppy');
94 $feed = $this->gdata
->getVideoFeed($query);
95 foreach ($feed as $videoEntry) {
96 $videoResponsesLink = $videoEntry->getVideoResponsesLink();
97 $videoRatingsLink = $videoEntry->getVideoRatingsLink();
98 $videoComplaintsLink = $videoEntry->getVideoComplaintsLink();
101 $feed = $this->gdata
->getVideoFeed($query->getQueryUrl());
104 public function testRetrieveVideoEntry()
106 $entry = $this->gdata
->getVideoEntry('66wj2g5yz0M');
107 $this->assertEquals('TestMovie', $entry->title
->text
);
109 $entry = $this->gdata
->getVideoEntry(null, 'http://gdata.youtube.com/feeds/api/videos/66wj2g5yz0M');
110 $this->assertEquals('TestMovie', $entry->title
->text
);
113 public function testRetrieveOtherFeeds()
115 $feed = $this->gdata
->getRelatedVideoFeed('66wj2g5yz0M');
116 $feed = $this->gdata
->getVideoResponseFeed('66wj2g5yz0M');
117 $feed = $this->gdata
->getVideoCommentFeed('66wj2g5yz0M');
118 $feed = $this->gdata
->getWatchOnMobileVideoFeed();
119 $feed = $this->gdata
->getUserFavorites($this->ytAccount
);
122 public function testRetrieveUserProfile()
124 $entry = $this->gdata
->getUserProfile($this->ytAccount
);
125 $this->assertEquals($this->ytAccount
. ' Channel', $entry->title
->text
);
126 $this->assertEquals($this->ytAccount
, $entry->username
->text
);
127 $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.',
128 $entry->description
->text
);
129 $this->assertEquals(32, $entry->age
->text
);
130 $this->assertEquals('crime and punishment, ps i love you, the stand', $entry->books
->text
);
131 $this->assertEquals('Google', $entry->company
->text
);
132 $this->assertEquals('software engineering, information architecture, photography, travel', $entry->hobbies
->text
);
133 $this->assertEquals('Mountain View, CA', $entry->hometown
->text
);
134 $this->assertEquals('San Francisco, CA 94114, US', $entry->location
->text
);
135 $this->assertEquals('monk, heroes, law and order, top gun', $entry->movies
->text
);
136 $this->assertEquals('imogen heap, frou frou, thievory corp, morcheeba, barenaked ladies', $entry->music
->text
);
137 $this->assertEquals('Developer Programs', $entry->occupation
->text
);
138 $this->assertEquals('University of the World', $entry->school
->text
);
139 $this->assertEquals('f', $entry->gender
->text
);
140 $this->assertEquals('taken', $entry->relationship
->text
);
143 public function testRetrieveAndUpdatePlaylistList()
146 $service = Zend_Gdata_YouTube
::AUTH_SERVICE_NAME
;
147 $authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
148 $httpClient = Zend_Gdata_ClientLogin
::getHttpClient(
149 $username = $this->user
,
150 $password = $this->pass
,
153 $source = 'Google-UnitTests-1.0',
155 $loginCaptcha = null,
158 $this->gdata
= new Zend_Gdata_YouTube($httpClient,
159 'Google-UnitTests-1.0', 'ytapi-gdataops-12345-u78960r7-0',
160 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7Eyu1IuvkioESq' .
161 'zRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
163 $this->gdata
->setMajorProtocolVersion(2);
164 $feed = $this->gdata
->getPlaylistListFeed($this->ytAccount
);
165 $this->assertTrue($feed->totalResults
->text
> 0);
166 $this->assertEquals('Playlists of ' . $this->ytAccount
,
168 $this->assertTrue(count($feed->entry
) > 0);
170 foreach ($feed->entry
as $entry) {
171 $this->assertTrue($entry->title
->text
!= '');
173 $entry->title
->setText('new playlist title');
180 public function testRetrievePlaylistV2()
182 $this->gdata
->setMajorProtocolVersion(2);
183 $feed = $this->gdata
->getPlaylistListFeed($this->ytAccount
);
184 $firstEntry = $feed->entries
[0];
185 $this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_PlaylistListEntry
);
186 $this->assertTrue($firstEntry->getSummary()->text
!= null);
189 public function testRetrievePlaylistVideoFeed()
191 $listFeed = $this->gdata
->getPlaylistListFeed($this->ytAccount
);
193 $feed = $this->gdata
->getPlaylistVideoFeed($listFeed->entry
[0]->feedLink
[0]->href
);
194 $this->assertTrue($feed->totalResults
->text
> 0);
195 $this->assertTrue(count($feed->entry
) > 0);
196 foreach ($feed->entry
as $entry) {
197 $this->assertTrue($entry->title
->text
!= '');
201 public function testRetrieveTopRatedVideos()
203 $feed = $this->gdata
->getTopRatedVideoFeed();
204 $this->assertTrue($feed->totalResults
->text
> 10);
205 $this->assertEquals('Top Rated', $feed->title
->text
);
206 $this->assertTrue(count($feed->entry
) > 0);
207 foreach ($feed->entry
as $entry) {
208 $this->assertTrue($entry->rating
->average
> 3);
209 $this->assertEquals(1, $entry->rating
->min
);
210 $this->assertEquals(5, $entry->rating
->max
);
211 $this->assertTrue($entry->rating
->numRaters
> 2);
215 public function testRetrieveTopRatedVideosV2()
217 $this->gdata
->setMajorProtocolVersion(2);
218 $feed = $this->gdata
->getTopRatedVideoFeed();
219 $client = $this->gdata
->getHttpClient();
220 $positionOfAPIProjection = strpos(
221 $client->getLastRequest(), "/feeds/api/");
222 $this->assertTrue(is_numeric($positionOfAPIProjection));
225 public function testRetrieveMostViewedVideosV2()
227 $this->gdata
->setMajorProtocolVersion(2);
228 $feed = $this->gdata
->getMostViewedVideoFeed();
229 $client = $this->gdata
->getHttpClient();
230 $positionOfAPIProjection = strpos(
231 $client->getLastRequest(), "/feeds/api/");
232 $this->assertTrue(is_numeric($positionOfAPIProjection));
235 public function testRetrieveRecentlyFeaturedVideosV2()
237 $this->gdata
->setMajorProtocolVersion(2);
238 $feed = $this->gdata
->getRecentlyFeaturedVideoFeed();
239 $client = $this->gdata
->getHttpClient();
240 $positionOfAPIProjection = strpos(
241 $client->getLastRequest(), "/feeds/api/");
242 $this->assertTrue(is_numeric($positionOfAPIProjection));
245 public function testWatchOnMobileVideosV2()
247 $this->gdata
->setMajorProtocolVersion(2);
248 $feed = $this->gdata
->getWatchOnMobileVideoFeed();
249 $client = $this->gdata
->getHttpClient();
250 $positionOfAPIProjection = strpos(
251 $client->getLastRequest(), "/feeds/api/");
252 $this->assertTrue(is_numeric($positionOfAPIProjection));
255 public function testRetrieveMostViewedVideos()
257 $feed = $this->gdata
->getMostViewedVideoFeed();
258 $this->assertTrue($feed->totalResults
->text
> 10);
259 $this->assertEquals('Most Viewed', $feed->title
->text
);
260 $this->assertTrue(count($feed->entry
) > 0);
261 foreach ($feed->entry
as $entry) {
262 if ($entry->rating
) {
263 $this->assertEquals(1, $entry->rating
->min
);
264 $this->assertEquals(5, $entry->rating
->max
);
269 public function testPerformV2Query_Location()
271 $this->gdata
->setMajorProtocolVersion(2);
272 $query = $this->gdata
->newVideoQuery();
273 // Setting location to New York City
274 $query->setLocation('-37.0625,-95.677068');
275 $query->setLocationRadius('1000km');
276 $videoFeed = $this->gdata
->getVideoFeed($query);
277 $this->assertTrue(count($videoFeed->entry
) > 0,
278 'Could not retrieve a single entry for location search:' .
279 $query->getQueryUrl(2));
282 public function testPerformV2Query_SafeSearch()
284 $this->gdata
->setMajorProtocolVersion(2);
285 $query = $this->gdata
->newVideoQuery();
286 $query->setSafeSearch('strict');
287 $videoFeed = $this->gdata
->getVideoFeed($query);
288 $this->assertTrue(count($videoFeed->entry
) > 0,
289 'Could not retrieve a single entry for safeSearch=strict search:' .
290 $query->getQueryUrl(2));
293 public function testPeformV2Query_Uploader()
295 $this->gdata
->setMajorProtocolVersion(2);
296 $query = $this->gdata
->newVideoQuery();
297 $query->setUploader('partner');
298 $videoFeed = $this->gdata
->getVideoFeed($query);
299 $this->assertTrue(count($videoFeed->entry
) > 0,
300 'Could not retrieve a single entry for uploader=partner search:' .
301 $query->getQueryUrl(2));
303 foreach($videoFeed as $videoEntry) {
304 $mg = $videoEntry->getMediaGroup();
305 $this->assertEquals('partner',
306 $mg->getMediaCredit()->getYTtype());
310 public function testAddUpdateAndDeletePlaylistV2()
312 $service = Zend_Gdata_YouTube
::AUTH_SERVICE_NAME
;
314 'https://www.google.com/youtube/accounts/ClientLogin';
315 $httpClient = Zend_Gdata_ClientLogin
::getHttpClient(
316 $username = $this->user
,
317 $password = $this->pass
,
320 $source = 'Google-UnitTests-1.0',
322 $loginCaptcha = null,
325 $yt = new Zend_Gdata_YouTube(
326 $httpClient, 'Google-UnitTests-1.0',
327 'ytapi-gdataops-12345-u78960r7-0',
328 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
329 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
331 $yt->setMajorProtocolVersion(2);
332 $feed = $yt->getPlaylistListFeed($this->ytAccount
);
335 $newPlaylist = $yt->newPlaylistListEntry();
336 $newPlaylist->setMajorProtocolVersion(2);
337 $titleString = $this->generateRandomString(10);
338 $newPlaylist->title
= $yt->newTitle()->setText($titleString);
339 $newPlaylist->summary
= $yt->newSummary()->setText('testing');
340 $postUrl = 'http://gdata.youtube.com/feeds/api/users/default/playlists';
341 $successfulInsertion = true;
344 $yt->insertEntry($newPlaylist, $postUrl);
345 } catch (Zend_Gdata_App_Exception
$e) {
346 $successfulInsertion = false;
349 $this->assertTrue($successfulInsertion, 'Failed to insert a new ' .
352 $playlistListFeed = $yt->getPlaylistListFeed('default');
354 $playlistFound = false;
355 $newPlaylistEntry = null;
357 foreach ($playlistListFeed as $playlistListEntry) {
358 if ($playlistListEntry->title
->text
== $titleString) {
359 $playlistFound = true;
360 $newPlaylistEntry = $playlistListEntry;
365 $this->assertTrue($playlistFound, 'Could not find the newly inserted ' .
369 $newTitle = $this->generateRandomString(10);
370 $newPlaylistEntry->title
->setText($newTitle);
371 $updatedSuccesfully = true;
373 $newPlaylistEntry->save();
374 } catch (Zend_Gdata_App_Exception
$e) {
375 $updatedSuccesfully = false;
378 $this->assertTrue($updatedSuccesfully, 'Could not succesfully update ' .
382 $deletedSuccesfully = true;
384 $newPlaylistEntry->delete();
385 } catch (Zend_Gdata_App_Exception
$e) {
386 $deletedSuccesfully = false;
389 $this->assertTrue($deletedSuccesfully, 'Could not succesfully delete ' .
393 public function testAddAndDeleteSubscriptionToChannelV2()
395 $service = Zend_Gdata_YouTube
::AUTH_SERVICE_NAME
;
397 'https://www.google.com/youtube/accounts/ClientLogin';
398 $httpClient = Zend_Gdata_ClientLogin
::getHttpClient(
399 $username = $this->user
,
400 $password = $this->pass
,
403 $source = 'Google-UnitTests-1.0',
405 $loginCaptcha = null,
408 $yt = new Zend_Gdata_YouTube(
409 $httpClient, 'Google-UnitTests-1.0',
410 'ytapi-gdataops-12345-u78960r7-0',
411 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
412 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
414 $yt->setMajorProtocolVersion(2);
416 $channelToSubscribeTo = 'AssociatedPress';
418 // Test for deletion first in case something went wrong
419 // last time the test was run (network, etc...)
420 $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount
);
421 $successDeletionUpFront = true;
423 foreach($subscriptionFeed as $subscriptionEntry) {
424 $subscriptionType = null;
425 $categories = $subscriptionEntry->getCategory();
426 // examine the correct category element since there are multiple
427 foreach($categories as $category) {
428 if ($category->getScheme() ==
429 'http://gdata.youtube.com/schemas/2007/' .
430 'subscriptiontypes.cat') {
431 $subscriptionType = $category->getTerm();
434 if ($subscriptionType == 'channel') {
435 if ($subscriptionEntry->getUsername()->text
==
436 $channelToSubscribeTo) {
438 $subscriptionEntry->delete();
439 } catch (Zend_App_Exception
$e) {
440 $message = $e->getMessage();
441 $successDeletionUpFront = false;
446 $this->assertTrue($successDeletionUpFront, 'Found existing ' .
447 'subscription in unit test, could not delete prior to running ' .
448 'test -- ' . $message);
451 $newSubscription = $yt->newSubscriptionEntry();
452 $newSubscription->category
= array(
453 $yt->newCategory('channel',
454 $this->subscriptionTypeSchema
));
455 $newSubscription->setUsername($yt->newUsername(
456 $channelToSubscribeTo));
459 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
461 $successPosting = true;
463 $insertedSubscription = null;
465 $insertedSubscription = $yt->insertEntry(
466 $newSubscription, $postUrl,
467 'Zend_Gdata_YouTube_SubscriptionEntry');
468 } catch (Zend_App_Exception
$e) {
469 $message = $e->getMessage();
470 $successPosting = false;
473 $this->assertTrue($successPosting, $message);
476 $successDeletion = true;
479 $insertedSubscription->delete();
480 } catch (Zend_App_Exception
$e) {
481 $message = $e->getMessage();
482 $successDeletion = false;
485 $this->assertTrue($successDeletion, $message);
488 public function testAddAndDeleteSubscriptionToFavoritesV2()
490 $service = Zend_Gdata_YouTube
::AUTH_SERVICE_NAME
;
492 'https://www.google.com/youtube/accounts/ClientLogin';
493 $httpClient = Zend_Gdata_ClientLogin
::getHttpClient(
494 $username = $this->user
,
495 $password = $this->pass
,
498 $source = 'Google-UnitTests-1.0',
500 $loginCaptcha = null,
503 $yt = new Zend_Gdata_YouTube(
504 $httpClient, 'Google-UnitTests-1.0',
505 'ytapi-gdataops-12345-u78960r7-0',
506 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
507 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
509 $yt->setMajorProtocolVersion(2);
511 $usernameOfFavoritesToSubscribeTo = 'CBS';
513 // Test for deletion first in case something went wrong
514 // last time the test was run (network, etc...)
515 $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount
);
516 $successDeletionUpFront = true;
518 foreach($subscriptionFeed as $subscriptionEntry) {
519 $subscriptionType = null;
520 $categories = $subscriptionEntry->getCategory();
521 // examine the correct category element since there are multiple
522 foreach($categories as $category) {
523 if ($category->getScheme() ==
524 'http://gdata.youtube.com/schemas/2007/' .
525 'subscriptiontypes.cat') {
526 $subscriptionType = $category->getTerm();
529 if ($subscriptionType == 'favorites') {
530 if ($subscriptionEntry->getUsername()->text
==
531 $usernameOfFavoritesToSubscribeTo) {
533 $subscriptionEntry->delete();
534 } catch (Zend_App_Exception
$e) {
535 $message = $e->getMessage();
536 $successDeletionUpFront = false;
541 $this->assertTrue($successDeletionUpFront, 'Found existing ' .
542 'subscription in unit test, could not delete prior to running ' .
543 'test -- ' . $message);
546 $newSubscription = $yt->newSubscriptionEntry();
547 $newSubscription->category
= array(
548 $yt->newCategory('favorites',
549 $this->subscriptionTypeSchema
));
550 $newSubscription->setUsername($yt->newUsername(
551 $usernameOfFavoritesToSubscribeTo));
554 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
556 $successPosting = true;
558 $insertedSubscription = null;
560 $insertedSubscription = $yt->insertEntry(
561 $newSubscription, $postUrl,
562 'Zend_Gdata_YouTube_SubscriptionEntry');
563 } catch (Zend_App_Exception
$e) {
564 $message = $e->getMessage();
565 $successPosting = false;
568 $this->assertTrue($successPosting, $message);
571 $successDeletion = true;
574 $insertedSubscription->delete();
575 } catch (Zend_App_Exception
$e) {
576 $message = $e->getMessage();
577 $successDeletion = false;
580 $this->assertTrue($successDeletion, $message);
583 public function testAddAndDeleteSubscriptionToPlaylistV2()
585 $service = Zend_Gdata_YouTube
::AUTH_SERVICE_NAME
;
587 'https://www.google.com/youtube/accounts/ClientLogin';
588 $httpClient = Zend_Gdata_ClientLogin
::getHttpClient(
589 $username = $this->user
,
590 $password = $this->pass
,
593 $source = 'Google-UnitTests-1.0',
595 $loginCaptcha = null,
598 $yt = new Zend_Gdata_YouTube(
599 $httpClient, 'Google-UnitTests-1.0',
600 'ytapi-gdataops-12345-u78960r7-0',
601 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
602 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
604 $yt->setMajorProtocolVersion(2);
605 $playlistIdToSubscribeTo = '7A2BB4AFFEBED2A4';
607 // Test for deletion first in case something went wrong
608 // last time the test was run (network, etc...)
609 $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount
);
610 $successDeletionUpFront = true;
612 foreach($subscriptionFeed as $subscriptionEntry) {
613 $subscriptionType = null;
614 $categories = $subscriptionEntry->getCategory();
615 // examine the correct category element since there are multiple
616 foreach($categories as $category) {
617 if ($category->getScheme() ==
618 'http://gdata.youtube.com/schemas/2007/' .
619 'subscriptiontypes.cat') {
620 $subscriptionType = $category->getTerm();
623 if ($subscriptionType == 'playlist') {
624 if ($subscriptionEntry->getPlaylistId()->text
==
625 $playlistIdToSubscribeTo) {
627 $subscriptionEntry->delete();
628 } catch (Zend_App_Exception
$e) {
629 $message = $e->getMessage();
630 $successDeletionUpFront = false;
635 $this->assertTrue($successDeletionUpFront, 'Found existing ' .
636 'subscription in unit test, could not delete prior to running ' .
637 'test -- ' . $message);
639 // Playlist of McGyver videos
640 $newSubscription = $yt->newSubscriptionEntry();
641 $newSubscription->setMajorProtocolVersion(2);
642 $newSubscription->category
= array(
643 $yt->newCategory('playlist',
644 $this->subscriptionTypeSchema
));
645 $newSubscription->setPlaylistId($yt->newPlaylistId(
646 $playlistIdToSubscribeTo));
649 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
651 $successPosting = true;
653 $insertedSubscription = null;
655 $insertedSubscription = $yt->insertEntry(
656 $newSubscription, $postUrl,
657 'Zend_Gdata_YouTube_SubscriptionEntry');
658 } catch (Zend_App_Exception
$e) {
659 $message = $e->getMessage();
660 $successPosting = false;
663 $this->assertTrue($successPosting, $message);
666 $successDeletion = true;
669 $insertedSubscription->delete();
670 } catch (Zend_App_Exception
$e) {
671 $message = $e->getMessage();
672 $successDeletion = false;
675 $this->assertTrue($successDeletion, $message);
678 public function testAddAndDeleteSubscriptionToQueryV2()
680 $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
681 $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
683 $service = Zend_Gdata_YouTube
::AUTH_SERVICE_NAME
;
685 'https://www.google.com/youtube/accounts/ClientLogin';
686 $httpClient = Zend_Gdata_ClientLogin
::getHttpClient(
687 $username = $this->user
,
688 $password = $this->pass
,
691 $source = 'Google-UnitTests-1.0',
693 $loginCaptcha = null,
696 $yt = new Zend_Gdata_YouTube($httpClient, 'Google-UnitTests-1.0', $clientId, $developerKey);
698 $yt->setMajorProtocolVersion(2);
699 $queryStringToSubscribeTo = 'zend';
701 // Test for deletion first in case something went wrong
702 // last time the test was run (network, etc...)
703 $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount
);
704 $successDeletionUpFront = true;
706 foreach($subscriptionFeed as $subscriptionEntry) {
707 $subscriptionType = null;
708 $categories = $subscriptionEntry->getCategory();
709 // examine the correct category element since there are multiple
710 foreach($categories as $category) {
711 if ($category->getScheme() ==
712 'http://gdata.youtube.com/schemas/2007/' .
713 'subscriptiontypes.cat') {
714 $subscriptionType = $category->getTerm();
717 if ($subscriptionType == 'query') {
718 if ($subscriptionEntry->getQueryString() ==
719 $queryStringToSubscribeTo) {
721 $subscriptionEntry->delete();
722 } catch (Zend_App_Exception
$e) {
723 $message = $e->getMessage();
724 $successDeletionUpFront = false;
729 $this->assertTrue($successDeletionUpFront, 'Found existing ' .
730 'subscription in unit test, could not delete prior to running ' .
731 'test -- ' . $message);
734 $newSubscription = $yt->newSubscriptionEntry();
735 $newSubscription->category
= array(
736 $yt->newCategory('query',
737 $this->subscriptionTypeSchema
));
738 $newSubscription->setQueryString($yt->newQueryString(
739 $queryStringToSubscribeTo));
742 'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
744 $successPosting = true;
746 $insertedSubscription = null;
748 $insertedSubscription = $yt->insertEntry(
749 $newSubscription, $postUrl,
750 'Zend_Gdata_YouTube_SubscriptionEntry');
751 } catch (Zend_App_Exception
$e) {
752 $message = $e->getMessage();
753 $successPosting = false;
756 $this->assertTrue($successPosting, $message);
759 $successDeletion = true;
762 $insertedSubscription->delete();
763 } catch (Zend_App_Exception
$e) {
764 $message = $e->getMessage();
765 $successDeletion = false;
768 $this->assertTrue($successDeletion, $message);
771 public function generateRandomString($length)
773 $outputString = null;
774 for($i = 0; $i < $length; $i++
) {
775 $outputString .= chr(rand(65,90));
777 return $outputString;
780 public function testRetrieveActivityFeed()
782 $developerKey = constant(
783 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
784 $clientId = constant(
785 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
786 $client = Zend_Gdata_ClientLogin
::getHttpClient(
787 $this->user
, $this->pass
, 'youtube' , null, 'ZF_UnitTest', null, null,
788 'https://www.google.com/youtube/accounts/ClientLogin');
790 $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
791 $clientId, $developerKey);
792 $youtube->setMajorProtocolVersion(2);
794 $feed = $youtube->getActivityForUser($this->ytAccount
);
795 $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed
);
796 $this->assertTrue((count($feed->entries
) > 0));
797 $this->assertEquals('Activity of ' . $this->ytAccount
,
801 public function testExceptionIfNotUsingDeveloperKey()
803 $exceptionThrown = false;
804 $youtube = new Zend_Gdata_YouTube();
805 $youtube->setMajorProtocolVersion(2);
807 $youtube->getActivityForUser($this->ytAccount
);
808 } catch (Zend_Gdata_App_HttpException
$e) {
809 $exceptionThrown = true;
811 $this->assertTrue($exceptionThrown, 'Was expecting an exception when ' .
812 'making a request to the YouTube Activity feed without a ' .
816 public function testRetrieveActivityFeedForMultipleUsers()
818 $developerKey = constant(
819 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
820 $clientId = constant(
821 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
822 $client = Zend_Gdata_ClientLogin
::getHttpClient(
823 $this->user
, $this->pass
, 'youtube' , null, 'ZF_UnitTest', null, null,
824 'https://www.google.com/youtube/accounts/ClientLogin');
826 $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
827 $clientId, $developerKey);
828 $youtube->setMajorProtocolVersion(2);
830 $feed = $youtube->getActivityForUser($this->ytAccount
.
832 $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed
);
833 $this->assertTrue((count($feed->entries
) > 0));
834 $this->assertEquals('Activity of ' . $this->ytAccount
.
835 ',associatedpress', $feed->title
->text
);
838 public function testRetrieveFriendFeed()
840 $developerKey = constant(
841 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
842 $clientId = constant(
843 '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');
848 $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
849 $clientId, $developerKey);
850 $youtube->setMajorProtocolVersion(2);
852 $feed = $youtube->getFriendActivityForCurrentUser();
853 $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed
);
854 $this->assertTrue((count($feed->entries
) > 0));
855 $this->assertEquals('Activity of the friends of ' . $this->ytAccount
,
859 public function testThrowExceptionOnActivityFeedRequestForMoreThan20Users()
861 $exceptionThrown = false;
862 $listOfMoreThan20Users = null;
863 $youtube = new Zend_Gdata_YouTube();
864 $youtube->setMajorProtocolVersion(2);
866 for ($x = 0; $x < 30; $x++
) {
867 $listOfMoreThan20Users .= "user$x";
869 $listOfMoreThan20Users .= ",";
874 $youtube->getActivityForUser($listOfMoreThan20Users);
875 } catch (Zend_Gdata_App_InvalidArgumentException
$e) {
876 $exceptionThrown = true;
878 $this->assertTrue($exceptionThrown, 'Was expecting an exception on ' .
879 'a request to ->getActivityForUser when more than 20 users were ' .
880 'specified in YouTube.php');
883 public function testGetInboxFeedForCurrentUserV1()
885 $developerKey = constant(
886 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
887 $clientId = constant(
888 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
889 $client = Zend_Gdata_ClientLogin
::getHttpClient(
890 $this->user
, $this->pass
, 'youtube' , null, 'ZF_UnitTest', null, null,
891 'https://www.google.com/youtube/accounts/ClientLogin');
893 $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
894 $clientId, $developerKey);
896 $inboxFeed = $youtube->getInboxFeedForCurrentUser();
897 $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed
);
898 $this->assertTrue(count($inboxFeed->entries
) > 0, 'Test account ' .
899 $this->ytAccount
. ' had no messages in their inbox.');
901 // get the first entry
902 $inboxFeed->rewind();
903 $inboxEntry = $inboxFeed->current();
905 $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry
);
906 $this->assertTrue($inboxEntry->getTitle()->text
!= '');
909 public function testGetInboxFeedForCurrentUserV2()
911 $developerKey = constant(
912 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
913 $clientId = constant(
914 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
915 $client = Zend_Gdata_ClientLogin
::getHttpClient(
916 $this->user
, $this->pass
, 'youtube' , null, 'ZF_UnitTest', null, null,
917 'https://www.google.com/youtube/accounts/ClientLogin');
919 $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
920 $clientId, $developerKey);
921 $youtube->setMajorProtocolVersion(2);
923 $inboxFeed = $youtube->getInboxFeedForCurrentUser();
924 $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed
);
925 $this->assertTrue(count($inboxFeed->entries
) > 0, 'Test account ' .
926 $this->ytAccount
. ' had no messages in their inbox.');
928 // get the first entry
929 $inboxFeed->rewind();
930 $inboxEntry = $inboxFeed->current();
932 $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry
);
933 $this->assertTrue($inboxEntry->getTitle()->text
!= '');
937 public function testSendAMessageV2()
939 $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
940 $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
941 $client = Zend_Gdata_ClientLogin
::getHttpClient(
942 $this->user
, $this->pass
, 'youtube' , null, 'ZF_UnitTest', null, null,
943 'https://www.google.com/youtube/accounts/ClientLogin');
945 $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
946 $clientId, $developerKey);
947 $youtube->setMajorProtocolVersion(2);
949 // get a video from the recently featured video feed
950 $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
951 $videoEntry = $videoFeed->entry
[0];
952 $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry
);
954 // sending message to gdpython (python client library unit test user)
955 $sentMessage = $youtube->sendVideoMessage(
956 'Sending a v2 test message from Zend_Gdata_YouTubeOnlineTest.',
957 $videoEntry, null, 'gdpython');
959 $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry
);
962 public function testSendAMessageV1()
964 $developerKey = constant(
965 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
966 $clientId = constant(
967 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
968 $client = Zend_Gdata_ClientLogin
::getHttpClient(
969 $this->user
, $this->pass
, 'youtube' , null, 'ZF_UnitTest', null, null,
970 'https://www.google.com/youtube/accounts/ClientLogin');
972 $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
973 $clientId, $developerKey);
974 $youtube->setMajorProtocolVersion(1);
976 // get a video from the recently featured video feed
977 $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
978 $videoEntry = $videoFeed->entry
[0];
979 $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry
);
981 // sending message to gdpython (python client library unit test user)
982 $sentMessage = $youtube->sendVideoMessage(
983 'Sending a v1 test message from Zend_Gdata_YouTubeOnlineTest.',
984 $videoEntry, null, 'gdpython');
986 $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry
);
989 public function testThrowExceptionOnSendingMessageWithoutVideo()
991 $exceptionCaught = false;
992 $this->gdata
= new Zend_Gdata_YouTube();
994 $this->gdata
->sendVideoMessage('Should fail', null, null, 'foo');
995 } catch (Zend_Gdata_App_InvalidArgumentException
$e) {
996 $exceptionCaught = true;
998 $this->assertTrue($exceptionCaught, 'Was expecting an exception if ' .
999 'sending a message without a video');
1002 public function testCommentOnAComment()
1004 $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
1005 $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
1006 $client = Zend_Gdata_ClientLogin
::getHttpClient(
1007 $this->user
, $this->pass
, 'youtube' , null, 'ZF_UnitTest', null, null,
1008 'https://www.google.com/youtube/accounts/ClientLogin');
1009 $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
1010 $clientId, $developerKey);
1011 $youtube->setMajorProtocolVersion(2);
1013 $mostDiscussedFeed = $youtube->getVideoFeed(
1014 'http://gdata.youtube.com/feeds/api/standardfeeds/most_discussed');
1017 $mostDiscussedFeed->rewind();
1018 $firstEntry = $mostDiscussedFeed->current();
1020 $this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_VideoEntry
);
1022 $commentFeed = $youtube->getVideoCommentFeed($firstEntry->getVideoId());
1024 // get first comment
1025 $commentFeed->rewind();
1026 $firstCommentEntry = $commentFeed->current();
1028 $commentedComment = $youtube->replyToCommentEntry($firstCommentEntry,
1029 'awesome ! (ZFUnitTest-test)');
1031 $commentedComment instanceof Zend_Gdata_YouTube_CommentEntry
);