Projet

Général

Profil

Paste
Télécharger (3,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / content_access / tests / content_access_test_help.php @ ae34fb26

1
<?php
2

    
3
/**
4
 * @file
5
 * Helper class with auxiliary functions for content access module tests
6
 */
7

    
8
class ContentAccessTestCase extends DrupalWebTestCase {
9

    
10
  var $test_user;
11
  var $rid;
12
  var $admin_user;
13
  var $content_type;
14
  var $url_content_type_name;
15
  var $node1;
16
  var $node2;
17

    
18
  /**
19
   * Preparation work that is done before each test.
20
   * Test users, content types, nodes etc. are created.
21
   */
22
  function setUp($module = '') {
23
    if (empty($module)) {
24
      // Enable content access module
25
      parent::setUp('content_access');
26
    }
27
    else {
28
      // Enable content access module plus another module
29
      parent::setUp('content_access', $module);
30
      // Stop setup when module could not be enabled
31
      if (!module_exists($module)) {
32
        $this->pass('No ' . $module . ' module present, skipping test');
33
        return;
34
      }
35
    }
36

    
37
    // Create test user with separate role.
38
    $this->test_user = $this->drupalCreateUser(array('access content'));
39

    
40
    // Get the value of the new role.
41
    // Needed in D7 because drupalCreateUser() creates two roles for new users:
42
    // one role is Authenticated and the second has the given permissions.
43
    // @see drupalCreateUser()
44
    $roles = $this->test_user->roles;
45
    unset($roles[DRUPAL_AUTHENTICATED_RID]);
46
    $this->rid = key($roles);
47

    
48
    // Create admin user
49
    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'grant content access', 'grant own content access', 'bypass node access', 'administer nodes', 'access administration pages'));
50
    $this->drupalLogin($this->admin_user);
51

    
52
    // Rebuild content access permissions
53
    node_access_rebuild();
54

    
55
    // Create test content type
56
    $this->content_type = $this->drupalCreateContentType();
57
  }
58

    
59
  /**
60
   * Change access permissions for a content type
61
   */
62
  function changeAccessContentType($access_settings) {
63
    $this->drupalPost('admin/structure/types/manage/'. $this->content_type->type .'/access', $access_settings, t('Submit'));
64
    $this->assertText(t('Your changes have been saved.'), 'access rules of content type were updated successfully');
65
  }
66

    
67
  /**
68
   * Change access permissions for a content type by a given keyword (view, update or delete)
69
   * for the role of the user
70
   */
71
  function changeAccessContentTypeKeyword($keyword, $access = TRUE, $user = NULL) {
72
    if ($user === NULL) {
73
      $user = $this->test_user;
74
      $roles[$this->rid] = $user->roles[$this->rid];
75
    } else {
76
      foreach ($user->roles as $rid => $role) {
77
        if (!in_array($rid, array(DRUPAL_AUTHENTICATED_RID))) {
78
          $roles[$rid] = $user->roles[$rid];
79
          break;
80
        }
81
      }
82
    }
83

    
84
    $access_settings = array(
85
      $keyword .'['. key($roles) .']' => $access,
86
    );
87

    
88
    $this->changeAccessContentType($access_settings);
89
  }
90

    
91
  /**
92
   * Change the per node access setting for a content type
93
   */
94
  function changeAccessPerNode($access = TRUE) {
95
    $access_permissions = array(
96
      'per_node' => $access,
97
    );
98
    $this->changeAccessContentType($access_permissions);
99
  }
100

    
101
  /**
102
   * Change access permissions for a node by a given keyword (view, update or delete)
103
   */
104
  function changeAccessNodeKeyword($node, $keyword, $access = TRUE) {
105
    $user = $this->test_user;
106
    $roles[$this->rid] = $user->roles[$this->rid];
107

    
108
    $access_settings = array(
109
      $keyword .'['. key($roles) .']' => $access,
110
    );
111

    
112
    $this->changeAccessNode($node, $access_settings);
113
  }
114

    
115
  /**
116
   * Change access permission for a node
117
   */
118
  function changeAccessNode($node, $access_settings) {
119
    $this->drupalPost('node/'. $node->nid .'/access', $access_settings, t('Submit'));
120
    $this->assertText(t('Your changes have been saved.'), 'access rules of node were updated successfully');
121
  }
122
}