Projet

Général

Profil

Paste
Télécharger (6,87 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / acl / tests / acl.test @ cfceb792

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for the ACL module.
6
 */
7

    
8
/**
9
 * Test case for ACL module.
10
 */
11
class AclWebTestCase extends DrupalWebTestCase {
12

    
13
  /**
14
   * Implements getInfo().
15
   *
16
   * @return array
17
   */
18
  public static function getInfo() {
19
    return array(
20
      'name' => 'ACL access permissions',
21
      'description' => 'Test ACL permissions for reading a node.',
22
      'group' => 'ACL',
23
    );
24
  }
25

    
26
  /**
27
   * Implements setUp().
28
   */
29
  function setUp() {
30
    parent::setUp('acl', 'acl_node_test');
31
    $web_user = $this->drupalCreateUser(array('create page content'));
32
    $this->drupalLogin($web_user);
33
  }
34

    
35
  /**
36
   * Includes acl_create_acl, acl_delete_acl, acl_get_id_by_name
37
   */
38
  function testNodeAclCreateDelete() {
39
    // Add a node.
40
    $node1 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
41
    $this->assertTrue(node_load($node1->nid), t('Page node created.'));
42

    
43
    acl_create_acl('test1', $node1->title);
44
    $acl_id = acl_get_id_by_name('test1', $node1->title);
45
    $this->assertNotNull($acl_id, t('ACL ID was succesfully found.'), $group = 'ACL');
46
    $records = db_query('SELECT acl_id, name FROM {acl} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id))->fetchAll();
47
    $this->assertEqual(count($records), 1, t('ACL was succesfully created.'), $group = 'ACL');
48
    acl_delete_acl($records[0]->acl_id);
49

    
50
    $records = db_query('SELECT acl_id, name FROM {acl} WHERE acl_id = :acl_id', array(':acl_id' => $records[0]->acl_id))->fetchAll();
51
    $this->pass(var_export($records, TRUE));
52
    $this->assertEqual(count($records), 0, t('ACL was succesfully removed.'), $group = 'ACL');
53
  }
54

    
55
  /**
56
   * Includes acl_add_user, acl_remove_user, acl_has_users,
57
   * acl_get_id_by_name, acl_has_user, acl_get_uids
58
   */
59
  function testNodeAclSingleUserAddRemove() {
60
    // Add a node.
61
    $node1 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
62
    $this->assertTrue(node_load($node1->nid), t('Page node created.'));
63

    
64
    acl_create_acl('test2', $node1->title);
65
    // Check to see if grants added by node_test_node_access_records()
66
    // made it in.
67
    $acl_id = acl_get_id_by_name('test2', $node1->title);
68
    $this->assertNotNull($acl_id, t('ACL ID was succesfully found.'), $group = 'ACL');
69
    $records = db_query('SELECT acl_id, name FROM {acl} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id))->fetchAll();
70
    $this->assertEqual(count($records), 1, t('ACL was succesfully created.'), $group = 'ACL');
71

    
72
    // Add user (can't we use the user created in setup?).
73
    $web_user_1 = $this->drupalCreateUser();
74
    //$this->drupalLogin($web_user);
75
    acl_add_user($acl_id, $web_user_1->uid);
76
    $records = db_query('SELECT acl_id, uid FROM {acl_user} WHERE uid = :uid', array(':uid' => $web_user_1->uid))->fetchAll();
77
    // Verify user is added.
78
    $this->assertEqual(count($records), 1, t('User was succesfully added.'), $group = 'ACL');
79

    
80
    // Remove user.
81
    acl_remove_user($acl_id, $web_user_1->uid);
82
    $records = db_query('SELECT acl_id, uid FROM {acl_user} WHERE uid = :uid', array(':uid' => $web_user_1->uid))->fetchAll();
83
    // Verify user is removed.
84
    $this->assertEqual(count($records), 0, t('User was succesfully removed.'), $group = 'ACL');
85
  }
86

    
87
  /**
88
   * Includes acl_node_add_acl, acl_node_remove_acl, acl_node_clear_acls
89
   */
90
  function testNodeAclAddRemoveFromNode() {
91
    // Add nodes.
92
    $node1 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
93
    $this->assertTrue(node_load($node1->nid), t('Page node created.'));
94
    $node2 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
95
    $this->assertTrue(node_load($node2->nid), t('Page node created.'));
96
    $node3 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
97
    $this->assertTrue(node_load($node3->nid), t('Page node created.'));
98

    
99
    // Create an ACL and add nodes.
100
    $acl_id1 = acl_create_acl('test3', 'test', 1);
101
    $this->assertNotNull($acl_id1, t('ACL ID was created.'), $group = 'ACL');
102
    // Add two nodes.
103
    $query = db_select('node', 'n')
104
      ->fields('n', array('nid'))
105
      ->condition('nid', array($node1->nid, $node2->nid), 'IN');
106
    acl_add_nodes($query, $acl_id1, 1, 1, 1);
107
    $count = db_query('SELECT COUNT(nid) FROM {acl_node} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id1))->fetchField();
108
    $this->assertEqual($count, 2, t("2 nodes under control ($count)."), $group = 'ACL');
109
    // Add a third node.
110
    acl_node_add_acl($node3->nid, $acl_id1, 1, 1, 1);
111
    $count = db_query('SELECT COUNT(nid) FROM {acl_node} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id1))->fetchField();
112
    $this->assertEqual($count, 3, t('3 nodes under control.'), $group = 'ACL');
113
    // Add the second node again.
114
    acl_node_add_acl($node2->nid, $acl_id1, 1, 1, 1);
115
    $count = db_query('SELECT COUNT(nid) FROM {acl_node} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id1))->fetchField();
116
    $this->assertEqual($count, 3, t('Still only 3 nodes under control.'), $group = 'ACL');
117

    
118
    // Remove the second node again.
119
    acl_node_remove_acl($node2->nid, $acl_id1);
120
    $count = db_query('SELECT COUNT(nid) FROM {acl_node} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id1))->fetchField();
121
    $this->assertEqual($count, 2, t('2 nodes left under control.'), $group = 'ACL');
122
    // Remove the second node again.
123
    acl_node_remove_acl($node2->nid, $acl_id1);
124
    $count = db_query('SELECT COUNT(nid) FROM {acl_node} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id1))->fetchField();
125
    $this->assertEqual($count, 2, t('Still 2 nodes left under control.'), $group = 'ACL');
126

    
127
    // Create another ACL and add nodes.
128
    $acl_id2 = acl_create_acl('test3', 'test', 2);
129
    $this->assertNotNull($acl_id2, t('ACL ID was created.'), $group = 'ACL');
130
    acl_node_add_acl($node1->nid, $acl_id2, 1, 1, 1);
131
    acl_node_add_acl($node2->nid, $acl_id2, 1, 1, 1);
132
    $count = db_query('SELECT COUNT(nid) FROM {acl_node} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id2))->fetchField();
133
    $this->assertEqual($count, 2, t('2 nodes under control.'), $group = 'ACL');
134
    // Remove a node (which has two ACLs).
135
    acl_node_clear_acls($node1->nid, 'test3');
136
    $count = db_query('SELECT COUNT(nid) FROM {acl_node} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id1))->fetchField();
137
    $this->assertEqual($count, 1, t('1 node left under control.'), $group = 'ACL');
138
    $count = db_query('SELECT COUNT(nid) FROM {acl_node} WHERE acl_id = :acl_id', array(':acl_id' => $acl_id2))->fetchField();
139
    $this->assertEqual($count, 1, t('1 node left under control.'), $group = 'ACL');
140
  }
141

    
142
}
143

    
144
/**
145
 * Included acl_node_delete
146
 */
147
function testNodeAclAddAndRemoveNode() {
148
}
149

    
150
/**
151
 * Included acl_user_cancel
152
 */
153
function testNodeAclAddAndRemoveUser() {
154
}
155

    
156
/**
157
 * Includes independ check of the permissions by checking the grants
158
 * table AND by trying to view the node as a unauthorised user and an
159
 * authorized user for each of the 3 use cases (view, edit, delete)
160
 */
161
function testNodeAclPermissionCheck() {
162
}