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