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.
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
23
require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
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';
33
* @subpackage UnitTests
35
class Zend_Gdata_GappsOnlineTest extends PHPUnit_Framework_TestCase
38
const GIVEN_NAME = 'Zend_Gdata';
39
const FAMILY_NAME = 'Automated Test Account';
40
const PASSWORD = '4ohtladfl;';
41
const PASSWORD_HASH = 'SHA-1';
43
public function setUp()
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);
52
// Container to hold users and lists created during tests. All entries in
53
// here will have delete() called during tear down.
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();
60
public function tearDown()
62
// Delete all entries in $this->autoDeletePool.
63
foreach ($this->autoDeletePool as $x) {
66
} catch (Exception $e) {
67
// Failed deletes are okay. Try and delete the rest anyway.
72
// Schedule an entry for deletion at test tear-down.
73
protected function autoDelete($entry) {
74
$this->autoDeletePool[] = $entry;
77
// Test Create/Read/Update/Destroy operations on a UserEntry
78
public function testUserCRUDOperations() {
80
$user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
81
sha1(self::PASSWORD), self::PASSWORD_HASH);
82
$this->autoDelete($user);
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);
89
// Since we can't retrieve the password or hash function via the
90
// API, let's see if a ClientLogin auth request succeeds
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.");
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);
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());
107
// Delete user (uses builtin delete method, convenience delete
108
// method tested further down)
111
// Ensure that user was deleted
112
$deletedUser = $this->gdata->retrieveUser($this->id);
113
$this->assertNull($deletedUser);
116
// Test to make sure that users with unicode characters can be created
118
public function testUsersSupportUnicode() {
120
$user = $this->gdata->createUser($this->id, 'テスト', 'ユーザー',
121
sha1(self::PASSWORD), self::PASSWORD_HASH);
122
$this->autoDelete($user);
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);
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);
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);
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);
156
$feed = $this->gdata->retrieveAllUsers();
157
$this->assertTrue(count($feed->entry) > 0);
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);
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);
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);
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);
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);
193
// Assert that the user exists, just in case...
194
$rUser = $this->gdata->retrieveUser($this->id);
195
$this->assertNotNull($rUser);
198
$this->gdata->deleteUser($this->id);
200
// Ensure that user was deleted
201
$rUser = $this->gdata->retrieveUser($this->id);
202
$this->assertNull($rUser);
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);
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);
220
$nickname = $this->gdata->retrieveNickname($generatedNickname);
221
$this->assertEquals($generatedNickname, $nickname->nickname->name);
222
$this->assertEquals($this->id, $nickname->login->username);
224
// Delete nickname (uses builtin delete method, convenience delete
225
// method tested further down)
228
// Ensure that nickname was deleted
229
$nickname = $this->gdata->retrieveNickname($generatedNickname);
230
$this->assertNull($nickname);
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);
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]);
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,
250
$this->assertNotSame(false, $searchResult);
251
unset($generatedNickname[$searchResult]);
253
$this->assertEquals(0, count($generatedNickname));
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);
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]);
267
// Test to make sure that we receive at least 5 nicknames back
269
$results = $this->gdata->retrievePageOfNicknames();
270
$this->assertTrue(count($results->entry) >= 5);
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);
285
// Test to make sure that we receive at least 5 nicknames back
287
$results = $this->gdata->retrieveAllNicknames();
288
$this->assertTrue(count($results->entry) >= 30);
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);
299
// Assert that the nickname exists, just in case...
300
$rNick = $this->gdata->retrieveNickname($generatedNickname);
301
$this->assertNotNull($rNick);
304
$this->gdata->deleteNickname($generatedNickname);
306
// Ensure that nickname was deleted
307
$rNick = $this->gdata->retrieveNickname($generatedNickname);
308
$this->assertNull($rNick);
311
public function testEmailListCRUDOperations() {
313
$generatedListName = strtolower(uniqid('zf-list-'));
314
$list = $this->gdata->createEmailList($generatedListName);
315
$this->autoDelete($list);
316
$this->assertEquals($generatedListName, $list->emailList->name);
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);
324
// Delete email list (uses builtin delete method, convenience delete
325
// method tested further down)
328
// Ensure that nickname was deleted
329
$listFeed = $this->gdata->getEmailListFeed($query);
330
$this->assertEquals($entryCount - 1, count($listFeed->entry));
333
public function testCanAssignMultipleEmailListsToOneUser() {
335
$user = $this->gdata->createUser($this->id, self::GIVEN_NAME, self::FAMILY_NAME,
336
sha1(self::PASSWORD), self::PASSWORD_HASH);
337
$this->autoDelete($user);
339
// Create two email lists
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);
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));
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);
360
// Try retrieving the email list feed
361
$feed = $this->gdata->retrievePageOfEmailLists();
362
$this->assertTrue(count($feed->entry) > 0);
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);
374
// Create a whole bunch of email lists to make sure we trigger
376
for ($i = 0; $i < 30; $i++) {
377
$generatedListName = strtolower(uniqid('zf-list-'));
378
$list = $this->gdata->createEmailList($generatedListName);
379
$this->autoDelete($list);
382
// Try retrieving the email list feed
383
$feed = $this->gdata->retrieveAllEmailLists();
384
$this->assertTrue(count($feed->entry) >= 30);
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);
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);
401
$this->gdata->deleteEmailList($generatedListName);
403
// Ensure that nickname was deleted
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' .
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);
416
// Exception thrown for an unexpected reason
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);
428
// Create two users and assign them to the email list
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,
440
// Retrieve recipients
442
$this->gdata->retrievePageOfRecipients($generatedListName);
443
$this->assertTrue(count($recipientFeed->entry) == $userCount);
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);
452
// Create enough users to trigger paging and assign them to the email
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,
465
// Retrieve recipients
467
$this->gdata->retrieveAllRecipients($generatedListName);
468
$this->assertTrue(count($recipientFeed->entry) == $userCount);
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);
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);
484
// Assert that the recipient exists, just in case...
486
$this->gdata->retrieveAllRecipients($generatedListName);
487
$this->assertTrue(count($recipients->entry) == 1);
489
// Remove the user from the list
490
$this->gdata->removeRecipientFromEmailList($user->login->username,
493
// Ensure that user was deleted
495
$this->gdata->retrieveAllRecipients($generatedListName);
496
$this->assertTrue(count($recipients->entry) == 0);