PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework-1.10.0/tests/Zend/Gdata/GappsOnlineTest.php

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