3 namespace Drupal\Tests\book\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Create a book, add pages, and test book interface.
12 class BookBreadcrumbTest extends BrowserTestBase {
19 public static $modules = ['book', 'block', 'book_breadcrumb_test'];
24 * @var \Drupal\node\NodeInterface
29 * A user with permission to create and edit books.
31 * @var \Drupal\user\Entity\User
33 protected $bookAuthor;
36 * A user without the 'node test view' permission.
38 * @var \Drupal\user\UserInterface
40 protected $webUserWithoutNodeAccess;
45 protected function setUp() {
47 $this->drupalPlaceBlock('system_breadcrumb_block');
48 $this->drupalPlaceBlock('page_title_block');
51 $this->bookAuthor = $this->drupalCreateUser(['create new books', 'create book content', 'edit own book content', 'add content to books']);
52 $this->adminUser = $this->drupalCreateUser(['create new books', 'create book content', 'edit any book content', 'delete any book content', 'add content to books', 'administer blocks', 'administer permissions', 'administer book outlines', 'administer content types', 'administer site configuration']);
56 * Creates a new book with a page hierarchy.
58 * @return \Drupal\node\NodeInterface[]
59 * The created book nodes.
61 protected function createBreadcrumbBook() {
63 $this->drupalLogin($this->bookAuthor);
65 $this->book = $this->createBookNode('new');
69 * Add page hierarchy to book.
80 $nodes[0] = $this->createBookNode($book->id());
81 $nodes[1] = $this->createBookNode($book->id(), $nodes[0]->id());
82 $nodes[2] = $this->createBookNode($book->id(), $nodes[0]->id());
83 $nodes[3] = $this->createBookNode($book->id(), $nodes[2]->id());
84 $nodes[4] = $this->createBookNode($book->id(), $nodes[3]->id());
85 $nodes[5] = $this->createBookNode($book->id(), $nodes[4]->id());
86 $nodes[6] = $this->createBookNode($book->id());
88 $this->drupalLogout();
94 * Creates a book node.
96 * @param int|string $book_nid
97 * A book node ID or set to 'new' to create a new book.
98 * @param int|null $parent
99 * (optional) Parent book reference ID. Defaults to NULL.
101 * @return \Drupal\node\NodeInterface
104 protected function createBookNode($book_nid, $parent = NULL) {
105 // $number does not use drupal_static as it should not be reset since it
106 // uniquely identifies each call to createBookNode(). It is used to ensure
107 // that when sorted nodes stay in same order.
111 $edit['title[0][value]'] = str_pad($number, 2, '0', STR_PAD_LEFT) . ' - SimpleTest test node ' . $this->randomMachineName(10);
112 $edit['body[0][value]'] = 'SimpleTest test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
113 $edit['book[bid]'] = $book_nid;
115 if ($parent !== NULL) {
116 $this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
118 $edit['book[pid]'] = $parent;
119 $this->drupalPostForm(NULL, $edit, t('Save'));
120 // Make sure the parent was flagged as having children.
121 $parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
122 $this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
125 $this->drupalPostForm('node/add/book', $edit, t('Save'));
128 // Check to make sure the book node was created.
129 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
130 $this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
137 * Test that the breadcrumb is updated when book content changes.
139 public function testBreadcrumbTitleUpdates() {
140 // Create a new book.
141 $nodes = $this->createBreadcrumbBook();
144 $this->drupalLogin($this->bookAuthor);
146 $this->drupalGet($nodes[4]->toUrl());
147 // Fetch each node title in the current breadcrumb.
148 $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
149 $got_breadcrumb = [];
150 foreach ($links as $link) {
151 $got_breadcrumb[] = $link->getText();
153 // Home link and four parent book nodes should be in the breadcrumb.
154 $this->assertEqual(5, count($got_breadcrumb));
155 $this->assertEqual($nodes[3]->getTitle(), end($got_breadcrumb));
157 'title[0][value]' => 'Updated node5 title',
159 $this->drupalPostForm($nodes[3]->toUrl('edit-form'), $edit, 'Save');
160 $this->drupalGet($nodes[4]->toUrl());
161 // Fetch each node title in the current breadcrumb.
162 $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
163 $got_breadcrumb = [];
164 foreach ($links as $link) {
165 $got_breadcrumb[] = $link->getText();
167 $this->assertEqual(5, count($got_breadcrumb));
168 $this->assertEqual($edit['title[0][value]'], end($got_breadcrumb));
172 * Test that the breadcrumb is updated when book access changes.
174 public function testBreadcrumbAccessUpdates() {
175 // Create a new book.
176 $nodes = $this->createBreadcrumbBook();
177 $this->drupalLogin($this->bookAuthor);
179 'title[0][value]' => "you can't see me",
181 $this->drupalPostForm($nodes[3]->toUrl('edit-form'), $edit, 'Save');
182 $this->drupalGet($nodes[4]->toUrl());
183 $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
184 $got_breadcrumb = [];
185 foreach ($links as $link) {
186 $got_breadcrumb[] = $link->getText();
188 $this->assertEqual(5, count($got_breadcrumb));
189 $this->assertEqual($edit['title[0][value]'], end($got_breadcrumb));
190 $config = $this->container->get('config.factory')->getEditable('book_breadcrumb_test.settings');
191 $config->set('hide', TRUE)->save();
192 $this->drupalGet($nodes[4]->toUrl());
193 $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
194 $got_breadcrumb = [];
195 foreach ($links as $link) {
196 $got_breadcrumb[] = $link->getText();
198 $this->assertEqual(4, count($got_breadcrumb));
199 $this->assertEqual($nodes[2]->getTitle(), end($got_breadcrumb));
200 $this->drupalGet($nodes[3]->toUrl());
201 $this->assertResponse(403);