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