~clinton-collins/familyproject/trunk

« back to all changes in this revision

Viewing changes to ZendFramework/tests/Zend/Gdata/GappsOnlineTest.php

  • Committer: Clinton Collins
  • Date: 2009-06-26 19:54:58 UTC
  • Revision ID: clinton.collins@gmail.com-20090626195458-5ebba0qcvo15xlpy
Initial Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Zend Framework
 
5
 *
 
6
 * LICENSE
 
7
 *
 
8
 * This source file is subject to the new BSD license that is bundled
 
9
 * with this package in the file LICENSE.txt.
 
10
 * It is also available through the world-wide-web at this URL:
 
11
 * http://framework.zend.com/license/new-bsd
 
12
 * If you did not receive a copy of the license and are unable to
 
13
 * obtain it through the world-wide-web, please send an email
 
14
 * to license@zend.com so we can send you a copy immediately.
 
15
 *
 
16
 * @category   Zend
 
17
 * @package    Zend_Gdata
 
18
 * @subpackage UnitTests
 
19
 * @copyright  Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
 
20
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
21
 */
 
22
 
 
23
require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
 
24
 
 
25
require_once 'Zend/Gdata/Gapps.php';
 
26
require_once 'Zend/Gdata/Gapps/UserEntry.php';
 
27
require_once 'Zend/Gdata/Gapps/UserQuery.php';
 
28
require_once 'Zend/Gdata/ClientLogin.php';
 
29
require_once 'Zend/Http/Client.php';
 
30
 
 
31
/**
 
32
 * @package Zend_Gdata
 
33
 * @subpackage UnitTests
 
34
 */
 
35
class Zend_Gdata_GappsOnlineTest extends PHPUnit_Framework_TestCase
 
36
{
 
37
 
 
38
    const GIVEN_NAME = 'Zend_Gdata';
 
39
    const FAMILY_NAME = 'Automated Test Account';
 
40
    const PASSWORD = '4ohtladfl;';
 
41
    const PASSWORD_HASH = 'SHA-1';
 
42
 
 
43
    public function setUp()
 
44
    {
 
45
        $this->id = uniqid('ZF-');
 
46
        $username = constant('TESTS_ZEND_GDATA_GAPPS_EMAIL');
 
47
        $pass = constant('TESTS_ZEND_GDATA_GAPPS_PASSWORD');
 
48
        $this->domain = constant('TESTS_ZEND_GDATA_GAPPS_DOMAIN');
 
49
        $client = Zend_Gdata_ClientLogin::getHttpClient($username, $pass, Zend_Gdata_Gapps::AUTH_SERVICE_NAME);
 
50
        $this->gdata = new Zend_Gdata_Gapps($client, $this->domain);
 
51
 
 
52
        // Container to hold users and lists created during tests. All entries in
 
53
        // here will have delete() called during tear down.
 
54
        //
 
55
        // Failed deletions are okay, so add everying creatd in here, even if
 
56
        // you plan to delete the user yourself!
 
57
        $this->autoDeletePool = array();
 
58
    }
 
59
 
 
60
    public function tearDown()
 
61
    {
 
62
        // Delete all entries in $this->autoDeletePool.
 
63
        foreach ($this->autoDeletePool as $x) {
 
64
            try {
 
65
                $x->delete();
 
66
            } catch (Exception $e) {
 
67
                // Failed deletes are okay. Try and delete the rest anyway.
 
68
            }
 
69
        }
 
70
    }
 
71
 
 
72
    // Schedule an entry for deletion at test tear-down.
 
73
    protected function autoDelete($entry) {
 
74
        $this->autoDeletePool[] = $entry;
 
75
    }
 
76
 
 
77
    // Test Create/Read/Update/Destroy operations on a UserEntry
 
78
    public function testUserCRUDOperations() {
 
79
        // Create a new user
 
80
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
 
81
            sha1(self::PASSWORD), self::PASSWORD_HASH);
 
82
        $this->autoDelete($user);
 
83
 
 
84
        // Verify that returned values are correct
 
85
        $this->assertEquals($this->id, $user->login->username);
 
86
        $this->assertEquals(self::GIVEN_NAME, $user->name->givenName);
 
87
        $this->assertEquals(self::FAMILY_NAME, $user->name->familyName);
 
88
 
 
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
            Zend_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
 
 
98
        // Check to make sure there are no extension elements/attributes
 
99
        // in the retrieved user
 
100
        $this->assertTrue(count($user->extensionElements) == 0);
 
101
        $this->assertTrue(count($user->extensionAttributes) == 0);
 
102
 
 
103
        // Try searching for the same user and make sure that they're returned
 
104
        $user2 = $this->gdata->retrieveUser($this->id);
 
105
        $this->assertEquals($user->saveXML(), $user2->saveXML());
 
106
 
 
107
        // Delete user (uses builtin delete method, convenience delete
 
108
        // method tested further down)
 
109
        $user->delete();
 
110
 
 
111
        // Ensure that user was deleted
 
112
        $deletedUser = $this->gdata->retrieveUser($this->id);
 
113
        $this->assertNull($deletedUser);
 
114
    }
 
115
 
 
116
    // Test to make sure that users with unicode characters can be created 
 
117
    // okay.
 
118
    public function testUsersSupportUnicode() {
 
119
        // Create a user
 
120
        $user = $this->gdata->createUser($this->id, 'テスト', 'ユーザー',
 
121
            sha1(self::PASSWORD), self::PASSWORD_HASH);
 
122
        $this->autoDelete($user);
 
123
        
 
124
        // Make sure the user is the same as returned by the server
 
125
        $this->assertEquals('テスト', $user->name->givenName);
 
126
        $this->assertEquals('ユーザー', $user->name->familyName);
 
127
    }
 
128
 
 
129
    // Test to make sure that a page of users can be retrieved.
 
130
    public function testRetrievePageOfUsers() {
 
131
        $feed = $this->gdata->retrievePageOfUsers();
 
132
        $this->assertTrue(count($feed->entries) > 0);
 
133
    }
 
134
 
 
135
    // Test to make sure that a page of users can be retrieved with a
 
136
    // startUsername parameter.
 
137
    public function testRetrievePageOfUsersWithStartingUsername() {
 
138
        $feed = $this->gdata->retrievePageOfUsers();
 
139
        $this->assertTrue(count($feed->entries) > 0);
 
140
        $username = $feed->entries[0]->login->username;
 
141
        $feed = $this->gdata->retrievePageOfUsers($username);
 
142
        $this->assertTrue(count($feed->entries) > 0);
 
143
    }
 
144
 
 
145
    // Test to see if all users can be retrieved
 
146
    // NOTE: This test may timeout if the domain used for testing contains
 
147
    //       many pages of users.
 
148
    public function testRetrieveAllUsers() {
 
149
        // Create 35 users to make sure that there's more than one page.
 
150
        for ($i = 0; $i < 25; $i++) {
 
151
            $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME,
 
152
                self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
 
153
            $this->autoDelete($user);
 
154
        }
 
155
 
 
156
        $feed = $this->gdata->retrieveAllUsers();
 
157
        $this->assertTrue(count($feed->entry) > 0);
 
158
    }
 
159
 
 
160
    // Test to see if a user can be manually updated by calling updateUser().
 
161
    public function testManualUserEntryUpdate() {
 
162
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
 
163
            sha1(self::PASSWORD), self::PASSWORD_HASH);
 
164
        $this->autoDelete($user);
 
165
        $user->name->givenName = "Renamed";
 
166
        $user2 = $this->gdata->updateUser($this->id, $user);
 
167
        $this->assertEquals("Renamed", $user2->name->givenName);
 
168
    }
 
169
 
 
170
    // Test to see if a user can be suspended, then un-suspended
 
171
    public function testCanSuspendAndRestoreUser() {
 
172
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
 
173
            sha1(self::PASSWORD), self::PASSWORD_HASH);
 
174
        $this->autoDelete($user);
 
175
 
 
176
        $returned = $this->gdata->suspendUser($this->id);
 
177
        $user = $this->gdata->retrieveUser($this->id);
 
178
        $this->assertEquals(true, $user->login->suspended);
 
179
        $this->assertEquals($this->id, $returned->login->username);
 
180
 
 
181
        $returned = $this->gdata->restoreUser($this->id);
 
182
        $user = $this->gdata->retrieveUser($this->id);
 
183
        $this->assertEquals(false, $user->login->suspended);
 
184
        $this->assertEquals($this->id, $returned->login->username);
 
185
    }
 
186
 
 
187
    // Test the convenience delete method for users
 
188
    public function testCanDeleteUser() {
 
189
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
 
190
            sha1(self::PASSWORD), self::PASSWORD_HASH);
 
191
        $this->autoDelete($user);
 
192
 
 
193
        // Assert that the user exists, just in case...
 
194
        $rUser = $this->gdata->retrieveUser($this->id);
 
195
        $this->assertNotNull($rUser);
 
196
 
 
197
        // Delete user
 
198
        $this->gdata->deleteUser($this->id);
 
199
 
 
200
        // Ensure that user was deleted
 
201
        $rUser = $this->gdata->retrieveUser($this->id);
 
202
        $this->assertNull($rUser);
 
203
    }
 
204
 
 
205
    public function testNicknameCRUDOperations() {
 
206
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
 
207
            sha1(self::PASSWORD), self::PASSWORD_HASH);
 
208
        $this->autoDelete($user);
 
209
 
 
210
        // Create nickname
 
211
        // Apps will convert the nickname to lowercase on the server, so
 
212
        // we just make sure the generated nickname is lowercase here to start
 
213
        // to avoid confusion later on.
 
214
        $generatedNickname = strtolower(uniqid('zf-nick-'));
 
215
        $nickname = $this->gdata->createNickname($this->id, $generatedNickname);
 
216
        $this->assertEquals($generatedNickname, $nickname->nickname->name);
 
217
        $this->assertEquals($this->id, $nickname->login->username);
 
218
 
 
219
        // Retrieve nickname
 
220
        $nickname = $this->gdata->retrieveNickname($generatedNickname);
 
221
        $this->assertEquals($generatedNickname, $nickname->nickname->name);
 
222
        $this->assertEquals($this->id, $nickname->login->username);
 
223
 
 
224
        // Delete nickname (uses builtin delete method, convenience delete
 
225
        // method tested further down)
 
226
        $nickname->delete();
 
227
 
 
228
        // Ensure that nickname was deleted
 
229
        $nickname = $this->gdata->retrieveNickname($generatedNickname);
 
230
        $this->assertNull($nickname);
 
231
    }
 
232
 
 
233
    public function testRetrieveNicknames() {
 
234
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
 
235
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
 
236
        $this->autoDelete($user);
 
237
 
 
238
        // Create 5 nicknames
 
239
        for ($i = 0; $i < 5; $i++) {
 
240
            $generatedNickname[$i] = strtolower(uniqid('zf-nick-'));
 
241
            $this->gdata->createNickname($this->id, $generatedNickname[$i]);
 
242
        }
 
243
 
 
244
        // Retrieve all nicknames for the test user and see if they match
 
245
        $nicknameFeed = $this->gdata->retrieveNicknames($this->id);
 
246
        $this->assertEquals(count($generatedNickname), count($nicknameFeed->entry));
 
247
        foreach ($nicknameFeed as $nicknameEntry) {
 
248
            $searchResult = array_search($nicknameEntry->nickname->name,
 
249
                $generatedNickname);
 
250
            $this->assertNotSame(false, $searchResult);
 
251
            unset($generatedNickname[$searchResult]);
 
252
        }
 
253
        $this->assertEquals(0, count($generatedNickname));
 
254
    }
 
255
 
 
256
    public function testRetrievePageOfNicknames() {
 
257
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
 
258
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
 
259
        $this->autoDelete($user);
 
260
 
 
261
        // Create 5 nicknames
 
262
        for ($i = 0; $i < 5; $i++) {
 
263
            $generatedNickname[$i] = strtolower(uniqid('zf-nick-'));
 
264
            $this->gdata->createNickname($this->id, $generatedNickname[$i]);
 
265
        }
 
266
 
 
267
        // Test to make sure that we receive at least 5 nicknames back
 
268
        // from the server
 
269
        $results = $this->gdata->retrievePageOfNicknames();
 
270
        $this->assertTrue(count($results->entry) >= 5);
 
271
    }
 
272
 
 
273
    public function testRetrieveAllNicknames() {
 
274
        // Create 3 users, each with 10 nicknames
 
275
        for ($i = 0; $i < 3; $i++) {
 
276
            $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME,
 
277
                self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
 
278
            $this->autoDelete($user);
 
279
            for ($j = 0; $j < 10; $j++) {
 
280
                $generatedNickname = strtolower(uniqid('zf-nick-'));
 
281
                $this->gdata->createNickname($user->login->username, $generatedNickname);
 
282
            }
 
283
        }
 
284
 
 
285
        // Test to make sure that we receive at least 5 nicknames back
 
286
        // from the server
 
287
        $results = $this->gdata->retrieveAllNicknames();
 
288
        $this->assertTrue(count($results->entry) >= 30);
 
289
    }
 
290
 
 
291
    // Test the convenience delete method for nicknames
 
292
    public function testCanDeleteNickname() {
 
293
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
 
294
            sha1(self::PASSWORD), self::PASSWORD_HASH);
 
295
        $this->autoDelete($user);
 
296
        $generatedNickname = strtolower(uniqid('zf-nick-'));
 
297
        $this->gdata->createNickname($this->id, $generatedNickname);
 
298
 
 
299
        // Assert that the nickname exists, just in case...
 
300
        $rNick = $this->gdata->retrieveNickname($generatedNickname);
 
301
        $this->assertNotNull($rNick);
 
302
 
 
303
        // Delete nickname
 
304
        $this->gdata->deleteNickname($generatedNickname);
 
305
 
 
306
        // Ensure that nickname was deleted
 
307
        $rNick = $this->gdata->retrieveNickname($generatedNickname);
 
308
        $this->assertNull($rNick);
 
309
    }
 
310
 
 
311
    public function testEmailListCRUDOperations() {
 
312
        // Create email list
 
313
        $generatedListName = strtolower(uniqid('zf-list-'));
 
314
        $list = $this->gdata->createEmailList($generatedListName);
 
315
        $this->autoDelete($list);
 
316
        $this->assertEquals($generatedListName, $list->emailList->name);
 
317
 
 
318
        // Retrieve email list
 
319
        $query = $this->gdata->newEmailListQuery();
 
320
        $listFeed = $this->gdata->getEmailListFeed($query);
 
321
        $entryCount = count($listFeed->entry);
 
322
        $this->assertTrue($entryCount > 0);
 
323
 
 
324
        // Delete email list (uses builtin delete method, convenience delete
 
325
        // method tested further down)
 
326
        $list->delete();
 
327
 
 
328
        // Ensure that nickname was deleted
 
329
        $listFeed = $this->gdata->getEmailListFeed($query);
 
330
        $this->assertEquals($entryCount - 1, count($listFeed->entry));
 
331
    }
 
332
 
 
333
    public function testCanAssignMultipleEmailListsToOneUser() {
 
334
        // Create a user
 
335
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
 
336
            sha1(self::PASSWORD), self::PASSWORD_HASH);
 
337
        $this->autoDelete($user);
 
338
 
 
339
        // Create two email lists
 
340
        $listCount = 2;
 
341
 
 
342
        for ($i = 0; $i < $listCount; $i++) {
 
343
            $generatedListName = strtolower(uniqid('zf-list-'));
 
344
            $list = $this->gdata->createEmailList($generatedListName);
 
345
            $this->autoDelete($list);
 
346
            $this->gdata->addRecipientToEmailList($this->id, $generatedListName);
 
347
        }
 
348
 
 
349
        // Make sure that the user is subscribed to both lists
 
350
        $subscriptions = $this->gdata->retrieveEmailLists($this->id);
 
351
        $this->assertEquals($listCount, count($subscriptions->entry));
 
352
    }
 
353
 
 
354
    public function testCanRetrievePageOfEmailLists() {
 
355
        // Create an email list
 
356
        $generatedListName = strtolower(uniqid('zf-list-'));
 
357
        $list = $this->gdata->createEmailList($generatedListName);
 
358
        $this->autoDelete($list);
 
359
 
 
360
        // Try retrieving the email list feed
 
361
        $feed = $this->gdata->retrievePageOfEmailLists();
 
362
        $this->assertTrue(count($feed->entry) > 0);
 
363
    }
 
364
 
 
365
    public function testCanRetrieveAllEmailLists() {
 
366
        // Create a couple of users to make sure we don't hit the limit
 
367
        // on the max number of email lists.
 
368
        for ($i = 0; $i < 3; $i++) {
 
369
            $user = $this->gdata->createUser(uniqid('ZF-'), self::GIVEN_NAME, self::FAMILY_NAME,
 
370
                sha1(self::PASSWORD), self::PASSWORD_HASH);
 
371
            $this->autoDelete($user);
 
372
        }
 
373
 
 
374
        // Create a whole bunch of email lists to make sure we trigger
 
375
        // paging.
 
376
        for ($i = 0; $i < 30; $i++) {
 
377
            $generatedListName = strtolower(uniqid('zf-list-'));
 
378
            $list = $this->gdata->createEmailList($generatedListName);
 
379
            $this->autoDelete($list);
 
380
        }
 
381
 
 
382
        // Try retrieving the email list feed
 
383
        $feed = $this->gdata->retrieveAllEmailLists();
 
384
        $this->assertTrue(count($feed->entry) >= 30);
 
385
    }
 
386
 
 
387
    // Test the convenience delete method for email lists
 
388
    public function testCanDeleteEmailList() {
 
389
        // Create an email list
 
390
        $generatedListName = strtolower(uniqid('zf-list-'));
 
391
        $list = $this->gdata->createEmailList($generatedListName);
 
392
        $this->autoDelete($list);
 
393
 
 
394
        // Assert that the email list exists, just in case...
 
395
        $query = $this->gdata->newEmailListQuery();
 
396
        $query->setEmailListName($generatedListName);
 
397
        $entry = $this->gdata->getEmailListEntry($query);
 
398
        $this->assertNotNull($entry);
 
399
 
 
400
        // Delete nickname
 
401
        $this->gdata->deleteEmailList($generatedListName);
 
402
 
 
403
        // Ensure that nickname was deleted
 
404
        try {
 
405
            $query = $this->gdata->newEmailListQuery();
 
406
            $query->setEmailListName($generatedListName);
 
407
            $entry = $this->gdata->getEmailListEntry($query);
 
408
            // This souldn't execute
 
409
            $this->fail('Retrieving a non-existant email list entry didn\'t' .
 
410
                'raise exception.');
 
411
        } catch (Zend_Gdata_Gapps_ServiceException $e) {
 
412
            if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
 
413
                // Dummy assertion just to say we tested something here.
 
414
                $this->assertTrue(true);
 
415
            } else {
 
416
                // Exception thrown for an unexpected reason
 
417
                throw $e;
 
418
            }
 
419
        }
 
420
    }
 
421
 
 
422
    public function testCanRetrievePageOfRecipients() {
 
423
        // Create a new email list
 
424
        $generatedListName = strtolower(uniqid('zf-list-'));
 
425
        $list = $this->gdata->createEmailList($generatedListName);
 
426
        $this->autoDelete($list);
 
427
 
 
428
        // Create two users and assign them to the email list
 
429
        $userCount = 2;
 
430
        for ($i = 0; $i < $userCount; $i++) {
 
431
            $generatedUsername = uniqid('ZF-');
 
432
            $user = $this->gdata->createUser($generatedUsername,
 
433
                self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
 
434
                self::PASSWORD_HASH);
 
435
            $this->autoDelete($user);
 
436
            $this->gdata->addRecipientToEmailList($generatedUsername,
 
437
                $generatedListName);
 
438
        }
 
439
 
 
440
        // Retrieve recipients
 
441
        $recipientFeed =
 
442
            $this->gdata->retrievePageOfRecipients($generatedListName);
 
443
        $this->assertTrue(count($recipientFeed->entry) == $userCount);
 
444
    }
 
445
 
 
446
    public function testCanRetrievAllRecipients() {
 
447
        // Create a new email list
 
448
        $generatedListName = strtolower(uniqid('zf-list-'));
 
449
        $list = $this->gdata->createEmailList($generatedListName);
 
450
        $this->autoDelete($list);
 
451
 
 
452
        // Create enough users to trigger paging and assign them to the email
 
453
        // list
 
454
        $userCount = 30;
 
455
        for ($i = 0; $i < $userCount; $i++) {
 
456
            $generatedUsername = uniqid('ZF-');
 
457
            $user = $this->gdata->createUser($generatedUsername,
 
458
                self::GIVEN_NAME, self::FAMILY_NAME, sha1(self::PASSWORD),
 
459
                self::PASSWORD_HASH);
 
460
            $this->autoDelete($user);
 
461
            $this->gdata->addRecipientToEmailList($generatedUsername,
 
462
                $generatedListName);
 
463
        }
 
464
 
 
465
        // Retrieve recipients
 
466
        $recipientFeed =
 
467
            $this->gdata->retrieveAllRecipients($generatedListName);
 
468
        $this->assertTrue(count($recipientFeed->entry) == $userCount);
 
469
    }
 
470
 
 
471
    // Test the convenience delete method for email list recipients
 
472
    public function testCanDeleteEmailListRecipient() {
 
473
        // Create an email list
 
474
        $generatedListName = strtolower(uniqid('zf-list-'));
 
475
        $list = $this->gdata->createEmailList($generatedListName);
 
476
        $this->autoDelete($list);
 
477
 
 
478
        // Create a user for the email list
 
479
        $user = $this->gdata->createUser($this->id, self::GIVEN_NAME,
 
480
            self::FAMILY_NAME, sha1(self::PASSWORD), self::PASSWORD_HASH);
 
481
        $this->autoDelete($user);
 
482
        $this->gdata->addRecipientToEmailList($this->id, $generatedListName);
 
483
 
 
484
        // Assert that the recipient exists, just in case...
 
485
        $recipients =
 
486
            $this->gdata->retrieveAllRecipients($generatedListName);
 
487
        $this->assertTrue(count($recipients->entry) == 1);
 
488
 
 
489
        // Remove the user from the list
 
490
        $this->gdata->removeRecipientFromEmailList($user->login->username,
 
491
            $generatedListName);
 
492
 
 
493
        // Ensure that user was deleted
 
494
        $recipients =
 
495
            $this->gdata->retrieveAllRecipients($generatedListName);
 
496
        $this->assertTrue(count($recipients->entry) == 0);
 
497
    }
 
498
 
 
499
}