PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/cakesocial.local/app/vendors/zendgdata/tests/Zend/Gdata/GappsOnlineTest.php

https://github.com/miamiruby/cakestuff
PHP | 499 lines | 308 code | 76 blank | 115 comment | 18 complexity | dd35075290fc50b97aade4889ec5eae6 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, MIT
  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/Gapps.php';
  23. require_once 'Zend/Gdata/Gapps/UserEntry.php';
  24. require_once 'Zend/Gdata/Gapps/UserQuery.php';
  25. require_once 'Zend/Gdata/ClientLogin.php';
  26. require_once 'Zend/Http/Client.php';
  27. /**
  28. * @package Zend_Gdata
  29. * @subpackage UnitTests
  30. */
  31. class Zend_Gdata_GappsOnlineTest extends PHPUnit_Framework_TestCase
  32. {
  33. const GIVEN_NAME = 'Zend_Gdata';
  34. const FAMILY_NAME = 'Automated Test Account';
  35. const PASSWORD = '4ohtladfl;';
  36. const PASSWORD_HASH = 'SHA-1';
  37. public function setUp()
  38. {
  39. $this->id = uniqid('ZF-');
  40. $username = constant('TESTS_ZEND_GDATA_GAPPS_EMAIL');
  41. $pass = constant('TESTS_ZEND_GDATA_GAPPS_PASSWORD');
  42. $this->domain = constant('TESTS_ZEND_GDATA_GAPPS_DOMAIN');
  43. $client = Zend_Gdata_ClientLogin::getHttpClient($username, $pass, Zend_Gdata_Gapps::AUTH_SERVICE_NAME);
  44. $this->gdata = new Zend_Gdata_Gapps($client, $this->domain);
  45. // Container to hold users and lists created during tests. All entries in
  46. // here will have delete() called during tear down.
  47. //
  48. // Failed deletions are okay, so add everying creatd in here, even if
  49. // you plan to delete the user yourself!
  50. $this->autoDeletePool = array();
  51. }
  52. public function tearDown()
  53. {
  54. // Delete all entries in $this->autoDeletePool.
  55. foreach ($this->autoDeletePool as $x) {
  56. try {
  57. $x->delete();
  58. } catch (Exception $e) {
  59. // Failed deletes are okay. Try and delete the rest anyway.
  60. }
  61. }
  62. }
  63. // Schedule an entry for deletion at test tear-down.
  64. protected function autoDelete($entry) {
  65. $this->autoDeletePool[] = $entry;
  66. }
  67. // Test Create/Read/Update/Destroy operations on a UserEntry
  68. public function testUserCRUDOperations() {
  69. // Create a new user
  70. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  71. sha1(self::PASSWORD), self::PASSWORD_HASH);
  72. $this->autoDelete($user);
  73. // Verify that returned values are correct
  74. $this->assertEquals($this->id, $user->login->username);
  75. $this->assertEquals(self::GIVEN_NAME, $user->name->givenName);
  76. $this->assertEquals(self::FAMILY_NAME, $user->name->familyName);
  77. // Since we can't retrieve the password or hash function via the
  78. // API, let's see if a ClientLogin auth request succeeds
  79. try {
  80. Zend_Gdata_ClientLogin::getHTTPClient($this->id . '@' .
  81. $this->domain, self::PASSWORD, 'xapi');
  82. } catch (Zend_Gdata_App_AuthException $e) {
  83. $this->fail("Unable to authenticate new user via ClientLogin.");
  84. }
  85. // Check to make sure there are no extension elements/attributes
  86. // in the retrieved user
  87. $this->assertTrue(count($user->extensionElements) == 0);
  88. $this->assertTrue(count($user->extensionAttributes) == 0);
  89. // Try searching for the same user and make sure that they're returned
  90. $user2 = $this->gdata->retrieveUser($this->id);
  91. $this->assertEquals($user->saveXML(), $user2->saveXML());
  92. // Delete user (uses builtin delete method, convenience delete
  93. // method tested further down)
  94. $user->delete();
  95. // Ensure that user was deleted
  96. $deletedUser = $this->gdata->retrieveUser($this->id);
  97. $this->assertNull($deletedUser);
  98. }
  99. // Test to make sure that users with unicode characters can be created
  100. // okay.
  101. public function testUsersSupportUnicode() {
  102. // Create a user
  103. $user = $this->gdata->createUser($this->id, 'テスト', 'ユーザー',
  104. sha1(self::PASSWORD), self::PASSWORD_HASH);
  105. $this->autoDelete($user);
  106. // Make sure the user is the same as returned by the server
  107. $this->assertEquals('テスト', $user->name->givenName);
  108. $this->assertEquals('ユーザー', $user->name->familyName);
  109. }
  110. // Test to make sure that a page of users can be retrieved.
  111. public function testRetrievePageOfUsers() {
  112. $feed = $this->gdata->retrievePageOfUsers();
  113. $this->assertTrue(count($feed->entries) > 0);
  114. }
  115. // Test to make sure that a page of users can be retrieved with a
  116. // startUsername parameter.
  117. public function testRetrievePageOfUsersWithStartingUsername() {
  118. $feed = $this->gdata->retrievePageOfUsers();
  119. $this->assertTrue(count($feed->entries) > 0);
  120. $username = $feed->entries[0]->login->username;
  121. $feed = $this->gdata->retrievePageOfUsers($username);
  122. $this->assertTrue(count($feed->entries) > 0);
  123. }
  124. // Test to see if all users can be retrieved
  125. // NOTE: This test may timeout if the domain used for testing contains
  126. // many pages of users.
  127. public function testRetrieveAllUsers() {
  128. // Create 35 users to make sure that there's more than one page.
  129. for ($i = 0; $i < 25; $i++) {
  130. $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME,
  131. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  132. $this->autoDelete($user);
  133. }
  134. $feed = $this->gdata->retrieveAllUsers();
  135. $this->assertTrue(count($feed->entry) > 0);
  136. }
  137. // Test to see if a user can be manually updated by calling updateUser().
  138. public function testManualUserEntryUpdate() {
  139. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  140. sha1(self::PASSWORD), self::PASSWORD_HASH);
  141. $this->autoDelete($user);
  142. $user->name->givenName = "Renamed";
  143. $user2 = $this->gdata->updateUser($this->id, $user);
  144. $this->assertEquals("Renamed", $user2->name->givenName);
  145. }
  146. // Test to see if a user can be suspended, then un-suspended
  147. public function testCanSuspendAndRestoreUser() {
  148. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  149. sha1(self::PASSWORD), self::PASSWORD_HASH);
  150. $this->autoDelete($user);
  151. $returned = $this->gdata->suspendUser($this->id);
  152. $user = $this->gdata->retrieveUser($this->id);
  153. $this->assertEquals(true, $user->login->suspended);
  154. $this->assertEquals($this->id, $returned->login->username);
  155. $returned = $this->gdata->restoreUser($this->id);
  156. $user = $this->gdata->retrieveUser($this->id);
  157. $this->assertEquals(false, $user->login->suspended);
  158. $this->assertEquals($this->id, $returned->login->username);
  159. }
  160. // Test the convenience delete method for users
  161. public function testCanDeleteUser() {
  162. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  163. sha1(self::PASSWORD), self::PASSWORD_HASH);
  164. $this->autoDelete($user);
  165. // Assert that the user exists, just in case...
  166. $rUser = $this->gdata->retrieveUser($this->id);
  167. $this->assertNotNull($rUser);
  168. // Delete user
  169. $this->gdata->deleteUser($this->id);
  170. // Ensure that user was deleted
  171. $rUser = $this->gdata->retrieveUser($this->id);
  172. $this->assertNull($rUser);
  173. }
  174. public function testNicknameCRUDOperations() {
  175. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  176. sha1(self::PASSWORD), self::PASSWORD_HASH);
  177. $this->autoDelete($user);
  178. // Create nickname
  179. // Apps will convert the nickname to lowercase on the server, so
  180. // we just make sure the generated nickname is lowercase here to start
  181. // to avoid confusion later on.
  182. $generatedNickname = strtolower(uniqid('zf-nick-'));
  183. $nickname = $this->gdata->createNickname($this->id, $generatedNickname);
  184. $this->assertEquals($generatedNickname, $nickname->nickname->name);
  185. $this->assertEquals($this->id, $nickname->login->username);
  186. // Retrieve nickname
  187. $nickname = $this->gdata->retrieveNickname($generatedNickname);
  188. $this->assertEquals($generatedNickname, $nickname->nickname->name);
  189. $this->assertEquals($this->id, $nickname->login->username);
  190. // Delete nickname (uses builtin delete method, convenience delete
  191. // method tested further down)
  192. $nickname->delete();
  193. // Ensure that nickname was deleted
  194. $nickname = $this->gdata->retrieveNickname($generatedNickname);
  195. $this->assertNull($nickname);
  196. }
  197. public function testRetrieveNicknames() {
  198. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  199. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  200. $this->autoDelete($user);
  201. // Create 5 nicknames
  202. for ($i = 0; $i < 5; $i++) {
  203. $generatedNickname[$i] = strtolower(uniqid('zf-nick-'));
  204. $this->gdata->createNickname($this->id, $generatedNickname[$i]);
  205. }
  206. // Retrieve all nicknames for the test user and see if they match
  207. $nicknameFeed = $this->gdata->retrieveNicknames($this->id);
  208. $this->assertEquals(count($generatedNickname), count($nicknameFeed->entry));
  209. foreach ($nicknameFeed as $nicknameEntry) {
  210. $searchResult = array_search($nicknameEntry->nickname->name,
  211. $generatedNickname);
  212. $this->assertNotSame(false, $searchResult);
  213. unset($generatedNickname[$searchResult]);
  214. }
  215. $this->assertEquals(0, count($generatedNickname));
  216. }
  217. public function testRetrievePageOfNicknames() {
  218. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  219. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  220. $this->autoDelete($user);
  221. // Create 5 nicknames
  222. for ($i = 0; $i < 5; $i++) {
  223. $generatedNickname[$i] = strtolower(uniqid('zf-nick-'));
  224. $this->gdata->createNickname($this->id, $generatedNickname[$i]);
  225. }
  226. // Test to make sure that we receive at least 5 nicknames back
  227. // from the server
  228. $results = $this->gdata->retrievePageOfNicknames();
  229. $this->assertTrue(count($results->entry) >= 5);
  230. }
  231. public function testRetrieveAllNicknames() {
  232. // Create 3 users, each with 10 nicknames
  233. for ($i = 0; $i < 3; $i++) {
  234. $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME,
  235. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  236. $this->autoDelete($user);
  237. for ($j = 0; $j < 10; $j++) {
  238. $generatedNickname = strtolower(uniqid('zf-nick-'));
  239. $this->gdata->createNickname($user->login->username, $generatedNickname);
  240. }
  241. }
  242. // Test to make sure that we receive at least 5 nicknames back
  243. // from the server
  244. $results = $this->gdata->retrieveAllNicknames();
  245. $this->assertTrue(count($results->entry) >= 30);
  246. }
  247. // Test the convenience delete method for nicknames
  248. public function testCanDeleteNickname() {
  249. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  250. sha1(self::PASSWORD), self::PASSWORD_HASH);
  251. $this->autoDelete($user);
  252. $generatedNickname = strtolower(uniqid('zf-nick-'));
  253. $this->gdata->createNickname($this->id, $generatedNickname);
  254. // Assert that the nickname exists, just in case...
  255. $rNick = $this->gdata->retrieveNickname($generatedNickname);
  256. $this->assertNotNull($rNick);
  257. // Delete nickname
  258. $this->gdata->deleteNickname($generatedNickname);
  259. // Ensure that nickname was deleted
  260. $rNick = $this->gdata->retrieveNickname($generatedNickname);
  261. $this->assertNull($rNick);
  262. }
  263. public function testEmailListCRUDOperations() {
  264. // Create email list
  265. $generatedListName = strtolower(uniqid('zf-list-'));
  266. $list = $this->gdata->createEmailList($generatedListName);
  267. $this->autoDelete($list);
  268. $this->assertEquals($generatedListName, $list->emailList->name);
  269. // Retrieve email list
  270. $query = $this->gdata->newEmailListQuery();
  271. $listFeed = $this->gdata->getEmailListFeed($query);
  272. $entryCount = count($listFeed->entry);
  273. $this->assertTrue($entryCount > 0);
  274. // Delete email list (uses builtin delete method, convenience delete
  275. // method tested further down)
  276. $list->delete();
  277. // Ensure that nickname was deleted
  278. $listFeed = $this->gdata->getEmailListFeed($query);
  279. $this->assertEquals($entryCount - 1, count($listFeed->entry));
  280. }
  281. public function testCanAssignMultipleEmailListsToOneUser() {
  282. // Create a user
  283. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  284. sha1(self::PASSWORD), self::PASSWORD_HASH);
  285. $this->autoDelete($user);
  286. // Create two email lists
  287. $listCount = 2;
  288. for ($i = 0; $i < $listCount; $i++) {
  289. $generatedListName = strtolower(uniqid('zf-list-'));
  290. $list = $this->gdata->createEmailList($generatedListName);
  291. $this->autoDelete($list);
  292. $this->gdata->addRecipientToEmailList($this->id, $generatedListName);
  293. }
  294. // Make sure that the user is subscribed to both lists
  295. $subscriptions = $this->gdata->retrieveEmailLists($this->id);
  296. $this->assertEquals($listCount, count($subscriptions->entry));
  297. }
  298. public function testCanRetrievePageOfEmailLists() {
  299. // Create an email list
  300. $generatedListName = strtolower(uniqid('zf-list-'));
  301. $list = $this->gdata->createEmailList($generatedListName);
  302. $this->autoDelete($list);
  303. // Try retrieving the email list feed
  304. $feed = $this->gdata->retrievePageOfEmailLists();
  305. $this->assertTrue(count($feed->entry) > 0);
  306. }
  307. public function testCanRetrieveAllEmailLists() {
  308. // Create a couple of users to make sure we don't hit the limit
  309. // on the max number of email lists.
  310. for ($i = 0; $i < 3; $i++) {
  311. $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME, self::FAMILY_NAME,
  312. sha1(self::PASSWORD), self::PASSWORD_HASH);
  313. $this->autoDelete($user);
  314. }
  315. // Create a whole bunch of email lists to make sure we trigger
  316. // paging.
  317. for ($i = 0; $i < 30; $i++) {
  318. $generatedListName = strtolower(uniqid('zf-list-'));
  319. $list = $this->gdata->createEmailList($generatedListName);
  320. $this->autoDelete($list);
  321. }
  322. // Try retrieving the email list feed
  323. $feed = $this->gdata->retrieveAllEmailLists();
  324. $this->assertTrue(count($feed->entry) >= 30);
  325. }
  326. // Test the convenience delete method for email lists
  327. public function testCanDeleteEmailList() {
  328. // Create an email list
  329. $generatedListName = strtolower(uniqid('zf-list-'));
  330. $list = $this->gdata->createEmailList($generatedListName);
  331. $this->autoDelete($list);
  332. // Assert that the email list exists, just in case...
  333. $query = $this->gdata->newEmailListQuery();
  334. $query->setEmailListName($generatedListName);
  335. $entry = $this->gdata->getEmailListEntry($query);
  336. $this->assertNotNull($entry);
  337. // Delete nickname
  338. $this->gdata->deleteEmailList($generatedListName);
  339. // Ensure that nickname was deleted
  340. try {
  341. $query = $this->gdata->newEmailListQuery();
  342. $query->setEmailListName($generatedListName);
  343. $entry = $this->gdata->getEmailListEntry($query);
  344. // This souldn't execute
  345. $this->fail('Retrieving a non-existant email list entry didn\'t' .
  346. 'raise exception.');
  347. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  348. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  349. // Dummy assertion just to say we tested something here.
  350. $this->assertTrue(true);
  351. } else {
  352. // Exception thrown for an unexpected reason
  353. throw $e;
  354. }
  355. }
  356. }
  357. public function testCanRetrievePageOfRecipients() {
  358. // Create a new email list
  359. $generatedListName = strtolower(uniqid('zf-list-'));
  360. $list = $this->gdata->createEmailList($generatedListName);
  361. $this->autoDelete($list);
  362. // Create two users and assign them to the email list
  363. $userCount = 2;
  364. for ($i = 0; $i < $userCount; $i++) {
  365. $generatedUsername = uniqid('ZF-');
  366. $user = $this->gdata->createUser($generatedUsername,
  367. self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
  368. self::PASSWORD_HASH);
  369. $this->autoDelete($user);
  370. $this->gdata->addRecipientToEmailList($generatedUsername,
  371. $generatedListName);
  372. }
  373. // Retrieve recipients
  374. $recipientFeed =
  375. $this->gdata->retrievePageOfRecipients($generatedListName);
  376. $this->assertTrue(count($recipientFeed->entry) == $userCount);
  377. }
  378. public function testCanRetrievAllRecipients() {
  379. // Create a new email list
  380. $generatedListName = strtolower(uniqid('zf-list-'));
  381. $list = $this->gdata->createEmailList($generatedListName);
  382. $this->autoDelete($list);
  383. // Create enough users to trigger paging and assign them to the email
  384. // list
  385. $userCount = 30;
  386. for ($i = 0; $i < $userCount; $i++) {
  387. $generatedUsername = uniqid('ZF-');
  388. $user = $this->gdata->createUser($generatedUsername,
  389. self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
  390. self::PASSWORD_HASH);
  391. $this->autoDelete($user);
  392. $this->gdata->addRecipientToEmailList($generatedUsername,
  393. $generatedListName);
  394. }
  395. // Retrieve recipients
  396. $recipientFeed =
  397. $this->gdata->retrieveAllRecipients($generatedListName);
  398. $this->assertTrue(count($recipientFeed->entry) == $userCount);
  399. }
  400. // Test the convenience delete method for email list recipients
  401. public function testCanDeleteEmailListRecipient() {
  402. // Create an email list
  403. $generatedListName = strtolower(uniqid('zf-list-'));
  404. $list = $this->gdata->createEmailList($generatedListName);
  405. $this->autoDelete($list);
  406. // Create a user for the email list
  407. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  408. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  409. $this->autoDelete($user);
  410. $this->gdata->addRecipientToEmailList($this->id, $generatedListName);
  411. // Assert that the recipient exists, just in case...
  412. $recipients =
  413. $this->gdata->retrieveAllRecipients($generatedListName);
  414. $this->assertTrue(count($recipients->entry) == 1);
  415. // Remove the user from the list
  416. $this->gdata->removeRecipientFromEmailList($user->login->username,
  417. $generatedListName);
  418. // Ensure that user was deleted
  419. $recipients =
  420. $this->gdata->retrieveAllRecipients($generatedListName);
  421. $this->assertTrue(count($recipients->entry) == 0);
  422. }
  423. }