Projet

Général

Profil

Paste
Télécharger (4,61 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / oauth2_client / tests / oauth2_client_test.install @ 286092dc

1
<?php
2
/**
3
 * @file
4
 * Enable and disable hook functions.
5
 */
6

    
7
/**
8
 * Implements hook_enable().
9
 */
10
function oauth2_client_test_enable() {
11
  oauth2_client_test_disable();
12

    
13
  _oauth2_client_test_create();
14
  _oauth2_client_test_grant_permissions();
15
}
16

    
17
/**
18
 * Implements hook_disable().
19
 */
20
function oauth2_client_test_disable() {
21
  _oauth2_client_test_delete();
22
  //_oauth2_client_test_revoke_permissions();
23
}
24

    
25
/**
26
 * Create a test server, along with test clients and scopes.
27
 */
28
function _oauth2_client_test_create() {
29
  // Create the server and client.
30
  $server = entity_create('oauth2_server', array());
31
  $server->name = 'test_oauth2_server';
32
  $server->label = 'Test';
33
  $server->settings = array(
34
    'default_scope' => 'scope1',
35
    'enforce_state' => TRUE,
36
    'allow_implicit' => TRUE,
37
    'require_exact_redirect_uri' => FALSE,
38
    'grant_types' => array(
39
      'authorization_code' => 'authorization_code',
40
      'client_credentials' => 'client_credentials',
41
      'refresh_token' => 'refresh_token',
42
      'password' => 'password',
43
    ),
44
    'always_issue_new_refresh_token' => TRUE,
45
    // For testing purposes, set short expire times.
46
    'access_lifetime' => 20,
47
    'refresh_token_lifetime' => 30,
48
  );
49
  $server->save();
50

    
51
  // Add a client.
52
  $client = entity_create('oauth2_server_client', array());
53
  $client->server = $server->name;
54
  $client->label = 'Client 1';
55
  $client->client_key = 'client1';
56
  $client->client_secret = 'secret1';
57
  $client->redirect_uri = url('oauth2/authorized', array('absolute' => TRUE));
58
  $client->automatic_authorization = FALSE;
59
  $client->save();
60

    
61
  // The second client has automatic_authorization TRUE.
62
  $client = entity_create('oauth2_server_client', array());
63
  $client->server = $server->name;
64
  $client->label = 'Client 2';
65
  $client->client_key = 'client2';
66
  $client->client_secret = 'secret2';
67
  $client->redirect_uri = url('oauth2/authorized', array('absolute' => TRUE));
68
  $client->automatic_authorization = TRUE;
69
  $client->save();
70

    
71
  // Creates some scopes.
72
  $scopes = array(
73
    'scope1' => 'Scope 1',
74
    'scope2' => 'Scope 2',
75
  );
76
  foreach ($scopes as $scope_name => $scope_label) {
77
    $scope = entity_create('oauth2_server_scope', array());
78
    $scope->server = $server->name;
79
    $scope->name = $scope_name;
80
    $scope->description = $scope_label;
81
    $scope->save();
82
  }
83

    
84
  // Create a test user.
85
  user_save('', array(
86
      'name' => 'user1',
87
      'pass' => 'pass1',
88
      'status' => 1,
89
    ));
90
}
91

    
92
/**
93
 * Delete test servers, clients and scopes.
94
 */
95
function _oauth2_client_test_delete() {
96
  $server_name = 'test_oauth2_server';
97

    
98
  // Delete the test clients.
99
  $clients = array('client1', 'client2');
100
  foreach ($clients as $client_key) {
101
    $query = new EntityFieldQuery();
102
    $clients = $query->entityCondition('entity_type', 'oauth2_server_client')
103
      ->propertyCondition('client_key', $client_key)
104
      ->execute();
105
    if (isset($clients['oauth2_server_client'])) {
106
      $ids = array_keys($clients['oauth2_server_client']);
107
      foreach ($ids as $id) {
108
        entity_delete('oauth2_server_client', $id);
109
      }
110
    }
111
  }
112

    
113
  // Delete the test scopes.
114
  $scopes = array('scope1', 'scope2');
115
  foreach ($scopes as $scope_name) {
116
    $query = new EntityFieldQuery();
117
    $scopes = $query->entityCondition('entity_type', 'oauth2_server_scope')
118
      ->propertyCondition('name', $scope_name)
119
      ->execute();
120
    if (isset($scopes['oauth2_server_scope'])) {
121
      $ids = array_keys($scopes['oauth2_server_scope']);
122
      foreach ($ids as $id) {
123
        entity_delete('oauth2_server_scope', $id);
124
      }
125
    }
126
  }
127

    
128
  // Delete the test oauth2 server.
129
  $query = new EntityFieldQuery();
130
  $servers = $query->entityCondition('entity_type', 'oauth2_server')
131
    ->propertyCondition('name', $server_name)
132
    ->execute();
133
  if (isset($servers['oauth2_server'])) {
134
    $ids = array_keys($servers['oauth2_server']);
135
    foreach ($ids as $id) {
136
      entity_delete('oauth2_server', $id);
137
    }
138
  }
139

    
140
  // Delete the test user.
141
  if ($user = user_load_by_name('user1')) {
142
    user_delete($user->uid);
143
  }
144
}
145

    
146
function _oauth2_client_test_grant_permissions() {
147
  // Make sure that users have the permission to use the oauth2 server.
148
  foreach (array('anonymous user', 'authenticated user') as $role_name) {
149
    $role = user_role_load_by_name($role_name);
150
    user_role_grant_permissions($role->rid, array('use oauth2 server'));
151
  }
152
}
153

    
154
function _oauth2_client_test_revoke_permissions() {
155
  // Remove the permission for using the oauth2 server.
156
  foreach (array('anonymous user', 'authenticated user') as $role_name) {
157
    $role = user_role_load_by_name($role_name);
158
    user_role_revoke_permissions($role->rid, array('use oauth2 server'));
159
  }
160
}