2
0

SubscriptionEntryTest.php 14 KB

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