/[drupal]/drupal/modules/book/book.test
ViewVC logotype

Contents of /drupal/modules/book/book.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.19 - (show annotations) (download) (as text)
Sun Nov 1 12:11:10 2009 UTC (3 weeks, 5 days ago) by dries
Branch: MAIN
Changes since 1.18: +2 -2 lines
File MIME type: text/x-php
- Patch #595084 by c960657: use type hinting for .
1 <?php
2 // $Id: book.test,v 1.18 2009/10/16 23:48:37 webchick Exp $
3
4 class BookTestCase extends DrupalWebTestCase {
5 protected $book;
6
7 public static function getInfo() {
8 return array(
9 'name' => 'Book functionality',
10 'description' => 'Create a book, add pages, and test book interface.',
11 'group' => 'Book',
12 );
13 }
14
15 function setUp() {
16 parent::setUp('book');
17 }
18
19 /**
20 * Test book functionality through node interfaces.
21 */
22 function testBook() {
23 // Create users.
24 $book_author = $this->drupalCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books'));
25 $web_user = $this->drupalCreateUser(array('access printer-friendly version'));
26
27 // Create new book.
28 $this->drupalLogin($book_author);
29
30 $this->book = $this->createBookNode('new');
31 $book = $this->book;
32
33 /*
34 * Add page hierarchy to book.
35 * Book
36 * |- Node 0
37 * |- Node 1
38 * |- Node 2
39 * |- Node 3
40 * |- Node 4
41 */
42 $nodes = array();
43 $nodes[] = $this->createBookNode($book->nid); // Node 0.
44 $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 1.
45 $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 2.
46 $nodes[] = $this->createBookNode($book->nid); // Node 3.
47 $nodes[] = $this->createBookNode($book->nid); // Node 4.
48
49 $this->drupalLogout();
50 $this->drupalLogin($web_user);
51
52 // Check that book pages display along with the correct outlines and
53 // previous/next links.
54 $this->checkBookNode($book, array($nodes[0], $nodes[3], $nodes[4]), FALSE, FALSE, $nodes[0], array());
55 $this->checkBookNode($nodes[0], array($nodes[1], $nodes[2]), $book, $book, $nodes[1], array($book));
56 $this->checkBookNode($nodes[1], NULL, $nodes[0], $nodes[0], $nodes[2], array($book, $nodes[0]));
57 $this->checkBookNode($nodes[2], NULL, $nodes[1], $nodes[0], $nodes[3], array($book, $nodes[0]));
58 $this->checkBookNode($nodes[3], NULL, $nodes[2], $book, $nodes[4], array($book));
59 $this->checkBookNode($nodes[4], NULL, $nodes[3], $book, FALSE, array($book));
60
61 $this->drupalLogout();
62
63 // Create a second book, and move an existing book page into it.
64 $this->drupalLogin($book_author);
65 $other_book = $this->createBookNode('new');
66 $node = $this->createBookNode($book->nid);
67 $edit = array('book[bid]' => $other_book->nid);
68 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
69
70 $this->drupalLogout();
71 $this->drupalLogin($web_user);
72
73 // Check that the nodes in the second book are displayed correctly.
74 // First we must set $this->book to the second book, so that the
75 // correct regex will be generated for testing the outline.
76 $this->book = $other_book;
77 $this->checkBookNode($other_book, array($node), FALSE, FALSE, $node, array());
78 $this->checkBookNode($node, NULL, $other_book, $other_book, FALSE, array($other_book));
79 }
80
81 /**
82 * Check the outline of sub-pages; previous, up, and next; and printer friendly version.
83 *
84 * @param $node
85 * Node to check.
86 * @param $nodes
87 * Nodes that should be in outline.
88 * @param $previous
89 * Previous link node.
90 * @param $up
91 * Up link node.
92 * @param $next
93 * Next link node.
94 * @param $breadcrumb
95 * The nodes that should be displayed in the breadcrumb.
96 */
97 function checkBookNode(stdClass $node, $nodes, $previous = FALSE, $up = FALSE, $next = FALSE, array $breadcrumb) {
98 // $number does not use drupal_static as it should not be reset
99 // since it uniquely identifies each call to checkBookNode().
100 static $number = 0;
101 $this->drupalGet('node/' . $node->nid);
102
103 // Check outline structure.
104 if ($nodes !== NULL) {
105 $this->assertPattern($this->generateOutlinePattern($nodes), t('Node ' . $number . ' outline confirmed.'));
106 }
107 else {
108 $this->pass(t('Node ' . $number . ' doesn\'t have outline.'));
109 }
110
111 // Check previous, up, and next links.
112 if ($previous) {
113 $this->assertRaw(l('‹ ' . $previous->title[FIELD_LANGUAGE_NONE][0]['value'], 'node/' . $previous->nid, array('attributes' => array('class' => array('page-previous'), 'title' => t('Go to previous page')))), t('Previous page link found.'));
114 }
115
116 if ($up) {
117 $this->assertRaw(l('up', 'node/' . $up->nid, array('attributes' => array('class' => array('page-up'), 'title' => t('Go to parent page')))), t('Up page link found.'));
118 }
119
120 if ($next) {
121 $this->assertRaw(l($next->title[FIELD_LANGUAGE_NONE][0]['value'] . ' ›', 'node/' . $next->nid, array('attributes' => array('class' => array('page-next'), 'title' => t('Go to next page')))), t('Next page link found.'));
122 }
123
124 // Compute the expected breadcrumb.
125 $expected_breadcrumb = array();
126 $expected_breadcrumb[] = url('');
127 foreach ($breadcrumb as $a_node) {
128 $expected_breadcrumb[] = url('node/' . $a_node->nid);
129 }
130
131 // Fetch links in the current breadcrumb.
132 $links = $this->xpath("//div[@class='breadcrumb']/a");
133 $got_breadcrumb = array();
134 foreach ($links as $link) {
135 $got_breadcrumb[] = (string) $link['href'];
136 }
137
138 // Compare expected and got breadcrumbs.
139 $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, t('The breadcrumb is correctly displayed on the page.'));
140
141 // Check printer friendly version.
142 $this->drupalGet('book/export/html/' . $node->nid);
143 $this->assertText($node->title[FIELD_LANGUAGE_NONE][0]['value'], t('Printer friendly title found.'));
144 $this->assertRaw(check_markup($node->body[FIELD_LANGUAGE_NONE][0]['value'], $node->body[FIELD_LANGUAGE_NONE][0]['format']), t('Printer friendly body found.'));
145
146 $number++;
147 }
148
149 /**
150 * Create a regular expression to check for the sub-nodes in the outline.
151 *
152 * @param array $nodes Nodes to check in outline.
153 */
154 function generateOutlinePattern($nodes) {
155 $outline = '';
156 foreach ($nodes as $node) {
157 $outline .= '(node\/' . $node->nid . ')(.*?)(' . $node->title[FIELD_LANGUAGE_NONE][0]['value'] . ')(.*?)';
158 }
159
160 return '/<div id="book-navigation-' . $this->book->nid . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
161 }
162
163 /**
164 * Create book node.
165 *
166 * @param integer $book_nid Book node id or set to 'new' to create new book.
167 * @param integer $parent Parent book reference id.
168 */
169 function createBookNode($book_nid, $parent = NULL) {
170 // $number does not use drupal_static as it should not be reset
171 // since it uniquely identifies each call to createBookNode().
172 static $number = 0; // Used to ensure that when sorted nodes stay in same order.
173
174 $edit = array();
175 $langcode = FIELD_LANGUAGE_NONE;
176 $edit["title[$langcode][0][value]"] = $number . ' - SimpleTest test node ' . $this->randomName(10);
177 $edit["body[$langcode][0][value]"] = 'SimpleTest test body ' . $this->randomName(32) . ' ' . $this->randomName(32);
178 $edit['book[bid]'] = $book_nid;
179
180 if ($parent !== NULL) {
181 $this->drupalPost('node/add/book', $edit, t('Change book (update list of parents)'));
182
183 $edit['book[plid]'] = $parent;
184 $this->drupalPost(NULL, $edit, t('Save'));
185 }
186 else {
187 $this->drupalPost('node/add/book', $edit, t('Save'));
188 }
189
190 // Check to make sure the book node was created.
191 $node = $this->drupalGetNodeByTitle($edit["title[$langcode][0][value]"]);
192 $this->assertNotNull(($node === FALSE ? NULL : $node), t('Book node found in database.'));
193 $number++;
194
195 return $node;
196 }
197 }
198
199 class BookBlockTestCase extends DrupalWebTestCase {
200 public static function getInfo() {
201 return array(
202 'name' => 'Block availability',
203 'description' => 'Check if the book navigation block is available.',
204 'group' => 'Book',
205 );
206 }
207
208 function setUp() {
209 parent::setUp('book');
210
211 // Create and login user
212 $admin_user = $this->drupalCreateUser(array('administer blocks'));
213 $this->drupalLogin($admin_user);
214 }
215
216 function testBookNavigationBlock() {
217 // Set block title to confirm that the interface is availble.
218 $this->drupalPost('admin/structure/block/manage/book/navigation/configure', array('title' => $this->randomName(8)), t('Save block'));
219 $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));
220
221 // Set the block to a region to confirm block is availble.
222 $edit = array();
223 $edit['book_navigation[region]'] = 'footer';
224 $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
225 $this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
226 }
227 }

  ViewVC Help
Powered by ViewVC 1.1.2