blog.test

Tests for blog.module.

Classes

Namesort descending Description
BlogTestCase

File

drupal/modules/blog/blog.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for blog.module.
  5. */
  6. class BlogTestCase extends DrupalWebTestCase {
  7. protected $big_user;
  8. protected $own_user;
  9. protected $any_user;
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Blog functionality',
  13. 'description' => 'Create, view, edit, delete, and change blog entries and verify its consistency in the database.',
  14. 'group' => 'Blog',
  15. );
  16. }
  17. /**
  18. * Enable modules and create users with specific permissions.
  19. */
  20. function setUp() {
  21. parent::setUp('blog');
  22. // Create users.
  23. $this->big_user = $this->drupalCreateUser(array('administer blocks'));
  24. $this->own_user = $this->drupalCreateUser(array('create blog content', 'edit own blog content', 'delete own blog content'));
  25. $this->any_user = $this->drupalCreateUser(array('create blog content', 'edit any blog content', 'delete any blog content', 'access administration pages'));
  26. }
  27. /**
  28. * Confirm that the "You are not allowed to post a new blog entry." message
  29. * shows up if a user submitted blog entries, has been denied that
  30. * permission, and goes to the blog page.
  31. */
  32. function testUnprivilegedUser() {
  33. // Create a blog node for a user with no blog permissions.
  34. $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->big_user->uid));
  35. $this->drupalLogin($this->big_user);
  36. $this->drupalGet('blog/' . $this->big_user->uid);
  37. $this->assertResponse(200);
  38. $this->assertTitle(t("@name's blog", array('@name' => format_username($this->big_user))) . ' | Drupal', 'Blog title was displayed');
  39. $this->assertText(t('You are not allowed to post a new blog entry.'), 'No new entries can be posted without the right permission');
  40. }
  41. /**
  42. * View the blog of a user with no blog entries as another user.
  43. */
  44. function testBlogPageNoEntries() {
  45. $this->drupalLogin($this->big_user);
  46. $this->drupalGet('blog/' . $this->own_user->uid);
  47. $this->assertResponse(200);
  48. $this->assertTitle(t("@name's blog", array('@name' => format_username($this->own_user))) . ' | Drupal', 'Blog title was displayed');
  49. $this->assertText(t('@author has not created any blog entries.', array('@author' => format_username($this->own_user))), 'Users blog displayed with no entries');
  50. }
  51. /**
  52. * Login users, create blog nodes, and test blog functionality through the admin and user interfaces.
  53. */
  54. function testBlog() {
  55. // Login the admin user.
  56. $this->drupalLogin($this->big_user);
  57. // Enable the recent blog block.
  58. $edit = array();
  59. $edit['blocks[blog_recent][region]'] = 'sidebar_second';
  60. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  61. $this->assertResponse(200);
  62. // Verify ability to change number of recent blog posts in block.
  63. $edit = array();
  64. $edit['blog_block_count'] = 5;
  65. $this->drupalPost('admin/structure/block/manage/blog/recent/configure', $edit, t('Save block'));
  66. $this->assertEqual(variable_get('blog_block_count', 10), 5, 'Number of recent blog posts changed.');
  67. // Do basic tests for each user.
  68. $this->doBasicTests($this->any_user, TRUE);
  69. $this->doBasicTests($this->own_user, FALSE);
  70. // Create another blog node for the any blog user.
  71. $node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->any_user->uid));
  72. // Verify the own blog user only has access to the blog view node.
  73. $this->verifyBlogs($this->any_user, $node, FALSE, 403);
  74. // Create another blog node for the own blog user.
  75. $node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->own_user->uid));
  76. // Login the any blog user.
  77. $this->drupalLogin($this->any_user);
  78. // Verify the any blog user has access to all the blog nodes.
  79. $this->verifyBlogs($this->own_user, $node, TRUE);
  80. }
  81. /**
  82. * Run basic tests on the indicated user.
  83. *
  84. * @param object $user
  85. * The logged in user.
  86. * @param boolean $admin
  87. * User has 'access administration pages' privilege.
  88. */
  89. private function doBasicTests($user, $admin) {
  90. // Login the user.
  91. $this->drupalLogin($user);
  92. // Create blog node.
  93. $node = $this->drupalCreateNode(array('type' => 'blog'));
  94. // Verify the user has access to all the blog nodes.
  95. $this->verifyBlogs($user, $node, $admin);
  96. // Create one more node to test the blog page with more than one node
  97. $this->drupalCreateNode(array('type' => 'blog', 'uid' => $user->uid));
  98. // Verify the blog links are displayed.
  99. $this->verifyBlogLinks($user);
  100. }
  101. /**
  102. * Verify the logged in user has the desired access to the various blog nodes.
  103. *
  104. * @param object $node_user
  105. * The user who creates the node.
  106. * @param object $node
  107. * A node object.
  108. * @param boolean $admin
  109. * User has 'access administration pages' privilege.
  110. * @param integer $response
  111. * HTTP response code.
  112. */
  113. private function verifyBlogs($node_user, $node, $admin, $response = 200) {
  114. $response2 = ($admin) ? 200 : 403;
  115. // View blog help node.
  116. $this->drupalGet('admin/help/blog');
  117. $this->assertResponse($response2);
  118. if ($response2 == 200) {
  119. $this->assertTitle(t('Blog | Drupal'), 'Blog help node was displayed');
  120. $this->assertText(t('Blog'), 'Blog help node was displayed');
  121. }
  122. // Verify the blog block was displayed.
  123. $this->drupalGet('');
  124. $this->assertResponse(200);
  125. $this->assertText(t('Recent blog posts'), 'Blog block was displayed');
  126. // View blog node.
  127. $this->drupalGet('node/' . $node->nid);
  128. $this->assertResponse(200);
  129. $this->assertTitle($node->title . ' | Drupal', 'Blog node was displayed');
  130. $breadcrumb = array(
  131. l(t('Home'), NULL),
  132. l(t('Blogs'), 'blog'),
  133. l(t("!name's blog", array('!name' => format_username($node_user))), 'blog/' . $node_user->uid),
  134. );
  135. $this->assertRaw(theme('breadcrumb', array('breadcrumb' => $breadcrumb)), 'Breadcrumbs were displayed');
  136. // View blog edit node.
  137. $this->drupalGet('node/' . $node->nid . '/edit');
  138. $this->assertResponse($response);
  139. if ($response == 200) {
  140. $this->assertTitle('Edit Blog entry ' . $node->title . ' | Drupal', 'Blog edit node was displayed');
  141. }
  142. if ($response == 200) {
  143. // Edit blog node.
  144. $edit = array();
  145. $langcode = LANGUAGE_NONE;
  146. $edit["title"] = 'node/' . $node->nid;
  147. $edit["body[$langcode][0][value]"] = $this->randomName(256);
  148. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  149. $this->assertRaw(t('Blog entry %title has been updated.', array('%title' => $edit["title"])), 'Blog node was edited');
  150. // Delete blog node.
  151. $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
  152. $this->assertResponse($response);
  153. $this->assertRaw(t('Blog entry %title has been deleted.', array('%title' => $edit["title"])), 'Blog node was deleted');
  154. }
  155. }
  156. /**
  157. * Verify the blog links are displayed to the logged in user.
  158. *
  159. * @param object $user
  160. * The logged in user.
  161. */
  162. private function verifyBlogLinks($user) {
  163. // Confirm blog entries link exists on the user page.
  164. $this->drupalGet('user/' . $user->uid);
  165. $this->assertResponse(200);
  166. $this->assertText(t('View recent blog entries'), 'View recent blog entries link was displayed');
  167. // Confirm the recent blog entries link goes to the user's blog page.
  168. $this->clickLink('View recent blog entries');
  169. $this->assertTitle(t("@name's blog | Drupal", array('@name' => format_username($user))), 'View recent blog entries link target was correct');
  170. // Confirm a blog page was displayed.
  171. $this->drupalGet('blog');
  172. $this->assertResponse(200);
  173. $this->assertTitle('Blogs | Drupal', 'Blog page was displayed');
  174. $this->assertText(t('Home'), 'Breadcrumbs were displayed');
  175. $this->assertLink(t('Create new blog entry'));
  176. // Confirm a blog page was displayed per user.
  177. $this->drupalGet('blog/' . $user->uid);
  178. $this->assertTitle(t("@name's blog | Drupal", array('@name' => format_username($user))), 'User blog node was displayed');
  179. // Confirm a blog feed was displayed.
  180. $this->drupalGet('blog/feed');
  181. $this->assertTitle(t('Drupal blogs'), 'Blog feed was displayed');
  182. // Confirm a blog feed was displayed per user.
  183. $this->drupalGet('blog/' . $user->uid . '/feed');
  184. $this->assertTitle(t("@name's blog", array('@name' => format_username($user))), 'User blog feed was displayed');
  185. }
  186. }