zf2 /tests/Zend/GData/GAppsOnlineTest.php

Language PHP Lines 845
MD5 Hash e9f9658efff1ab1eca839fb39bc1b77c
Repository https://github.com/mridgway/zf2.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_GData_GApps
 * @subpackage UnitTests
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id $
 */

/**
 * @namespace
 */
namespace ZendTest\GData;
use Zend\GData;
use Zend\GData\GApps;

/**
 * @category   Zend
 * @package    Zend_GData_GApps
 * @subpackage UnitTests
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @group      Zend_GData
 * @group      Zend_GData_GApps
 */
class GAppsOnlineTest extends \PHPUnit_Framework_TestCase
{

    const GIVEN_NAME = 'Zend_GData';
    const FAMILY_NAME = 'Automated Test Account';
    const PASSWORD = '4ohtladfl;';
    const PASSWORD_HASH = 'SHA-1';

    public function setUp()
    {
        if (!constant('TESTS_ZEND_GDATA_ONLINE_ENABLED')) {
            $this->markTestSkipped('Zend_GData online tests are not enabled');
        }
        
        if (!constant('TESTS_ZEND_GDATA_GAPPS_ONLINE_ENABLED')) {
            $this->markTestSkipped('GAppsOnlineTest is skipped');
        }
        
        
        $this->id = uniqid('ZF-');
        $username = constant('TESTS_ZEND_GDATA_GAPPS_EMAIL');
        $pass = constant('TESTS_ZEND_GDATA_GAPPS_PASSWORD');
        $this->domain = constant('TESTS_ZEND_GDATA_GAPPS_DOMAIN');
        $client = GData\ClientLogin::getHttpClient($username, $pass, GApps::AUTH_SERVICE_NAME);
        $this->gdata = new GApps($client, $this->domain);

        // Container to hold users and lists created during tests. All entries in
        // here will have delete() called during tear down.
        //
        // Failed deletions are okay, so add everying creatd in here, even if
        // you plan to delete the user yourself!
        $this->autoDeletePool = array();
    }

    public function tearDown()
    {
        // Delete all entries in $this->autoDeletePool.
        foreach ($this->autoDeletePool as $x) {
            try {
                $x->delete();
            } catch (\Exception $e) {
                // Failed deletes are okay. Try and delete the rest anyway.
            }
        }
    }

    // Schedule an entry for deletion at test tear-down.
    protected function autoDelete($entry) {
        $this->autoDeletePool[] = $entry;
    }

    // Test Create/Read/Update/Destroy operations on a UserEntry
    public function testUserCRUDOperations() {
        // Create a new user
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        // Verify that returned values are correct
        $this->assertEquals($this->id, $user->login->username);
        $this->assertEquals(self::GIVEN_NAME, $user->name->givenName);
        $this->assertEquals(self::FAMILY_NAME, $user->name->familyName);

        // Since we can't retrieve the password or hash function via the
        // API, let's see if a ClientLogin auth request succeeds
        try {
            GData\ClientLogin::getHttpClient($this->id . '@' .
                $this->domain, self::PASSWORD, 'xapi');
        } catch (\Zend\GData\App\AuthException $e) {
           $this->fail("Unable to authenticate new user via ClientLogin.");
        }

        // Check to make sure there are no extension elements/attributes
        // in the retrieved user
        $this->assertTrue(count($user->extensionElements) == 0);
        $this->assertTrue(count($user->extensionAttributes) == 0);

        // Try searching for the same user and make sure that they're returned
        $user2 = $this->gdata->retrieveUser($this->id);
        $this->assertEquals($user->saveXML(), $user2->saveXML());

        // Delete user (uses builtin delete method, convenience delete
        // method tested further down)
        $user->delete();

        // Ensure that user was deleted
        $deletedUser = $this->gdata->retrieveUser($this->id);
        $this->assertNull($deletedUser);
    }

    // Test to make sure that users with unicode characters can be created
    // okay.
    public function testUsersSupportUnicode() {
        // Create a user
        $user = $this->gdata->createUser($this->id, 'テスト', 'ユーザー',
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        // Make sure the user is the same as returned by the server
        $this->assertEquals('テスト', $user->name->givenName);
        $this->assertEquals('ユーザー', $user->name->familyName);
    }

    // Test to make sure that a page of users can be retrieved.
    public function testRetrievePageOfUsers() {
        $feed = $this->gdata->retrievePageOfUsers();
        $this->assertTrue(count($feed->entries) > 0);
    }

    // Test to make sure that a page of users can be retrieved with a
    // startUsername parameter.
    public function testRetrievePageOfUsersWithStartingUsername() {
        $feed = $this->gdata->retrievePageOfUsers();
        $this->assertTrue(count($feed->entries) > 0);
        $username = $feed->entries[0]->login->username;
        $feed = $this->gdata->retrievePageOfUsers($username);
        $this->assertTrue(count($feed->entries) > 0);
    }

    // Test to see if all users can be retrieved
    // NOTE: This test may timeout if the domain used for testing contains
    //       many pages of users.
    public function testRetrieveAllUsers() {
        // Create 35 users to make sure that there's more than one page.
        for ($i = 0; $i < 25; $i++) {
            $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME,
                self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
            $this->autoDelete($user);
        }

        $feed = $this->gdata->retrieveAllUsers();
        $this->assertTrue(count($feed->entry) > 0);
    }

    // Test to see if a user can be manually updated by calling updateUser().
    public function testManualUserEntryUpdate() {
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);
        $user->name->givenName = "Renamed";
        $user2 = $this->gdata->updateUser($this->id, $user);
        $this->assertEquals("Renamed", $user2->name->givenName);
    }

    // Test to see if a user can be suspended, then un-suspended
    public function testCanSuspendAndRestoreUser() {
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        $returned = $this->gdata->suspendUser($this->id);
        $user = $this->gdata->retrieveUser($this->id);
        $this->assertEquals(true, $user->login->suspended);
        $this->assertEquals($this->id, $returned->login->username);

        $returned = $this->gdata->restoreUser($this->id);
        $user = $this->gdata->retrieveUser($this->id);
        $this->assertEquals(false, $user->login->suspended);
        $this->assertEquals($this->id, $returned->login->username);
    }

    // Test the convenience delete method for users
    public function testCanDeleteUser() {
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        // Assert that the user exists, just in case...
        $rUser = $this->gdata->retrieveUser($this->id);
        $this->assertNotNull($rUser);

        // Delete user
        $this->gdata->deleteUser($this->id);

        // Ensure that user was deleted
        $rUser = $this->gdata->retrieveUser($this->id);
        $this->assertNull($rUser);
    }

    public function testNicknameCRUDOperations() {
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        // Create nickname
        // Apps will convert the nickname to lowercase on the server, so
        // we just make sure the generated nickname is lowercase here to start
        // to avoid confusion later on.
        $generatedNickname = strtolower(uniqid('zf-nick-'));
        $nickname = $this->gdata->createNickname($this->id, $generatedNickname);
        $this->assertEquals($generatedNickname, $nickname->nickname->name);
        $this->assertEquals($this->id, $nickname->login->username);

        // Retrieve nickname
        $nickname = $this->gdata->retrieveNickname($generatedNickname);
        $this->assertEquals($generatedNickname, $nickname->nickname->name);
        $this->assertEquals($this->id, $nickname->login->username);

        // Delete nickname (uses builtin delete method, convenience delete
        // method tested further down)
        $nickname->delete();

        // Ensure that nickname was deleted
        $nickname = $this->gdata->retrieveNickname($generatedNickname);
        $this->assertNull($nickname);
    }

    public function testRetrieveNicknames() {
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        // Create 5 nicknames
        for ($i = 0; $i < 5; $i++) {
            $generatedNickname[$i] = strtolower(uniqid('zf-nick-'));
            $this->gdata->createNickname($this->id, $generatedNickname[$i]);
        }

        // Retrieve all nicknames for the test user and see if they match
        $nicknameFeed = $this->gdata->retrieveNicknames($this->id);
        $this->assertEquals(count($generatedNickname), count($nicknameFeed->entry));
        foreach ($nicknameFeed as $nicknameEntry) {
            $searchResult = array_search($nicknameEntry->nickname->name,
                $generatedNickname);
            $this->assertNotSame(false, $searchResult);
            unset($generatedNickname[$searchResult]);
        }
        $this->assertEquals(0, count($generatedNickname));
    }

    public function testRetrievePageOfNicknames() {
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        // Create 5 nicknames
        for ($i = 0; $i < 5; $i++) {
            $generatedNickname[$i] = strtolower(uniqid('zf-nick-'));
            $this->gdata->createNickname($this->id, $generatedNickname[$i]);
        }

        // Test to make sure that we receive at least 5 nicknames back
        // from the server
        $results = $this->gdata->retrievePageOfNicknames();
        $this->assertTrue(count($results->entry) >= 5);
    }

    public function testRetrieveAllNicknames() {
        // Create 3 users, each with 10 nicknames
        for ($i = 0; $i < 3; $i++) {
            $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME,
                self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
            $this->autoDelete($user);
            for ($j = 0; $j < 10; $j++) {
                $generatedNickname = strtolower(uniqid('zf-nick-'));
                $this->gdata->createNickname($user->login->username, $generatedNickname);
            }
        }

        // Test to make sure that we receive at least 5 nicknames back
        // from the server
        $results = $this->gdata->retrieveAllNicknames();
        $this->assertTrue(count($results->entry) >= 30);
    }

    // Test the convenience delete method for nicknames
    public function testCanDeleteNickname() {
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);
        $generatedNickname = strtolower(uniqid('zf-nick-'));
        $this->gdata->createNickname($this->id, $generatedNickname);

        // Assert that the nickname exists, just in case...
        $rNick = $this->gdata->retrieveNickname($generatedNickname);
        $this->assertNotNull($rNick);

        // Delete nickname
        $this->gdata->deleteNickname($generatedNickname);

        // Ensure that nickname was deleted
        $rNick = $this->gdata->retrieveNickname($generatedNickname);
        $this->assertNull($rNick);
    }

    public function testGroupCRUDOperations() {
        // Create group
        $generatedGroupName = strtolower(uniqid('zf-group-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'zf-group-',
                'testGroupCRUDOperations()');
        $this->autoDelete($group);

        $groupId = null;
        $properties = $group->getProperty();
        foreach ($properties as $property) {
            if($property->name == 'groupId') {
                $groupId = $property->value;
            }
        }

        $this->assertEquals($generatedGroupName, $groupId);

        // Retrieve group
        $query = $this->gdata->newGroupQuery();
        $groupFeed = $this->gdata->getGroupFeed($query);
        $entryCount = count($groupFeed->entry);
        $this->assertTrue($entryCount > 0);

        // Delete group (uses builtin delete method, convenience delete
        // method tested further down)
        $group->delete();

        // Ensure that group was deleted
        $groupFeed = $this->gdata->getGroupFeed($query);
        $this->assertEquals($entryCount - 1, count($groupFeed->entry));

    }

    public function testCanAssignMultipleGroupsToOneUser() {
        // Create a user
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        // Create two groups
        $groupCount = 2;

        for ($i = 0; $i < $groupCount; $i++) {
            $generatedGroupName = strtolower(uniqid('zf-group-'));
            $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                    'testCanAssignMultipleGroupsToOneUser() ' . $i);
            $this->autoDelete($group);
            $this->gdata->addMemberToGroup($this->id, $generatedGroupName);
        }

        // Make sure that the user is subscribed to both groups
        $subscriptions = $this->gdata->retrieveGroups($this->id);
        $this->assertEquals($groupCount, count($subscriptions->entry));

    }

    public function testCanRetrievePageOfGroups() {
        // Create a group
        $generatedGroupName = strtolower(uniqid('zf-group-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testCanRetrievePageOfGroups()');
        $this->autoDelete($group);

        // Try retrieving the group feed
        $feed = $this->gdata->retrievePageOfGroups();
        $this->assertTrue(count($feed->entry) > 0);

    }

    public function testCanRetrieveAllGroups() {
        // Create a couple of users to make sure we don't hit the limit
        // on the max number of groups.
        for ($i = 0; $i < 3; $i++) {
            $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME, self::FAMILY_NAME,
                sha1(self::PASSWORD), self::PASSWORD_HASH);
            $this->autoDelete($user);
        }

        // Create a whole bunch of groups to make sure we trigger
        // paging.
        for ($i = 0; $i < 30; $i++) {
            $generatedGroupName = strtolower(uniqid('zf-group-'));
            $group = $this->gdata->createGroup($generatedGroupName, 'Test Group ' . $i,
                    'testCanRetrieveAllGroups()');
            $this->autoDelete($group);
        }

        // Try retrieving the group feed
        $feed = $this->gdata->retrieveAllGroups();
        $this->assertTrue(count($feed->entry) >= 30);

    }

    // Test the convenience delete method for groups
    public function testCanDeleteGroup() {  
        // Create a group
        $generatedGroupName = strtolower(uniqid('zf-group-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testCanDeleteGroup()');
        $this->autoDelete($group);

        // Assert that the group exists, just in case...
        $query = $this->gdata->newGroupQuery();
        $query->setGroupId($generatedGroupName);
        $entry = $this->gdata->getGroupEntry($query);
        $this->assertNotNull($entry);

        // Delete group
        $this->gdata->deleteGroup($generatedGroupName);

        // Ensure that group was deleted
        try {
            $query = $this->gdata->newGroupQuery();
            $query->setGroupId($generatedGroupName);
            $entry = $this->gdata->getGroupEntry($query);
            // This souldn't execute
            $this->fail('Retrieving a non-existant group entry didn\'t' .
                'raise exception.');
        } catch (Zend_Gdata_Gapps_ServiceException $e) {
            if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
                // Dummy assertion just to say we tested something here.
                $this->assertTrue(true);
            } else {
                // Exception thrown for an unexpected reason
                throw $e;
            }
        }

    }

    public function testCanRetrievePageOfMembers() {
        // Create a new group
        $generatedGroupName = strtolower(uniqid('zf-group-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testCanRetrievePageOfMembers()');
        $this->autoDelete($group);

        // Create two users and assign them to the group
        $userCount = 2;
        for ($i = 0; $i < $userCount; $i++) {
            $generatedUsername = uniqid('ZF-');
            $user = $this->gdata->createUser($generatedUsername,
                self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
                self::PASSWORD_HASH);
            $this->autoDelete($user);
            $this->gdata->addMemberToGroup($generatedUsername,
                $generatedGroupName);
        }

        // Retrieve members
        $memberFeed = $this->gdata->retrievePageOfMembers($generatedGroupName);
        $this->assertTrue(count($memberFeed->entry) == $userCount);

    }

    public function testCanRetrievAllMembers() {
        // Create a new group
        $generatedGroupName = strtolower(uniqid('zf-list-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testCanRetrievAllMembers()');
        $this->autoDelete($group);

        // Create enough users to trigger paging and assign them to the group
        $userCount = 30;
        for ($i = 0; $i < $userCount; $i++) {
            $generatedUsername = uniqid('ZF-');
            $user = $this->gdata->createUser($generatedUsername,
                self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
                self::PASSWORD_HASH);
            $this->autoDelete($user);
            $this->gdata->addMemberToGroup($generatedUsername, $generatedGroupName);
        }

        // Retrieve members
        $memberFeed = $this->gdata->retrieveAllMembers($generatedGroupName);
        $this->assertTrue(count($memberFeed->entry) == $userCount);

    }

    // Test the convenience removeMemberFromGroup method for group members
    public function testCanRemoveMemberFromGroup() {
        // Create a group
        $generatedGroupName = strtolower(uniqid('zf-list-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testCanDeleteGroupMember()');
        $this->autoDelete($group);

        // Create a user for the group
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);
        $this->gdata->addMemberToGroup($this->id, $generatedGroupName);

        // Assert that the member exists, just in case...
        $members = $this->gdata->retrieveAllMembers($generatedGroupName);
        $this->assertTrue(count($members->entry) == 1);

        // Remove the member from the group
        $this->gdata->removeMemberFromGroup($user->login->username,
            $generatedGroupName);

        // Ensure that user was deleted
        $members =  $this->gdata->retrieveAllMembers($generatedGroupName);
        $this->assertTrue(count($members->entry) == 0);

    }

    public function testCanRetrieveGroupOwners() {
        // Create a new group
        $generatedGroupName = strtolower(uniqid('zf-list-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testCanRetrievAllOwners()');
        $this->autoDelete($group);

        $userCount = 3;
        for ($i = 0; $i < $userCount; $i++) {
            $generatedUsername = uniqid('ZF-');
            $user = $this->gdata->createUser($generatedUsername,
                self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
                self::PASSWORD_HASH);
            $this->autoDelete($user);
            $this->gdata->addOwnerToGroup($generatedUsername,
                $generatedGroupName);
        }

        // Retrieve owners
        $ownerFeed = $this->gdata->retrieveGroupOwners($generatedGroupName);
        $this->assertTrue(count($ownerFeed->entry) == $userCount);

    }

    // Test the convenience removeOwnerFromGroup method for group owners
    public function testCanRemoveOwnerFromGroup() {
        // Create a group
        $generatedGroupName = strtolower(uniqid('zf-list-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testCanDeleteGroupOwner()');
        $this->autoDelete($group);

        // Create a user for the group
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);
        $this->gdata->addOwnerToGroup($this->id, $generatedGroupName);

        // Assert that the owner exists, just in case...
        $owners = $this->gdata->retrieveGroupOwners($generatedGroupName);
        $this->assertTrue(count($owners->entry) == 1);

        // Remove the owner from the group
        $this->gdata->removeOwnerFromGroup($user->login->username,
            $generatedGroupName);

        // Ensure that user was deleted
        $owners = $this->gdata->retrieveGroupOwners($generatedGroupName);
        $this->assertTrue(count($owners->entry) == 0);
    }

    // Test the convenience isMember method
    public function testIsMember() {
        // Create a group
        $generatedGroupName = strtolower(uniqid('zf-list-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testIsMember()');
        $this->autoDelete($group);

        // Create a user for the group
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);
        $this->gdata->addMemberToGroup($this->id, $generatedGroupName);

        $isMember = $this->gdata->isMember($this->id, $generatedGroupName);

        $this->assertTrue($isMember);

        $isMember = $this->gdata->isMember('foo_' . $this->id, $generatedGroupName);

        $this->assertFalse($isMember);

    }

    // Test the convenience isOwner method
    public function testIsOwner() {
        // Create a group
        $generatedGroupName = strtolower(uniqid('zf-list-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testIsMember()');
        $this->autoDelete($group);

        // Create a user for the group
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);
        $this->gdata->addOwnerToGroup($this->id, $generatedGroupName);

        $isOwner = $this->gdata->isOwner($this->id, $generatedGroupName);

        $this->assertTrue($isOwner);

        $isOwner = $this->gdata->isOwner('foo_' . $this->id, $generatedGroupName);

        $this->assertFalse($isOwner);

    }

    // Test the convenience updateGroup method
    public function testCanUpdateGroup() {
        // Create a group
        $generatedGroupName = strtolower(uniqid('zf-list-'));
        $group = $this->gdata->createGroup($generatedGroupName, 'Test Group',
                'testCanUpdateGroup()');
        $this->autoDelete($group);

        //set new value and save it

        $group = $this->gdata->updateGroup($generatedGroupName, null, 'new description here');

        //verify new value
        $description = null;

        $properties = $group->getProperty();
        foreach ($properties as $property) {
            if($property->name == 'description') {
                $description = $property->value;
            }
        }

        $this->assertEquals('new description here', $description);

    }
    
    public function testEmailListCRUDOperations() {
        // Create email list
        $generatedListName = strtolower(uniqid('zf-list-'));
        $list = $this->gdata->createEmailList($generatedListName);
        $this->autoDelete($list);
        $this->assertEquals($generatedListName, $list->emailList->name);

        // Retrieve email list
        $query = $this->gdata->newEmailListQuery();
        $listFeed = $this->gdata->getEmailListFeed($query);
        $entryCount = count($listFeed->entry);
        $this->assertTrue($entryCount > 0);

        // Delete email list (uses builtin delete method, convenience delete
        // method tested further down)
        $list->delete();

        // Ensure that nickname was deleted
        $listFeed = $this->gdata->getEmailListFeed($query);
        $this->assertEquals($entryCount - 1, count($listFeed->entry));
    }

    public function testCanAssignMultipleEmailListsToOneUser() {
        // Create a user
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
            sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);

        // Create two email lists
        $listCount = 2;

        for ($i = 0; $i < $listCount; $i++) {
            $generatedListName = strtolower(uniqid('zf-list-'));
            $list = $this->gdata->createEmailList($generatedListName);
            $this->autoDelete($list);
            $this->gdata->addRecipientToEmailList($this->id, $generatedListName);
        }

        // Make sure that the user is subscribed to both lists
        $subscriptions = $this->gdata->retrieveEmailLists($this->id);
        $this->assertEquals($listCount, count($subscriptions->entry));
    }

    public function testCanRetrievePageOfEmailLists() {
        // Create an email list
        $generatedListName = strtolower(uniqid('zf-list-'));
        $list = $this->gdata->createEmailList($generatedListName);
        $this->autoDelete($list);

        // Try retrieving the email list feed
        $feed = $this->gdata->retrievePageOfEmailLists();
        $this->assertTrue(count($feed->entry) > 0);
    }

    public function testCanRetrieveAllEmailLists() {
        // Create a couple of users to make sure we don't hit the limit
        // on the max number of email lists.
        for ($i = 0; $i < 3; $i++) {
            $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME, self::FAMILY_NAME,
                sha1(self::PASSWORD), self::PASSWORD_HASH);
            $this->autoDelete($user);
        }

        // Create a whole bunch of email lists to make sure we trigger
        // paging.
        for ($i = 0; $i < 30; $i++) {
            $generatedListName = strtolower(uniqid('zf-list-'));
            $list = $this->gdata->createEmailList($generatedListName);
            $this->autoDelete($list);
        }

        // Try retrieving the email list feed
        $feed = $this->gdata->retrieveAllEmailLists();
        $this->assertTrue(count($feed->entry) >= 30);
    }

    // Test the convenience delete method for email lists
    public function testCanDeleteEmailList() {
        // Create an email list
        $generatedListName = strtolower(uniqid('zf-list-'));
        $list = $this->gdata->createEmailList($generatedListName);
        $this->autoDelete($list);

        // Assert that the email list exists, just in case...
        $query = $this->gdata->newEmailListQuery();
        $query->setEmailListName($generatedListName);
        $entry = $this->gdata->getEmailListEntry($query);
        $this->assertNotNull($entry);

        // Delete nickname
        $this->gdata->deleteEmailList($generatedListName);

        // Ensure that nickname was deleted
        try {
            $query = $this->gdata->newEmailListQuery();
            $query->setEmailListName($generatedListName);
            $entry = $this->gdata->getEmailListEntry($query);
            // This souldn't execute
            $this->fail('Retrieving a non-existant email list entry didn\'t' .
                'raise exception.');
        } catch (GApps\ServiceException $e) {
            if ($e->hasError(GApps\Error::ENTITY_DOES_NOT_EXIST)) {
                // Dummy assertion just to say we tested something here.
                $this->assertTrue(true);
            } else {
                // Exception thrown for an unexpected reason
                throw $e;
            }
        }
    }

    public function testCanRetrievePageOfRecipients() {
        // Create a new email list
        $generatedListName = strtolower(uniqid('zf-list-'));
        $list = $this->gdata->createEmailList($generatedListName);
        $this->autoDelete($list);

        // Create two users and assign them to the email list
        $userCount = 2;
        for ($i = 0; $i < $userCount; $i++) {
            $generatedUsername = uniqid('ZF-');
            $user = $this->gdata->createUser($generatedUsername,
                self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
                self::PASSWORD_HASH);
            $this->autoDelete($user);
            $this->gdata->addRecipientToEmailList($generatedUsername,
                $generatedListName);
        }

        // Retrieve recipients
        $recipientFeed =
            $this->gdata->retrievePageOfRecipients($generatedListName);
        $this->assertTrue(count($recipientFeed->entry) == $userCount);
    }

    public function testCanRetrievAllRecipients() {
        // Create a new email list
        $generatedListName = strtolower(uniqid('zf-list-'));
        $list = $this->gdata->createEmailList($generatedListName);
        $this->autoDelete($list);

        // Create enough users to trigger paging and assign them to the email
        // list
        $userCount = 30;
        for ($i = 0; $i < $userCount; $i++) {
            $generatedUsername = uniqid('ZF-');
            $user = $this->gdata->createUser($generatedUsername,
                self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
                self::PASSWORD_HASH);
            $this->autoDelete($user);
            $this->gdata->addRecipientToEmailList($generatedUsername,
                $generatedListName);
        }

        // Retrieve recipients
        $recipientFeed =
            $this->gdata->retrieveAllRecipients($generatedListName);
        $this->assertTrue(count($recipientFeed->entry) == $userCount);
    }

    // Test the convenience delete method for email list recipients
    public function testCanDeleteEmailListRecipient() {
        // Create an email list
        $generatedListName = strtolower(uniqid('zf-list-'));
        $list = $this->gdata->createEmailList($generatedListName);
        $this->autoDelete($list);

        // Create a user for the email list
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
        $this->autoDelete($user);
        $this->gdata->addRecipientToEmailList($this->id, $generatedListName);

        // Assert that the recipient exists, just in case...
        $recipients =
            $this->gdata->retrieveAllRecipients($generatedListName);
        $this->assertTrue(count($recipients->entry) == 1);

        // Remove the user from the list
        $this->gdata->removeRecipientFromEmailList($user->login->username,
            $generatedListName);

        // Ensure that user was deleted
        $recipients =
            $this->gdata->retrieveAllRecipients($generatedListName);
        $this->assertTrue(count($recipients->entry) == 0);
    }

}
Back to Top