PhotosOnlineTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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
  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. require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  22. require_once 'Zend/Gdata/Photos.php';
  23. require_once 'Zend/Http/Client.php';
  24. require_once 'Zend/Http/Client/Adapter/Test.php';
  25. require_once 'Zend/Gdata/ClientLogin.php';
  26. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  27. /**
  28. * @package Zend_Gdata
  29. * @subpackage UnitTests
  30. */
  31. class Zend_Gdata_PhotosOnlineTest extends PHPUnit_Framework_TestCase
  32. {
  33. protected $photos = null;
  34. public function setUp()
  35. {
  36. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  37. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  38. $service = 'lh2';
  39. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  40. $this->photos = new Zend_Gdata_Photos($client);
  41. }
  42. /**
  43. * Verify that a given property is set to a specific value
  44. * and that the getter and magic variable return the same value.
  45. *
  46. * @param object $obj The object to be interrogated.
  47. * @param string $name The name of the property to be verified.
  48. * @param string $secondName 2nd level accessor function name
  49. * @param object $value The expected value of the property.
  50. */
  51. protected function verifyProperty($obj, $name, $secondName, $value)
  52. {
  53. $propName = $name;
  54. $propGetter = "get" . ucfirst($name);
  55. $secondGetter = "get" . ucfirst($secondName);
  56. $this->assertEquals($obj->$propGetter(), $obj->$propName);
  57. $this->assertEquals($value, $obj->$propGetter()->$secondGetter());
  58. }
  59. public function createAlbum()
  60. {
  61. $client = $this->photos;
  62. $album = new Zend_Gdata_Photos_AlbumEntry();
  63. $album->setTitle($client->newTitle("testAlbum"));
  64. $album->setCategory(array($client->newCategory(
  65. 'http://schemas.google.com/photos/2007#album',
  66. 'http://schemas.google.com/g/2005#kind')));
  67. $newAlbum = $client->insertAlbumEntry($album);
  68. $this->assertEquals($album->getTitle(), $newAlbum->getTitle());
  69. $this->assertEquals($newAlbum->getTitle(), $client->getAlbumEntry($newAlbum->getLink('self')->href)->getTitle());
  70. $albumFeedUri = $newAlbum->getLink('http://schemas.google.com/g/2005#feed')->href;
  71. $albumFeed = $client->getAlbumFeed($albumFeedUri);
  72. $this->verifyProperty($albumFeed, "title", "text", "testAlbum");
  73. return $newAlbum;
  74. }
  75. public function createPhoto($album)
  76. {
  77. $client = $this->photos;
  78. $fd = $client->newMediaFileSource('Zend/Gdata/_files/testImage.jpg');
  79. $fd->setContentType('image/jpeg');
  80. $photo = new Zend_Gdata_Photos_PhotoEntry();
  81. $photo->setMediaSource($fd);
  82. $photo->setTitle($client->newTitle("test photo"));
  83. $photo->setCategory(array($client->newCategory(
  84. 'http://schemas.google.com/photos/2007#photo',
  85. 'http://schemas.google.com/g/2005#kind')));
  86. $newPhoto = $client->insertPhotoEntry($photo, $album);
  87. $this->assertEquals($photo->getTitle(), $newPhoto->getTitle());
  88. $this->assertEquals($newPhoto->getTitle(), $client->getPhotoEntry($newPhoto->getLink('self')->href)->getTitle());
  89. $photoFeedUri = $newPhoto->getLink('http://schemas.google.com/g/2005#feed')->href;
  90. $photoFeed = $client->getPhotoFeed($photoFeedUri);
  91. $this->verifyProperty($photoFeed, "title", "text", "test photo");
  92. return $newPhoto;
  93. }
  94. public function updatePhotoMetaData()
  95. {
  96. $client = $this->photos;
  97. $album = $this->createAlbum();
  98. $insertedEntry = $this->createPhoto($album);
  99. $insertedEntry->title->text = "New Photo";
  100. $insertedEntry->summary->text = "Photo caption";
  101. $keywords = new Zend_Gdata_Media_Extension_MediaKeywords();
  102. $keywords->setText("foo, bar, baz");
  103. $insertedEntry->mediaGroup->keywords = $keywords;
  104. $updatedEntry = $insertedEntry->save();
  105. return array($updatedEntry, $album);
  106. }
  107. public function createComment($photo)
  108. {
  109. $client = $this->photos;
  110. $comment = new Zend_Gdata_Photos_CommentEntry();
  111. $comment->setTitle($client->newTitle("test comment"));
  112. $comment->setContent($client->newContent("test comment"));
  113. $comment->setCategory(array($client->newCategory(
  114. 'http://schemas.google.com/photos/2007#comment',
  115. 'http://schemas.google.com/g/2005#kind')));
  116. $newComment = $client->insertCommentEntry($comment, $photo);
  117. $this->assertEquals($comment->getContent(), $newComment->getContent());
  118. $this->assertEquals($newComment->getContent(), $client->getCommentEntry($newComment->getLink('self')->href)->getContent());
  119. return $newComment;
  120. }
  121. public function createTag($photo)
  122. {
  123. $client = $this->photos;
  124. $tag = new Zend_Gdata_Photos_TagEntry();
  125. $tag->setTitle($client->newTitle("test tag"));
  126. $tag->setContent($client->newContent("test tag"));
  127. $tag->setCategory(array($client->newCategory(
  128. 'http://schemas.google.com/photos/2007#tag',
  129. 'http://schemas.google.com/g/2005#kind')));
  130. $newTag = $client->insertTagEntry($tag, $photo);
  131. $this->assertEquals($tag->getTitle(), $newTag->getTitle());
  132. $this->assertEquals($newTag->getTitle(), $client->getTagEntry($newTag->getLink('self')->href)->getTitle());
  133. return $newTag;
  134. }
  135. public function testCreateAlbumAndUploadPhoto()
  136. {
  137. $client = $this->photos;
  138. $album = $this->createAlbum();
  139. $photo = $this->createPhoto($album);
  140. // Clean up the mess
  141. $client->deletePhotoEntry($photo, true);
  142. $client->deleteAlbumEntry($album, true);
  143. }
  144. public function testUpdatePhotoMetadata()
  145. {
  146. $client = $this->photos;
  147. $dataArray = $this->updatePhotoMetaData();
  148. $updatedPhoto = $dataArray[0];
  149. $album = $dataArray[1];
  150. $this->assertTrue($updatedPhoto instanceof Zend_Gdata_Photos_PhotoEntry);
  151. // Clean up the mess
  152. $client->deletePhotoEntry($updatedPhoto, true);
  153. $client->deleteAlbumEntry($album, true);
  154. }
  155. public function testUserFeedAndEntry()
  156. {
  157. $client = $this->photos;
  158. $userEntryUri = "http://picasaweb.google.com/data/entry/api/user/" .
  159. constant('TESTS_ZEND_GDATA_PHOTOS_USERNAME');
  160. $userEntry = $client->getUserEntry($userEntryUri);
  161. $this->verifyProperty($userEntry, "id", "text",
  162. "http://picasaweb.google.com/data/entry/api/user/" .
  163. constant('TESTS_ZEND_GDATA_PHOTOS_USERNAME'));
  164. $userFeed = $client->getUserFeed(constant('TESTS_ZEND_GDATA_PHOTOS_USERNAME'));
  165. $this->verifyProperty($userFeed, "id", "text",
  166. "http://picasaweb.google.com/data/feed/api/user/" .
  167. constant('TESTS_ZEND_GDATA_PHOTOS_USERNAME'));
  168. }
  169. public function testCreatePhotoCommentAndTag()
  170. {
  171. $client = $this->photos;
  172. $album = $this->createAlbum();
  173. $photo = $this->createPhoto($album);
  174. $comment = $this->createComment($photo);
  175. $tag = $this->createTag($photo);
  176. // Clean up the mess
  177. $client->deleteTagEntry($tag, true);
  178. $client->deleteCommentEntry($comment, true);
  179. $client->deletePhotoEntry($photo, true);
  180. $client->deleteAlbumEntry($album, true);
  181. }
  182. public function testInvalidEntryFetchingAndInserting()
  183. {
  184. $client = $this->photos;
  185. try {
  186. $userEntry = $client->getUserEntry(null);
  187. } catch (Exception $e) {
  188. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  189. }
  190. try {
  191. $userEntry = $client->getAlbumEntry(null);
  192. } catch (Exception $e) {
  193. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  194. }
  195. try {
  196. $photoEntry = $client->getPhotoEntry(null);
  197. } catch (Exception $e) {
  198. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  199. }
  200. try {
  201. $tagEntry = $client->getTagEntry(null);
  202. } catch (Exception $e) {
  203. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  204. }
  205. try {
  206. $commentEntry = $client->getCommentEntry(null);
  207. } catch (Exception $e) {
  208. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  209. }
  210. try {
  211. $photo = new Zend_Gdata_Photos_PhotoEntry();
  212. $result = $client->insertPhotoEntry($photo, null);
  213. } catch (Exception $e) {
  214. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  215. }
  216. try {
  217. $comment = new Zend_Gdata_Photos_CommentEntry();
  218. $result = $client->insertCommentEntry($comment, null);
  219. } catch (Exception $e) {
  220. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  221. }
  222. try {
  223. $tag = new Zend_Gdata_Photos_TagEntry();
  224. $result = $client->insertTagEntry($tag, null);
  225. } catch (Exception $e) {
  226. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  227. }
  228. }
  229. public function testInvalidFeedFetching()
  230. {
  231. $client = $this->photos;
  232. try {
  233. $albumFeed = $client->getAlbumFeed(null);
  234. } catch (Exception $e) {
  235. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  236. }
  237. }
  238. }