PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/google/tests/Zend/Gdata/GappsOnlineTest.php

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