Spike PHPCoverage Details: node.test

Line #FrequencySource Line
1 <?php
2 // $Id: node.test,v 1.11 2008/04/01 23:33:54 boombatower Exp $
3 
4 class NodeRevisionsTestCase extends DrupalTestCase {
5   /**
6    * Implementation of getInfo() for information
7    */
8   function getInfo() {
9     return array(
101      'name' => t('Node revisions tests'),
11       'description' => t('Creates a node of type page and then a user tries various revision actions such as viewing, reverting to, and deleting revisions.'),
12       'group' => 'Node Tests',
13     );
14   }
15 
16   /**
17    * Setup function used by tests. Creates a node with three revisions.
18    *
19    * If $log is TRUE, then a log message will be recorded.
20    */
21   function prepareRevisions($log = FALSE) {
22 
23     $returnarray = array();
24     $numtimes = 3; // First, middle, last.
25     for ($i = 0; $i < $numtimes; $i++) {
26       $settings = array('revision' => 1);
27       if ($log && $i == 1) {
28         $logmessage = $this->randomName(32);
29         $settings['log'] = $logmessage;
30         $returnarray['log'] = $logmessage;
31       }
32       if ($i != 0) {
33         $settings['nid'] = $node->nid;
34       }
35       $node = $this->drupalCreateNode($settings);
36       if ($i == 1) {
37         $returnarray['text'] = $node->body;
38         $returnarray['vid'] = $node->vid;
39       }
40       // Avoid confusion on the revisions overview page which is sorted by r.timestamp.
41       sleep(1);
42     }
43     $returnarray['node'] = $node;
44     return $returnarray;
45   }
46 
47   /**
48    * Simpletest test. Tests to make sure the correct revision text appears on "view revisions" page.
49    */
50   function testNodeRevisions() {
51     extract( $this->prepareRevisions() );
52 
53     $test_user = $this->drupalCreateUser(array('view revisions'));
54     $this->drupalLogin($test_user);
55     $this->drupalGet("node/$node->nid/revisions/$vid/view");
56     $this->assertText($text, 'Check to make sure correct revision text appears on "view revisions" page.');
57 
58     $this->cleanup($node->nid);
59   }
60 
61   /**
62    * Simpletest test. Tests to make sure the correct log message appears on "revisions overview" page.
63    */
64   function testLogMessage() {
65     extract( $this->prepareRevisions(TRUE) );
66 
67     $test_user = $this->drupalCreateUser(array('view revisions'));
68     $this->drupalLogin($test_user);
69     $this->drupalGet("node/$node->nid/revisions");
70     $this->assertText($log, 'Check to make sure log message is properly displayed.');
71 
72     $this->cleanup($node->nid);
73   }
74 
75   /**
76    * Simpletest test. Tests to make sure the that revisions revert properly.
77    */
78   function testRevisionRevert() {
79     extract( $this->prepareRevisions() );
80 
81     $test_user = $this->drupalCreateUser(array('revert revisions', 'edit any page content'));
82     $this->drupalLogin($test_user);
83     $this->drupalPost("node/$node->nid/revisions/$vid/revert", array(), t('Revert'));
84     $newnode = node_load($node->nid);
85     $this->assertTrue(($text == $newnode->body), 'Check to make sure reversions occur properly');
86 
87     $this->cleanup($node->nid);
88   }
89 
90   /**
91    * Simpletest test. Tests to make sure the revision deletes properly.
92    */
93   function testRevisionDelete() {
94     extract( $this->prepareRevisions() );
95 
96     $test_user = $this->drupalCreateUser(array('delete revisions', 'delete any page content'));
97     $this->drupalLogin($test_user);
98     $this->drupalPost("node/$node->nid/revisions/$vid/delete", array(), t('Delete'));
99     $this->assertTrue(db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid = %d and VID = %d', $node->nid, $vid)) == 0, 'Check to make sure revisions delete properly');    $this->cleanup($node->nid);
100 
101     $this->cleanup($node->nid);
102   }
103 
104   /**
105    * Cleanup function used by tests. Deletes the associated node.
106    */
107   function cleanup($nid) {
108     node_delete($nid);
109   }
110 }
111 
112 
113 class NodeTeaserTestCase extends DrupalTestCase {
114   /**
115    * Implementation of getInfo() for information
116    */
117   function getInfo() {
118     return array(
1191      'name' => t('Node teaser tests'),
120       'description' => t('Calls node_teaser() with different strings and lengths.'),
121       'group' => 'Node Tests',
122     );
123   }
124 
125   function setUp() {
126     parent::setUp();
127   }
128 
129   function tearDown() {
130     parent::tearDown();
131   }
132 
133   /**
134    * Simpletest test. Tests an edge case where if the first sentence is a
135    * question and subsequent sentences are not.
136    * This failed in drupal 5.
137    * Test and patch for drupal 6 (committed) from
138    * http://drupal.org/node/180425
139    */
140   function testFirstSentenceQuestion() {
141     $body = 'A question? A sentence. Another sentence.';
142     $expectedTeaser = 'A question? A sentence.';
143     $this->callNodeTeaser($body, $expectedTeaser, NULL, 30);
144   }
145 
146   /**
147    * Simpletest test. A real-life example of the above edge case.
148    */
149   function testFirstSentenceQuestion2() {
150     $body = 'Are you an UberBabe? (Or an appreciator of UberBabes?) I am most definitely an UberBabe, and I\'m proud of it. Now, before anyone screams "sexism" or "bias" or "cheap" or anything more profane, let me clarify. An UberBabe is not someone who\'s playfully pierced navel protrudes from a belly bearing top. Not necessarily anyway. An UberBabe is a woman who likes being totally feminine, but is also smart as hell, brave, a rule breaker, speaks her mind, finds her own way, goes up against "the system" in a way that allows the system to evolve, and so on. UberBabes, frankly, kick booty - and they just may save the world.';
151     $expectedTeaser = 'Are you an UberBabe? (Or an appreciator of UberBabes?) I am most definitely an UberBabe, and I\'m proud of it. Now, before anyone screams "sexism" or "bias" or "cheap" or anything more profane, let me clarify.';
152     $this->callNodeTeaser($body, $expectedTeaser, NULL, 300);
153   }
154 
155   /**
156    * Simpletest test. Runs a test adapted from
157    * http://drupal.org/node/180425#comment-634230
158    */
159   function testLength() {
160     // This body string tests a number of edge cases.
161     $body = "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>";
162 
163     // The teasers we expect node_teaser() to return when $size is the index
164     // of each array item.
165     // Using an input format with no line-break filter:
166     $teasers = array(
167         "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
168         "<",
169         "<p",
170         "<p>",
171         "<p>\n",
172         "<p>\nH",
173         "<p>\nHi",
174         "<p>\nHi\n",
175         "<p>\nHi\n<",
176         "<p>\nHi\n</",
177         "<p>\nHi\n</p",
178         "<p>\nHi\n</p>",
179         "<p>\nHi\n</p>",
180         "<p>\nHi\n</p>",
181         "<p>\nHi\n</p>",
182         "<p>\nHi\n</p>",
183         "<p>\nHi\n</p>",
184         "<p>\nHi\n</p>",
185         "<p>\nHi\n</p>",
186         "<p>\nHi\n</p>",
187         "<p>\nHi\n</p>",
188         "<p>\nHi\n</p>",
189         "<p>\nHi\n</p>",
190         "<p>\nHi\n</p>",
191         "<p>\nHi\n</p>",
192         "<p>\nHi\n</p>",
193         "<p>\nHi\n</p>",
194         "<p>\nHi\n</p>",
195         "<p>\nHi\n</p>",
196         "<p>\nHi\n</p>",
197         "<p>\nHi\n</p>",
198         "<p>\nHi\n</p>",
199         "<p>\nHi\n</p>",
200         "<p>\nHi\n</p>",
201         "<p>\nHi\n</p>",
202         "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
203         "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
204         "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
205       );
206     // And Using an input format WITH the line-break filter.
207     $teasers_lb = array(
208         "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
209         "<",
210         "<p",
211         "<p>",
212         "<p>",
213         "<p>",
214         "<p>",
215         "<p>\nHi",
216         "<p>\nHi",
217         "<p>\nHi",
218         "<p>\nHi",
219         "<p>\nHi\n</p>",
220         "<p>\nHi\n</p>",
221         "<p>\nHi\n</p>",
222         "<p>\nHi\n</p>",
223         "<p>\nHi\n</p>",
224         "<p>\nHi\n</p>",
225         "<p>\nHi\n</p>",
226         "<p>\nHi\n</p>",
227         "<p>\nHi\n</p>",
228         "<p>\nHi\n</p>",
229         "<p>\nHi\n</p>",
230         "<p>\nHi\n</p>",
231         "<p>\nHi\n</p>",
232         "<p>\nHi\n</p>",
233         "<p>\nHi\n</p>",
234         "<p>\nHi\n</p>",
235         "<p>\nHi\n</p>",
236         "<p>\nHi\n</p>",
237         "<p>\nHi\n</p>",
238         "<p>\nHi\n</p>",
239         "<p>\nHi\n</p>",
240         "<p>\nHi\n</p>",
241         "<p>\nHi\n</p>",
242         "<p>\nHi\n</p>",
243         "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
244         "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
245         "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
246       );
247 
248     // Test node_teaser() for different sizes.
249     for ($i = 0; $i <= 37; $i++) {
250       $this->callNodeTeaser($body, $teasers[$i],    NULL, $i);
251       $this->callNodeTeaser($body, $teasers_lb[$i], 1,    $i);
252       $this->callNodeTeaser($body, $teasers_lb[$i], 2,    $i);
253     }
254   }
255 
256   /**
257    * Calls node_teaser() and asserts that the expected teaser is returned.
258    */
259   function callNodeTeaser($body, $expectedTeaser, $format = NULL, $size = NULL) {
260     $teaser = node_teaser($body, $format, $size);
261     $this->assertIdentical($teaser, $expectedTeaser);
262   }
263 }
264 
265 class PageEditTestCase extends DrupalTestCase {
266   function getInfo() {
267     return array(
2681      'name'  => 'Page edit test',
269       'description'  => t('We want a working edit for pages, uh?'),
270       'group' => 'Node Tests');
271   }
272   function testPageEdit() {
273 
274     /* Prepare settings */
275     $this->drupalVariableSet('node_options_page', array('status', 'promote'));
276     /* Prepare a user to do the stuff */
277     $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
278     $this->drupalLogin($web_user);
279     $edit = array(
280     'title' => '!SimpleTest! test title' . $this->randomName(20),
281     'body' => '!SimpleTest! test body' . $this->randomName(200),
282   );
283 
284     //Create the page to edit
285     $this->drupalPost('node/add/page', $edit, t('Save'));
286 
287     $node = node_load(array('title' => $edit['title']));
288     $this->assertNotNull($node, 'Node found in database');
289 
290     $this->clickLink(t('Edit'));
291     $editurl = url("node/$node->nid/edit", array('absolute' => true));
292     $acturl = $this->getURL();
293     $this->assertEqual($editurl, $acturl);
294 
295     $this->assertText(t('Edit'), 'Edit text is here');
296     $this->assertText(t($edit['title']), 'Hello, the random title');
297     $this->assertText(t($edit['body']), 'test is over, the body\'s still there');
298 
299     $edit = array(
300     'title' => '!SimpleTest! test title' . $this->randomName(20),
301     'body' => '!SimpleTest! test body' . $this->randomName(200),
302   );
303 
304 
305     //edit the content of the page
306     $this->drupalPost("node/$node->nid/edit", $edit, t('Save'));
307 
308     $this->assertText(t($edit['title']), 'Hello, the random title');
309     $this->assertText(t($edit['body']), 'test is over, the body\'s still there');
310   }
311 
312 }
313 
314 class PagePreviewTestCase extends DrupalTestCase {
315   function getInfo() {
316     return array(
3171   'name'  => 'Page preview test',
318    'description'  => t('We want a working preview for pages, uh?'),
319    'group' => 'Node Tests');
320   }
321 
322   function testPagePreview() {
323     /* Prepare settings */
324     $this->drupalVariableSet('node_options_page', array('status', 'promote'));
325     /* Prepare a user to do the stuff */
326     $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
327     $this->drupalLogin($web_user);
328 
329     $edit = array(
330       'title'=>'!SimpleTest! title' . $this->randomName(20),
331       'body'=>'!SimpleTest! body' . $this->randomName(200),
332     );
333     $this->drupalPost('node/add/page', $edit, t('Preview'));
334 
335     $this->assertText(t('Preview'), 'Preview text is here');
336     $this->assertText(t($edit['title']), 'Hello, the random title');
337     $this->assertText(t($edit['body']), 'test is over, the body\'s still there');
338 
339     $this->assertFieldByName('title', $edit['title'], 'The title is on it\'s place');
340 
341   }
342 
343 }
344 
345 class PageCreationTestCase extends  DrupalTestCase {
346 
347   /**
348    * Implementation of getInfo() for information
349    */
350   function getInfo() {
351     return array(
3521      'name' => t('Page node creation'),
353       'description' => t('Create a page node and verify its consistency in the database.'),
354       'group' => 'Node Tests',
355     );
356   }
357 
358   function testPageCreation() {
359     /* Prepare settings */
360     $this->drupalVariableSet('node_options_page', array('status', 'promote'));
361 
362     /* Prepare a user to do the stuff */
363     $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
364     $this->drupalLogin($web_user);
365 
366     $edit = array();
367     $edit['title']    = '!SimpleTest test node! ' . $this->randomName(10);
368     $edit['body']     = '!SimpleTest test body! ' . $this->randomName(32) . ' ' . $this->randomName(32);
369     $this->drupalPost('node/add/page', $edit, t('Save'));
370 
371     $this->assertRaw(t('!post %title has been created.', array ('!post' => 'Page', '%title' => $edit['title'])), 'Page created');
372 
373     $node = node_load(array('title' => $edit['title']));
374     $this->assertNotNull($node, t('Node !title found in database.', array ('!title' => $edit['title'])));
375 
376   }
377 }
378 
379 class PageViewTestCase extends DrupalTestCase {
380   /**
381    * Implementation of getInfo() for information
382    */
383   function getInfo() {
384     return array(
3851      'name' => t('Unauthorized node view'),
386       'description' => t('Creates a node of type page and then an unpermissioned user attempts to edit the node, '
387         . 'before tries with an anonymous user. Asserts failure.'
388         . '</ br>WARNING: This is based on default registered user permuissions (no administer nodes).')
389       , 'group' => 'Node Tests',
390   );
391   }
392 
393   function testPageView() {
394     /* Prepare a node to view */
395     global $user;
396     $node = $this->drupalCreateNode();
397     $this->assertNotNull(node_load($node->nid), 'Node created');
398 
399     /* Tries to edit with anonymous user */
400     $html = $this->drupalGet("node/$node->nid/edit");
401     $this->assertResponse(403);
402 
403     /* Prepare a user to request the node view */
404     $test_user = $this->drupalCreateUser(array('access content'));
405     $this->drupalLogin($test_user);
406 
407     $html = $this->drupalGet("node/$node->nid/edit");
408     $this->assertResponse(403);
409 
410     $test_user = $this->drupalCreateUser(array('administer nodes'));
411     //TODO: Add edit page attempt with administer nodes user
412     node_delete($node->nid);
413   }
414 }