8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
18
* @subpackage UnitTests
19
* @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
20
* @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
* @subpackage UnitTests
33
class Zend_Gdata_YouTubeOnlineTest extends PHPUnit_Framework_TestCase
36
public function setUp()
38
$this->ytAccount = constant('TESTS_ZEND_GDATA_YOUTUBE_ACCOUNT');
39
$this->subscriptionTypeSchema = 'http://gdata.youtube.com/schemas/' .
40
'2007/subscriptiontypes.cat';
41
$this->gdata = new Zend_Gdata_YouTube();
44
public function tearDown()
48
public function testRetrieveSubScriptionFeed()
50
$feed = $this->gdata->getSubscriptionFeed($this->ytAccount);
51
$this->assertTrue($feed->totalResults->text > 0);
52
$this->assertEquals('Subscriptions of ' . $this->ytAccount,
54
$this->assertTrue(count($feed->entry) > 0);
55
foreach ($feed->entry as $entry) {
56
$this->assertTrue($entry->title->text != '');
60
public function testRetrieveContactFeed()
62
$feed = $this->gdata->getContactFeed($this->ytAccount);
63
$this->assertTrue($feed->totalResults->text > 0);
64
$this->assertEquals('Contacts of ' . $this->ytAccount,
66
$this->assertTrue(count($feed->entry) > 0);
67
foreach ($feed->entry as $entry) {
68
$this->assertTrue($entry->title->text != '');
70
$this->assertEquals('ytgdatatest1', $feed->entry[0]->username->text);
73
public function testRetrieveUserVideos()
75
$feed = $this->gdata->getUserUploads($this->ytAccount);
76
$this->assertEquals('Uploads by ' . $this->ytAccount,
78
$this->assertTrue(count($feed->entry) === 1);
81
public function testRetrieveVideoFeed()
83
$feed = $this->gdata->getVideoFeed();
85
$query = new Zend_Gdata_YouTube_VideoQuery();
86
$query->setVideoQuery('puppy');
87
$feed = $this->gdata->getVideoFeed($query);
88
foreach ($feed as $videoEntry) {
89
$videoResponsesLink = $videoEntry->getVideoResponsesLink();
90
$videoRatingsLink = $videoEntry->getVideoRatingsLink();
91
$videoComplaintsLink = $videoEntry->getVideoComplaintsLink();
94
$feed = $this->gdata->getVideoFeed($query->getQueryUrl());
97
public function testRetrieveVideoEntry()
99
$entry = $this->gdata->getVideoEntry('66wj2g5yz0M');
100
$this->assertEquals('TestMovie', $entry->title->text);
102
$entry = $this->gdata->getVideoEntry(null, 'http://gdata.youtube.com/feeds/api/videos/66wj2g5yz0M');
103
$this->assertEquals('TestMovie', $entry->title->text);
106
public function testRetrieveOtherFeeds()
108
$feed = $this->gdata->getRelatedVideoFeed('66wj2g5yz0M');
109
$feed = $this->gdata->getVideoResponseFeed('66wj2g5yz0M');
110
$feed = $this->gdata->getVideoCommentFeed('66wj2g5yz0M');
111
$feed = $this->gdata->getWatchOnMobileVideoFeed();
112
$feed = $this->gdata->getUserFavorites($this->ytAccount);
115
public function testRetrieveUserProfile()
117
$entry = $this->gdata->getUserProfile($this->ytAccount);
118
$this->assertEquals($this->ytAccount . ' Channel', $entry->title->text);
119
$this->assertEquals($this->ytAccount, $entry->username->text);
120
$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.',
121
$entry->description->text);
122
$this->assertEquals(32, $entry->age->text);
123
$this->assertEquals('crime and punishment, ps i love you, the stand', $entry->books->text);
124
$this->assertEquals('Google', $entry->company->text);
125
$this->assertEquals('software engineering, information architecture, photography, travel', $entry->hobbies->text);
126
$this->assertEquals('Mountain View, CA', $entry->hometown->text);
127
$this->assertEquals('San Francisco, CA 94114, US', $entry->location->text);
128
$this->assertEquals('monk, heroes, law and order, top gun', $entry->movies->text);
129
$this->assertEquals('imogen heap, frou frou, thievory corp, morcheeba, barenaked ladies', $entry->music->text);
130
$this->assertEquals('Developer Programs', $entry->occupation->text);
131
$this->assertEquals('University of the World', $entry->school->text);
132
$this->assertEquals('f', $entry->gender->text);
133
$this->assertEquals('taken', $entry->relationship->text);
136
public function testRetrieveAndUpdatePlaylistList()
139
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
140
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
141
$service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
142
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
143
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
148
$source = 'Google-UnitTests-1.0',
150
$loginCaptcha = null,
153
$this->gdata = new Zend_Gdata_YouTube($httpClient,
154
'Google-UnitTests-1.0', 'ytapi-gdataops-12345-u78960r7-0',
155
'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7Eyu1IuvkioESq' .
156
'zRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
158
$this->gdata->setMajorProtocolVersion(2);
159
$feed = $this->gdata->getPlaylistListFeed($this->ytAccount);
160
$this->assertTrue($feed->totalResults->text > 0);
161
$this->assertEquals('Playlists of ' . $this->ytAccount,
163
$this->assertTrue(count($feed->entry) > 0);
165
foreach ($feed->entry as $entry) {
166
$this->assertTrue($entry->title->text != '');
168
$entry->title->setText('new playlist title');
175
public function testRetrievePlaylistV2()
177
$this->gdata->setMajorProtocolVersion(2);
178
$feed = $this->gdata->getPlaylistListFeed($this->ytAccount);
179
$firstEntry = $feed->entries[0];
180
$this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_PlaylistListEntry);
181
$this->assertTrue($firstEntry->getSummary()->text != null);
184
public function testRetrievePlaylistVideoFeed()
186
$listFeed = $this->gdata->getPlaylistListFeed($this->ytAccount);
188
$feed = $this->gdata->getPlaylistVideoFeed($listFeed->entry[0]->feedLink[0]->href);
189
$this->assertTrue($feed->totalResults->text > 0);
190
$this->assertTrue(count($feed->entry) > 0);
191
foreach ($feed->entry as $entry) {
192
$this->assertTrue($entry->title->text != '');
196
public function testRetrieveTopRatedVideos()
198
$feed = $this->gdata->getTopRatedVideoFeed();
199
$this->assertTrue($feed->totalResults->text > 10);
200
$this->assertEquals('Top Rated', $feed->title->text);
201
$this->assertTrue(count($feed->entry) > 0);
202
foreach ($feed->entry as $entry) {
203
$this->assertTrue($entry->rating->average > 3);
204
$this->assertEquals(1, $entry->rating->min);
205
$this->assertEquals(5, $entry->rating->max);
206
$this->assertTrue($entry->rating->numRaters > 2);
210
public function testRetrieveTopRatedVideosV2()
212
$this->gdata->setMajorProtocolVersion(2);
213
$feed = $this->gdata->getTopRatedVideoFeed();
214
$client = $this->gdata->getHttpClient();
215
$positionOfAPIProjection = strpos(
216
$client->getLastRequest(), "/feeds/api/");
217
$this->assertTrue(is_numeric($positionOfAPIProjection));
220
public function testRetrieveMostViewedVideosV2()
222
$this->gdata->setMajorProtocolVersion(2);
223
$feed = $this->gdata->getMostViewedVideoFeed();
224
$client = $this->gdata->getHttpClient();
225
$positionOfAPIProjection = strpos(
226
$client->getLastRequest(), "/feeds/api/");
227
$this->assertTrue(is_numeric($positionOfAPIProjection));
230
public function testRetrieveRecentlyFeaturedVideosV2()
232
$this->gdata->setMajorProtocolVersion(2);
233
$feed = $this->gdata->getRecentlyFeaturedVideoFeed();
234
$client = $this->gdata->getHttpClient();
235
$positionOfAPIProjection = strpos(
236
$client->getLastRequest(), "/feeds/api/");
237
$this->assertTrue(is_numeric($positionOfAPIProjection));
240
public function testWatchOnMobileVideosV2()
242
$this->gdata->setMajorProtocolVersion(2);
243
$feed = $this->gdata->getWatchOnMobileVideoFeed();
244
$client = $this->gdata->getHttpClient();
245
$positionOfAPIProjection = strpos(
246
$client->getLastRequest(), "/feeds/api/");
247
$this->assertTrue(is_numeric($positionOfAPIProjection));
250
public function testRetrieveMostViewedVideos()
252
$feed = $this->gdata->getMostViewedVideoFeed();
253
$this->assertTrue($feed->totalResults->text > 10);
254
$this->assertEquals('Most Viewed', $feed->title->text);
255
$this->assertTrue(count($feed->entry) > 0);
256
foreach ($feed->entry as $entry) {
257
if ($entry->rating) {
258
$this->assertEquals(1, $entry->rating->min);
259
$this->assertEquals(5, $entry->rating->max);
264
public function testPerformV2Query_Location()
266
$this->gdata->setMajorProtocolVersion(2);
267
$query = $this->gdata->newVideoQuery();
268
// Setting location to New York City
269
$query->setLocation('-37.0625,-95.677068');
270
$query->setLocationRadius('1000km');
271
$videoFeed = $this->gdata->getVideoFeed($query);
272
$this->assertTrue(count($videoFeed->entry) > 0,
273
'Could not retrieve a single entry for location search:' .
274
$query->getQueryUrl(2));
277
public function testPerformV2Query_SafeSearch()
279
$this->gdata->setMajorProtocolVersion(2);
280
$query = $this->gdata->newVideoQuery();
281
$query->setSafeSearch('strict');
282
$videoFeed = $this->gdata->getVideoFeed($query);
283
$this->assertTrue(count($videoFeed->entry) > 0,
284
'Could not retrieve a single entry for safeSearch=strict search:' .
285
$query->getQueryUrl(2));
288
public function testPeformV2Query_Uploader()
290
$this->gdata->setMajorProtocolVersion(2);
291
$query = $this->gdata->newVideoQuery();
292
$query->setUploader('partner');
293
$videoFeed = $this->gdata->getVideoFeed($query);
294
$this->assertTrue(count($videoFeed->entry) > 0,
295
'Could not retrieve a single entry for uploader=partner search:' .
296
$query->getQueryUrl(2));
298
foreach($videoFeed as $videoEntry) {
299
$mg = $videoEntry->getMediaGroup();
300
$this->assertEquals('partner',
301
$mg->getMediaCredit()->getYTtype());
305
public function testAddUpdateAndDeletePlaylistV2()
307
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
308
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
309
$service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
311
'https://www.google.com/youtube/accounts/ClientLogin';
312
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
317
$source = 'Google-UnitTests-1.0',
319
$loginCaptcha = null,
322
$yt = new Zend_Gdata_YouTube(
323
$httpClient, 'Google-UnitTests-1.0',
324
'ytapi-gdataops-12345-u78960r7-0',
325
'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
326
'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
328
$yt->setMajorProtocolVersion(2);
329
$feed = $yt->getPlaylistListFeed($this->ytAccount);
332
$newPlaylist = $yt->newPlaylistListEntry();
333
$newPlaylist->setMajorProtocolVersion(2);
334
$titleString = $this->generateRandomString(10);
335
$newPlaylist->title = $yt->newTitle()->setText($titleString);
336
$newPlaylist->summary = $yt->newSummary()->setText('testing');
337
$postUrl = 'http://gdata.youtube.com/feeds/api/users/default/playlists';
338
$successfulInsertion = true;
341
$yt->insertEntry($newPlaylist, $postUrl);
342
} catch (Zend_Gdata_App_Exception $e) {
343
$successfulInsertion = false;
346
$this->assertTrue($successfulInsertion, 'Failed to insert a new ' .
349
$playlistListFeed = $yt->getPlaylistListFeed('default');
351
$playlistFound = false;
352
$newPlaylistEntry = null;
354
foreach ($playlistListFeed as $playlistListEntry) {
355
if ($playlistListEntry->title->text == $titleString) {
356
$playlistFound = true;
357
$newPlaylistEntry = $playlistListEntry;
362
$this->assertTrue($playlistFound, 'Could not find the newly inserted ' .
366
$newTitle = $this->generateRandomString(10);
367
$newPlaylistEntry->title->setText($newTitle);
368
$updatedSuccesfully = true;
370
$newPlaylistEntry->save();
371
} catch (Zend_Gdata_App_Exception $e) {
372
$updatedSuccesfully = false;
375
$this->assertTrue($updatedSuccesfully, 'Could not succesfully update ' .
379
$deletedSuccesfully = true;
381
$newPlaylistEntry->delete();
382
} catch (Zend_Gdata_App_Exception $e) {
383
$deletedSuccesfully = false;
386
$this->assertTrue($deletedSuccesfully, 'Could not succesfully delete ' .
390
public function testAddAndDeleteSubscriptionToChannelV2()
392
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
393
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
394
$service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
396
'https://www.google.com/youtube/accounts/ClientLogin';
397
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
402
$source = 'Google-UnitTests-1.0',
404
$loginCaptcha = null,
407
$yt = new Zend_Gdata_YouTube(
408
$httpClient, 'Google-UnitTests-1.0',
409
'ytapi-gdataops-12345-u78960r7-0',
410
'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
411
'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
413
$yt->setMajorProtocolVersion(2);
415
$channelToSubscribeTo = 'AssociatedPress';
417
// Test for deletion first in case something went wrong
418
// last time the test was run (network, etc...)
419
$subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
420
$successDeletionUpFront = true;
422
foreach($subscriptionFeed as $subscriptionEntry) {
423
$subscriptionType = null;
424
$categories = $subscriptionEntry->getCategory();
425
// examine the correct category element since there are multiple
426
foreach($categories as $category) {
427
if ($category->getScheme() ==
428
'http://gdata.youtube.com/schemas/2007/' .
429
'subscriptiontypes.cat') {
430
$subscriptionType = $category->getTerm();
433
if ($subscriptionType == 'channel') {
434
if ($subscriptionEntry->getUsername()->text ==
435
$channelToSubscribeTo) {
437
$subscriptionEntry->delete();
438
} catch (Zend_App_Exception $e) {
439
$message = $e->getMessage();
440
$successDeletionUpFront = false;
445
$this->assertTrue($successDeletionUpFront, 'Found existing ' .
446
'subscription in unit test, could not delete prior to running ' .
447
'test -- ' . $message);
450
$newSubscription = $yt->newSubscriptionEntry();
451
$newSubscription->category = array(
452
$yt->newCategory('channel',
453
$this->subscriptionTypeSchema));
454
$newSubscription->setUsername($yt->newUsername(
455
$channelToSubscribeTo));
458
'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
460
$successPosting = true;
462
$insertedSubscription = null;
464
$insertedSubscription = $yt->insertEntry(
465
$newSubscription, $postUrl,
466
'Zend_Gdata_YouTube_SubscriptionEntry');
467
} catch (Zend_App_Exception $e) {
468
$message = $e->getMessage();
469
$successPosting = false;
472
$this->assertTrue($successPosting, $message);
475
$successDeletion = true;
478
$insertedSubscription->delete();
479
} catch (Zend_App_Exception $e) {
480
$message = $e->getMessage();
481
$successDeletion = false;
484
$this->assertTrue($successDeletion, $message);
487
public function testAddAndDeleteSubscriptionToFavoritesV2()
489
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
490
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
491
$service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
493
'https://www.google.com/youtube/accounts/ClientLogin';
494
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
499
$source = 'Google-UnitTests-1.0',
501
$loginCaptcha = null,
504
$yt = new Zend_Gdata_YouTube(
505
$httpClient, 'Google-UnitTests-1.0',
506
'ytapi-gdataops-12345-u78960r7-0',
507
'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
508
'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
510
$yt->setMajorProtocolVersion(2);
512
$usernameOfFavoritesToSubscribeTo = 'CBS';
514
// Test for deletion first in case something went wrong
515
// last time the test was run (network, etc...)
516
$subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
517
$successDeletionUpFront = true;
519
foreach($subscriptionFeed as $subscriptionEntry) {
520
$subscriptionType = null;
521
$categories = $subscriptionEntry->getCategory();
522
// examine the correct category element since there are multiple
523
foreach($categories as $category) {
524
if ($category->getScheme() ==
525
'http://gdata.youtube.com/schemas/2007/' .
526
'subscriptiontypes.cat') {
527
$subscriptionType = $category->getTerm();
530
if ($subscriptionType == 'favorites') {
531
if ($subscriptionEntry->getUsername()->text ==
532
$usernameOfFavoritesToSubscribeTo) {
534
$subscriptionEntry->delete();
535
} catch (Zend_App_Exception $e) {
536
$message = $e->getMessage();
537
$successDeletionUpFront = false;
542
$this->assertTrue($successDeletionUpFront, 'Found existing ' .
543
'subscription in unit test, could not delete prior to running ' .
544
'test -- ' . $message);
547
$newSubscription = $yt->newSubscriptionEntry();
548
$newSubscription->category = array(
549
$yt->newCategory('favorites',
550
$this->subscriptionTypeSchema));
551
$newSubscription->setUsername($yt->newUsername(
552
$usernameOfFavoritesToSubscribeTo));
555
'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
557
$successPosting = true;
559
$insertedSubscription = null;
561
$insertedSubscription = $yt->insertEntry(
562
$newSubscription, $postUrl,
563
'Zend_Gdata_YouTube_SubscriptionEntry');
564
} catch (Zend_App_Exception $e) {
565
$message = $e->getMessage();
566
$successPosting = false;
569
$this->assertTrue($successPosting, $message);
572
$successDeletion = true;
575
$insertedSubscription->delete();
576
} catch (Zend_App_Exception $e) {
577
$message = $e->getMessage();
578
$successDeletion = false;
581
$this->assertTrue($successDeletion, $message);
584
public function testAddAndDeleteSubscriptionToPlaylistV2()
586
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
587
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
588
$service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
590
'https://www.google.com/youtube/accounts/ClientLogin';
591
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
596
$source = 'Google-UnitTests-1.0',
598
$loginCaptcha = null,
601
$yt = new Zend_Gdata_YouTube(
602
$httpClient, 'Google-UnitTests-1.0',
603
'ytapi-gdataops-12345-u78960r7-0',
604
'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
605
'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
607
$yt->setMajorProtocolVersion(2);
608
$playlistIdToSubscribeTo = '7A2BB4AFFEBED2A4';
610
// Test for deletion first in case something went wrong
611
// last time the test was run (network, etc...)
612
$subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
613
$successDeletionUpFront = true;
615
foreach($subscriptionFeed as $subscriptionEntry) {
616
$subscriptionType = null;
617
$categories = $subscriptionEntry->getCategory();
618
// examine the correct category element since there are multiple
619
foreach($categories as $category) {
620
if ($category->getScheme() ==
621
'http://gdata.youtube.com/schemas/2007/' .
622
'subscriptiontypes.cat') {
623
$subscriptionType = $category->getTerm();
626
if ($subscriptionType == 'playlist') {
627
if ($subscriptionEntry->getPlaylistId()->text ==
628
$playlistIdToSubscribeTo) {
630
$subscriptionEntry->delete();
631
} catch (Zend_App_Exception $e) {
632
$message = $e->getMessage();
633
$successDeletionUpFront = false;
638
$this->assertTrue($successDeletionUpFront, 'Found existing ' .
639
'subscription in unit test, could not delete prior to running ' .
640
'test -- ' . $message);
642
// Playlist of McGyver videos
643
$newSubscription = $yt->newSubscriptionEntry();
644
$newSubscription->setMajorProtocolVersion(2);
645
$newSubscription->category = array(
646
$yt->newCategory('playlist',
647
$this->subscriptionTypeSchema));
648
$newSubscription->setPlaylistId($yt->newPlaylistId(
649
$playlistIdToSubscribeTo));
652
'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
654
$successPosting = true;
656
$insertedSubscription = null;
658
$insertedSubscription = $yt->insertEntry(
659
$newSubscription, $postUrl,
660
'Zend_Gdata_YouTube_SubscriptionEntry');
661
} catch (Zend_App_Exception $e) {
662
$message = $e->getMessage();
663
$successPosting = false;
666
$this->assertTrue($successPosting, $message);
669
$successDeletion = true;
672
$insertedSubscription->delete();
673
} catch (Zend_App_Exception $e) {
674
$message = $e->getMessage();
675
$successDeletion = false;
678
$this->assertTrue($successDeletion, $message);
681
public function testAddAndDeleteSubscriptionToQueryV2()
683
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
684
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
685
$developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
686
$clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
688
$service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
690
'https://www.google.com/youtube/accounts/ClientLogin';
691
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
696
$source = 'Google-UnitTests-1.0',
698
$loginCaptcha = null,
701
$yt = new Zend_Gdata_YouTube($httpClient, $clientId, $developerKey);
703
$yt->setMajorProtocolVersion(2);
704
$queryStringToSubscribeTo = 'zend';
706
// Test for deletion first in case something went wrong
707
// last time the test was run (network, etc...)
708
$subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
709
$successDeletionUpFront = true;
711
foreach($subscriptionFeed as $subscriptionEntry) {
712
$subscriptionType = null;
713
$categories = $subscriptionEntry->getCategory();
714
// examine the correct category element since there are multiple
715
foreach($categories as $category) {
716
if ($category->getScheme() ==
717
'http://gdata.youtube.com/schemas/2007/' .
718
'subscriptiontypes.cat') {
719
$subscriptionType = $category->getTerm();
722
if ($subscriptionType == 'query') {
723
if ($subscriptionEntry->getQueryString() ==
724
$queryStringToSubscribeTo) {
726
$subscriptionEntry->delete();
727
} catch (Zend_App_Exception $e) {
728
$message = $e->getMessage();
729
$successDeletionUpFront = false;
734
$this->assertTrue($successDeletionUpFront, 'Found existing ' .
735
'subscription in unit test, could not delete prior to running ' .
736
'test -- ' . $message);
739
$newSubscription = $yt->newSubscriptionEntry();
740
$newSubscription->category = array(
741
$yt->newCategory('query',
742
$this->subscriptionTypeSchema));
743
$newSubscription->setQueryString($yt->newQueryString(
744
$queryStringToSubscribeTo));
747
'http://gdata.youtube.com/feeds/api/users/default/subscriptions';
749
$successPosting = true;
751
$insertedSubscription = null;
753
$insertedSubscription = $yt->insertEntry(
754
$newSubscription, $postUrl,
755
'Zend_Gdata_YouTube_SubscriptionEntry');
756
} catch (Zend_App_Exception $e) {
757
$message = $e->getMessage();
758
$successPosting = false;
761
$this->assertTrue($successPosting, $message);
764
$successDeletion = true;
767
$insertedSubscription->delete();
768
} catch (Zend_App_Exception $e) {
769
$message = $e->getMessage();
770
$successDeletion = false;
773
$this->assertTrue($successDeletion, $message);
776
public function generateRandomString($length)
778
$outputString = null;
779
for($i = 0; $i < $length; $i++) {
780
$outputString .= chr(rand(65,90));
782
return $outputString;
785
public function testRetrieveActivityFeed()
787
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
788
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
789
$developerKey = constant(
790
'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
791
$clientId = constant(
792
'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
793
$client = Zend_Gdata_ClientLogin::getHttpClient(
794
$user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
795
'https://www.google.com/youtube/accounts/ClientLogin');
797
$youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
798
$clientId, $developerKey);
799
$youtube->setMajorProtocolVersion(2);
801
$feed = $youtube->getActivityForUser($this->ytAccount);
802
$this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
803
$this->assertTrue((count($feed->entries) > 0));
804
$this->assertEquals('Activity of ' . $this->ytAccount,
808
public function testExceptionIfNotUsingDeveloperKey()
810
$exceptionThrown = false;
811
$youtube = new Zend_Gdata_YouTube();
812
$youtube->setMajorProtocolVersion(2);
814
$youtube->getActivityForUser($this->ytAccount);
815
} catch (Zend_Gdata_App_HttpException $e) {
816
$exceptionThrown = true;
818
$this->assertTrue($exceptionThrown, 'Was expecting an exception when ' .
819
'making a request to the YouTube Activity feed without a ' .
823
public function testRetrieveActivityFeedForMultipleUsers()
825
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
826
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
827
$developerKey = constant(
828
'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
829
$clientId = constant(
830
'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
831
$client = Zend_Gdata_ClientLogin::getHttpClient(
832
$user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
833
'https://www.google.com/youtube/accounts/ClientLogin');
835
$youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
836
$clientId, $developerKey);
837
$youtube->setMajorProtocolVersion(2);
839
$feed = $youtube->getActivityForUser($this->ytAccount .
841
$this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
842
$this->assertTrue((count($feed->entries) > 0));
843
$this->assertEquals('Activity of ' . $this->ytAccount .
844
',associatedpress', $feed->title->text);
847
public function testRetrieveFriendFeed()
849
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
850
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
851
$developerKey = constant(
852
'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
853
$clientId = constant(
854
'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
855
$client = Zend_Gdata_ClientLogin::getHttpClient(
856
$user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
857
'https://www.google.com/youtube/accounts/ClientLogin');
859
$youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
860
$clientId, $developerKey);
861
$youtube->setMajorProtocolVersion(2);
863
$feed = $youtube->getFriendActivityForCurrentUser();
864
$this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
865
$this->assertTrue((count($feed->entries) > 0));
866
$this->assertEquals('Activity of the friends of ' . $this->ytAccount,
870
public function testThrowExceptionOnActivityFeedRequestForMoreThan20Users()
872
$exceptionThrown = false;
873
$listOfMoreThan20Users = null;
874
$youtube = new Zend_Gdata_YouTube();
875
$youtube->setMajorProtocolVersion(2);
877
for ($x = 0; $x < 30; $x++) {
878
$listOfMoreThan20Users .= "user$x";
880
$listOfMoreThan20Users .= ",";
885
$youtube->getActivityForUser($listOfMoreThan20Users);
886
} catch (Zend_Gdata_App_InvalidArgumentException $e) {
887
$exceptionThrown = true;
889
$this->assertTrue($exceptionThrown, 'Was expecting an exception on ' .
890
'a request to ->getActivityForUser when more than 20 users were ' .
891
'specified in YouTube.php');
894
public function testGetInboxFeedForCurrentUserV1()
896
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
897
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
898
$developerKey = constant(
899
'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
900
$clientId = constant(
901
'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
902
$client = Zend_Gdata_ClientLogin::getHttpClient(
903
$user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
904
'https://www.google.com/youtube/accounts/ClientLogin');
906
$youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
907
$clientId, $developerKey);
909
$inboxFeed = $youtube->getInboxFeedForCurrentUser();
910
$this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
911
$this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
912
$this->ytAccount . ' had no messages in their inbox.');
914
// get the first entry
915
$inboxFeed->rewind();
916
$inboxEntry = $inboxFeed->current();
918
$inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
919
$this->assertTrue($inboxEntry->getTitle()->text != '');
922
public function testGetInboxFeedForCurrentUserV2()
924
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
925
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
926
$developerKey = constant(
927
'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
928
$clientId = constant(
929
'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
930
$client = Zend_Gdata_ClientLogin::getHttpClient(
931
$user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
932
'https://www.google.com/youtube/accounts/ClientLogin');
934
$youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
935
$clientId, $developerKey);
936
$youtube->setMajorProtocolVersion(2);
938
$inboxFeed = $youtube->getInboxFeedForCurrentUser();
939
$this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
940
$this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
941
$this->ytAccount . ' had no messages in their inbox.');
943
// get the first entry
944
$inboxFeed->rewind();
945
$inboxEntry = $inboxFeed->current();
947
$inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
948
$this->assertTrue($inboxEntry->getTitle()->text != '');
952
public function testSendAMessageV2()
954
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
955
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
956
$developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
957
$clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
958
$client = Zend_Gdata_ClientLogin::getHttpClient(
959
$user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
960
'https://www.google.com/youtube/accounts/ClientLogin');
962
$youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
963
$clientId, $developerKey);
964
$youtube->setMajorProtocolVersion(2);
966
// get a video from the recently featured video feed
967
$videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
968
$videoEntry = $videoFeed->entry[0];
969
$this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
971
// sending message to gdpython (python client library unit test user)
972
$sentMessage = $youtube->sendVideoMessage(
973
'Sending a v2 test message from Zend_Gdata_YouTubeOnlineTest.',
974
$videoEntry, null, 'gdpython');
976
$sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
979
public function testSendAMessageV1()
981
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
982
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
983
$developerKey = constant(
984
'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
985
$clientId = constant(
986
'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
987
$client = Zend_Gdata_ClientLogin::getHttpClient(
988
$user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
989
'https://www.google.com/youtube/accounts/ClientLogin');
991
$youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
992
$clientId, $developerKey);
993
$youtube->setMajorProtocolVersion(1);
995
// get a video from the recently featured video feed
996
$videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
997
$videoEntry = $videoFeed->entry[0];
998
$this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
1000
// sending message to gdpython (python client library unit test user)
1001
$sentMessage = $youtube->sendVideoMessage(
1002
'Sending a v1 test message from Zend_Gdata_YouTubeOnlineTest.',
1003
$videoEntry, null, 'gdpython');
1005
$sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
1008
public function testThrowExceptionOnSendingMessageWithoutVideo()
1010
$exceptionCaught = false;
1011
$this->gdata = new Zend_Gdata_YouTube();
1013
$this->gdata->sendVideoMessage('Should fail', null, null, 'foo');
1014
} catch (Zend_Gdata_App_InvalidArgumentException $e) {
1015
$exceptionCaught = true;
1017
$this->assertTrue($exceptionCaught, 'Was expecting an exception if ' .
1018
'sending a message without a video');
1021
public function testCommentOnAComment()
1023
$user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
1024
$pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
1025
$developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
1026
$clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
1027
$client = Zend_Gdata_ClientLogin::getHttpClient(
1028
$user, $pass, 'youtube' , null, 'ZF_UnitTest', null, null,
1029
'https://www.google.com/youtube/accounts/ClientLogin');
1030
$youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
1031
$clientId, $developerKey);
1032
$youtube->setMajorProtocolVersion(2);
1034
$mostDiscussedFeed = $youtube->getVideoFeed(
1035
'http://gdata.youtube.com/feeds/api/standardfeeds/most_discussed');
1038
$mostDiscussedFeed->rewind();
1039
$firstEntry = $mostDiscussedFeed->current();
1041
$this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_VideoEntry);
1043
$commentFeed = $youtube->getVideoCommentFeed($firstEntry->getVideoId());
1045
// get first comment
1046
$commentFeed->rewind();
1047
$firstCommentEntry = $commentFeed->current();
1049
$commentedComment = $youtube->replyToCommentEntry($firstCommentEntry,
1050
'awesome ! (ZFUnitTest-test)');
1052
$commentedComment instanceof Zend_Gdata_YouTube_CommentEntry);