Spike PHPCoverage Details: blog.test

Line #FrequencySource Line
1 <?php
2 // $Id: blog.test,v 1.3 2008/04/01 23:33:54 boombatower Exp $
3 
4 class BlogModuleTestCase extends DrupalTestCase {
5   protected $big_user;
6   protected $own_user;
7   protected $any_user;
8 
9   /**
10    * Implementation of getInfo().
11    */
12   function getInfo() {
13     return array(
141      'name' => t('Blog functionality'),
15       'description' => t('Create, view, edit, delete, and change blog entries and verify its consistency in the database.'),
16       'group' => t('Blog Tests'),
17     );
18   }
19 
20   /**
21    * Enable modules and create users with specific permissions.
22    */
23   function setUp() {
24     parent::setUp();
25 
26     $this->drupalModuleEnable('blog');
27 
28     // Create users.
29     $this->big_user = $this->drupalCreateUser(array('administer blocks'));
30     $this->own_user = $this->drupalCreateUser(array('create blog content', 'edit own blog content', 'delete own blog content'));
31     $this->any_user = $this->drupalCreateUser(array('create blog content', 'edit any blog content', 'delete any blog content', 'access administration pages'));
32   }
33 
34   /**
35    * Login users, create blog nodes, and test blog functionality through the admin and user interfaces.
36    */
37   function testBlog() {
38     // Login the admin user.
39     $this->drupalLogin($this->big_user);
40     // Enable the recent blog block.
41     $edit = array();
42     $edit['blog_0[region]'] = 'right';
43     $this->drupalPost('admin/build/block', $edit, t('Save blocks'));
44     $this->assertResponse(200);
45 
46     // Do basic tests for each user.
47     $this->doBasicTests($this->any_user, TRUE);
48     $this->doBasicTests($this->own_user, FALSE);
49 
50     // Create another blog node for the any blog user.
51     $node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->any_user->uid));
52     // Verify the own blog user only has access to the blog view node.
53     $this->verifyBlogs($this->any_user, $node, FALSE, 403);
54 
55     // Create another blog node for the own blog user.
56     $node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->own_user->uid));
57     // Login the any blog user.
58     $this->drupalLogin($this->any_user);
59     // Verify the any blog user has access to all the blog nodes.
60     $this->verifyBlogs($this->own_user, $node, TRUE);
61   }
62 
63   /**
64    * Run basic tests on the indicated user.
65    *
66    * @param object $user The logged in user.
67    * @param boolean $admin User has 'access administration pages' privilege.
68    */
69   private function doBasicTests($user, $admin) {
70     // Login the user.
71     $this->drupalLogin($user);
72     // Create blog node.
73     $node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $user->uid));
74     // Verify the user has access to all the blog nodes.
75     $this->verifyBlogs($user, $node, $admin);
76     // Verify the blog links are displayed.
77     $this->verifyBlogLinks($user);
78   }
79 
80   /**
81    * Verify the logged in user has the desired access to the various blog nodes.
82    *
83    * @param object $node_user The user who creates the node.
84    * @param object $node Node.
85    * @param boolean $admin User has 'access administration pages' privilege.
86    * @param integer $response HTTP response code.
87    */
88   private function verifyBlogs($node_user, $node, $admin, $response = 200) {
89     $crumb = '›';
90     $quote = '&#039;';
91 
92     $response2 = ($admin) ? 200 : 403;
93 
94     // View blog help node.
95     $this->drupalGet('admin/help/blog');
96     $this->assertResponse($response2);
97     // NOTE: The two commented asserts fail because the get returns the 'admin/help' node instead of the indicated node???
98     if ($response2 == 200) {
99 //      $this->assertTitle(t('Blog | Drupal'), t('Blog help node was displayed'));
100       $this->assertText(t('Blog'), t('Blog help node was displayed'));
101 //      $this->assertText(t('Home '. $crumb .' Administer '. $crumb .' Help'), t('Breadcrumbs were displayed'));
102     }
103 
104     // Verify the blog block was displayed.
105     $this->drupalGet('');
106     $this->assertResponse(200);
107     $this->assertText(t('Recent blog posts'), t('Blog block was displayed'));
108 
109     // View blog node.
110     $this->drupalGet('node/'. $node->nid);
111     $this->assertResponse(200);
112     $this->assertTitle($node->title. ' | Drupal', t('Blog node was displayed'));
113     $this->assertText(t('Home '. $crumb .' Blogs '. $crumb .' @name'. $quote .'s blog', array('@name' => $node_user->name)), t('Breadcrumbs were displayed'));
114 
115     // View blog edit node.
116     $this->drupalGet('node/'. $node->nid .'/edit');
117     $this->assertResponse($response);
118     if ($response == 200) {
119       $this->assertTitle($node->title. ' | Drupal', t('Blog edit node was displayed'));
120       $this->assertText(t('Home '. $crumb .' @title', array('@title' => $node->title)), t('Breadcrumbs were displayed'));
121     }
122 
123     if ($response == 200) {
124       // Edit blog node.
125       $edit = array();
126       $edit['title'] = 'node/' . $node->nid;
127       $edit['body'] = $this->randomName(256);
128       $this->drupalPost('node/'. $node->nid .'/edit', $edit, t('Save'));
129       $this->assertRaw(t('Blog entry %title has been updated.', array('%title' => $edit['title'])), t('Blog node was edited'));
130 
131       // Delete blog node.
132       $this->drupalPost('node/'. $node->nid .'/delete', array(), t('Delete'));
133       $this->assertResponse($response);
134       $this->assertRaw(t('Blog entry %title has been deleted.', array('%title' => $edit['title'])), t('Blog node was deleted'));
135     }
136   }
137 
138   /**
139    * Verify the blog links are displayed to the logged in user.
140    *
141    * @param object $user The logged in user.
142    */
143   private function verifyBlogLinks($user) {
144     $crumb = '›';
145 
146     // Confirm blog entries link exists on the user page.
147     $this->drupalGet('user/'. $user->uid);
148     $this->assertResponse(200);
149     $this->assertText(t('View recent blog entries'), t('View recent blog entries link was displayed'));
150 
151     // Confirm the recent blog entries link links to the user's blog page.
152     $this->clickLink('View recent blog entries');
153     $this->assertTitle(t("@name's blog | Drupal", array('@name' => $user->name)), t('View recent blog entries link target was correct'));
154 
155     // Confirm a blog page was displayed.
156     $this->drupalGet('blog');
157     $this->assertResponse(200);
158     $this->assertTitle('Blogs | Drupal', t('Blog page was displayed'));
159     $this->assertText(t('Home'), t('Breadcrumbs were displayed'));
160 
161     // 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     // Confirm a blog feed was displayed.
167     $this->drupalGet('blog/feed');
168     $this->assertTitle(t('Drupal blogs'), t('Blog feed was displayed'));
169 
170     // Confirm a blog feed was displayed per user.
171     $this->drupalGet('blog/'. $user->uid .'/feed');
172     $this->assertTitle(t("@name's blog", array('@name' => $user->name)), t('User blog feed was displayed'));
173   }
174 }