]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/OauthTokenTest.php
Release 6.5.14
[Github/sugarcrm.git] / tests / service / OauthTokenTest.php
1 <?php
2 /*********************************************************************************
3  * By installing or using this file, you are confirming on behalf of the entity
4  * subscribed to the SugarCRM Inc. product ("Company") that Company is bound by
5  * the SugarCRM Inc. Master Subscription Agreement (“MSA”), which is viewable at:
6  * http://www.sugarcrm.com/master-subscription-agreement
7  *
8  * If Company is not bound by the MSA, then by installing or using this file
9  * you are agreeing unconditionally that Company will be bound by the MSA and
10  * certifying that you have authority to bind Company accordingly.
11  *
12  * Copyright (C) 2004-2013 SugarCRM Inc.  All rights reserved.
13  ********************************************************************************/
14 require_once 'modules/OAuthTokens/OAuthToken.php';
15
16 class OAuthTokenTest extends Sugar_PHPUnit_Framework_TestCase
17 {
18     public static function setUpBeforeClass()
19     {
20         SugarTestHelper::setUp('current_user');
21     }
22
23     public static function tearDownAfterClass()
24     {
25         OAuthToken::deleteByUser($GLOBALS['current_user']->id);
26         SugarTestHelper::tearDown();
27     }
28
29     /**
30      * @ticket 62822
31      */
32     public function testCleanup()
33     {
34         // create request token
35         $tok = OAuthToken::generate();
36         $tok->consumer = create_guid();
37         $tok->setState(OAuthToken::REQUEST);
38         $tok->assigned_user_id = $GLOBALS['current_user']->id;
39         $tok->save();
40         // create invalid token
41         $tok = OAuthToken::generate();
42         $tok->consumer = create_guid();
43         $tok->setState(OAuthToken::INVALID);
44         $tok->assigned_user_id = $GLOBALS['current_user']->id;
45         $tok->save();
46
47         $cnt = $GLOBALS['db']->getOne("SELECT count(*) c FROM {$tok->table_name} WHERE assigned_user_id=".$GLOBALS['db']->quoted($GLOBALS['current_user']->id));
48         $this->assertEquals(2, $cnt, "Wrong number of tokens in the table");
49
50         // set time way in the past
51         $GLOBALS['db']->query("UPDATE {$tok->table_name} SET token_ts=1 WHERE assigned_user_id=".$GLOBALS['db']->quoted($GLOBALS['current_user']->id));
52
53         // run cleanup
54         OAuthToken::cleanup();
55
56         // ensure tokens are gone
57         $cnt = $GLOBALS['db']->getOne("SELECT count(*) c FROM {$tok->table_name} WHERE assigned_user_id=".$GLOBALS['db']->quoted($GLOBALS['current_user']->id));
58         $this->assertEquals(0, $cnt, "Tokens were not deleted");
59     }
60 }