| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Gdata_YouTube
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id $
- */
- require_once 'Zend/Gdata/YouTube.php';
- require_once 'Zend/Gdata/YouTube/VideoQuery.php';
- require_once 'Zend/Gdata/ClientLogin.php';
- /**
- * @category Zend
- * @package Zend_Gdata_YouTube
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Gdata
- * @group Zend_Gdata_YouTube
- */
- class Zend_Gdata_YouTubeOnlineTest extends PHPUnit_Framework_TestCase
- {
- public function setUp()
- {
- $this->ytAccount = constant('TESTS_ZEND_GDATA_YOUTUBE_ACCOUNT');
- $this->user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
- $this->pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
- $this->subscriptionTypeSchema = 'http://gdata.youtube.com/schemas/' .
- '2007/subscriptiontypes.cat';
- $this->gdata = new Zend_Gdata_YouTube();
- }
- public function tearDown()
- {
- }
- public function testRetrieveSubScriptionFeed()
- {
- $feed = $this->gdata->getSubscriptionFeed($this->ytAccount);
- $this->assertTrue($feed->totalResults->text > 0);
- $this->assertEquals('Subscriptions of ' . $this->ytAccount,
- $feed->title->text);
- $this->assertTrue(count($feed->entry) > 0);
- foreach ($feed->entry as $entry) {
- $this->assertTrue($entry->title->text != '');
- }
- }
- public function testRetrieveContactFeed()
- {
- $feed = $this->gdata->getContactFeed($this->ytAccount);
- $this->assertTrue($feed->totalResults->text > 0);
- $this->assertEquals('Contacts of ' . $this->ytAccount,
- $feed->title->text);
- $this->assertTrue(count($feed->entry) > 0);
- foreach ($feed->entry as $entry) {
- $this->assertTrue($entry->title->text != '');
- }
- $this->assertEquals('ytgdatatest1', $feed->entry[0]->username->text);
- }
- public function testRetrieveUserVideos()
- {
- $feed = $this->gdata->getUserUploads($this->ytAccount);
- $this->assertEquals('Uploads by ' . $this->ytAccount,
- $feed->title->text);
- $this->assertTrue(count($feed->entry) === 1);
- }
- public function testRetrieveVideoFeed()
- {
- $feed = $this->gdata->getVideoFeed();
- $query = new Zend_Gdata_YouTube_VideoQuery();
- $query->setVideoQuery('puppy');
- $feed = $this->gdata->getVideoFeed($query);
- foreach ($feed as $videoEntry) {
- $videoResponsesLink = $videoEntry->getVideoResponsesLink();
- $videoRatingsLink = $videoEntry->getVideoRatingsLink();
- $videoComplaintsLink = $videoEntry->getVideoComplaintsLink();
- }
- $feed = $this->gdata->getVideoFeed($query->getQueryUrl());
- }
- public function testRetrieveVideoEntry()
- {
- $entry = $this->gdata->getVideoEntry('66wj2g5yz0M');
- $this->assertEquals('TestMovie', $entry->title->text);
- $entry = $this->gdata->getVideoEntry(null, 'https://gdata.youtube.com/feeds/api/videos/66wj2g5yz0M');
- $this->assertEquals('TestMovie', $entry->title->text);
- }
- public function testRetrieveOtherFeeds()
- {
- $feed = $this->gdata->getRelatedVideoFeed('66wj2g5yz0M');
- $feed = $this->gdata->getVideoResponseFeed('66wj2g5yz0M');
- $feed = $this->gdata->getVideoCommentFeed('66wj2g5yz0M');
- $feed = $this->gdata->getWatchOnMobileVideoFeed();
- $feed = $this->gdata->getUserFavorites($this->ytAccount);
- }
- public function testRetrieveUserProfile()
- {
- $entry = $this->gdata->getUserProfile($this->ytAccount);
- $this->assertEquals($this->ytAccount . ' Channel', $entry->title->text);
- $this->assertEquals($this->ytAccount, $entry->username->text);
- $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.',
- $entry->description->text);
- $this->assertEquals(32, $entry->age->text);
- $this->assertEquals('crime and punishment, ps i love you, the stand', $entry->books->text);
- $this->assertEquals('Google', $entry->company->text);
- $this->assertEquals('software engineering, information architecture, photography, travel', $entry->hobbies->text);
- $this->assertEquals('Mountain View, CA', $entry->hometown->text);
- $this->assertEquals('San Francisco, CA 94114, US', $entry->location->text);
- $this->assertEquals('monk, heroes, law and order, top gun', $entry->movies->text);
- $this->assertEquals('imogen heap, frou frou, thievory corp, morcheeba, barenaked ladies', $entry->music->text);
- $this->assertEquals('Developer Programs', $entry->occupation->text);
- $this->assertEquals('University of the World', $entry->school->text);
- $this->assertEquals('f', $entry->gender->text);
- $this->assertEquals('taken', $entry->relationship->text);
- }
- public function testRetrieveAndUpdatePlaylistList()
- {
- $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
- $authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
- $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user,
- $this->pass,
- $service,
- null, // client
- 'Google-UnitTests-1.0', // source
- null, // loginToken
- null, // loginCaptcha
- $authenticationURL);
- $this->gdata = new Zend_Gdata_YouTube($httpClient,
- 'Google-UnitTests-1.0', 'ytapi-gdataops-12345-u78960r7-0',
- 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7Eyu1IuvkioESq' .
- 'zRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
- $this->gdata->setMajorProtocolVersion(2);
- $feed = $this->gdata->getPlaylistListFeed($this->ytAccount);
- $this->assertTrue($feed->totalResults->text > 0);
- $this->assertEquals('Playlists of ' . $this->ytAccount,
- $feed->title->text);
- $this->assertTrue(count($feed->entry) > 0);
- $i = 0;
- foreach ($feed->entry as $entry) {
- $this->assertTrue($entry->title->text != '');
- if ($i == 0) {
- $entry->title->setText('new playlist title');
- $entry->save();
- }
- $i++;
- }
- }
- public function testRetrievePlaylistV2()
- {
- $this->gdata->setMajorProtocolVersion(2);
- $feed = $this->gdata->getPlaylistListFeed($this->ytAccount);
- $firstEntry = $feed->entries[0];
- $this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_PlaylistListEntry);
- $this->assertTrue($firstEntry->getSummary()->text != null);
- }
- public function testRetrievePlaylistVideoFeed()
- {
- $listFeed = $this->gdata->getPlaylistListFeed($this->ytAccount);
- $feed = $this->gdata->getPlaylistVideoFeed($listFeed->entry[0]->feedLink[0]->href);
- $this->assertTrue($feed->totalResults->text > 0);
- $this->assertTrue(count($feed->entry) > 0);
- foreach ($feed->entry as $entry) {
- $this->assertTrue($entry->title->text != '');
- }
- }
- public function testRetrieveTopRatedVideos()
- {
- $feed = $this->gdata->getTopRatedVideoFeed();
- $this->assertTrue($feed->totalResults->text > 10);
- $this->assertEquals('Top Rated', $feed->title->text);
- $this->assertTrue(count($feed->entry) > 0);
- foreach ($feed->entry as $entry) {
- $this->assertTrue($entry->rating->average > 3);
- $this->assertEquals(1, $entry->rating->min);
- $this->assertEquals(5, $entry->rating->max);
- $this->assertTrue($entry->rating->numRaters > 2);
- }
- }
- public function testRetrieveTopRatedVideosV2()
- {
- $this->gdata->setMajorProtocolVersion(2);
- $feed = $this->gdata->getTopRatedVideoFeed();
- $client = $this->gdata->getHttpClient();
- $positionOfAPIProjection = strpos(
- $client->getLastRequest(), "/feeds/api/");
- $this->assertTrue(is_numeric($positionOfAPIProjection));
- }
- public function testRetrieveMostViewedVideosV2()
- {
- $this->gdata->setMajorProtocolVersion(2);
- $feed = $this->gdata->getMostViewedVideoFeed();
- $client = $this->gdata->getHttpClient();
- $positionOfAPIProjection = strpos(
- $client->getLastRequest(), "/feeds/api/");
- $this->assertTrue(is_numeric($positionOfAPIProjection));
- }
- public function testRetrieveRecentlyFeaturedVideosV2()
- {
- $this->gdata->setMajorProtocolVersion(2);
- $feed = $this->gdata->getRecentlyFeaturedVideoFeed();
- $client = $this->gdata->getHttpClient();
- $positionOfAPIProjection = strpos(
- $client->getLastRequest(), "/feeds/api/");
- $this->assertTrue(is_numeric($positionOfAPIProjection));
- }
- public function testWatchOnMobileVideosV2()
- {
- $this->gdata->setMajorProtocolVersion(2);
- $feed = $this->gdata->getWatchOnMobileVideoFeed();
- $client = $this->gdata->getHttpClient();
- $positionOfAPIProjection = strpos(
- $client->getLastRequest(), "/feeds/api/");
- $this->assertTrue(is_numeric($positionOfAPIProjection));
- }
- public function testRetrieveMostViewedVideos()
- {
- $feed = $this->gdata->getMostViewedVideoFeed();
- $this->assertTrue($feed->totalResults->text > 10);
- $this->assertEquals('Most Viewed', $feed->title->text);
- $this->assertTrue(count($feed->entry) > 0);
- foreach ($feed->entry as $entry) {
- if ($entry->rating) {
- $this->assertEquals(1, $entry->rating->min);
- $this->assertEquals(5, $entry->rating->max);
- }
- }
- }
- public function testPerformV2Query_Location()
- {
- $this->gdata->setMajorProtocolVersion(2);
- $query = $this->gdata->newVideoQuery();
- // Setting location to New York City
- $query->setLocation('-37.0625,-95.677068');
- $query->setLocationRadius('1000km');
- $videoFeed = $this->gdata->getVideoFeed($query);
- $this->assertTrue(count($videoFeed->entry) > 0,
- 'Could not retrieve a single entry for location search:' .
- $query->getQueryUrl(2));
- }
- public function testPerformV2Query_SafeSearch()
- {
- $this->gdata->setMajorProtocolVersion(2);
- $query = $this->gdata->newVideoQuery();
- $query->setSafeSearch('strict');
- $videoFeed = $this->gdata->getVideoFeed($query);
- $this->assertTrue(count($videoFeed->entry) > 0,
- 'Could not retrieve a single entry for safeSearch=strict search:' .
- $query->getQueryUrl(2));
- }
- public function testPeformV2Query_Uploader()
- {
- $this->gdata->setMajorProtocolVersion(2);
- $query = $this->gdata->newVideoQuery();
- $query->setUploader('partner');
- $videoFeed = $this->gdata->getVideoFeed($query);
- $this->assertTrue(count($videoFeed->entry) > 0,
- 'Could not retrieve a single entry for uploader=partner search:' .
- $query->getQueryUrl(2));
- foreach($videoFeed as $videoEntry) {
- $mg = $videoEntry->getMediaGroup();
- $this->assertEquals('partner',
- $mg->getMediaCredit()->getYTtype());
- }
- }
- public function testAddUpdateAndDeletePlaylistV2()
- {
- $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
- $authenticationURL =
- 'https://www.google.com/youtube/accounts/ClientLogin';
- $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user,
- $this->pass,
- $service,
- null, // client
- 'Google-UnitTests-1.0', // source
- null, // $loginToken
- null, // loginCaptcha
- $authenticationURL);
- $yt = new Zend_Gdata_YouTube(
- $httpClient, 'Google-UnitTests-1.0',
- 'ytapi-gdataops-12345-u78960r7-0',
- 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
- 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
- $yt->setMajorProtocolVersion(2);
- $feed = $yt->getPlaylistListFeed($this->ytAccount);
- // Add new
- $newPlaylist = $yt->newPlaylistListEntry();
- $newPlaylist->setMajorProtocolVersion(2);
- $titleString = $this->generateRandomString(10);
- $newPlaylist->title = $yt->newTitle()->setText($titleString);
- $newPlaylist->summary = $yt->newSummary()->setText('testing');
- $postUrl = 'https://gdata.youtube.com/feeds/api/users/default/playlists';
- $successfulInsertion = true;
- try {
- $yt->insertEntry($newPlaylist, $postUrl);
- } catch (Zend_Gdata_App_Exception $e) {
- $successfulInsertion = false;
- }
- $this->assertTrue($successfulInsertion, 'Failed to insert a new ' .
- 'playlist.');
- $playlistListFeed = $yt->getPlaylistListFeed('default');
- $playlistFound = false;
- $newPlaylistEntry = null;
- foreach ($playlistListFeed as $playlistListEntry) {
- if ($playlistListEntry->title->text == $titleString) {
- $playlistFound = true;
- $newPlaylistEntry = $playlistListEntry;
- break;
- }
- }
- $this->assertTrue($playlistFound, 'Could not find the newly inserted ' .
- 'playlist.');
- // Update it
- $newTitle = $this->generateRandomString(10);
- $newPlaylistEntry->title->setText($newTitle);
- $updatedSuccesfully = true;
- try {
- $newPlaylistEntry->save();
- } catch (Zend_Gdata_App_Exception $e) {
- $updatedSuccesfully = false;
- }
- $this->assertTrue($updatedSuccesfully, 'Could not succesfully update ' .
- 'a new playlist.');
- // Delete it
- $deletedSuccesfully = true;
- try {
- $newPlaylistEntry->delete();
- } catch (Zend_Gdata_App_Exception $e) {
- $deletedSuccesfully = false;
- }
- $this->assertTrue($deletedSuccesfully, 'Could not succesfully delete ' .
- 'a new playlist.');
- }
- public function testAddAndDeleteSubscriptionToChannelV2()
- {
- $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
- $authenticationURL =
- 'https://www.google.com/youtube/accounts/ClientLogin';
- $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user,
- $this->pass,
- $service,
- null, // client
- 'Google-UnitTests-1.0', // source
- null, // loginToken
- null, // loginCaptcha
- $authenticationURL);
- $yt = new Zend_Gdata_YouTube(
- $httpClient, 'Google-UnitTests-1.0',
- 'ytapi-gdataops-12345-u78960r7-0',
- 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
- 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
- $yt->setMajorProtocolVersion(2);
- $channelToSubscribeTo = 'AssociatedPress';
- // Test for deletion first in case something went wrong
- // last time the test was run (network, etc...)
- $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
- $successDeletionUpFront = true;
- $message = null;
- foreach($subscriptionFeed as $subscriptionEntry) {
- $subscriptionType = null;
- $categories = $subscriptionEntry->getCategory();
- // examine the correct category element since there are multiple
- foreach($categories as $category) {
- if ($category->getScheme() ==
- 'http://gdata.youtube.com/schemas/2007/' .
- 'subscriptiontypes.cat') {
- $subscriptionType = $category->getTerm();
- }
- }
- if ($subscriptionType == 'channel') {
- if ($subscriptionEntry->getUsername()->text ==
- $channelToSubscribeTo) {
- try {
- $subscriptionEntry->delete();
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successDeletionUpFront = false;
- }
- }
- }
- }
- $this->assertTrue($successDeletionUpFront, 'Found existing ' .
- 'subscription in unit test, could not delete prior to running ' .
- 'test -- ' . $message);
- // Channel
- $newSubscription = $yt->newSubscriptionEntry();
- $newSubscription->category = array(
- $yt->newCategory('channel',
- $this->subscriptionTypeSchema));
- $newSubscription->setUsername($yt->newUsername(
- $channelToSubscribeTo));
- $postUrl =
- 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
- $successPosting = true;
- $message = null;
- $insertedSubscription = null;
- try {
- $insertedSubscription = $yt->insertEntry(
- $newSubscription, $postUrl,
- 'Zend_Gdata_YouTube_SubscriptionEntry');
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successPosting = false;
- }
- $this->assertTrue($successPosting, $message);
- // Delete it
- $successDeletion = true;
- $message = null;
- try {
- $insertedSubscription->delete();
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successDeletion = false;
- }
- $this->assertTrue($successDeletion, $message);
- }
- public function testAddAndDeleteSubscriptionToFavoritesV2()
- {
- $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
- $authenticationURL =
- 'https://www.google.com/youtube/accounts/ClientLogin';
- $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
- $username = $this->user,
- $password = $this->pass,
- $service = $service,
- $client = null,
- $source = 'Google-UnitTests-1.0',
- $loginToken = null,
- $loginCaptcha = null,
- $authenticationURL);
- $yt = new Zend_Gdata_YouTube(
- $httpClient, 'Google-UnitTests-1.0',
- 'ytapi-gdataops-12345-u78960r7-0',
- 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
- 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
- $yt->setMajorProtocolVersion(2);
- $usernameOfFavoritesToSubscribeTo = 'CBS';
- // Test for deletion first in case something went wrong
- // last time the test was run (network, etc...)
- $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
- $successDeletionUpFront = true;
- $message = null;
- foreach($subscriptionFeed as $subscriptionEntry) {
- $subscriptionType = null;
- $categories = $subscriptionEntry->getCategory();
- // examine the correct category element since there are multiple
- foreach($categories as $category) {
- if ($category->getScheme() ==
- 'http://gdata.youtube.com/schemas/2007/' .
- 'subscriptiontypes.cat') {
- $subscriptionType = $category->getTerm();
- }
- }
- if ($subscriptionType == 'favorites') {
- if ($subscriptionEntry->getUsername()->text ==
- $usernameOfFavoritesToSubscribeTo) {
- try {
- $subscriptionEntry->delete();
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successDeletionUpFront = false;
- }
- }
- }
- }
- $this->assertTrue($successDeletionUpFront, 'Found existing ' .
- 'subscription in unit test, could not delete prior to running ' .
- 'test -- ' . $message);
- // CBS's favorites
- $newSubscription = $yt->newSubscriptionEntry();
- $newSubscription->category = array(
- $yt->newCategory('favorites',
- $this->subscriptionTypeSchema));
- $newSubscription->setUsername($yt->newUsername(
- $usernameOfFavoritesToSubscribeTo));
- $postUrl =
- 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
- $successPosting = true;
- $message = null;
- $insertedSubscription = null;
- try {
- $insertedSubscription = $yt->insertEntry(
- $newSubscription, $postUrl,
- 'Zend_Gdata_YouTube_SubscriptionEntry');
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successPosting = false;
- }
- $this->assertTrue($successPosting, $message);
- // Delete it
- $successDeletion = true;
- $message = null;
- try {
- $insertedSubscription->delete();
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successDeletion = false;
- }
- $this->assertTrue($successDeletion, $message);
- }
- public function testAddAndDeleteSubscriptionToPlaylistV2()
- {
- $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
- $authenticationURL =
- 'https://www.google.com/youtube/accounts/ClientLogin';
- $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
- $username = $this->user,
- $password = $this->pass,
- $service = $service,
- $client = null,
- $source = 'Google-UnitTests-1.0',
- $loginToken = null,
- $loginCaptcha = null,
- $authenticationURL);
- $yt = new Zend_Gdata_YouTube(
- $httpClient, 'Google-UnitTests-1.0',
- 'ytapi-gdataops-12345-u78960r7-0',
- 'AI39si6c-ZMGFZ5fkDAEJoCNHP9LOM2LSO1XuycZF7E' .
- 'yu1IuvkioESqzRcf3voDLymIUGIrxdMx2aTufdbf5D7E51NyLYyfeaw');
- $yt->setMajorProtocolVersion(2);
- $playlistIdToSubscribeTo = '7A2BB4AFFEBED2A4';
- // Test for deletion first in case something went wrong
- // last time the test was run (network, etc...)
- $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
- $successDeletionUpFront = true;
- $message = null;
- foreach($subscriptionFeed as $subscriptionEntry) {
- $subscriptionType = null;
- $categories = $subscriptionEntry->getCategory();
- // examine the correct category element since there are multiple
- foreach($categories as $category) {
- if ($category->getScheme() ==
- 'http://gdata.youtube.com/schemas/2007/' .
- 'subscriptiontypes.cat') {
- $subscriptionType = $category->getTerm();
- }
- }
- if ($subscriptionType == 'playlist') {
- if ($subscriptionEntry->getPlaylistId()->text ==
- $playlistIdToSubscribeTo) {
- try {
- $subscriptionEntry->delete();
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successDeletionUpFront = false;
- }
- }
- }
- }
- $this->assertTrue($successDeletionUpFront, 'Found existing ' .
- 'subscription in unit test, could not delete prior to running ' .
- 'test -- ' . $message);
- // Playlist of McGyver videos
- $newSubscription = $yt->newSubscriptionEntry();
- $newSubscription->setMajorProtocolVersion(2);
- $newSubscription->category = array(
- $yt->newCategory('playlist',
- $this->subscriptionTypeSchema));
- $newSubscription->setPlaylistId($yt->newPlaylistId(
- $playlistIdToSubscribeTo));
- $postUrl =
- 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
- $successPosting = true;
- $message = null;
- $insertedSubscription = null;
- try {
- $insertedSubscription = $yt->insertEntry(
- $newSubscription, $postUrl,
- 'Zend_Gdata_YouTube_SubscriptionEntry');
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successPosting = false;
- }
- $this->assertTrue($successPosting, $message);
- // Delete it
- $successDeletion = true;
- $message = null;
- try {
- $insertedSubscription->delete();
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successDeletion = false;
- }
- $this->assertTrue($successDeletion, $message);
- }
- public function testAddAndDeleteSubscriptionToQueryV2()
- {
- $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $service = Zend_Gdata_YouTube::AUTH_SERVICE_NAME;
- $authenticationURL =
- 'https://www.google.com/youtube/accounts/ClientLogin';
- $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
- $username = $this->user,
- $password = $this->pass,
- $service = $service,
- $client = null,
- $source = 'Google-UnitTests-1.0',
- $loginToken = null,
- $loginCaptcha = null,
- $authenticationURL);
- $yt = new Zend_Gdata_YouTube($httpClient, 'Google-UnitTests-1.0', $clientId, $developerKey);
- $yt->setMajorProtocolVersion(2);
- $queryStringToSubscribeTo = 'zend';
- // Test for deletion first in case something went wrong
- // last time the test was run (network, etc...)
- $subscriptionFeed = $yt->getSubscriptionFeed($this->ytAccount);
- $successDeletionUpFront = true;
- $message = null;
- foreach($subscriptionFeed as $subscriptionEntry) {
- $subscriptionType = null;
- $categories = $subscriptionEntry->getCategory();
- // examine the correct category element since there are multiple
- foreach($categories as $category) {
- if ($category->getScheme() ==
- 'http://gdata.youtube.com/schemas/2007/' .
- 'subscriptiontypes.cat') {
- $subscriptionType = $category->getTerm();
- }
- }
- if ($subscriptionType == 'query') {
- if ($subscriptionEntry->getQueryString() ==
- $queryStringToSubscribeTo) {
- try {
- $subscriptionEntry->delete();
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successDeletionUpFront = false;
- }
- }
- }
- }
- $this->assertTrue($successDeletionUpFront, 'Found existing ' .
- 'subscription in unit test, could not delete prior to running ' .
- 'test -- ' . $message);
- // Query
- $newSubscription = $yt->newSubscriptionEntry();
- $newSubscription->category = array(
- $yt->newCategory('query',
- $this->subscriptionTypeSchema));
- $newSubscription->setQueryString($yt->newQueryString(
- $queryStringToSubscribeTo));
- $postUrl =
- 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
- $successPosting = true;
- $message = null;
- $insertedSubscription = null;
- try {
- $insertedSubscription = $yt->insertEntry(
- $newSubscription, $postUrl,
- 'Zend_Gdata_YouTube_SubscriptionEntry');
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successPosting = false;
- }
- $this->assertTrue($successPosting, $message);
- // Delete it
- $successDeletion = true;
- $message = null;
- try {
- $insertedSubscription->delete();
- } catch (Zend_App_Exception $e) {
- $message = $e->getMessage();
- $successDeletion = false;
- }
- $this->assertTrue($successDeletion, $message);
- }
- public function generateRandomString($length)
- {
- $outputString = null;
- for($i = 0; $i < $length; $i++) {
- $outputString .= chr(rand(65,90));
- }
- return $outputString;
- }
- public function testRetrieveActivityFeed()
- {
- $developerKey = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $client = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
- 'https://www.google.com/youtube/accounts/ClientLogin');
- $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
- $clientId, $developerKey);
- $youtube->setMajorProtocolVersion(2);
- $feed = $youtube->getActivityForUser($this->ytAccount);
- $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
- $this->assertTrue((count($feed->entries) > 0));
- $this->assertEquals('Activity of ' . $this->ytAccount,
- $feed->title->text);
- }
- public function testExceptionIfNotUsingDeveloperKey()
- {
- $exceptionThrown = false;
- $youtube = new Zend_Gdata_YouTube();
- $youtube->setMajorProtocolVersion(2);
- try {
- $youtube->getActivityForUser($this->ytAccount);
- } catch (Zend_Gdata_App_HttpException $e) {
- $exceptionThrown = true;
- }
- $this->assertTrue($exceptionThrown, 'Was expecting an exception when ' .
- 'making a request to the YouTube Activity feed without a ' .
- 'developer key.');
- }
- public function testRetrieveActivityFeedForMultipleUsers()
- {
- $developerKey = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $client = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
- 'https://www.google.com/youtube/accounts/ClientLogin');
- $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
- $clientId, $developerKey);
- $youtube->setMajorProtocolVersion(2);
- $feed = $youtube->getActivityForUser($this->ytAccount .
- ',associatedpress');
- $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
- $this->assertTrue((count($feed->entries) > 0));
- $this->assertEquals('Activity of ' . $this->ytAccount .
- ',associatedpress', $feed->title->text);
- }
- public function testRetrieveFriendFeed()
- {
- $developerKey = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $client = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
- 'https://www.google.com/youtube/accounts/ClientLogin');
- $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
- $clientId, $developerKey);
- $youtube->setMajorProtocolVersion(2);
- $feed = $youtube->getFriendActivityForCurrentUser();
- $this->assertTrue($feed instanceof Zend_Gdata_YouTube_ActivityFeed);
- $this->assertTrue((count($feed->entries) > 0));
- $this->assertEquals('Activity of the friends of ' . $this->ytAccount,
- $feed->title->text);
- }
- public function testThrowExceptionOnActivityFeedRequestForMoreThan20Users()
- {
- $exceptionThrown = false;
- $listOfMoreThan20Users = null;
- $youtube = new Zend_Gdata_YouTube();
- $youtube->setMajorProtocolVersion(2);
- for ($x = 0; $x < 30; $x++) {
- $listOfMoreThan20Users .= "user$x";
- if ($x != 29) {
- $listOfMoreThan20Users .= ",";
- }
- }
- try {
- $youtube->getActivityForUser($listOfMoreThan20Users);
- } catch (Zend_Gdata_App_InvalidArgumentException $e) {
- $exceptionThrown = true;
- }
- $this->assertTrue($exceptionThrown, 'Was expecting an exception on ' .
- 'a request to ->getActivityForUser when more than 20 users were ' .
- 'specified in YouTube.php');
- }
- public function testGetInboxFeedForCurrentUserV1()
- {
- $developerKey = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $client = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
- 'https://www.google.com/youtube/accounts/ClientLogin');
- $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
- $clientId, $developerKey);
- $inboxFeed = $youtube->getInboxFeedForCurrentUser();
- $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
- $this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
- $this->ytAccount . ' had no messages in their inbox.');
- // get the first entry
- $inboxFeed->rewind();
- $inboxEntry = $inboxFeed->current();
- $this->assertTrue(
- $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
- $this->assertTrue($inboxEntry->getTitle()->text != '');
- }
- public function testGetInboxFeedForCurrentUserV2()
- {
- $developerKey = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $client = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
- 'https://www.google.com/youtube/accounts/ClientLogin');
- $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
- $clientId, $developerKey);
- $youtube->setMajorProtocolVersion(2);
- $inboxFeed = $youtube->getInboxFeedForCurrentUser();
- $this->assertTrue($inboxFeed instanceof Zend_Gdata_YouTube_InboxFeed);
- $this->assertTrue(count($inboxFeed->entries) > 0, 'Test account ' .
- $this->ytAccount . ' had no messages in their inbox.');
- // get the first entry
- $inboxFeed->rewind();
- $inboxEntry = $inboxFeed->current();
- $this->assertTrue(
- $inboxEntry instanceof Zend_Gdata_YouTube_InboxEntry);
- $this->assertTrue($inboxEntry->getTitle()->text != '');
- }
- public function testSendAMessageV2()
- {
- $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $client = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
- 'https://www.google.com/youtube/accounts/ClientLogin');
- $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
- $clientId, $developerKey);
- $youtube->setMajorProtocolVersion(2);
- // get a video from the recently featured video feed
- $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
- $videoEntry = $videoFeed->entry[0];
- $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
- // sending message to gdpython (python client library unit test user)
- $sentMessage = $youtube->sendVideoMessage(
- 'Sending a v2 test message from Zend_Gdata_YouTubeOnlineTest.',
- $videoEntry, null, 'gdpython');
- $this->assertTrue(
- $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
- }
- public function testSendAMessageV1()
- {
- $developerKey = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant(
- 'TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $client = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
- 'https://www.google.com/youtube/accounts/ClientLogin');
- $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
- $clientId, $developerKey);
- $youtube->setMajorProtocolVersion(1);
- // get a video from the recently featured video feed
- $videoFeed = $youtube->getRecentlyFeaturedVideoFeed();
- $videoEntry = $videoFeed->entry[0];
- $this->assertTrue($videoEntry instanceof Zend_Gdata_YouTube_VideoEntry);
- // sending message to gdpython (python client library unit test user)
- $sentMessage = $youtube->sendVideoMessage(
- 'Sending a v1 test message from Zend_Gdata_YouTubeOnlineTest.',
- $videoEntry, null, 'gdpython');
- $this->assertTrue(
- $sentMessage instanceof Zend_Gdata_YouTube_InboxEntry);
- }
- public function testThrowExceptionOnSendingMessageWithoutVideo()
- {
- $exceptionCaught = false;
- $this->gdata = new Zend_Gdata_YouTube();
- try {
- $this->gdata->sendVideoMessage('Should fail', null, null, 'foo');
- } catch (Zend_Gdata_App_InvalidArgumentException $e) {
- $exceptionCaught = true;
- }
- $this->assertTrue($exceptionCaught, 'Was expecting an exception if ' .
- 'sending a message without a video');
- }
- public function testCommentOnAComment()
- {
- $developerKey = constant('TESTS_ZEND_GDATA_YOUTUBE_DEVELOPER_KEY');
- $clientId = constant('TESTS_ZEND_GDATA_YOUTUBE_CLIENT_ID');
- $client = Zend_Gdata_ClientLogin::getHttpClient(
- $this->user, $this->pass, 'youtube' , null, 'ZF_UnitTest', null, null,
- 'https://www.google.com/youtube/accounts/ClientLogin');
- $youtube = new Zend_Gdata_YouTube($client, 'ZF_UnitTest',
- $clientId, $developerKey);
- $youtube->setMajorProtocolVersion(2);
- $mostDiscussedFeed = $youtube->getVideoFeed(
- 'https://gdata.youtube.com/feeds/api/standardfeeds/most_discussed');
- // get first entry
- $mostDiscussedFeed->rewind();
- $firstEntry = $mostDiscussedFeed->current();
- $this->assertTrue($firstEntry instanceof Zend_Gdata_YouTube_VideoEntry);
- $commentFeed = $youtube->getVideoCommentFeed($firstEntry->getVideoId());
- // get first comment
- $commentFeed->rewind();
- $firstCommentEntry = $commentFeed->current();
- $commentedComment = $youtube->replyToCommentEntry($firstCommentEntry,
- 'awesome ! (ZFUnitTest-test)');
- $this->assertTrue(
- $commentedComment instanceof Zend_Gdata_YouTube_CommentEntry);
- }
- }
|