PhotosOnlineTest.php 10 KB

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