SubscriptionEntryTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata_YouTube
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  26. require_once 'Zend/Gdata/YouTube/SubscriptionEntry.php';
  27. require_once 'Zend/Gdata/YouTube.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Gdata_YouTube
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Gdata
  35. * @group Zend_Gdata_YouTube
  36. */
  37. class Zend_Gdata_YouTube_SubscriptionEntryTest extends PHPUnit_Framework_TestCase
  38. {
  39. public function setUp() {
  40. $this->entryText = file_get_contents(
  41. 'Zend/Gdata/YouTube/_files/SubscriptionEntryDataSample1.xml',
  42. true);
  43. $this->v2entryText_channel = file_get_contents(
  44. 'Zend/Gdata/YouTube/_files/' .
  45. 'SubscriptionEntryDataSample_channelV2.xml',
  46. true);
  47. $this->v2entryText_playlist = file_get_contents(
  48. 'Zend/Gdata/YouTube/_files/' .
  49. 'SubscriptionEntryDataSample_playlistV2.xml',
  50. true);
  51. $this->v2entryText_favorites = file_get_contents(
  52. 'Zend/Gdata/YouTube/_files/' .
  53. 'SubscriptionEntryDataSample_favoritesV2.xml',
  54. true);
  55. $this->v2entryText_query = file_get_contents(
  56. 'Zend/Gdata/YouTube/_files/' .
  57. 'SubscriptionEntryDataSample_queryV2.xml',
  58. true);
  59. $this->entry = new Zend_Gdata_YouTube_SubscriptionEntry();
  60. }
  61. private function verifyAllSamplePropertiesAreCorrect ($subscriptionListEntry) {
  62. $this->assertEquals(
  63. 'http://gdata.youtube.com/feeds/users/testuser/' .
  64. 'subscriptions/35bbde297dba88db',
  65. $subscriptionListEntry->id->text);
  66. $this->assertEquals('2007-03-02T11:58:22.000-08:00',
  67. $subscriptionListEntry->updated->text);
  68. $this->assertEquals(
  69. 'http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat',
  70. $subscriptionListEntry->category[1]->scheme);
  71. $this->assertEquals('publisher',
  72. $subscriptionListEntry->category[1]->term);
  73. $this->assertEquals('http://schemas.google.com/g/2005#kind',
  74. $subscriptionListEntry->category[0]->scheme);
  75. $this->assertEquals(
  76. 'http://gdata.youtube.com/schemas/2007#subscription',
  77. $subscriptionListEntry->category[0]->term);
  78. $this->assertEquals('text', $subscriptionListEntry->title->type);
  79. $this->assertEquals('Videos published by : BBC',
  80. $subscriptionListEntry->title->text);;
  81. $this->assertEquals('self',
  82. $subscriptionListEntry->getLink('self')->rel);
  83. $this->assertEquals('application/atom+xml',
  84. $subscriptionListEntry->getLink('self')->type);
  85. $this->assertEquals(
  86. 'http://gdata.youtube.com/feeds/users/testuser/' .
  87. 'subscriptions/35bbde297dba88db',
  88. $subscriptionListEntry->getLink('self')->href);
  89. $this->assertEquals('testuser',
  90. $subscriptionListEntry->author[0]->name->text);
  91. $this->assertEquals('http://gdata.youtube.com/feeds/users/testuser',
  92. $subscriptionListEntry->author[0]->uri->text);
  93. $this->assertEquals('http://gdata.youtube.com/feeds/users/BBC/uploads',
  94. $subscriptionListEntry->feedLink[0]->href);
  95. $this->assertEquals('697',
  96. $subscriptionListEntry->feedLink[0]->countHint);
  97. $this->assertEquals(
  98. 'http://gdata.youtube.com/schemas/2007#user.uploads',
  99. $subscriptionListEntry->feedLink[0]->rel);
  100. }
  101. public function verifyAllSamplePropertiesAreCorrectV2(
  102. $subscriptionListEntry) {
  103. $this->assertEquals(
  104. 'tag:youtube.com,2008:user:googledevelopers:subscription:' .
  105. 'Z1Lm-S9gkRQ',
  106. $subscriptionListEntry->id->text);
  107. $this->assertEquals('2007-11-16T15:15:17.000-08:00',
  108. $subscriptionListEntry->published->text);
  109. $this->assertEquals('2007-11-16T15:15:17.000-08:00',
  110. $subscriptionListEntry->updated->text);
  111. $this->assertEquals(
  112. 'http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat',
  113. $subscriptionListEntry->category[0]->scheme);
  114. $this->assertEquals('channel',
  115. $subscriptionListEntry->category[0]->term);
  116. $this->assertEquals('http://schemas.google.com/g/2005#kind',
  117. $subscriptionListEntry->category[1]->scheme);
  118. $this->assertEquals(
  119. 'http://gdata.youtube.com/schemas/2007#subscription',
  120. $subscriptionListEntry->category[1]->term);
  121. $this->assertEquals('self',
  122. $subscriptionListEntry->getLink('self')->rel);
  123. $this->assertEquals('application/atom+xml',
  124. $subscriptionListEntry->getLink('self')->type);
  125. $this->assertEquals(
  126. 'http://gdata.youtube.com/feeds/api/users/googledevelopers/' .
  127. 'subscriptions/Z1Lm-S9gkRQ?v=2',
  128. $subscriptionListEntry->getLink('self')->href);
  129. $this->assertEquals('related',
  130. $subscriptionListEntry->getLink('related')->rel);
  131. $this->assertEquals('application/atom+xml',
  132. $subscriptionListEntry->getLink('related')->type);
  133. $this->assertEquals(
  134. 'http://gdata.youtube.com/feeds/api/users/googledevelopers?v=2',
  135. $subscriptionListEntry->getLink('related')->href);
  136. $this->assertEquals('alternate',
  137. $subscriptionListEntry->getLink('alternate')->rel);
  138. $this->assertEquals('text/html',
  139. $subscriptionListEntry->getLink('alternate')->type);
  140. $this->assertEquals(
  141. 'http://www.youtube.com/profile_videos?user=androiddevelopers',
  142. $subscriptionListEntry->getLink('alternate')->href);
  143. $this->assertEquals('GoogleDevelopers',
  144. $subscriptionListEntry->author[0]->name->text);
  145. $this->assertEquals(
  146. 'http://gdata.youtube.com/feeds/api/users/googledevelopers',
  147. $subscriptionListEntry->author[0]->uri->text);
  148. $this->assertEquals('androiddevelopers',
  149. $subscriptionListEntry->getUsername()->text);
  150. $this->assertEquals('50',
  151. $subscriptionListEntry->getCountHint()->text);
  152. $thumbnail = $subscriptionListEntry->getMediaThumbnail();
  153. $this->assertTrue(
  154. $thumbnail instanceof Zend_Gdata_Media_Extension_MediaThumbnail);
  155. $this->assertTrue($thumbnail->getUrl() != null);
  156. $this->assertEquals(
  157. 'http://gdata.youtube.com/feeds/api/users/' .
  158. 'androiddevelopers/uploads?v=2',
  159. $subscriptionListEntry->getContent()->getSrc());
  160. }
  161. public function testEmptyEntryShouldHaveNoExtensionElements() {
  162. $this->assertTrue(is_array($this->entry->extensionElements));
  163. $this->assertTrue(count($this->entry->extensionElements) == 0);
  164. }
  165. public function testEmptyEntryShouldHaveNoExtensionAttributes() {
  166. $this->assertTrue(is_array($this->entry->extensionAttributes));
  167. $this->assertTrue(count($this->entry->extensionAttributes) == 0);
  168. }
  169. public function testSampleEntryShouldHaveNoExtensionElements() {
  170. $this->entry->transferFromXML($this->entryText);
  171. $this->assertTrue(is_array($this->entry->extensionElements));
  172. $this->assertTrue(count($this->entry->extensionElements) == 0);
  173. }
  174. public function testSampleEntryShouldHaveNoExtensionAttributes() {
  175. $this->entry->transferFromXML($this->entryText);
  176. $this->assertTrue(is_array($this->entry->extensionAttributes));
  177. $this->assertTrue(count($this->entry->extensionAttributes) == 0);
  178. }
  179. public function testSampleEntryShouldHaveNoExtensionElementsV2() {
  180. $this->entry->transferFromXML($this->v2entryText_channel);
  181. $this->assertTrue(is_array($this->entry->extensionElements));
  182. $this->assertTrue(count($this->entry->extensionElements) == 0);
  183. }
  184. public function testSampleEntryShouldHaveNoExtensionAttributesV2() {
  185. $this->entry->transferFromXML($this->v2entryText_channel);
  186. $this->assertTrue(is_array($this->entry->extensionAttributes));
  187. $this->assertTrue(count($this->entry->extensionAttributes) == 0);
  188. }
  189. public function testEmptySubscriptionEntryToAndFromStringShouldMatch() {
  190. $entryXml = $this->entry->saveXML();
  191. $newSubscriptionEntry = new Zend_Gdata_YouTube_SubscriptionEntry();
  192. $newSubscriptionEntry->transferFromXML($entryXml);
  193. $newSubscriptionEntryXml = $newSubscriptionEntry->saveXML();
  194. $this->assertTrue($entryXml == $newSubscriptionEntryXml);
  195. }
  196. public function testEmptySubscriptionEntryToAndFromStringShouldMatchV2() {
  197. $this->entry->transferFromXML($this->v2entryText_channel);
  198. $entryXml = $this->entry->saveXML();
  199. $newSubscriptionEntry = new Zend_Gdata_YouTube_SubscriptionEntry();
  200. $newSubscriptionEntry->transferFromXML($entryXml);
  201. $newSubscriptionEntry->setMajorProtocolVersion(2);
  202. $newSubscriptionEntryXml = $newSubscriptionEntry->saveXML();
  203. $this->assertTrue($entryXml == $newSubscriptionEntryXml);
  204. }
  205. public function testGetFeedLinkReturnsAllStoredEntriesWhenUsedWithNoParameters() {
  206. // Prepare test data
  207. $entry1 = new Zend_Gdata_Extension_FeedLink();
  208. $entry1->rel = "first";
  209. $entry1->href= "foo";
  210. $entry2 = new Zend_Gdata_Extension_FeedLink();
  211. $entry2->rel = "second";
  212. $entry2->href= "bar";
  213. $data = array($entry1, $entry2);
  214. // Load test data and run test
  215. $this->entry->feedLink = $data;
  216. $this->assertEquals(2, count($this->entry->feedLink));
  217. }
  218. public function testGetFeedLinkCanReturnEntriesByRelValue() {
  219. // Prepare test data
  220. $entry1 = new Zend_Gdata_Extension_FeedLink();
  221. $entry1->rel = "first";
  222. $entry1->href= "foo";
  223. $entry2 = new Zend_Gdata_Extension_FeedLink();
  224. $entry2->rel = "second";
  225. $entry2->href= "bar";
  226. $data = array($entry1, $entry2);
  227. // Load test data and run test
  228. $this->entry->feedLink = $data;
  229. $this->assertEquals($entry1, $this->entry->getFeedLink('first'));
  230. $this->assertEquals($entry2, $this->entry->getFeedLink('second'));
  231. }
  232. public function testSamplePropertiesAreCorrect () {
  233. $this->entry->transferFromXML($this->entryText);
  234. $this->verifyAllSamplePropertiesAreCorrect($this->entry);
  235. }
  236. public function testSamplePropertiesAreCorrectV2 () {
  237. $this->entry->transferFromXML($this->v2entryText_channel);
  238. $this->entry->setMajorProtocolVersion(2);
  239. $this->verifyAllSamplePropertiesAreCorrectV2($this->entry);
  240. }
  241. public function testConvertSubscriptionEntryToAndFromString() {
  242. $this->entry->transferFromXML($this->entryText);
  243. $entryXml = $this->entry->saveXML();
  244. $newSubscriptionEntry = new Zend_Gdata_YouTube_SubscriptionEntry();
  245. $newSubscriptionEntry->transferFromXML($entryXml);
  246. $this->verifyAllSamplePropertiesAreCorrect($newSubscriptionEntry);
  247. $newSubscriptionEntryXml = $newSubscriptionEntry->saveXML();
  248. $this->assertEquals($entryXml, $newSubscriptionEntryXml);
  249. }
  250. public function testExceptionThrownInChannelSubscription() {
  251. $this->entry->transferFromXML($this->entryText);
  252. $exceptionCaught = false;
  253. try {
  254. $this->entry->getCountHint();
  255. } catch (Zend_Gdata_App_VersionException $e) {
  256. $exceptionCaught = true;
  257. }
  258. $this->assertTrue($exceptionCaught, 'Expected a VersionException on ' .
  259. 'calling getCountHint() on a v1 subscription entry.');
  260. }
  261. public function testChannelSubscriptionFunctionalityV2() {
  262. $this->entry->transferFromXML($this->v2entryText_channel);
  263. $this->assertEquals('androiddevelopers',
  264. $this->entry->getUsername()->text);
  265. $categories = $this->entry->getCategory();
  266. foreach($categories as $category) {
  267. if ($category->getScheme() ==
  268. 'http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat') {
  269. $this->assertEquals('channel', $category->getTerm());
  270. }
  271. }
  272. }
  273. public function testPlaylistSubscriptionFunctionalityV2() {
  274. $this->entry->transferFromXML($this->v2entryText_playlist);
  275. $this->entry->setMajorProtocolVersion(2);
  276. $this->assertEquals('From Google Engineers',
  277. $this->entry->getPlaylistTitle()->text);
  278. $this->assertEquals('4AE5C0D23C2EB82D',
  279. $this->entry->getPlaylistId()->text);
  280. $categories = $this->entry->getCategory();
  281. foreach($categories as $category) {
  282. if ($category->getScheme() ==
  283. 'http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat') {
  284. $this->assertEquals('playlist', $category->getTerm());
  285. }
  286. }
  287. }
  288. public function testFavoritesSubscriptionFunctionalityV2() {
  289. $this->entry->transferFromXML($this->v2entryText_favorites);
  290. $categories = $this->entry->getCategory();
  291. foreach($categories as $category) {
  292. if ($category->getScheme() ==
  293. 'http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat') {
  294. $this->assertEquals('favorites', $category->getTerm());
  295. }
  296. }
  297. }
  298. public function testQuerySubscriptionFunctionalityV2() {
  299. $this->entry->transferFromXML($this->v2entryText_query);
  300. $categories = $this->entry->getCategory();
  301. foreach($categories as $category) {
  302. if ($category->getScheme() ==
  303. 'http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat') {
  304. $this->assertEquals('query', $category->getTerm());
  305. }
  306. }
  307. }
  308. }