Projet

Général

Profil

Paste
Télécharger (9,01 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / tracker / tracker.test @ f7a2490e

1
<?php
2

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

    
8
/**
9
 * Defines a base class for testing tracker.module.
10
 */
11
class TrackerTest extends DrupalWebTestCase {
12

    
13
  /**
14
   * The main user for testing.
15
   *
16
   * @var object
17
   */
18
  protected $user;
19

    
20
  /**
21
   * A second user that will 'create' comments and nodes.
22
   *
23
   * @var object
24
   */
25
  protected $other_user;
26

    
27
  public static function getInfo() {
28
    return array(
29
      'name' => 'Tracker',
30
      'description' => 'Create and delete nodes and check for their display in the tracker listings.',
31
      'group' => 'Tracker'
32
    );
33
  }
34

    
35
  function setUp() {
36
    parent::setUp('comment', 'tracker');
37

    
38
    $permissions = array('access comments', 'create page content', 'post comments', 'skip comment approval');
39
    $this->user = $this->drupalCreateUser($permissions);
40
    $this->other_user = $this->drupalCreateUser($permissions);
41

    
42
    // Make node preview optional.
43
    variable_set('comment_preview_page', 0);
44
  }
45

    
46
  /**
47
   * Tests for the presence of nodes on the global tracker listing.
48
   */
49
  function testTrackerAll() {
50
    $this->drupalLogin($this->user);
51

    
52
    $unpublished = $this->drupalCreateNode(array(
53
      'title' => $this->randomName(8),
54
      'status' => 0,
55
    ));
56
    $published = $this->drupalCreateNode(array(
57
      'title' => $this->randomName(8),
58
      'status' => 1,
59
    ));
60

    
61
    $this->drupalGet('tracker');
62
    $this->assertNoText($unpublished->title, 'Unpublished node do not show up in the tracker listing.');
63
    $this->assertText($published->title, 'Published node show up in the tracker listing.');
64
    $this->assertLink(t('My recent content'), 0, 'User tab shows up on the global tracker page.');
65

    
66
    // Delete a node and ensure it no longer appears on the tracker.
67
    node_delete($published->nid);
68
    $this->drupalGet('tracker');
69
    $this->assertNoText($published->title, 'Deleted node do not show up in the tracker listing.');
70
  }
71

    
72
  /**
73
   * Tests for the presence of nodes on a user's tracker listing.
74
   */
75
  function testTrackerUser() {
76
    $this->drupalLogin($this->user);
77

    
78
    $unpublished = $this->drupalCreateNode(array(
79
      'title' => $this->randomName(8),
80
      'uid' => $this->user->uid,
81
      'status' => 0,
82
    ));
83
    $my_published = $this->drupalCreateNode(array(
84
      'title' => $this->randomName(8),
85
      'uid' => $this->user->uid,
86
      'status' => 1,
87
    ));
88
    $other_published_no_comment = $this->drupalCreateNode(array(
89
      'title' => $this->randomName(8),
90
      'uid' => $this->other_user->uid,
91
      'status' => 1,
92
    ));
93
    $other_published_my_comment = $this->drupalCreateNode(array(
94
      'title' => $this->randomName(8),
95
      'uid' => $this->other_user->uid,
96
      'status' => 1,
97
    ));
98
    $comment = array(
99
      'subject' => $this->randomName(),
100
      'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
101
    );
102
    $this->drupalPost('comment/reply/' . $other_published_my_comment->nid, $comment, t('Save'));
103

    
104
    $this->drupalGet('user/' . $this->user->uid . '/track');
105
    $this->assertNoText($unpublished->title, "Unpublished nodes do not show up in the users's tracker listing.");
106
    $this->assertText($my_published->title, "Published nodes show up in the user's tracker listing.");
107
    $this->assertNoText($other_published_no_comment->title, "Other user's nodes do not show up in the user's tracker listing.");
108
    $this->assertText($other_published_my_comment->title, "Nodes that the user has commented on appear in the user's tracker listing.");
109

    
110
    // Verify that unpublished comments are removed from the tracker.
111
    $admin_user = $this->drupalCreateUser(array('administer comments', 'access user profiles'));
112
    $this->drupalLogin($admin_user);
113
    $this->drupalPost('comment/1/edit', array('status' => COMMENT_NOT_PUBLISHED), t('Save'));
114
    $this->drupalGet('user/' . $this->user->uid . '/track');
115
    $this->assertNoText($other_published_my_comment->title, 'Unpublished comments are not counted on the tracker listing.');
116
  }
117

    
118
  /**
119
   * Tests for the presence of the "new" flag for nodes.
120
   */
121
  function testTrackerNewNodes() {
122
    $this->drupalLogin($this->user);
123

    
124
    $edit = array(
125
      'title' => $this->randomName(8),
126
    );
127

    
128
    $node = $this->drupalCreateNode($edit);
129
    $title = $edit['title'];
130
    $this->drupalGet('tracker');
131
    $this->assertPattern('/' . $title . '.*new/', 'New nodes are flagged as such in the tracker listing.');
132

    
133
    $this->drupalGet('node/' . $node->nid);
134
    $this->drupalGet('tracker');
135
    $this->assertNoPattern('/' . $title . '.*new/', 'Visited nodes are not flagged as new.');
136

    
137
    $this->drupalLogin($this->other_user);
138
    $this->drupalGet('tracker');
139
    $this->assertPattern('/' . $title . '.*new/', 'For another user, new nodes are flagged as such in the tracker listing.');
140

    
141
    $this->drupalGet('node/' . $node->nid);
142
    $this->drupalGet('tracker');
143
    $this->assertNoPattern('/' . $title . '.*new/', 'For another user, visited nodes are not flagged as new.');
144
  }
145

    
146
  /**
147
   * Tests for comment counters on the tracker listing.
148
   */
149
  function testTrackerNewComments() {
150
    $this->drupalLogin($this->user);
151

    
152
    $node = $this->drupalCreateNode(array(
153
      'comment' => 2,
154
      'title' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(8)))),
155
    ));
156

    
157
    // Add a comment to the page.
158
    $comment = array(
159
      'subject' => $this->randomName(),
160
      'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
161
    );
162
    // The new comment is automatically viewed by the current user.
163
    $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
164

    
165
    $this->drupalLogin($this->other_user);
166
    $this->drupalGet('tracker');
167
    $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
168
    $this->drupalGet('node/' . $node->nid);
169

    
170
    // Add another comment as other_user.
171
    $comment = array(
172
      'subject' => $this->randomName(),
173
      'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
174
    );
175
    // If the comment is posted in the same second as the last one then Drupal
176
    // can't tell the difference, so we wait one second here.
177
    sleep(1);
178
    $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
179

    
180
    $this->drupalLogin($this->user);
181
    $this->drupalGet('tracker');
182
    $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
183
  }
184

    
185
  /**
186
   * Tests that existing nodes are indexed by cron.
187
   */
188
  function testTrackerCronIndexing() {
189
    $this->drupalLogin($this->user);
190

    
191
    // Create 3 nodes.
192
    $edits = array();
193
    $nodes = array();
194
    for ($i = 1; $i <= 3; $i++) {
195
      $edits[$i] = array(
196
        'comment' => 2,
197
        'title' => $this->randomName(),
198
      );
199
      $nodes[$i] = $this->drupalCreateNode($edits[$i]);
200
    }
201

    
202
    // Add a comment to the last node as other user.
203
    $this->drupalLogin($this->other_user);
204
    $comment = array(
205
      'subject' => $this->randomName(),
206
      'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
207
    );
208
    $this->drupalPost('comment/reply/' . $nodes[3]->nid, $comment, t('Save'));
209

    
210
    // Start indexing backwards from node 3.
211
    variable_set('tracker_index_nid', 3);
212

    
213
    // Clear the current tracker tables and rebuild them.
214
    db_delete('tracker_node')
215
      ->execute();
216
    db_delete('tracker_user')
217
      ->execute();
218
    tracker_cron();
219

    
220
    $this->drupalLogin($this->user);
221

    
222
    // Fetch the user's tracker.
223
    $this->drupalGet('tracker/' . $this->user->uid);
224

    
225
    // Assert that all node titles are displayed.
226
    foreach ($nodes as $i => $node) {
227
      $this->assertText($node->title, format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
228
    }
229
    $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
230
    $this->assertText('updated', 'Node is listed as updated');
231

    
232
    // Fetch the site-wide tracker.
233
    $this->drupalGet('tracker');
234

    
235
    // Assert that all node titles are displayed.
236
    foreach ($nodes as $i => $node) {
237
      $this->assertText($node->title, format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
238
    }
239
    $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
240
  }
241

    
242
  /**
243
   * Tests that publish/unpublish works at admin/content/node.
244
   */
245
  function testTrackerAdminUnpublish() {
246
    $admin_user = $this->drupalCreateUser(array('access content overview', 'administer nodes', 'bypass node access'));
247
    $this->drupalLogin($admin_user);
248

    
249
    $node = $this->drupalCreateNode(array(
250
      'comment' => 2,
251
      'title' => $this->randomName(),
252
    ));
253

    
254
    // Assert that the node is displayed.
255
    $this->drupalGet('tracker');
256
    $this->assertText($node->title, 'Node is displayed on the tracker listing pages.');
257

    
258
    // Unpublish the node and ensure that it's no longer displayed.
259
    $edit = array(
260
      'operation' => 'unpublish',
261
      'nodes[' . $node->nid . ']' => $node->nid,
262
    );
263
    $this->drupalPost('admin/content', $edit, t('Update'));
264

    
265
    $this->drupalGet('tracker');
266
    $this->assertText(t('No content available.'), 'Node is displayed on the tracker listing pages.');
267
  }
268
}