blog.test 6.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
// $Id$

class BlogTestCase extends DrupalWebTestCase {
  protected $big_user;
  protected $own_user;
  protected $any_user;

  function getInfo() {
    return array(
      'name' => t('Blog functionality'),
      'description' => t('Create, view, edit, delete, and change blog entries and verify its consistency in the database.'),
      'group' => t('Blog'),
    );
  }

  /**
   * Enable modules and create users with specific permissions.
   */
  function setUp() {
    parent::setUp('blog');
    // Create users.
    $this->big_user = $this->drupalCreateUser(array('administer blocks'));
    $this->own_user = $this->drupalCreateUser(array('create blog content', 'edit own blog content', 'delete own blog content'));
    $this->any_user = $this->drupalCreateUser(array('create blog content', 'edit any blog content', 'delete any blog content', 'access administration pages'));
  }

  /**
   * Login users, create blog nodes, and test blog functionality through the admin and user interfaces.
   */
  function testBlog() {
    // Login the admin user.
    $this->drupalLogin($this->big_user);
    // Enable the recent blog block.
    $edit = array();
    $edit['blog_recent[region]'] = 'right';
    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));
    $this->assertResponse(200);

    // Do basic tests for each user.
    $this->doBasicTests($this->any_user, TRUE);
    $this->doBasicTests($this->own_user, FALSE);

    // Create another blog node for the any blog user.
    $node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->any_user->uid));
    // Verify the own blog user only has access to the blog view node.
    $this->verifyBlogs($this->any_user, $node, FALSE, 403);

    // Create another blog node for the own blog user.
    $node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->own_user->uid));
    // Login the any blog user.
    $this->drupalLogin($this->any_user);
    // Verify the any blog user has access to all the blog nodes.
    $this->verifyBlogs($this->own_user, $node, TRUE);
  }

  /**
   * Run basic tests on the indicated user.
   *
60
61
62
63
   * @param object $user
   *   The logged in user.
   * @param boolean $admin
   *   User has 'access administration pages' privilege.
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
   */
  private function doBasicTests($user, $admin) {
    // Login the user.
    $this->drupalLogin($user);
    // Create blog node.
    $node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $user->uid));
    // Verify the user has access to all the blog nodes.
    $this->verifyBlogs($user, $node, $admin);
    // Verify the blog links are displayed.
    $this->verifyBlogLinks($user);
  }

  /**
   * Verify the logged in user has the desired access to the various blog nodes.
   *
79
80
81
82
83
84
85
86
   * @param object $node_user
   *   The user who creates the node.
   * @param object $node
   *   A node object.
   * @param boolean $admin
   *   User has 'access administration pages' privilege.
   * @param integer $response
   *   HTTP response code.
87
88
89
90
91
92
93
94
95
96
97
   */
  private function verifyBlogs($node_user, $node, $admin, $response = 200) {
    $crumb = '›';
    $quote = '&#039;';

    $response2 = ($admin) ? 200 : 403;

    // View blog help node.
    $this->drupalGet('admin/help/blog');
    $this->assertResponse($response2);
    if ($response2 == 200) {
98
      $this->assertTitle(t('Blog | Drupal'), t('Blog help node was displayed'));
99
      $this->assertText(t('Blog'), t('Blog help node was displayed'));
100
      $this->assertText(t('Home ' . $crumb . ' Administer ' . $crumb . ' Help'), t('Breadcrumbs were displayed'));
101
102
103
104
105
106
107
108
    }

    // Verify the blog block was displayed.
    $this->drupalGet('');
    $this->assertResponse(200);
    $this->assertText(t('Recent blog posts'), t('Blog block was displayed'));

    // View blog node.
109
    $this->drupalGet('node/' . $node->nid);
110
111
    $this->assertResponse(200);
    $this->assertTitle($node->title. ' | Drupal', t('Blog node was displayed'));
112
    $this->assertText(t('Home ' . $crumb . ' Blogs ' . $crumb . ' @name' . $quote . 's blog', array('@name' => $node_user->name)), t('Breadcrumbs were displayed'));
113
114

    // View blog edit node.
115
    $this->drupalGet('node/' . $node->nid . '/edit');
116
117
    $this->assertResponse($response);
    if ($response == 200) {
118
      $this->assertTitle('Edit Blog entry ' . $node->title. ' | Drupal', t('Blog edit node was displayed'));
119
      $this->assertText(t('Home ' . $crumb . ' @title', array('@title' => $node->title)), t('Breadcrumbs were displayed'));
120
121
122
123
124
125
126
    }

    if ($response == 200) {
      // Edit blog node.
      $edit = array();
      $edit['title'] = 'node/' . $node->nid;
      $edit['body'] = $this->randomName(256);
127
      $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
128
129
130
      $this->assertRaw(t('Blog entry %title has been updated.', array('%title' => $edit['title'])), t('Blog node was edited'));

      // Delete blog node.
131
      $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
132
133
134
135
136
137
138
139
      $this->assertResponse($response);
      $this->assertRaw(t('Blog entry %title has been deleted.', array('%title' => $edit['title'])), t('Blog node was deleted'));
    }
  }

  /**
   * Verify the blog links are displayed to the logged in user.
   *
140
141
   * @param object $user
   *   The logged in user.
142
143
144
145
146
   */
  private function verifyBlogLinks($user) {
    $crumb = '›';

    // Confirm blog entries link exists on the user page.
147
    $this->drupalGet('user/' . $user->uid);
148
149
150
    $this->assertResponse(200);
    $this->assertText(t('View recent blog entries'), t('View recent blog entries link was displayed'));

151
    // Confirm the recent blog entries link goes to the user's blog page.
152
153
154
155
156
157
158
159
160
161
    $this->clickLink('View recent blog entries');
    $this->assertTitle(t("@name's blog | Drupal", array('@name' => $user->name)), t('View recent blog entries link target was correct'));

    // Confirm a blog page was displayed.
    $this->drupalGet('blog');
    $this->assertResponse(200);
    $this->assertTitle('Blogs | Drupal', t('Blog page was displayed'));
    $this->assertText(t('Home'), t('Breadcrumbs were displayed'));

    // Confirm a blog page was displayed per user.
162
    $this->drupalGet('blog/' . $user->uid);
163
    $this->assertTitle(t("@name's blog | Drupal", array('@name' => $user->name)), t('User blog node was displayed'));
164
    $this->assertText(t('Home ' . $crumb . ' Blogs'), t('Breadcrumbs were displayed'));
165
166
167
168
169
170

    // Confirm a blog feed was displayed.
    $this->drupalGet('blog/feed');
    $this->assertTitle(t('Drupal blogs'), t('Blog feed was displayed'));

    // Confirm a blog feed was displayed per user.
171
    $this->drupalGet('blog/' . $user->uid . '/feed');
172
173
174
    $this->assertTitle(t("@name's blog", array('@name' => $user->name)), t('User blog feed was displayed'));
  }
}