PageRenderTime 69ms CodeModel.GetById 18ms RepoModel.GetById 9ms app.codeStats 1ms

/tests/Zend/GData/GAppsOnlineTest.php

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