PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/gapps/ZendGdata-1.10.8/tests/Zend/Gdata/GappsOnlineTest.php

https://github.com/broadinstitute/gdata-cli
PHP | 836 lines | 519 code | 143 blank | 174 comment | 37 complexity | 95134603f96c2e00bd326d4131dbbf14 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-2.1
  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_Gapps
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 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 dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  23. require_once 'Zend/Gdata/Gapps.php';
  24. require_once 'Zend/Gdata/Gapps/UserEntry.php';
  25. require_once 'Zend/Gdata/Gapps/UserQuery.php';
  26. require_once 'Zend/Gdata/ClientLogin.php';
  27. require_once 'Zend/Http/Client.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Gdata_Gapps
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Gdata
  35. * @group Zend_Gdata_Gapps
  36. */
  37. class Zend_Gdata_GappsOnlineTest extends PHPUnit_Framework_TestCase
  38. {
  39. const GIVEN_NAME = 'Zend_Gdata';
  40. const FAMILY_NAME = 'Automated Test Account';
  41. const PASSWORD = '4ohtladfl;';
  42. const PASSWORD_HASH = 'SHA-1';
  43. public function setUp()
  44. {
  45. $this->id = uniqid('ZF-');
  46. $username = constant('TESTS_ZEND_GDATA_GAPPS_EMAIL');
  47. $pass = constant('TESTS_ZEND_GDATA_GAPPS_PASSWORD');
  48. $this->domain = constant('TESTS_ZEND_GDATA_GAPPS_DOMAIN');
  49. $client = Zend_Gdata_ClientLogin::getHttpClient($username, $pass, Zend_Gdata_Gapps::AUTH_SERVICE_NAME);
  50. $this->gdata = new Zend_Gdata_Gapps($client, $this->domain);
  51. // Container to hold users and lists created during tests. All entries in
  52. // here will have delete() called during tear down.
  53. //
  54. // Failed deletions are okay, so add everying creatd in here, even if
  55. // you plan to delete the user yourself!
  56. $this->autoDeletePool = array();
  57. }
  58. public function tearDown()
  59. {
  60. // Delete all entries in $this->autoDeletePool.
  61. foreach ($this->autoDeletePool as $x) {
  62. try {
  63. $x->delete();
  64. } catch (Exception $e) {
  65. // Failed deletes are okay. Try and delete the rest anyway.
  66. }
  67. }
  68. }
  69. // Schedule an entry for deletion at test tear-down.
  70. protected function autoDelete($entry) {
  71. $this->autoDeletePool[] = $entry;
  72. }
  73. // Test Create/Read/Update/Destroy operations on a UserEntry
  74. public function testUserCRUDOperations() {
  75. // Create a new user
  76. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  77. sha1(self::PASSWORD), self::PASSWORD_HASH);
  78. $this->autoDelete($user);
  79. // Verify that returned values are correct
  80. $this->assertEquals($this->id, $user->login->username);
  81. $this->assertEquals(self::GIVEN_NAME, $user->name->givenName);
  82. $this->assertEquals(self::FAMILY_NAME, $user->name->familyName);
  83. // Since we can't retrieve the password or hash function via the
  84. // API, let's see if a ClientLogin auth request succeeds
  85. try {
  86. Zend_Gdata_ClientLogin::getHTTPClient($this->id . '@' .
  87. $this->domain, self::PASSWORD, 'xapi');
  88. } catch (Zend_Gdata_App_AuthException $e) {
  89. $this->fail("Unable to authenticate new user via ClientLogin.");
  90. }
  91. // Check to make sure there are no extension elements/attributes
  92. // in the retrieved user
  93. $this->assertTrue(count($user->extensionElements) == 0);
  94. $this->assertTrue(count($user->extensionAttributes) == 0);
  95. // Try searching for the same user and make sure that they're returned
  96. $user2 = $this->gdata->retrieveUser($this->id);
  97. $this->assertEquals($user->saveXML(), $user2->saveXML());
  98. // Delete user (uses builtin delete method, convenience delete
  99. // method tested further down)
  100. $user->delete();
  101. // Ensure that user was deleted
  102. $deletedUser = $this->gdata->retrieveUser($this->id);
  103. $this->assertNull($deletedUser);
  104. }
  105. // Test to make sure that users with unicode characters can be created
  106. // okay.
  107. public function testUsersSupportUnicode() {
  108. // Create a user
  109. $user = $this->gdata->createUser($this->id, 'テスト', 'ユーザー',
  110. sha1(self::PASSWORD), self::PASSWORD_HASH);
  111. $this->autoDelete($user);
  112. // Make sure the user is the same as returned by the server
  113. $this->assertEquals('テスト', $user->name->givenName);
  114. $this->assertEquals('ユーザー', $user->name->familyName);
  115. }
  116. // Test to make sure that a page of users can be retrieved.
  117. public function testRetrievePageOfUsers() {
  118. $feed = $this->gdata->retrievePageOfUsers();
  119. $this->assertTrue(count($feed->entries) > 0);
  120. }
  121. // Test to make sure that a page of users can be retrieved with a
  122. // startUsername parameter.
  123. public function testRetrievePageOfUsersWithStartingUsername() {
  124. $feed = $this->gdata->retrievePageOfUsers();
  125. $this->assertTrue(count($feed->entries) > 0);
  126. $username = $feed->entries[0]->login->username;
  127. $feed = $this->gdata->retrievePageOfUsers($username);
  128. $this->assertTrue(count($feed->entries) > 0);
  129. }
  130. // Test to see if all users can be retrieved
  131. // NOTE: This test may timeout if the domain used for testing contains
  132. // many pages of users.
  133. public function testRetrieveAllUsers() {
  134. // Create 35 users to make sure that there's more than one page.
  135. for ($i = 0; $i < 25; $i++) {
  136. $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME,
  137. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  138. $this->autoDelete($user);
  139. }
  140. $feed = $this->gdata->retrieveAllUsers();
  141. $this->assertTrue(count($feed->entry) > 0);
  142. }
  143. // Test to see if a user can be manually updated by calling updateUser().
  144. public function testManualUserEntryUpdate() {
  145. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  146. sha1(self::PASSWORD), self::PASSWORD_HASH);
  147. $this->autoDelete($user);
  148. $user->name->givenName = "Renamed";
  149. $user2 = $this->gdata->updateUser($this->id, $user);
  150. $this->assertEquals("Renamed", $user2->name->givenName);
  151. }
  152. // Test to see if a user can be suspended, then un-suspended
  153. public function testCanSuspendAndRestoreUser() {
  154. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  155. sha1(self::PASSWORD), self::PASSWORD_HASH);
  156. $this->autoDelete($user);
  157. $returned = $this->gdata->suspendUser($this->id);
  158. $user = $this->gdata->retrieveUser($this->id);
  159. $this->assertEquals(true, $user->login->suspended);
  160. $this->assertEquals($this->id, $returned->login->username);
  161. $returned = $this->gdata->restoreUser($this->id);
  162. $user = $this->gdata->retrieveUser($this->id);
  163. $this->assertEquals(false, $user->login->suspended);
  164. $this->assertEquals($this->id, $returned->login->username);
  165. }
  166. // Test the convenience delete method for users
  167. public function testCanDeleteUser() {
  168. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  169. sha1(self::PASSWORD), self::PASSWORD_HASH);
  170. $this->autoDelete($user);
  171. // Assert that the user exists, just in case...
  172. $rUser = $this->gdata->retrieveUser($this->id);
  173. $this->assertNotNull($rUser);
  174. // Delete user
  175. $this->gdata->deleteUser($this->id);
  176. // Ensure that user was deleted
  177. $rUser = $this->gdata->retrieveUser($this->id);
  178. $this->assertNull($rUser);
  179. }
  180. public function testNicknameCRUDOperations() {
  181. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  182. sha1(self::PASSWORD), self::PASSWORD_HASH);
  183. $this->autoDelete($user);
  184. // Create nickname
  185. // Apps will convert the nickname to lowercase on the server, so
  186. // we just make sure the generated nickname is lowercase here to start
  187. // to avoid confusion later on.
  188. $generatedNickname = strtolower(uniqid('zf-nick-'));
  189. $nickname = $this->gdata->createNickname($this->id, $generatedNickname);
  190. $this->assertEquals($generatedNickname, $nickname->nickname->name);
  191. $this->assertEquals($this->id, $nickname->login->username);
  192. // Retrieve nickname
  193. $nickname = $this->gdata->retrieveNickname($generatedNickname);
  194. $this->assertEquals($generatedNickname, $nickname->nickname->name);
  195. $this->assertEquals($this->id, $nickname->login->username);
  196. // Delete nickname (uses builtin delete method, convenience delete
  197. // method tested further down)
  198. $nickname->delete();
  199. // Ensure that nickname was deleted
  200. $nickname = $this->gdata->retrieveNickname($generatedNickname);
  201. $this->assertNull($nickname);
  202. }
  203. public function testRetrieveNicknames() {
  204. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  205. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  206. $this->autoDelete($user);
  207. // Create 5 nicknames
  208. for ($i = 0; $i < 5; $i++) {
  209. $generatedNickname[$i] = strtolower(uniqid('zf-nick-'));
  210. $this->gdata->createNickname($this->id, $generatedNickname[$i]);
  211. }
  212. // Retrieve all nicknames for the test user and see if they match
  213. $nicknameFeed = $this->gdata->retrieveNicknames($this->id);
  214. $this->assertEquals(count($generatedNickname), count($nicknameFeed->entry));
  215. foreach ($nicknameFeed as $nicknameEntry) {
  216. $searchResult = array_search($nicknameEntry->nickname->name,
  217. $generatedNickname);
  218. $this->assertNotSame(false, $searchResult);
  219. unset($generatedNickname[$searchResult]);
  220. }
  221. $this->assertEquals(0, count($generatedNickname));
  222. }
  223. public function testRetrievePageOfNicknames() {
  224. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  225. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  226. $this->autoDelete($user);
  227. // Create 5 nicknames
  228. for ($i = 0; $i < 5; $i++) {
  229. $generatedNickname[$i] = strtolower(uniqid('zf-nick-'));
  230. $this->gdata->createNickname($this->id, $generatedNickname[$i]);
  231. }
  232. // Test to make sure that we receive at least 5 nicknames back
  233. // from the server
  234. $results = $this->gdata->retrievePageOfNicknames();
  235. $this->assertTrue(count($results->entry) >= 5);
  236. }
  237. public function testRetrieveAllNicknames() {
  238. // Create 3 users, each with 10 nicknames
  239. for ($i = 0; $i < 3; $i++) {
  240. $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME,
  241. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  242. $this->autoDelete($user);
  243. for ($j = 0; $j < 10; $j++) {
  244. $generatedNickname = strtolower(uniqid('zf-nick-'));
  245. $this->gdata->createNickname($user->login->username, $generatedNickname);
  246. }
  247. }
  248. // Test to make sure that we receive at least 5 nicknames back
  249. // from the server
  250. $results = $this->gdata->retrieveAllNicknames();
  251. $this->assertTrue(count($results->entry) >= 30);
  252. }
  253. // Test the convenience delete method for nicknames
  254. public function testCanDeleteNickname() {
  255. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  256. sha1(self::PASSWORD), self::PASSWORD_HASH);
  257. $this->autoDelete($user);
  258. $generatedNickname = strtolower(uniqid('zf-nick-'));
  259. $this->gdata->createNickname($this->id, $generatedNickname);
  260. // Assert that the nickname exists, just in case...
  261. $rNick = $this->gdata->retrieveNickname($generatedNickname);
  262. $this->assertNotNull($rNick);
  263. // Delete nickname
  264. $this->gdata->deleteNickname($generatedNickname);
  265. // Ensure that nickname was deleted
  266. $rNick = $this->gdata->retrieveNickname($generatedNickname);
  267. $this->assertNull($rNick);
  268. }
  269. public function testGroupCRUDOperations() {
  270. // Create group
  271. $generatedGroupName = strtolower(uniqid('zf-group-'));
  272. $group = $this->gdata->createGroup($generatedGroupName, 'zf-group-',
  273. 'testGroupCRUDOperations()');
  274. $this->autoDelete($group);
  275. $groupId = null;
  276. $properties = $group->getProperty();
  277. foreach ($properties as $property) {
  278. if($property->name == 'groupId') {
  279. $groupId = $property->value;
  280. }
  281. }
  282. $this->assertEquals($generatedGroupName, $groupId);
  283. // Retrieve group
  284. $query = $this->gdata->newGroupQuery();
  285. $groupFeed = $this->gdata->getGroupFeed($query);
  286. $entryCount = count($groupFeed->entry);
  287. $this->assertTrue($entryCount > 0);
  288. // Delete group (uses builtin delete method, convenience delete
  289. // method tested further down)
  290. $group->delete();
  291. // Ensure that group was deleted
  292. $groupFeed = $this->gdata->getGroupFeed($query);
  293. $this->assertEquals($entryCount - 1, count($groupFeed->entry));
  294. }
  295. public function testCanAssignMultipleGroupsToOneUser() {
  296. // Create a user
  297. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  298. sha1(self::PASSWORD), self::PASSWORD_HASH);
  299. $this->autoDelete($user);
  300. // Create two groups
  301. $groupCount = 2;
  302. for ($i = 0; $i < $groupCount; $i++) {
  303. $generatedGroupName = strtolower(uniqid('zf-group-'));
  304. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  305. 'testCanAssignMultipleGroupsToOneUser() ' . $i);
  306. $this->autoDelete($group);
  307. $this->gdata->addMemberToGroup($this->id, $generatedGroupName);
  308. }
  309. // Make sure that the user is subscribed to both groups
  310. $subscriptions = $this->gdata->retrieveGroups($this->id);
  311. $this->assertEquals($groupCount, count($subscriptions->entry));
  312. }
  313. public function testCanRetrievePageOfGroups() {
  314. // Create a group
  315. $generatedGroupName = strtolower(uniqid('zf-group-'));
  316. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  317. 'testCanRetrievePageOfGroups()');
  318. $this->autoDelete($group);
  319. // Try retrieving the group feed
  320. $feed = $this->gdata->retrievePageOfGroups();
  321. $this->assertTrue(count($feed->entry) > 0);
  322. }
  323. public function testCanRetrieveAllGroups() {
  324. // Create a couple of users to make sure we don't hit the limit
  325. // on the max number of groups.
  326. for ($i = 0; $i < 3; $i++) {
  327. $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME, self::FAMILY_NAME,
  328. sha1(self::PASSWORD), self::PASSWORD_HASH);
  329. $this->autoDelete($user);
  330. }
  331. // Create a whole bunch of groups to make sure we trigger
  332. // paging.
  333. for ($i = 0; $i < 30; $i++) {
  334. $generatedGroupName = strtolower(uniqid('zf-group-'));
  335. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group ' . $i,
  336. 'testCanRetrieveAllGroups()');
  337. $this->autoDelete($group);
  338. }
  339. // Try retrieving the group feed
  340. $feed = $this->gdata->retrieveAllGroups();
  341. $this->assertTrue(count($feed->entry) >= 30);
  342. }
  343. // Test the convenience delete method for groups
  344. public function testCanDeleteGroup() {
  345. // Create a group
  346. $generatedGroupName = strtolower(uniqid('zf-group-'));
  347. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  348. 'testCanDeleteGroup()');
  349. $this->autoDelete($group);
  350. // Assert that the group exists, just in case...
  351. $query = $this->gdata->newGroupQuery();
  352. $query->setGroupId($generatedGroupName);
  353. $entry = $this->gdata->getGroupEntry($query);
  354. $this->assertNotNull($entry);
  355. // Delete group
  356. $this->gdata->deleteGroup($generatedGroupName);
  357. // Ensure that group was deleted
  358. try {
  359. $query = $this->gdata->newGroupQuery();
  360. $query->setGroupId($generatedGroupName);
  361. $entry = $this->gdata->getGroupEntry($query);
  362. // This souldn't execute
  363. $this->fail('Retrieving a non-existant group entry didn\'t' .
  364. 'raise exception.');
  365. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  366. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  367. // Dummy assertion just to say we tested something here.
  368. $this->assertTrue(true);
  369. } else {
  370. // Exception thrown for an unexpected reason
  371. throw $e;
  372. }
  373. }
  374. }
  375. public function testCanRetrievePageOfMembers() {
  376. // Create a new group
  377. $generatedGroupName = strtolower(uniqid('zf-group-'));
  378. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  379. 'testCanRetrievePageOfMembers()');
  380. $this->autoDelete($group);
  381. // Create two users and assign them to the group
  382. $userCount = 2;
  383. for ($i = 0; $i < $userCount; $i++) {
  384. $generatedUsername = uniqid('ZF-');
  385. $user = $this->gdata->createUser($generatedUsername,
  386. self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
  387. self::PASSWORD_HASH);
  388. $this->autoDelete($user);
  389. $this->gdata->addMemberToGroup($generatedUsername,
  390. $generatedGroupName);
  391. }
  392. // Retrieve members
  393. $memberFeed = $this->gdata->retrievePageOfMembers($generatedGroupName);
  394. $this->assertTrue(count($memberFeed->entry) == $userCount);
  395. }
  396. public function testCanRetrievAllMembers() {
  397. // Create a new group
  398. $generatedGroupName = strtolower(uniqid('zf-list-'));
  399. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  400. 'testCanRetrievAllMembers()');
  401. $this->autoDelete($group);
  402. // Create enough users to trigger paging and assign them to the group
  403. $userCount = 30;
  404. for ($i = 0; $i < $userCount; $i++) {
  405. $generatedUsername = uniqid('ZF-');
  406. $user = $this->gdata->createUser($generatedUsername,
  407. self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
  408. self::PASSWORD_HASH);
  409. $this->autoDelete($user);
  410. $this->gdata->addMemberToGroup($generatedUsername, $generatedGroupName);
  411. }
  412. // Retrieve members
  413. $memberFeed = $this->gdata->retrieveAllMembers($generatedGroupName);
  414. $this->assertTrue(count($memberFeed->entry) == $userCount);
  415. }
  416. // Test the convenience removeMemberFromGroup method for group members
  417. public function testCanRemoveMemberFromGroup() {
  418. // Create a group
  419. $generatedGroupName = strtolower(uniqid('zf-list-'));
  420. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  421. 'testCanDeleteGroupMember()');
  422. $this->autoDelete($group);
  423. // Create a user for the group
  424. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  425. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  426. $this->autoDelete($user);
  427. $this->gdata->addMemberToGroup($this->id, $generatedGroupName);
  428. // Assert that the member exists, just in case...
  429. $members = $this->gdata->retrieveAllMembers($generatedGroupName);
  430. $this->assertTrue(count($members->entry) == 1);
  431. // Remove the member from the group
  432. $this->gdata->removeMemberFromGroup($user->login->username,
  433. $generatedGroupName);
  434. // Ensure that user was deleted
  435. $members = $this->gdata->retrieveAllMembers($generatedGroupName);
  436. $this->assertTrue(count($members->entry) == 0);
  437. }
  438. public function testCanRetrieveGroupOwners() {
  439. // Create a new group
  440. $generatedGroupName = strtolower(uniqid('zf-list-'));
  441. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  442. 'testCanRetrievAllOwners()');
  443. $this->autoDelete($group);
  444. $userCount = 3;
  445. for ($i = 0; $i < $userCount; $i++) {
  446. $generatedUsername = uniqid('ZF-');
  447. $user = $this->gdata->createUser($generatedUsername,
  448. self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
  449. self::PASSWORD_HASH);
  450. $this->autoDelete($user);
  451. $this->gdata->addOwnerToGroup($generatedUsername,
  452. $generatedGroupName);
  453. }
  454. // Retrieve owners
  455. $ownerFeed = $this->gdata->retrieveGroupOwners($generatedGroupName);
  456. $this->assertTrue(count($ownerFeed->entry) == $userCount);
  457. }
  458. // Test the convenience removeOwnerFromGroup method for group owners
  459. public function testCanRemoveOwnerFromGroup() {
  460. // Create a group
  461. $generatedGroupName = strtolower(uniqid('zf-list-'));
  462. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  463. 'testCanDeleteGroupOwner()');
  464. $this->autoDelete($group);
  465. // Create a user for the group
  466. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  467. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  468. $this->autoDelete($user);
  469. $this->gdata->addOwnerToGroup($this->id, $generatedGroupName);
  470. // Assert that the owner exists, just in case...
  471. $owners = $this->gdata->retrieveGroupOwners($generatedGroupName);
  472. $this->assertTrue(count($owners->entry) == 1);
  473. // Remove the owner from the group
  474. $this->gdata->removeOwnerFromGroup($user->login->username,
  475. $generatedGroupName);
  476. // Ensure that user was deleted
  477. $owners = $this->gdata->retrieveGroupOwners($generatedGroupName);
  478. $this->assertTrue(count($owners->entry) == 0);
  479. }
  480. // Test the convenience isMember method
  481. public function testIsMember() {
  482. // Create a group
  483. $generatedGroupName = strtolower(uniqid('zf-list-'));
  484. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  485. 'testIsMember()');
  486. $this->autoDelete($group);
  487. // Create a user for the group
  488. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  489. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  490. $this->autoDelete($user);
  491. $this->gdata->addMemberToGroup($this->id, $generatedGroupName);
  492. $isMember = $this->gdata->isMember($this->id, $generatedGroupName);
  493. $this->assertTrue($isMember);
  494. $isMember = $this->gdata->isMember('foo_' . $this->id, $generatedGroupName);
  495. $this->assertFalse($isMember);
  496. }
  497. // Test the convenience isOwner method
  498. public function testIsOwner() {
  499. // Create a group
  500. $generatedGroupName = strtolower(uniqid('zf-list-'));
  501. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  502. 'testIsMember()');
  503. $this->autoDelete($group);
  504. // Create a user for the group
  505. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  506. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  507. $this->autoDelete($user);
  508. $this->gdata->addOwnerToGroup($this->id, $generatedGroupName);
  509. $isOwner = $this->gdata->isOwner($this->id, $generatedGroupName);
  510. $this->assertTrue($isOwner);
  511. $isOwner = $this->gdata->isOwner('foo_' . $this->id, $generatedGroupName);
  512. $this->assertFalse($isOwner);
  513. }
  514. // Test the convenience updateGroup method
  515. public function testCanUpdateGroup() {
  516. // Create a group
  517. $generatedGroupName = strtolower(uniqid('zf-list-'));
  518. $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
  519. 'testCanUpdateGroup()');
  520. $this->autoDelete($group);
  521. //set new value and save it
  522. $group = $this->gdata->updateGroup($generatedGroupName, null, 'new description here');
  523. //verify new value
  524. $description = null;
  525. $properties = $group->getProperty();
  526. foreach ($properties as $property) {
  527. if($property->name == 'description') {
  528. $description = $property->value;
  529. }
  530. }
  531. $this->assertEquals('new description here', $description);
  532. }
  533. public function testEmailListCRUDOperations() {
  534. // Create email list
  535. $generatedListName = strtolower(uniqid('zf-list-'));
  536. $list = $this->gdata->createEmailList($generatedListName);
  537. $this->autoDelete($list);
  538. $this->assertEquals($generatedListName, $list->emailList->name);
  539. // Retrieve email list
  540. $query = $this->gdata->newEmailListQuery();
  541. $listFeed = $this->gdata->getEmailListFeed($query);
  542. $entryCount = count($listFeed->entry);
  543. $this->assertTrue($entryCount > 0);
  544. // Delete email list (uses builtin delete method, convenience delete
  545. // method tested further down)
  546. $list->delete();
  547. // Ensure that nickname was deleted
  548. $listFeed = $this->gdata->getEmailListFeed($query);
  549. $this->assertEquals($entryCount - 1, count($listFeed->entry));
  550. }
  551. public function testCanAssignMultipleEmailListsToOneUser() {
  552. // Create a user
  553. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
  554. sha1(self::PASSWORD), self::PASSWORD_HASH);
  555. $this->autoDelete($user);
  556. // Create two email lists
  557. $listCount = 2;
  558. for ($i = 0; $i < $listCount; $i++) {
  559. $generatedListName = strtolower(uniqid('zf-list-'));
  560. $list = $this->gdata->createEmailList($generatedListName);
  561. $this->autoDelete($list);
  562. $this->gdata->addRecipientToEmailList($this->id, $generatedListName);
  563. }
  564. // Make sure that the user is subscribed to both lists
  565. $subscriptions = $this->gdata->retrieveEmailLists($this->id);
  566. $this->assertEquals($listCount, count($subscriptions->entry));
  567. }
  568. public function testCanRetrievePageOfEmailLists() {
  569. // Create an email list
  570. $generatedListName = strtolower(uniqid('zf-list-'));
  571. $list = $this->gdata->createEmailList($generatedListName);
  572. $this->autoDelete($list);
  573. // Try retrieving the email list feed
  574. $feed = $this->gdata->retrievePageOfEmailLists();
  575. $this->assertTrue(count($feed->entry) > 0);
  576. }
  577. public function testCanRetrieveAllEmailLists() {
  578. // Create a couple of users to make sure we don't hit the limit
  579. // on the max number of email lists.
  580. for ($i = 0; $i < 3; $i++) {
  581. $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME, self::FAMILY_NAME,
  582. sha1(self::PASSWORD), self::PASSWORD_HASH);
  583. $this->autoDelete($user);
  584. }
  585. // Create a whole bunch of email lists to make sure we trigger
  586. // paging.
  587. for ($i = 0; $i < 30; $i++) {
  588. $generatedListName = strtolower(uniqid('zf-list-'));
  589. $list = $this->gdata->createEmailList($generatedListName);
  590. $this->autoDelete($list);
  591. }
  592. // Try retrieving the email list feed
  593. $feed = $this->gdata->retrieveAllEmailLists();
  594. $this->assertTrue(count($feed->entry) >= 30);
  595. }
  596. // Test the convenience delete method for email lists
  597. public function testCanDeleteEmailList() {
  598. // Create an email list
  599. $generatedListName = strtolower(uniqid('zf-list-'));
  600. $list = $this->gdata->createEmailList($generatedListName);
  601. $this->autoDelete($list);
  602. // Assert that the email list exists, just in case...
  603. $query = $this->gdata->newEmailListQuery();
  604. $query->setEmailListName($generatedListName);
  605. $entry = $this->gdata->getEmailListEntry($query);
  606. $this->assertNotNull($entry);
  607. // Delete nickname
  608. $this->gdata->deleteEmailList($generatedListName);
  609. // Ensure that nickname was deleted
  610. try {
  611. $query = $this->gdata->newEmailListQuery();
  612. $query->setEmailListName($generatedListName);
  613. $entry = $this->gdata->getEmailListEntry($query);
  614. // This souldn't execute
  615. $this->fail('Retrieving a non-existant email list entry didn\'t' .
  616. 'raise exception.');
  617. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  618. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  619. // Dummy assertion just to say we tested something here.
  620. $this->assertTrue(true);
  621. } else {
  622. // Exception thrown for an unexpected reason
  623. throw $e;
  624. }
  625. }
  626. }
  627. public function testCanRetrievePageOfRecipients() {
  628. // Create a new email list
  629. $generatedListName = strtolower(uniqid('zf-list-'));
  630. $list = $this->gdata->createEmailList($generatedListName);
  631. $this->autoDelete($list);
  632. // Create two users and assign them to the email list
  633. $userCount = 2;
  634. for ($i = 0; $i < $userCount; $i++) {
  635. $generatedUsername = uniqid('ZF-');
  636. $user = $this->gdata->createUser($generatedUsername,
  637. self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
  638. self::PASSWORD_HASH);
  639. $this->autoDelete($user);
  640. $this->gdata->addRecipientToEmailList($generatedUsername,
  641. $generatedListName);
  642. }
  643. // Retrieve recipients
  644. $recipientFeed =
  645. $this->gdata->retrievePageOfRecipients($generatedListName);
  646. $this->assertTrue(count($recipientFeed->entry) == $userCount);
  647. }
  648. public function testCanRetrievAllRecipients() {
  649. // Create a new email list
  650. $generatedListName = strtolower(uniqid('zf-list-'));
  651. $list = $this->gdata->createEmailList($generatedListName);
  652. $this->autoDelete($list);
  653. // Create enough users to trigger paging and assign them to the email
  654. // list
  655. $userCount = 30;
  656. for ($i = 0; $i < $userCount; $i++) {
  657. $generatedUsername = uniqid('ZF-');
  658. $user = $this->gdata->createUser($generatedUsername,
  659. self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
  660. self::PASSWORD_HASH);
  661. $this->autoDelete($user);
  662. $this->gdata->addRecipientToEmailList($generatedUsername,
  663. $generatedListName);
  664. }
  665. // Retrieve recipients
  666. $recipientFeed =
  667. $this->gdata->retrieveAllRecipients($generatedListName);
  668. $this->assertTrue(count($recipientFeed->entry) == $userCount);
  669. }
  670. // Test the convenience delete method for email list recipients
  671. public function testCanDeleteEmailListRecipient() {
  672. // Create an email list
  673. $generatedListName = strtolower(uniqid('zf-list-'));
  674. $list = $this->gdata->createEmailList($generatedListName);
  675. $this->autoDelete($list);
  676. // Create a user for the email list
  677. $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
  678. self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
  679. $this->autoDelete($user);
  680. $this->gdata->addRecipientToEmailList($this->id, $generatedListName);
  681. // Assert that the recipient exists, just in case...
  682. $recipients =
  683. $this->gdata->retrieveAllRecipients($generatedListName);
  684. $this->assertTrue(count($recipients->entry) == 1);
  685. // Remove the user from the list
  686. $this->gdata->removeRecipientFromEmailList($user->login->username,
  687. $generatedListName);
  688. // Ensure that user was deleted
  689. $recipients =
  690. $this->gdata->retrieveAllRecipients($generatedListName);
  691. $this->assertTrue(count($recipients->entry) == 0);
  692. }
  693. }