Spike PHPCoverage Details: forum.test

Line #FrequencySource Line
1 <?php
2 // $Id: forum.test,v 1.10 2008/04/01 23:33:54 boombatower Exp $
3 
4 class ForumTestCase extends DrupalTestCase {
5   /**
6   * Implementation of getInfo() for information
7   */
8   function getInfo() {
91    return array('name' => t('Forum test functions'), 'description' => 'Helps the forum test cases run by providing common functions. Does not need to be checked.', 'group' => 'Forum');
10   }
11 
12   function setUp() {
13     parent::setUp();
14 
15     // Enable the forum module and its dependencies
16     $this->drupalModuleEnable('taxonomy');
17     $this->drupalModuleEnable('comment');
18     $this->drupalModuleEnable('forum');
19   }
20 
21   function createForumContainer() {
22     // Generate a random name/description
23     $title = $this->randomName(10);
24     $description = $this->randomName(100);
25 
26     $edit = array(
27       'name' => $title,
28       'description' => $description,
29       'parent[0]' => '0',
30       'weight' => '0',
31     );
32 
33     // Double check that the page says it has created the container
34     $this->drupalPost('admin/content/forum/add/container', $edit, t('Save'));
35     $type = t('forum container');
36     $this->assertRaw(t('Created new @type %term.', array('%term' => $title, '@type' => $type)), t('New forum container has been created'));
37 
38     // Grab the newly created container
39     $term = db_fetch_array(db_query("SELECT * FROM {term_data} t WHERE t.vid = %d AND t.name = '%s' AND t.description = '%s'", variable_get('forum_nav_vocabulary', ''), $title, $description));
40 
41     // Make sure we actually found a container
42     $this->assertTrue(!empty($term), 'The container actually exists in the database');
43 
44     return $term;
45   }
46 
47   function createForum() {
48     // Generate a random name/description
49     $title = $this->randomName(10);
50     $description = $this->randomName(100);
51 
52     $edit = array(
53       'name' => $title,
54       'description' => $description,
55       'parent[0]' => '0',
56       'weight' => '0',
57     );
58 
59     // Double check that the page says it has created the forum
60     $this->drupalPost('admin/content/forum/add/forum', $edit, t('Save'));
61     $type = t('forum');
62     $this->assertRaw(t('Created new @type %term.', array('%term' => $title, '@type' => $type)), t('New forum has been created'));
63 
64     // Grab the newly created forum
65     $term = db_fetch_array(db_query("SELECT * FROM {term_data} t WHERE t.vid = %d AND t.name = '%s' AND t.description = '%s'", variable_get('forum_nav_vocabulary', ''), $title, $description));
66 
67     // Make sure we actually found a forum
68     $this->assertTrue(!empty($term), 'The forum actually exists in the database');
69 
70     return $term;
71   }
72 }
73 
74 class AddForumTestCase extends ForumTestCase {
75   /**
76   * Implementation of getInfo() for information
77   */
78   function getInfo() {
791    return array('name' => t('Add forum'), 'description' => 'Adds a forum and a forum container and verifies that they have been created.', 'group' => 'Forum');
80   }
81 
82   function testAddForumContainer() {
83     // Attempt to create a forum container
84     $web_user = $this->drupalCreateUser(array(
85       'access administration pages',
86       'administer forums',
87     ));
88     $this->drupalLogin($web_user);
89 
90     // Create the container, all the assertions are handled in the function
91     $container = $this->createForumContainer();
92 
93     // Delete the forum container we created
94     if (!empty($container))
95       taxonomy_del_term($container['tid']);
96   }
97 
98   function testAddForum() {
99     // Attempt to create a forum
100     $web_user = $this->drupalCreateUser(array(
101       'access administration pages',
102       'administer forums',
103     ));
104     $this->drupalLogin($web_user);
105 
106     // Create the forum, all assertions are handled in the function
107     $forum = $this->createForum();
108 
109     // Delete the forum we created
110     if (!empty($forum))
111       taxonomy_del_term($forum['tid']);
112   }
113 }
114 
115 class EditForumTaxonomyTestCase extends ForumTestCase {
116   /**
117   * Implementation of getInfo() for information
118   */
119   function getInfo() {
1201    return array('name' => t('Edit forum taxonomy'), 'description' => 'Edits the forum taxonomy.', 'group' => 'Forum');
121   }
122 
123   function testEditForumTaxonomy() {
124     // Attempt to edit the forum taxonomy
125     $web_user = $this->drupalCreateUser(array(
126       'access administration pages',
127       'administer taxonomy',
128     ));
129     $this->drupalLogin($web_user);
130 
131     $vid = variable_get('forum_nav_vocabulary', '');
132     $original_settings = taxonomy_vocabulary_load($vid);
133 
134     // Generate a random name/description
135     $title = $this->randomName(10);
136     $description = $this->randomName(100);
137 
138     $edit = array(
139       'name' => $title,
140       'description' => $description,
141       'help' => '',
142       'weight' => -10
143     );
144 
145     // Double check that the page says it has edited the vocabulary
146     $this->drupalPost('admin/content/taxonomy/edit/vocabulary/'. $vid, $edit, t('Save'));
147     $this->assertRaw(t('Updated vocabulary %name.', array('%name' => $title)), t('Vocabulary has been edited'));
148 
149     // Grab the newly edited vocabulary
150     $cur_settings = db_fetch_array(db_query('SELECT v.* FROM {vocabulary} v WHERE v.vid = %d', $vid));
151 
152     // Make sure we actually edited the vocabulary properly
153     $this->assertTrue($cur_settings['name'] == $title, 'The name has been updated properly');
154     $this->assertTrue($cur_settings['description'] == $description, 'The description has been updated properly');
155 
156     // Restore the original settings
157     $original_settings = (array) $original_settings;
158 
159     taxonomy_save_vocabulary($original_settings);
160   }
161 }
162 
163 
164 class AddTopicToForumTestCase extends ForumTestCase {
165   /**
166   * Implementation of getInfo() for information
167   */
168   function getInfo() {
1691    return array('name' => t('Add/move topics'), 'description' => 'Tests adding and moving topics within forums.', 'group' => 'Forum');
170   }
171 
172   function testAddTopicToForum() {
173     // Attempt to create a forum
174     $web_user = $this->drupalCreateUser(array(
175       'access administration pages',
176       'administer forums',
177       'create forum content'
178     ));
179     $this->drupalLogin($web_user);
180 
181     // Generate a forum
182     $forum = $this->createForum();
183 
184     // Now, we try to create the topic in the forum
185     // Generate a random subject/body
186     $title = $this->randomName(20);
187     $description = $this->randomName(200);
188 
189     $edit = array(
190       'title' => $title,
191       'body' => $description
192     );
193 
194     // Double check that the page says it has created the topic
195     $this->drupalPost('node/add/forum/'. $forum['tid'], $edit, t('Save'));
196     $type = t('Forum topic');
197     $this->assertRaw(t('@type %term has been created.', array('%term' => $title, '@type' => $type)), t('New forum topic has been created'));
198     $this->assertNoRaw(t('The item %term is only a container for forums.', array('%term' => $forum['name'])), t('No error message shown'));
199 
200     // Then find the new topic, load it, and make sure the text we chose appears
201     $new_topic = node_load(array('title' => $title), null, true);
202     $this->drupalGet("node/$new_topic->nid");
203 
204     $this->assertRaw($title, t('Looking for subject text'));
205     $this->assertRaw($description, t('Looking for body text'));
206 
207     // Delete the topic
208     node_delete($new_topic->nid);
209 
210     // Delete the forum we created
211     if (!empty($forum))
212       taxonomy_del_term($forum['tid']);
213   }
214 
215   function testAddTopicToContainer() {
216     // Attempt to create a forum container
217     $web_user = $this->drupalCreateUser(array(
218       'access administration pages',
219       'administer forums',
220       'create forum content'
221     ));
222     $this->drupalLogin($web_user);
223 
224     // Create the container
225     $container = $this->createForumContainer();
226 
227     // Now, we try to create the topic in the forum
228     // Generate a random subject/body
229     $title = $this->randomName(20);
230     $description = $this->randomName(200);
231 
232     $edit = array(
233       'title' => $title,
234       'body' => $description
235     );
236 
237     // Double check that the page says it hasn't created the topic
238     $this->drupalPost('node/add/forum/'. $container['tid'], $edit, t('Save'));
239     $type = t('Forum topic');
240     $this->assertNoRaw(t('@type %term has been created.', array('%term' => $title, '@type' => $type)), t('No "new forum has been created" message'));
241     $this->assertRaw(t('The item %term is only a container for forums.', array('%term' => $container['name'])), t('Error message shown'));
242 
243     // Then make sure the node does not exist
244     $new_topic = node_load(array('title' => $title), null, true);
245     $this->assertTrue(empty($new_topic), t('There is no new topic'));
246 
247     // Delete the forum container we created
248     if (!empty($container))
249       taxonomy_del_term($container['tid']);
250   }
251 
252   function testMoveTopicToForum() {
253     // Attempt to create a forum
254     $web_user = $this->drupalCreateUser(array(
255       'access administration pages',
256       'administer forums',
257       'create forum content',
258       'edit any forum content'
259     ));
260     $this->drupalLogin($web_user);
261 
262     $forum1 = $this->createForum();
263     $forum2 = $this->createForum();
264 
265     // Now, we try to create the topic in the forum
266     // Generate a random subject/body
267     $title = $this->randomName(20);
268     $description = $this->randomName(200);
269 
270     $edit = array(
271       'title' => $title,
272       'body' => $description
273     );
274 
275     // Double check that the page says it has created the topic
276     $this->drupalPost('node/add/forum/'. $forum1['tid'], $edit, t('Save'));
277     $type = t('Forum topic');
278     $this->assertRaw(t('@type %term has been created.', array('%term' => $title, '@type' => $type)), t('New forum topic has been created'));
279     $this->assertNoRaw(t('The item %term is only a container for forums.', array('%term' => $forum1['name'])), t('No error message shown'));
280 
281     // Then find the new topic and edit it to move it
282     $new_topic = node_load(array('title' => $title), null, true);
283     $vid = variable_get('forum_nav_vocabulary', '');
284 
285     $edit = array(
286       'title' => $title,
287       'taxonomy['. $vid .']' => $forum2['tid'],
288       'body' => $description
289     );
290 
291     // Double check that the page says it has updated the topic
292     // Also, double check that the new forum name is there and not the old
293     $this->drupalPost('node/'. $new_topic->nid .'/edit', $edit, t('Save'));
294     $type = t('Forum topic');
295     $this->assertRaw(t('@type %term has been updated.', array('%term' => $title, '@type' => $type)), t('Topic has been moved'));
296     $this->assertRaw($forum2['name'], t('New forum name is present'));
297     $this->assertNoRaw($forum1['name'], t('Old forum name is not present'));
298 
299     // Delete the topic
300     node_delete($new_topic->nid);
301 
302     // Delete the forums we created
303     if (!empty($forum1))
304       taxonomy_del_term($forum1['tid']);
305     if (!empty($forum2))
306       taxonomy_del_term($forum2['tid']);
307   }
308 
309   function testMoveTopicWithCopyToForum() {
310     // Attempt to create a forum
311     $web_user = $this->drupalCreateUser(array(
312       'access administration pages',
313       'administer forums',
314       'create forum content',
315       'edit any forum content'
316     ));
317     $this->drupalLogin($web_user);
318 
319     $forum1 = $this->createForum();
320     $forum2 = $this->createForum();
321 
322     // Now, we try to create the topic in the forum
323     // Generate a random subject/body
324     $title = $this->randomName(20);
325     $description = $this->randomName(200);
326 
327     $edit = array(
328       'title' => $title,
329       'body' => $description
330     );
331 
332     // Double check that the page says it has created the topic
333     $this->drupalPost('node/add/forum/'. $forum1['tid'], $edit, t('Save'));
334     $type = t('Forum topic');
335     $this->assertRaw(t('@type %term has been created.', array('%term' => $title, '@type' => $type)), t('New forum topic has been created'));
336     $this->assertNoRaw(t('The item %term is only a container for forums.', array('%term' => $forum1['name'])), t('No error message shown'));
337 
338     // Then find the new topic and edit it to move it
339     $new_topic = node_load(array('title' => $title), null, true);
340     $vid = variable_get('forum_nav_vocabulary', '');
341 
342     $edit = array(
343       'title' => $title,
344       'taxonomy['. $vid .']' => $forum2['tid'],
345       'body' => $description
346     );
347 
348     // Double check that the page says it has updated the topic
349     // Also, double check that the new forum name is there and not the old
350     $this->drupalPost('node/'. $new_topic->nid .'/edit', $edit, t('Save'));
351     $type = t('Forum topic');
352     $this->assertRaw(t('@type %term has been updated.', array('%term' => $title, '@type' => $type)), t('Topic has been moved'));
353     $this->assertRaw($forum2['name'], t('New forum name is present'));
354     $this->assertNoRaw($forum1['name'], t('Old forum name is not present'));
355 
356     // Delete the topic
357     node_delete($new_topic->nid);
358 
359     // Delete the forums we created
360     if (!empty($forum1))
361       taxonomy_del_term($forum1['tid']);
362     if (!empty($forum2))
363       taxonomy_del_term($forum2['tid']);
364   }
365 }