Spike PHPCoverage Details: book.test

Line #FrequencySource Line
1 <?php
2 // $Id: book.test,v 1.10 2008/04/01 23:33:54 boombatower Exp $
3 
4 class BookTestCase extends DrupalTestCase {
5   protected $book;
6 
7   /**
8    * Implementation of getInfo().
9    */
10   function getInfo() {
11     return array(
121      'name' => t('Book functionality'),
13       'description' => t('Create a book, add pages, and test book interface.'),
14       'group' => t('Book Tests'),
15     );
16   }
17 
18   /**
19    * Implementation of setUp().
20    */
21   function setUp() {
22     parent::setUp();
23 
24     $this->drupalModuleEnable('book');
25   }
26 
27   /**
28    * Test book funcitonality through node interfaces.
29    */
30   function testBook() {
31     // Create users.
32     $book_author = $this->drupalCreateUser(array('create new books', 'create book content', 'add content to books'));
33     $web_user = $this->drupalCreateUser(array('access printer-friendly version'));
34 
35     // Create new book.
36     $this->drupalLogin($book_author);
37 
38     $this->book = $this->createBookNode('new');
39     $book = $this->book;
40 
41     /*
42      * Add page hiearchy to book.
43      * Book
44      *  |- Node 0
45      *   |- Node 1
46      *   |- Node 2
47      *  |- Node 3
48      *  |- Node 4
49      */
50     $nodes = array();
51     $nodes[] = $this->createBookNode($book->nid); // Node 0.
52     $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 1.
53     $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 2.
54     $nodes[] = $this->createBookNode($book->nid); // Node 3.
55     $nodes[] = $this->createBookNode($book->nid); // Node 4.
56 
57     $this->drupalLogout();
58 
59     // Check to make sure that book pages display properly.
60     $this->drupalLogin($web_user);
61 
62     $this->checkBookNode($book, array($nodes[0], $nodes[3], $nodes[4]), false, false, $nodes[0]);
63     $this->checkBookNode($nodes[0], array($nodes[1], $nodes[2]), $book, $book, $nodes[1]);
64     $this->checkBookNode($nodes[1], NULL, $nodes[0], $nodes[0], $nodes[2]);
65     $this->checkBookNode($nodes[2], NULL, $nodes[1], $nodes[0], $nodes[3]);
66     $this->checkBookNode($nodes[3], NULL, $nodes[2], $book, $nodes[4]);
67     $this->checkBookNode($nodes[4], NULL, $nodes[3], $book, false);
68   }
69 
70   /**
71    * Checks the outline of sub-pages; previous, up, and next; and check printer friendly version.
72    *
73    * @param Node $node Node to check.
74    * @param array $nodes Nodes that should be in outline.
75    * @param Node $previous Previous link node.
76    * @param Node $up Up link node.
77    * @param Node $next Next link node.
78    */
79   function checkBookNode($node, $nodes, $previous = false, $up = false, $next = false) {
80     static $number = 0;
81     $this->drupalGet('node/' . $node->nid);
82 
83     // Check outline structure.
84     if ($nodes !== NULL) {
85       $this->assertPattern($this->generateOutlinePattern($nodes), t('Node ' . $number . ' outline confirmed.'));
86     }
87     else {
88       $this->pass(t('Node ' . $number . ' doesn\'t have outline.'));
89     }
90 
91     // Check previous, up, and next links.
92     if ($previous) {
93       $this->assertRaw(l('‹ ' . $previous->title, 'node/' . $previous->nid, array('attributes' => array('class' => 'page-previous', 'title' => t('Go to previous page')))), t('Prevoius page link found.'));
94     }
95     if ($up) {
96       $this->assertRaw(l('up', 'node/' . $up->nid, array('attributes' => array('class' => 'page-up', 'title' => t('Go to parent page')))), t('Up page link found.'));
97     }
98     if ($next) {
99       $this->assertRaw(l($next->title . ' ›', 'node/' . $next->nid, array('attributes' => array('class' => 'page-next', 'title' => t('Go to next page')))), t('Next page link found.'));
100     }
101 
102     // Check printer friendly version.
103     $this->drupalGet('book/export/html/' . $node->nid);
104     $this->assertText($node->title, t('Printer friendly title found.'));
105     $node->body = str_replace('<!--break-->', '', $node->body);
106     $this->assertRaw(check_markup($node->body, $node->format), t('Printer friendly body found.'));
107 
108     $number++;
109   }
110 
111   /**
112    * Create a regular expression to check for the sub-nodes in the outline.
113    *
114    * @param array $nodes Nodes to check in outline.
115    */
116   function generateOutlinePattern($nodes) {
117     $outline = '';
118     foreach ($nodes as $node) {
119       $outline .= '(node\/' . $node->nid . ')(.*?)(' . $node->title . ')(.*?)';
120     }
121 
122     return '/<div id="book-navigation-' . $this->book->nid . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
123   }
124 
125   /**
126    * Create book node.
127    *
128    * @param integer $book_nid Book node id or set to 'new' to create new book.
129    * @param integer $parent Parent book reference id.
130    */
131   function createBookNode($book_nid, $parent = NULL) {
132     static $number = 0; // Used to ensure that when sorted nodes stay in same order.
133     $this->drupalVariableSet('node_options_page', array('status', 'promote'));
134 
135     $edit = array();
136     $edit['title'] = $number . ' - SimpleTest test node ' . $this->randomName(10);
137     $edit['body'] = 'SimpleTest test body ' . $this->randomName(32) . ' ' . $this->randomName(32);
138     $edit['book[bid]'] = $book_nid;
139 
140     if ($parent !== NULL) {
141       $this->drupalPost('node/add/book', $edit, t('Change book (update list of parents)'));
142 
143       $edit['book[plid]'] = $parent;
144       $this->drupalPost(NULL, $edit, t('Save'));
145     }
146     else {
147       $this->drupalPost('node/add/book', $edit, t('Save'));
148     }
149 
150     // Check to make sure the book node was created.
151     $node = node_load(array('title' => $edit['title']));
152     $this->assertNotNull(($node === FALSE ? NULL : $node), t('Book node found in database.'));
153     $number++;
154 
155     return $node;
156   }
157 }